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

Desiigner picture Desiigner · Sep 14, 2018 · Viewed 9.7k times · Source

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

Answer

smallpants picture smallpants · Apr 3, 2019

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.