LabelImg closes when attempting to label, error in "Canvas.py" file

694 Views Asked by At
  • SO: Windows 11 21H2
  • Python: 3.11.4
  • Version labelImg: 1.8.6
  • Installing collected packages: pyqt5 (5.15.9), lxml (4.9.3), PyQt5-sip (12.12.2), PyQt5-Qt5 (5.15.2)


Installation: pip install labelImg


Problem:

I am working on an object detection model with YOLOv7, for this I need the "labelImg" tool, I can install it, I can run it.

When I select the "Create RectBox" tool and move the cursor over the image (without making any selection yet), the program closes.


Error:

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'

I have tried to fix it with:

  1. Try looking at the part of the code in canvas.py that references drawLine and check how the arguments are passed.
    But I couldn't fix the way the values are passed to match the types expected by the drawLine function.

  2. Try to install "labelImg" by cloning the repository (same error)

  3. Install previous versions of the necessary packages, intuiting that it is an error in the latest version.

  4. I installed it on Windows 10 Home (same error)

You can check the "Canvas.py" file on the drive:

Canvas.py

2

There are 2 best solutions below

0
Gnomioo On

When several people try to fix these problems, they say to change the operating system, Python version and a bunch of other changes that do not imply solving the problem, but rather moving to another version of the system so that this problem does not occur. In this answer I will teach you how to fix the problem without anything like that, the answer is kind of self-taught by the error itself, but let's go.

In your Python directory there is a folder called C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\libs, go into it and look for a file called canvas.py. In this file you will go to line 526, 530 and 531 and change all the information that goes into the drawLine functions, as shown below.

Change the line 526 from p.drawRect(left_top.x(), left_top.y(), rect_width, rect_height) to p.drawRect(int(left_top.x()), int(left_top.y()), int(rect_width), int(rect_height)).

The line 530 from p.drawLine(self.prev_point.x(), 0, self.prev_point.x(), self.pixmap.height()) to p.drawLine(int(self.prev_point.x()), 0, int(self.prev_point.x()), int(self.pixmap.height())).

And finally, the line 531 from p.drawLine(0, self.prev_point.y(), self.pixmap.width(), self.prev_point.y()) to p.drawLine(0, int(self.prev_point.y()), int(self.pixmap.width()), int(self.prev_point.y())).

Note that I am only declaring the variables as integers within the functions, this may end up implying a minimum rounding of the drawings made, but nothing that will have a minimally significant dimension in the final result.

After that, just to make sure everything is ok, in the directory C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\labelImg look for the code called labelImg.py and change line 965 of it to bar.setValue(bar.value() + bar.singleStep() * units) to bar.setValue(int(bar.value() + bar.singleStep() * units)).

Explanation: the problem consists of an error in reading these variables by the drawing functions, which currently do not allow non-integer numbers, if I'm not mistaken, as of version 3.8 of Python. That said, these modifications should work for any version of Python above, I've even tested it on 3.11 and 3.12 and it worked normally regardless of whether it was Windows or Linux.

0
hamidreza zarrabi On

I switched to online Object Annotation tools after failing to fix the problem on Windows 10. There were some tools but I have chosen Object Annotation tool from Saiwa since it was fast and easy to use.