Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python OpenCV Error: Current thread is not the object's thread

I'm having an error running simple code using cv2 module.

It's just:

import cv2

img = cv2.imread('sudoku.png',0)

cv2.imshow('image',img)

And it fails with the following error:

QObject::moveToThread: Current thread (0x1b74720) is not the object's thread (0x1e57d70).
Cannot move to target thread (0x1b74720)

I googled this error and tried a lot of things but it doesn't help. I tried installing without pip, I tried using step-by-step installation (from official OpenCV) but nothing helps.

When I run:

cv2.__version__

It returns 3.4.3

like image 478
Desiigner Avatar asked Sep 14 '18 19:09

Desiigner


3 Answers

As noted already, the basis for this problem is discussed in opencv-python issue 46, and results from the duplication of the following libraries both on the host and the opencv-python distro libQtDBus libQtCore and libQtGui.

It has been lately addressed in the newest release of opencv-python. It is not a fix to the source code, rather the fix is to force pip to compile the newly available source via

pip install --no-binary opencv-python opencv-python

This will cause opencv-python to use the same libraries as the host, so the conflict no longer exists.

like image 163
waTeim Avatar answered Nov 19 '22 16:11

waTeim


According to this issue posted on the OpenCV GitHub, this is a known issue that the developer states is damn near impossible to fix. It is apparently caused by a conflict in any Qt installations on the system with the Qt that is shipped with OpenCV. There are some suggestions floating around to remove the libqt5x11extras5 package from the system. This may fix it for some but anyone running these libraries on a Linux distribution that uses a window manager based on Qt will render their desktop environment unusable by removing this package (having tried it myself).

You can try building OpenCV from source using the WITH_GTK=ON option when running cmake which will use GTK instead of Qt, circumventing the conflict. However, this is hard to make use of in Python when using virtual environments.

like image 7
smallpants Avatar answered Nov 19 '22 17:11

smallpants


I haven't spent the time to fully appreciate this problem, but as I understand this is caused by multiple conflicting versions of some plugin in the environment. I tried installing building opencv-python but there were errors with that approach. Another suggestion is to change your import order, but I've had mixed success with that, and I couldn't get it to work on a project today.

But I found a workaround that worked for me. Install opencv-python-headless instead of opencv-python. This will avoid installing the conflicting plugins. It may not work for you depending on what features of opencv you need.

$ pip uninstall opencv-python
$ pip install opencv-python-headless
like image 3
jmilloy Avatar answered Nov 19 '22 16:11

jmilloy