You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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()返回的是浮点数,导致类型不匹配。直接修改对应代码即可解决:

  1. 找到canvas.py文件(路径:C:\Users\Alecx\AppData\Local\Programs\Python\Python311\Lib\site-packages\libs\canvas.py)的第530行;
  2. 将原代码:
    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()))
    
  3. 检查文件中其他调用drawLine的位置,若存在同样的浮点数参数问题,也做整数转换处理;
  4. 保存修改后的文件,重新启动labelImg即可正常使用Create RectBox工具。

内容的提问来源于stack exchange,提问作者JuanPKl

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 17:00:23