labelImg选Create RectBox时闪退,Canvas.py出现TypeError错误
labelImg选择Create RectBox工具闪退问题解决
环境信息
- 操作系统:Windows 11 21H2
- Python版本:3.11.4
- labelImg版本:1.8.6
- 已安装依赖包:pyqt5(5.15.9)、lxml(4.9.3)、PyQt5-sip(12.12.2)、PyQt5-Qt5(5.15.2)
安装方式
通过pip install labelImg完成安装。
问题描述
基于YOLOv7开发目标检测模型时使用labelImg工具,工具可正常安装启动,但选择Create RectBox工具后,光标移至图像上(尚未进行选择操作)程序直接闪退。
错误信息
执行labelImg命令后触发如下报错:
C:\Users\Alecx>labelImg Traceback (most recent call last): File "C:\Users\Alecx\AppData\Local\Programs\Python\Python311\Lib\site-packages\libs\canvas.py", line 530, in paintEvent p.drawLine(self.prev_point.x(), 0, self.prev_point.x(), self.pixmap.height()) TypeError: arguments did not match any overloaded call: drawLine(self, l: QLineF): argument 1 has unexpected type 'float' drawLine(self, line: QLine): argument 1 has unexpected type 'float' drawLine(self, x1: int, y1: int, x2: int, y2: int): argument 1 has unexpected type 'float' drawLine(self, p1: QPoint, p2: QPoint): argument 1 has unexpected type 'float' drawLine(self, p1: Union[QPointF, QPoint], p2: Union[QPointF, QPoint]): argument 1 has unexpected type 'float'
已尝试的解决方法
- 查看
canvas.py中drawLine相关代码,但无法修正参数传递类型以匹配函数要求; - 克隆labelImg仓库安装,仍出现相同错误;
- 安装依赖包旧版本,推测是新版本兼容问题;
- 在Windows 10 Home系统安装,错误依旧。
针对性解决方法
根据报错信息,问题根源是canvas.py的paintEvent方法中,drawLine函数要求传入整数类型的坐标参数,但self.prev_point.x()返回的是浮点数,导致类型不匹配。直接修改对应代码即可解决:
- 找到
canvas.py文件(路径:C:\Users\Alecx\AppData\Local\Programs\Python\Python311\Lib\site-packages\libs\canvas.py)的第530行; - 将原代码:
修改为:p.drawLine(self.prev_point.x(), 0, self.prev_point.x(), self.pixmap.height())p.drawLine(int(self.prev_point.x()), 0, int(self.prev_point.x()), int(self.pixmap.height())) - 检查文件中其他调用
drawLine的位置,若存在同样的浮点数参数问题,也做整数转换处理; - 保存修改后的文件,重新启动labelImg即可正常使用Create RectBox工具。
内容的提问来源于stack exchange,提问作者JuanPKl
相关产品推荐
相关产品推荐

