opencv-python icon indicating copy to clipboard operation
opencv-python copied to clipboard

opencv GUI does not work

Open braindevices opened this issue 9 years ago • 91 comments

cv2.namedWindow('test', cv2.WINDOW_AUTOSIZE) results in an error:

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

here is the version:

cv2.__version__
Out[3]: '3.3.0'
QtCore.QT_VERSION_STR
Out[7]: '5.6.2'

braindevices avatar Sep 12 '17 07:09 braindevices

I removed libqt5x11extras5 but my display manager (kde plasma) broke. Seems like its no solution for me :(

uhdzf avatar Aug 09 '18 05:08 uhdzf

Unfortunately, I also suffer from this bug. I hope some smart person can fix it.

andyvk85 avatar Aug 12 '18 21:08 andyvk85

Have met this problem twice. Changing qt5 to qt4 works for me.

RunningLeon avatar Sep 21 '18 09:09 RunningLeon

I also have this issue. Any updates on potential fixes?

ThomasWMarshall avatar Sep 23 '18 20:09 ThomasWMarshall

The status is the same as before: OpenCV does not support namespaced Qt builds -> this issue cannot be fixed: https://github.com/skvark/opencv-python/issues/46#issuecomment-404942996

skvark avatar Sep 24 '18 16:09 skvark

If that is so, does that mean that OpenMV-python will never work with KDE, which is Qt-everything and turned out to be at the root of my problems with OpenMV? If that is the case, this should probably be mentioned somewhere predominantly as it would save lots of people lots of headaches.

On Monday, September 24, 2018 9:15:29 AM PDT Olli-Pekka Heinisuo wrote:

The status is the same as before: OpenCV does not support namespaced Qt builds -> this issue cannot be fixed: https://github.com/skvark/opencv-python/issues/46#issuecomment-404942996

Ekkehard avatar Sep 25 '18 00:09 Ekkehard

The precompiled opencv-python packages generated by this repository will not work properly in some GNU/Linux systems. Some solutions:

  • If you need GUI support, build OpenCV python bindings yourself and link the library against your system Qt.
  • If you need GUI support but do not wish to build opencv-python yourself, use opencv-python-headless and install some other Python library which you can use to display images.
  • If you don't need GUI support, just install opencv-python-headless.

skvark avatar Sep 25 '18 06:09 skvark

@skvark Just out of curiosity, I do have a KDE Neon install on which opencv works with gui support. It's using Qt 5.11.1. My other Neon install, which is a newer version, doesn't work. Do you know why it might work sometimes out of the box?

Additionally, might there be a way to run the guis with GTK instead of Qt?

ThomasWMarshall avatar Oct 07 '18 18:10 ThomasWMarshall

Most likely older Qt versions work better together with the Qt 4.8.7 which is bundled in the opencv-python wheels. The only real fix for this is the namespaced Qt build but unfortunately it's impossible to use it since being compatible with it would require a rewrite of the OpenCV Qt GUI code.

I tried to initially enable GTK but it got too complicated / impossible due to the many dependencies GTK has. These all depedencies must be available or built on the CentOS 5 environment which in turn is used to build the wheels.

CentOS 5 is a very old release. The manylinux1 format requires that the wheels are built on that old version to be compatible with as many as possible GNU/Linux distributions. Manylinux2 (https://github.com/pypa/manylinux/pull/152) will update the requirement to CentOS 6.x, which will be hopefully easier to work with and makes it possible to either switch to Gtk or upgrade Qt to the most recent version.

In any case, it's most likely to impossible to provide pre-built packages which satisfy the needs of all users on all different platforms. There's always some compromise which has to be made considering the complexity of OpenCV.

skvark avatar Oct 07 '18 20:10 skvark

Just want to pitch in and say, I used opencv-python for CV 3.4.3, and while I do get the same "cannot move to target thread" error, the imshow, and waitKey() functions work fine.

Building from source, I remember the focus would switch to the imshow() window. I see that behaviour when I try imshow(), waitKey(0), and destroyAllWindows() in pycharm. However, on the terminal, the focus doesn't switch, but the window does appear, and when I alt-tab to that window, the behaviour is as expected.

It's a weird bug, and I'm sure @skvark must have spent a lot of time on it, but not something that's breaking my workflow.

for reference, python 2.7 opencv-python 3.4.3.18 virtualenv 16.0.0 Ubuntu 16.04

QMake version 3.0 Using Qt version 5.5.1 in /usr/lib/x86_64-linux-gnu

Happy to pitch in with any further info. Thanks a lot @skvark for your work on this. I've spent hours and hours getting OpenCV running with Python before, and these pip binaries are the best ever!

SreenivasVRao avatar Oct 13 '18 17:10 SreenivasVRao

Hi folks,

I received the same error when working within a virtual environment in Python 3 and opencv 3.3 The following simple steps solved my issue. Hope it helps.

  1. sudo apt-get autoremove
  2. sudo apt-get autoclean
  3. sudo apt-get update

myaimmystyle avatar Oct 24 '18 12:10 myaimmystyle

I had two openCV (opencv-contrib-python==3.4.3.18 and opencv-python==3.4.1.15) on python 3.5, ubuntu 16.04. When I ran sudo pip uninstall opencv-python it unistalled opencv-python-3.4.1.15. I no longer get the same errors

lagvier avatar Nov 11 '18 10:11 lagvier

Same problem. Tried updating to opencv-contrib-python(3.4.3.18, 3.4.4.19), as well as multiple versions of opencv-python (3.1.0.0+). Version for python2 (2.4.9.1) works fine but it does not support python3. Thank you for looking into the problem!

alexpashevich avatar Dec 04 '18 12:12 alexpashevich

Just for the records: This does not help with the issue related to this package here, but you might solve your problem by installing OpenCV with Python bindings from source, as described here:

https://www.pyimagesearch.com/2018/08/15/how-to-install-opencv-4-on-ubuntu/

Solved it for me!

haimat avatar Jan 08 '19 14:01 haimat

I meet the same issue, and I used opencv-python&opencv-contrib-python=3.4.4.19 at the beginning, and then I updated to 4.0.0 but still have this error. And also I tried on both python2.7 and python 3.5 on Ubuntu 16.04... I find the libs of opencv have one more lib file than @skvark you showed before, which is libvpx-81a43c0a.so.5.0.0:

/usr/local/lib/python3.5/dist-packages/cv2/.libs$ ls
libavcodec-eac15e48.so.58.21.104   libQtTest-1183da5d.so.4.8.7
libavformat-b6bcbe33.so.58.17.101  libswresample-b4363bfa.so.3.2.100
libavutil-e1b1a17d.so.56.18.102    libswscale-15b3fdc6.so.5.2.100
libQtCore-9549151f.so.4.8.7        libvpx-81a43c0a.so.5.0.0
libQtGui-6d0f14dd.so.4.8.7         libz-a147dcb0.so.1.2.3

Idk if this matters. And my test code is:

$ python
Python 3.5.2 (default, Nov 12 2018, 13:43:14) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'4.0.0'
>>> im = cv2.imread('./data/cage/106018878.jpg')
>>> im.shape
(256, 256, 3)
>>> cv2.imshow('image', im)
QObject::moveToThread: Current thread (0x22e7370) is not the object's thread (0x238fed0).
Cannot move to target thread (0x22e7370)

Segmentation fault (core dumped)

And the function cv2.namedWindow() has ths same error while cv2.waitKey(0) works fine. And I show the Qt version if it could help

$ qmake -v
QMake version 2.01a
Using Qt version 4.8.7 in /usr/lib/x86_64-linux-gnu

chutongz avatar Jan 14 '19 02:01 chutongz

@chutongz libvpx is a dependency of FFmpeg, it's not related to this issue.

I will repeat myself:

The root of this issue is that opencv-python packages ship with their own Qt build and on some Linux systems there's a system Qt installation which does not play along with the one supplied with opencv-python.

To my understanding, the only way to avoid this issue completely is to use namespaced Qt build (https://wiki.qt.io/Qt_In_Namespace). It would require modifications to OpenCV GUI code to use such build of Qt. This is not practical and cannot be done in this repository. This is why the issue has been marked with a tag "wontfix".

One thing you could try is to check the link order of the dependencies (I don't know if this works):

export LD_DEBUG=all
python you_test_script.py

If that works, it will print the libs which the linker is accessing. See this for more info: http://www.bnikolic.co.uk/blog/linux-ld-debug.html

If you system Qt appears / is linked before the one supplied with opencv-python then the issue could be probably fixed by changing linker search order.

skvark avatar Jan 21 '19 07:01 skvark

So I just had the same problem and fixed it for my system (ubuntu 16.04).

I checked in my logs recent installations that contained dependencies with libraries that use Qt vim /var/log/apt/history.log

Looking in that file I saw that I recently installed okular and that okular installed libraries that depend on Qt. I removed okular and everything seems to be working now.

sudo apt-get remove okular
sudo apt-get autoremove

oarriaga avatar Mar 20 '19 10:03 oarriaga

Doing this https://github.com/skvark/opencv-python/issues/46#issuecomment-424217280

Borked my entire system and had to spend over an hour recovering it.

richardARPANET avatar Mar 29 '19 12:03 richardARPANET

How can I find out what the different Qt versions are?

martin3000 avatar Apr 20 '19 19:04 martin3000

i was having the same problem on ubuntu 18. compiling the opencv from source solved this issue. its clearly about qt so in cmake configurations i used -D WITH_GTK=ON
instead of -D WITH_QT=ON
and everything worked fine after this

bayvehbi avatar May 02 '19 15:05 bayvehbi