Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
799 views
in Technique[技术] by (71.8m points)

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...