10
votes

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

3
yeah, I only got 3 lines of code and it's everything that appears in the console - Desiigner
Also, which cv2 package are you using -- the official one or opencv-python? Which OS and Python implementation? Are you using this from an interactive console or ipython/jupyter? - ivan_pozdeev
I tried using the official one but it doesn't work, so I had to install opencv-python. OS is Ubuntu, I'm running the code via PyCharm but the same error I receive when I run it from the console - Desiigner
Are you running python2 or python3, I found similar issues on google but probably you also find them and tried suggestions. A temporary solution is to use matplotlib instead of opencv imshow. - unlut

3 Answers

10
votes

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.

7
votes

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.

2
votes

The error was fixed on my system, by simply updating one library.

To find out where it is coming from, assuming Linux, try the following,

LD_DEBUG=files python -c "import cv2"

or,

LD_DEBUG=files python -c "import cv2 ; img = cv2.imread('myimage.png',0) ; cv2.imshow('image',img) ; cv2.waitKey(0)"

On my machine, it failed in one of the blas libraries. I updated that library and the code now runs without error.

This is an old bug you can find it discussed in a number of online communities.

My test code is as follows. Notably the error has not reoccurred, and apparently has nothing to do with Qt.

import cv2

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

cv2.imshow('image',img)

cv2.waitKey(0)