Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Qt Error on cv2.imshow - Specific to version 4.2.0.32 #290

Closed
NeilGraham opened this issue Feb 8, 2020 · 17 comments · Fixed by #293
Closed

Qt Error on cv2.imshow - Specific to version 4.2.0.32 #290

NeilGraham opened this issue Feb 8, 2020 · 17 comments · Fixed by #293

Comments

@NeilGraham
Copy link

NeilGraham commented Feb 8, 2020

Issue

Calling cv2.imshow with opencv-python 4.2.0.32 results in the following error:

qt.qpa.plugin: Could not find the Qt platform plugin "cocoa" in ""
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

Solution

I am able to fix the issue by:

  • uninstalling the current version: 4.2.0.32
  • reinstalling the previous version: 4.1.2.30
pip uninstall opencv-python
pip install opencv-python==4.1.2.30

Repoduce

import cv2 
img = cv2.imread('./path/to/picture.png', cv2.IMREAD_GRAYSCALE)
cv2.imshow('image', img)
  • macOS 10.15.2 x86
  • Python 3.8.1
  • opencv-python 4.2.0.32
@NeilGraham NeilGraham changed the title Qt Error on cv2.imshow - Specific to ver. 4.2.0.32 Qt Error on cv2.imshow - Specific to version 4.2.0.32 Feb 8, 2020
@skvark
Copy link
Member

skvark commented Feb 8, 2020

Qt version was upgraded from 4 to 5 in the latest update (Qt 4 dependencies were too old and caused major issues during builds). The issue is most likely related to that upgrade. I don't own a Mac so it's quite hard for me to debug the issue. However, I'll try to look into it.

@skvark
Copy link
Member

skvark commented Feb 9, 2020

See: https://forum.qt.io/topic/98816/qt-could-not-find-the-platform-plugin-cocoa

Set QT_DEBUG_PLUGINS to 1 before running your script. It will tell where Qt is looking for the cocoa plugin.

@dlech
Copy link
Contributor

dlech commented Feb 10, 2020

This is with opencv-contrib-python 4.2.0.32 in a virtual environment.

QFactoryLoader::QFactoryLoader() checking directory path "/usr/local/Cellar/python@3.8/3.8.1/Frameworks/Python.framework/Versions/3.8/Resources/Python.app/Contents/MacOS/platforms" ...
qt.qpa.plugin: Could not find the Qt platform plugin "cocoa" in ""
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

zsh: abort      QT_DEBUG_PLUGINS=1 python __main__.py

I made some progress setting QT_QPA_PLATFORM_PLUGIN_PATH=/usr/local/Cellar/qt/5.13.1/plugins. It gives errors about not being compatible though since it is not the same build as the Qt library in the wheel.

There is /usr/local/Cellar/qt/5.13.1/plugins/platforms/libqcocoa.dylib installed by homebrew, but no such plugin in the opencv-contrib-python wheel.

So, I think the first step is to get libqcocoa.dylib in the binary wheel.

@NeilGraham
Copy link
Author

I am getting a similar error message:

export QT_DEBUG_PLUGINS=1
python3 run.py
QFactoryLoader::QFactoryLoader() checking directory path "/Library/Frameworks/Python.framework/Versions/3.8/Resources/Python.app/Contents/MacOS/platforms" ...
qt.qpa.plugin: Could not find the Qt platform plugin "cocoa" in ""
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

zsh: abort      python3 run.py
  • For what it's worth, I did have an independent version of Qt 5 installed on my computer when I encountered this issue. I attempted to reinstall Qt to fix the issue but had no luck (same error message). At the moment, it is not installed on my system as of executing the command above.

@skvark
Copy link
Member

skvark commented Feb 10, 2020

@dlech Yeah, it looks like libqcocoa.dylib needs to be embedded to the wheel. Also qt.conf file is maybe needed to set the plugin search path relative to the wheel binaries folder: https://doc.qt.io/qt-5/qt-conf.html

I don't own a Mac so it would be helpful if you can test if the Qt which is embedded to the wheels can be configured to look for the dylib from the wheel install directory.

@dlech
Copy link
Contributor

dlech commented Feb 10, 2020

The QT_QPA_PLATFORM_PLUGIN_PATH environment variable can be used to set the plugin search path.

So if we can make it so that QT_QPA_PLATFORM_PLUGIN_PATH is set when the cv2 module is imported, that could be a solution.

@skvark
Copy link
Member

skvark commented Feb 10, 2020

I'm pretty sure that won't work because the environment variable must be set outside / before the process.

@dlech
Copy link
Contributor

dlech commented Feb 10, 2020

It should be fine as long as it is set before we try to use Qt.

@skvark
Copy link
Member

skvark commented Feb 14, 2020

It's not very convenient to require every user to export that environment variable before use.

@dlech
Copy link
Contributor

dlech commented Feb 14, 2020

Yes. I am suggesting that we do it automatically when the cv2 module is imported so that users don't have to do it themselves.

@dlech
Copy link
Contributor

dlech commented Feb 14, 2020

I spend quite a few hours last night trying to get this repo to build so I can actually try it out. I never could get it to build with python3, but finally got it to build with python2. So making some progress.

@skvark
Copy link
Member

skvark commented Feb 14, 2020

It's quite hard to do the export because it must be done outside the Python process. You can't just set it with Python because the process has already started and afaik setting env variables during the process does not have any effect.

@dlech
Copy link
Contributor

dlech commented Feb 14, 2020

Please try this simple example to see that it is in fact possible to modify environment variables after a process starts. It is true that processes cannot modify the environment variables of another process, but the process itself can modify its own environment variables.

import os

os.environ['PYTHONINSPECT'] = 'on'

It has the same effect as PYTHONINSPECT=on python

@dlech dlech mentioned this issue Feb 14, 2020
@skvark
Copy link
Member

skvark commented Feb 15, 2020

Thanks for the clarification, I probably mixed this up with the case where another process needed to modify the environment variables (I was trying to do something like that a few years back with some other project...). Thanks for the PR, I'll have a look at it during next week.

@jkterry1
Copy link

When will this be fixed? This is a real problem for me

@prasadnoolu
Copy link

try degrading opencv-contrib by something like sudo pip install opencv-contrib-python==4.0.0.21

this will help

@skvark
Copy link
Member

skvark commented Apr 7, 2020

@prasadnoolu This has been fixed and released in 4.2.0.34.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants