pythonqtopencv

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

Solution

  • First of all, 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 install libopencv-dev python3-opencv