Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix the error "QObject::moveToThread:" in opencv in python?

I am using opencv2 in python with the code

import cv2 cv2.namedWindow("output", cv2.WINDOW_NORMAL)        cv2.imshow("output",im) cv2.resizeWindow('output', 400,400) cv2.waitKey(0) cv2.destroyAllWindows() 

I have the error as

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

I debug and found that it happened when I use cv2.waitKey(0). How should I fix it? Thanks

Update: I am using 3.3.0.0. If I use older version, I have error

OpenCV Error: Unspecified error (The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script) in cvDestroyAllWindows, file /io/opencv/modules/highgui/src/window.cpp, line 577 Traceback (most recent call last):   File "tools/demo_handbone.py", line 220, in <module>     demo(net, im_name)   File "tools/demo_handbone.py", line 159, in demo     cv2.destroyAllWindows() cv2.error: /io/opencv/modules/highgui/src/window.cpp:577: error: (-2) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function cvDestroyAllWindows 
like image 266
John Avatar asked Sep 27 '17 13:09

John


2 Answers

I got same problem, it was from opencv-python version problem for me.
My Linux machine's environment is as following:

$ cat /etc/lsb-release  ... DISTRIB_DESCRIPTION="Ubuntu 18.04.5 LTS" $ date Tue Aug 11 11:43:16 KST 2020 $ python --version Python 3.7.8 $ pip list|grep Qt PyQt5                5.15.0 PyQt5-sip            12.8.0 $ pip list|grep opencv-python opencv-python        4.3.0.38 

I downgraded opencv-python 4.3.0.38 to 4.3.0.36.

$ pip uninstall opencv-python $ pip install opencv-python==4.3.0.36 $ pip list|grep opencv-python opencv-python        4.3.0.36 
like image 105
Hill Avatar answered Sep 20 '22 23:09

Hill


First, uninstall any versions of OpenCV you may have installed. If you installed using pip:

sudo pip uninstall opencv-python 

Next, try installing OpenCV using your Linux distro's package manager. For Ubuntu/Debian, this is:

sudo apt-get install libopencv-dev python-opencv 
like image 37
Mateen Ulhaq Avatar answered Sep 18 '22 23:09

Mateen Ulhaq