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

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

Baby47 avatar May 06 '19 03:05 Baby47

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.

areche avatar May 24 '19 16:05 areche

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=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.

Ekkehard avatar May 24 '19 16:05 Ekkehard

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.

martin3000 avatar May 24 '19 17:05 martin3000

I also lost Okular (pdf viewer) when uninstalling kde-style-breeze-qt4.

ryanneph avatar Jun 07 '19 00:06 ryanneph

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.

native-api avatar Aug 02 '19 02:08 native-api

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

skvark avatar Aug 02 '19 05:08 skvark

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.

native-api avatar Aug 02 '19 06:08 native-api

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.

native-api avatar Aug 02 '19 07:08 native-api

Anyway, without knowing if linking statically is even possible and solves the problem, legal debates are over nothing.

native-api avatar Aug 02 '19 08:08 native-api

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.

martin3000 avatar Aug 02 '19 19:08 martin3000

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-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 environments
  • pip3 uninstall opencv-python on all instances of my python and virtual environments
  • sudo 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 pressed c, again pressed c, then pressed g to generate make files, then
  • sudo make -j8 after successful building, I copied build\lib\python3\cv2.cpython.xxxxxx.so to usr/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.

HamedJafarzadeh avatar Aug 06 '19 12:08 HamedJafarzadeh

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".

BlueSeven277 avatar Nov 11 '19 21:11 BlueSeven277

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...

fdornelasx avatar Jan 08 '20 15:01 fdornelasx

@areche hats off to you debugging skills, it solved my problem in no time..

mfahad1 avatar Mar 10 '20 19:03 mfahad1

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!

harshasunder-1 avatar Apr 07 '20 07:04 harshasunder-1

Next opencv-python release will include Qt5 instead of Qt4. That change will most likely solve this issue.

skvark avatar Apr 07 '20 07:04 skvark

Next opencv-python release 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.

native-api avatar Apr 07 '20 08:04 native-api

Next opencv-python release 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.

skvark avatar Apr 07 '20 08:04 skvark

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 libqt5x11extras5 or kde-style-breeze-qt4 for environment which need the modules. In my case, I work on Ubuntu with KDE. So libqt5x11extras5 or kde-style-breeze-qt4 are important for my desktop environment.

I found there are Qt conflicts between those in opencv-python package and those in system path. The steps to find the problem are the same as @areche:

  1. Display the library loading problems
    export LD_DEBUG=all
  1. Edit script to cause the problem
# fail.py
import cv2
im = cv2.imread('img.jpg')
cv2.imshow('image',im)
cv2.waitKey(0)
  1. Export the library loading information
    python fail.py 2>output.txt
  1. 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.7 in the python packages path(I use conda environment) and 4 in the system path. So it's probably the Qt conflicts.

There are 2 ways to solve this problem:

  1. 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 environment

    cp /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.so
    
  2. Just replace Qtlib in the python packages by Qtlib in system (according to output.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.7 
    

    I can't find libQtTest in my system path but it make no difference.

The 2 solution are OK for my case.

anik-kucse avatar Apr 07 '20 17:04 anik-kucse