opencv-python: opencv GUI does not work
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'
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 41
- Comments: 91 (28 by maintainers)
Commits related to this issue
- Add self-built python-opencv modcule This was necessary, because for the pip-install of opencv-contrib-python, the Qt GUI does not work. See this links for more details: https://www.learnopencv.com/i... — committed to nimwegenLab/moma-preprocessing by packoman 5 years ago
- Add self-built python-opencv modcule This was necessary, because for the pip-install of opencv-contrib-python, the Qt GUI does not work. See this links for more details: https://www.learnopencv.com/i... — committed to nimwegenLab/moma-preprocessing by packoman 5 years ago
- Update environment configuration 1. Force to use scikit-learn>=1.3 to avoid `DeprecationWarning` from NumPy. 2. Use opencv-contrib-python-headless to avoid conflicts with PyQt installed with conda (h... — committed to aywi/mprotonet by aywi 7 months ago
In my case the Qt conflicted with the Qt of the system due to a KDE plugin.
This is how I found it:
export LD_DEBUG=all
This allowed me to see all the libraries that are used.python cvFail.py 2> output.txt
I reproduce the problem and capture all the errors (which thanks to LD_DEBUG contain all library dependencies)unset LD_DEBUG
No need to the debug anymoregrep libQt output.txt
Found hundreds of references to Qt, but the more interesting was that two different versions appeared: libQtCore-3dbacd8a.so.4.8.7 (from the virtual environment) and libQtCore.so.4 (from the system)Using
less output.txt
search for libQtCore.so.4 to know which other library was loading it. I found that was breeze.so, so I search which other library was loading it and it turn out to be libQtCore-3dbacd8a.so.4.8.7.Conclusion: The OpenCV distributed Qt tries to load breeze.so and this library then loads the system Qt introducing an inconsistency and making the application crash.
Lets find who install breeze.so
dpkg -S /usr/lib/kde4/plugins/styles/breeze.so
It seems to be a library from a kde style plugin (kde-style-breeze-qt4
), so I remove this plugin:apt-get remove kde-style-breeze-qt4
And my problem was solved! šI hope this helps someone.
I didnāt have this issue at first, then cv2.namedWindow just stopped working for me without me making any system changes. I noticed in dmesg the following: python[29883]: segfault at 0 ip 00007fa8c3c114be sp 00007ffcf5c34740 error 4 in libxcb.so.1.1.0[7fa8c3c07000+21000]
After a quick Google, I found this thread, which recommended removing the libqt5x11extras5 package. I just removed that (and the 121 dependent packages), and itās working properly again. So like @skvark mentioned, itās probably due to Qt conflicts.
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
copy from https://reformatcode.com/code/python/how-to-fix-the-error-quotqobjectmovetothreadquot-in-opencv-in-python
I advise strongly against removing libqt5x11extras5 unless you know exactly what you are doing. It killed my lightdm display manager, and, sadly, didnāt fix the issue for me.
If you have this issue: try to remove libqt5x11extras5 package (see @coxbrandonās message above) and please report here if that solution works for you. Note: do this at your own risk.
GUI problem with opencv 3.4.0 and ubuntu 16.04 - python 2.7
Iāll try to rebuild the Docker images during next weekend and publish a new release after that.
After some googling with different keywords I found this: https://stackoverflow.com/a/42955557/5795428
It indeed seems that the Qt 4.8.7 which ships with
opencv-python
somehow conflicts with the system (or some other) Qt installation. I can try to rebuild the Docker images and Qt in them with-qtnamespace Qt
flag which should prevent symbol clashes.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.
The precompiled
opencv-python
packages generated by this repository will not work properly in some GNU/Linux systems. Some solutions:opencv-python-headless
and install some other Python library which you can use to display images.opencv-python-headless
.I guess the problem is that Ubuntu 16.04 opencv libraries are 2.4.9.1 and that doesnāt work with opencv-python 3.x. Iāve got the same problem here - the apt-installed opencv-python 2.4.9.1 works fine but pip-installed opencv-python 3.4.0 fails with the very same program (simple imread()/imshow()) with:
Unfortunately opencv-python 2.4.9 doesnāt support Python3 as far as I know so Iām stuck with Python2 for now.
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.
I have the same problem with opencv-python 3.4.0.12 (installed via pip) python 3.5.2, ubuntu 16.04.4 LTS, whenever I use cv2.namedWindow or cv2.imshow. No virtualenv, nothing fancy is needed:
Same is true for opencv-contrib-python.
My problem was caused by QT. before I install QT, my opencv was fine. I uninstalled QT but it did not help. I finally was able to make it work through āsudo apt-get autoremove libqt5x11extras5ā.
@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 withopencv-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):
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.New Docker images are now up. Iāll publish a new release during next week.
EDIT: Requires more work, the new Qt build is not linked properlyā¦
@coxbrandon thank you very much! Same problem, that solution worked like a charm. Thanks a lot! (I also had that number of dependent packages - on Linux Mint).
@caixiaofan This simply lets you use the opencv binding shipped in apt-get and stop using this repo. IMHO issues in this repo should focus on how to solve problems in this library.
Thanks @NikkoWei. As I donāt want to break my KDE DE I followed your instruction. test code runs perfectly.
In my case, I did almost all the solutions:
kdelibs5-plugins
which was an important module for most of my desktop environments, so I give up on uninstalling it.Finally, I found a solution for my case
pip uninstall opencv-python
on all instances of my python and virtual environmentspip3 uninstall opencv-python
on all instances of my python and virtual environmentssudo apt remove python-opencv
then I downloaded the most recent opencv, in my case : opencv-4.1.1 , I build it. for building :
mkdir build; cd build; ccmake ..
then I pressed pressedc
, again pressedc
, then pressedg
to generate make files, thensudo make -j8
after successful building, I copiedbuild\lib\python3\cv2.cpython.xxxxxx.so
tousr/local/lib/python3.5/dist-packages/
and renamed it to cv2.soDo the same for each virtualenv that you have. ( Iām not sure if it is logical, but it worked for me).
then everything worked fine without any error.
For those having this issue in Anaconda installation, just
pip uninstall your opencv-python
, and install opencv using conda :that should fix your problems.
thanks for your reply about this issue, @skvark, @chutongz, I meet the same error when using opencv-python:QObject::moveToThread: Current thread (0x22e7370) is not the objectās thread (0x238fed0). Cannot move to target thread (0x22e7370)
and I reinstall opencv-python under conda environment, now i can not even import cv2 under anaconda environment. It makes an error like this: ImportError: libavcodec-7625dabe.so.58.6.103: cannot open shared object file: No such file or directory
/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
All this files do not exist, How can i fix it ? Thanks for your help
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!
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.
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:
Iām sorry but Iām still unable to replicate this issue. I took a fresh Ubuntu 16.04.3 Virtualbox image and run multiple different tests and even tried to deliberately break the installation. No errors.
Test script which I used:
Dynamic linking is OK. The static library which ships with these wheels should look like below when inspected through readelf and ldd. RPATH entry should point to the .libs/ folder which should contain these files:
Readelf:
ldd:
I did also
sudo apt-get install libopencv-dev python-opencv
and installedpip install opencv-python
next to it (do not do this) but nothing broke.To be able to fix this or to provide some solution, I still need more information. This means that every thing related to these is important:
opencv-python
Please note that
opencv-python
versions < 3.3.0.10 do not support GUI functionality and thus support for it cannot be enabled in them without a full rebuild which is impossible because the packages are pre-built. So if you see this error, you are using too old package: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
Releases page contains changelogs for this package: https://github.com/skvark/opencv-python/releases