labelImg 闪退、报错IndexError

labelImg报错

  • 报错
  • 解决办法
    • 1.定位:
    • 2. 添加try exception
    • 3.解决

报错

标注后再打开的时候出现了闪退的情况,报错如下:
labelImg 闪退、报错IndexError_第1张图片

过程:
通常出现IndexError,是标注框和label不匹配的原因。
查看了这个博客https://blog.csdn.net/eicebear/article/details/109532220,评论里说:
把labelimg.py代码中filedir = filePath.split(basename)[0].split("/")[-2:-1][0]改成filedir = filePath.split(basename)[0].split("\")[-2:-1][0]就可以了
labelImg 闪退、报错IndexError_第2张图片
但发现他的报错(上图)和我的报错不同,我的labelImg.py中也没有上图红框的代码,此解决方案给有这行代码的小伙伴参考。



解决办法

labelImg 闪退、报错IndexError_第3张图片

1.定位:

分析了我的报错,问题出在labelImg.py和yolo_io.py的yoloLine2shape函数。

labelImg.py报错通常是因为引用了发生错误的函数,因此从yolo_io.py的yoloLine2shape函数开始排查错误。

函数结构如下:
labelImg 闪退、报错IndexError_第4张图片
引用部分如下:
labelImg 闪退、报错IndexError_第5张图片

2. 添加try exception

不希望被IndexError打断程序就需要在调用出错函数时添加try exception(而不是在出错函数中修改),改动如下:
labelImg 闪退、报错IndexError_第6张图片

#代码放上便于复制粘贴
def parseYoloFormat(self):
        bndBoxFile = open(self.filepath, 'r')
        for bndBox in bndBoxFile:
            classIndex, xcen, ycen, w, h = bndBox.split(' ')
            try:
                label, xmin, ymin, xmax, ymax = self.yoloLine2Shape(classIndex, xcen, ycen, w, h)
            except Exception:
                print("Exception")

            # Caveat: difficult flag is discarded when saved as yolo format.
            self.addShape(label, xmin, ymin, xmax, ymax, False)

3.解决

再次运行,不再中断,成功。
labelImg 闪退、报错IndexError_第7张图片

你可能感兴趣的:(Python,python)