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'
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
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 anymore
grep 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.
You made my day - maybe my year - Alex! :-)
Any chance you know if there is anything that uses or worse depends on this style?
Ekkehard
On Friday, May 24, 2019 9:16:24 AM PDT Alex Reche wrote:
In my case the Qt conflicted with the Qt of the system due to a kde4 plugin.
This is how I found it:
export LD_DEBUG=allThis allowed me to see all the libraries that are used.
python cvFail.py 2> output.txtI reproduce the problem and capture all the errors (which thanks to LD_DEBUG contain all library dependencies)
unset LD_DEBUGNo need to the debug anymore
grep libQt output.txtFound 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.txtsearch 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.soIt 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-qt4And my problem was solved! :-)I hope this helps someone.
apt-get remove kde-style-breeze-qt4
Description: Widget style for Qt 4 and KDE Software This is the default widget style for KDE's Plasma desktop. This build has the plugin for with Qt 4. This package is part of the Breeze theme module.
The following packages will be REMOVED: amarok amarok-utils kde-baseapps-bin kde-runtime kde-style-breeze-qt4
I found that I had opencv installed with apt and another version installed with pip3. After doing a pip3 uninstall, it worked.
I also lost Okular (pdf viewer) when uninstalling kde-style-breeze-qt4.
I had a thought here...
Would it help if we linked to Qt statically? Then there would be no conflicts between two sets of shared entitites 'cuz one of them won't be shared.
The affected users can test this by passing -static to Qt configure in the Dockerfile; I don't know if anything else will need tweaking.
That could be nice, but licensing is an issue. The LGPL license allows only dynamic linking. Static linking might be possible if some special rules are followed, but as there is a commercial company behind Qt, I would not go with the static linking. Also maintenance would become harder because
(1) If you statically link against an LGPLed library, you must also provide your application in an object (not necessarily source) format, so that a user has the opportunity to modify the library and relink the application. http://www.gnu.org/licenses/gpl-faq.html#LGPLStaticVsDynamic
The LGPL license allows only dynamic linking.
Not quite, see open source - GPL/LGPL and Static Linking - Stack Overflow (the corresponding part of LGPL is https://www.gnu.org/licenses/lgpl-3.0.html#section4). We provide the source code for the LGPLed parts in a permitted way1 and the entire work, so we are in the clear here since the user can compile the work with a modified version of the library yada yada.
1By storing the exact version in the result that can be retrieved with getBuildInformation() or by looking at the build scripts and implicitly promising the source code for it will be available on demand (e.g. on the official site) for at least 3 years from our release date.
We provide the source code for the LGPLed parts in a permitted way and the entire work
I stay (partially) corrected. We provide the source code but we don't oblige the users to do so (MIT license). So if they distribute a version with modifications to our portion and not provide the source, they'll be violating LGPL. Which means that the work as a whole is not effectively under the MIT license... We re still legally in the clear IMO since we ship all the applicable licenses and say what they apply to, but that's still not a very nice thing to do.
Anyway, without knowing if linking statically is even possible and solves the problem, legal debates are over nothing.
Here I had pip3 install opencv-contrib and apt install python3-opencv, so I had 2 packages. After uninstalling the pip3-package, the problem was gone.
In my case, I did almost all the solutions:
- installing using pip3 install opencv-python
- installing using apt install python-opencv
- I followed @areche solution, I end up with
kdelibs5-pluginswhich 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-pythonon all instances of my python and virtual environmentspip3 uninstall opencv-pythonon 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 pressedgto generate make files, thensudo make -j8after successful building, I copiedbuild\lib\python3\cv2.cpython.xxxxxx.sotousr/local/lib/python3.5/dist-packages/and renamed it to cv2.so
Do 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.
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".
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".
I did this and now I can't login on my Kubuntu.
Don't do this.
Now I am trying to fix and then I will post the solution...
@areche hats off to you debugging skills, it solved my problem in no time..
Areche debugging procedure above worked for me. In my case the problem library was kdelibs5-plugins which i think got installed with amarok, the music player. I guessed this was the problem library because i saw libQtCore being a part of this library in the output file.
Thanks areche!
Next opencv-python release will include Qt5 instead of Qt4. That change will most likely solve this issue.
Next
opencv-pythonrelease will include Qt5 instead of Qt4. That change will most likely solve this issue.
How? It's going to conflict with any other Qt5 installation present just the same.
Next
opencv-pythonrelease will include Qt5 instead of Qt4. That change will most likely solve this issue.How? It's going to conflict with any other Qt5 installation present just the same.
It's the same major version so I assume it should have better compatibility. But of course there might be still some conflicts.
Edit: by the same version I mean that most of the systems use Qt5 nowadays. Qt4 is very old and the issues might be related only to Qt4 versions.
Thanks @NikkoWei. As I don't want to break my KDE DE I followed your instruction. test code runs perfectly.
It's risky to remove
libqt5x11extras5orkde-style-breeze-qt4for environment which need the modules. In my case, I work on Ubuntu with KDE. Solibqt5x11extras5orkde-style-breeze-qt4are important for my desktop environment.I found there are Qt conflicts between those in
opencv-pythonpackage and those in system path. The steps to find the problem are the same as @areche:
- Display the library loading problems
export LD_DEBUG=all
- Edit script to cause the problem
# fail.py import cv2 im = cv2.imread('img.jpg') cv2.imshow('image',im) cv2.waitKey(0)
- Export the library loading information
python fail.py 2>output.txt
- I found there are two version of Qt are used by loader in
output.txt:... 10836: symbol=_ZNSbIwSt11char_traitsIwESaIwEEC1ERKS2_; lookup in file=/home/lnwei/.conda/envs/opencv_test/lib/python3.6/site-packages/cv2/.libs/libQtGui-903938cd.so.4.8.7 [0] 10836: symbol=_ZNSbIwSt11char_traitsIwESaIwEEC1ERKS2_; lookup in file=/home/lnwei/.conda/envs/opencv_test/lib/python3.6/site-packages/cv2/.libs/libQtTest-1183da5d.so.4.8.7 [0] 10836: symbol=_ZNSbIwSt11char_traitsIwESaIwEEC1ERKS2_; lookup in file=/home/lnwei/.conda/envs/opencv_test/lib/python3.6/site-packages/cv2/.libs/libQtCore-6df570dd.so.4.8.7 [0] ... 10836: symbol=_ZTI14QWindowSurface; lookup in file=/usr/lib/x86_64-linux-gnu/libQtDBus.so.4 [0] 10836: symbol=_ZTI14QWindowSurface; lookup in file=/usr/lib/x86_64-linux-gnu/libQtCore.so.4 [0] 10836: symbol=_ZTI14QWindowSurface; lookup in file=/usr/lib/x86_64-linux-gnu/libQtGui.so.4 [0] ...As you see, there are two version Qtlib are used:
4.8.7in the python packages path(I use conda environment) and4in the system path. So it's probably the Qt conflicts.There are 2 ways to solve this problem:
Rebuild opencv by source to link Qtlib in system path. After
make -j8, move the python library of opencv to overwrite the library in your python packages environmentcp /path/to/build/lib/python3/cv2.cpython-36m-x86_64-linux-gnu.so /home/lnwei/.conda/envs/opencv_test/lib/python3.6/site-packages/cv2/cv2.cpython-36m-x86_64-linux-gnu.soJust replace
Qtlibin the python packages byQtlibin system (according tooutput.txt)cp /usr/lib/x86_64-linux-gnu/libQtGui.so.4 ./libQtGui-903938cd.so.4.8.7 cp /usr/lib/x86_64-linux-gnu/libQtCore.so.4 ./libQtCore-6df570dd.so.4.8.7I can't find
libQtTestin my system path but it make no difference.The 2 solution are OK for my case.