25

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

9
  • Is this the complete error message? No traceback, no line numbers? Commented Sep 14, 2018 at 20:52
  • 1
    yeah, I only got 3 lines of code and it's everything that appears in the console Commented Sep 14, 2018 at 20:53
  • 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? Commented Sep 14, 2018 at 20:58
  • 1
    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 Commented Sep 14, 2018 at 21:01
  • 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. Commented Sep 14, 2018 at 21:34

7 Answers 7

24

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Be advised that with the latest opencv-python this compilation may take a somewhat long while
@matanster agreed, I was surprised to see that it took my PC Ryzen 7 3700X 64GB RAM, NVIDIA 2080Ti around 20-30 mins...... what a library~!
This worked for me, but I had to use sudo pip install --no-binary opencv-python opencv-python while running the python code, I had to use sudo python3 cv_qt_file.py. It didn't work without sudo for me. Any way I can fix the non-sudo version?
@OzoneX is your user in the video group? Without that, some stuff works, some doesn't, and the failures can be surprising
@ChrisH What do you mean by video group? I'm the admin single user, so making use of sudo while needed sometimes.
|
13

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

Comments

9

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.

8 Comments

Current hope seems to be that the issue will go away when OpenCV moves to Qt 5 in its next release.
Still the same problem in version 4.4.0.
@AndreasK. yep, I don't think they feel like fixing it. It's been three years.
It's not necessarily that at all. On my system it was traced to a blas libary, and updating that blas library makes the error go away. So, it seems like it is not entirely correct, despite winning 6 votes.
@DrM if my answer is a solution for some people but not others, is it not still a viable solution?
|
3

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)

4 Comments

I've noticed that the problem occurs at 'cv2.imshow' function because my code is everything that I pasted in here.
@Desiigner I am curious, did you try my suggestion, and did it fix your problem?
@DrM So I tried your code, and it works, no errors at all. I'm getting the original error when running Jupyter Notebook, so I think I should not run the notebook. I also found I can use the notebook as long as I leave out waitKey()
I have the problem with python 3.8 Ubuntu 18.04 and when running the LD_DEBUG I get " 26295: initialize program: python 26295: 26295: 26295: transferring control: python 26295: Traceback (most recent call last): File "<string>", line 1, in <module> ModuleNotFoundError: No module named 'cv2' 26295: 26295: calling fini: python [0] 26295: ". After pip install opencv-python in base conda environment I still have the problem.
0

For me, a fresh environment install fixed the issue. For Conda and environment name <env-name>:

$ conda create --name <env-name>
$ conda activate <env-name>
$ conda install python=3.11
$ pip install opencv-python

To reproduce the issue before and see the fix after, you can run:

import cv2
import os
img = cv2.imread('sample.jpg')
cv2.imshow("my_window", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

If you run into a less important, but annoying message similar to below saying invalid style, then just set the environment variable QT_STYLE_OVERRIDE to something you do have (in my case, Fusion).

QApplication: invalid style override 'kvantum' passed, ignoring it.
    Available styles: Windows, Fusion"

Then your sample code is simply:

import cv2
import os

os.environ['QT_STYLE_OVERRIDE'] = 'Fusion'

img = cv2.imread('sample.jpg')
cv2.imshow("my_window", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Comments

0

For me, neither

pip3 install opencv-python-headless

nor

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

worked. Since I am working in a miniconda env, installing opencv by conda instead of pip solves the problem, i.e.,

conda install opencv

Comments

-1

To me, the solution to this problem was removing Anaconda, then installing pip followed by installing OpenCV with a simple pip install command.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.