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

macOS crash in imshow with Qt5.15 #376

Closed
skvark opened this issue Aug 13, 2020 · 28 comments
Closed

macOS crash in imshow with Qt5.15 #376

skvark opened this issue Aug 13, 2020 · 28 comments
Assignees

Comments

@skvark
Copy link
Member

skvark commented Aug 13, 2020

See opencv/opencv#18079 for background.

I do not own a Mac so I cannot fix this easily by myself without trial and error which will take a lot of time. As a summary, Info.plist file should exist in the packages.

Similar issue in PyQt (note that their package structure is different): https://riverbankcomputing.com/pipermail/pyqt/2020-January/042419.html

Related things in this repository:

@skvark
Copy link
Member Author

skvark commented Aug 14, 2020

As a hotfix I'll publish new packages with Qt downgraded to 5.13. This issue needs to however solved somehow in the future.

@stuf
Copy link

stuf commented Aug 14, 2020

What timing! Stumbled upon this problem just 30 minutes ago, I'll have a look if it works.

@skvark
Copy link
Member Author

skvark commented Aug 14, 2020

Note that there is no new release yet, it will take a couple of days to go back to Qt 5.13. But it would be good if someone could have a look into copying the Info.plist file to the packages.

@skvark
Copy link
Member Author

skvark commented Aug 14, 2020

@skvark
Copy link
Member Author

skvark commented Aug 16, 2020

Qt is now forced to version 5.13.2 on macOS which should work as a temporary fix. New versions should be available in PyPI during next few days.

@jdifran
Copy link

jdifran commented Aug 27, 2020

@skvark I notice after upgrading to opencv-python 4.4.0.40 or 4.4.0.42 (using pip install opencv-python) on macOS 10.12 there are issues with cv2.imshow(). Zoom and pixel/location functionality is broken.

I've tracked this down to QT not being included in the new pypi package for macOS, but I don't know why this is, especially based on this thread which mentions it being updated. I've tried building the wheel locally as well and QT is no longer available under the listed GUI in cv2.getBuildInformation():

GUI:
Cocoa: YES
VTK support: NO

I was previously on opencv-python 4.1.1.26 where QT is listed under GUI, and cv2.imshow is working fine. I was upgrading for SIFT support as well as other new opencv features but this is a blocker as I have tools built with cv2 which do not work without QT GUI. On 4.1.1.26 cv2.getBuildInformation():

GUI:
QT: YES (ver 4.8.7 EDITION = OpenSource)
QT OpenGL support: NO
Cocoa: YES
VTK support: NO

Is there something I need to do for QT to be enabled or is this related to the other QT issues? I am wondering if anyone else is seeing this on MacOS as well

@skvark
Copy link
Member Author

skvark commented Aug 27, 2020

Qt should be enabled in the macOS builds (Qt 5.13). This thread tracks specific issue with Qt 5.15 which has been solved by not using version 5.15. Post the output of cv2.getBuildInformation() for 4.4.0.42. Enabling Qt in locally built packages requires that you have Qt development libraries installed on your system.

Edit: I do not have a Mac, but confirmed that Qt is included by downloading and extracting macOS wheel:

image

@mshabunin
Copy link

Package 4.4.0.42 uses Qt:

General configuration for OpenCV 4.4.0 =====================================
  Version control:               4.4.0-dirty

  Platform:
    Timestamp:                   2020-08-16T22:55:22Z
    Host:                        Darwin 17.7.0 x86_64
    CMake:                       3.18.0
    CMake generator:             Unix Makefiles
    CMake build tool:            /usr/bin/make
    Configuration:               Release
...
  GUI:
    QT:                          YES (ver 5.13.2)
      QT OpenGL support:         NO
    Cocoa:                       YES
    VTK support:                 NO
$ pip freeze
certifi==2020.6.20
numpy==1.19.1
opencv-python==4.4.0.42

@skvark
Copy link
Member Author

skvark commented Aug 27, 2020

Thanks @mshabunin!

@jdifran Please check that you are actually using the correct pre-built package / your environment is consistent. It's highly unlikely that I could fix the issue you are having in this repository.

@jdifran
Copy link

jdifran commented Aug 27, 2020

Thanks @skvark and @mshabunin , I am assuming this is due to being on an older macOS version (10.12) and Xcode 9 where QT5 requires per their support docs:

macOS 10.13, 10.14, 10.15 | x86_64 and x86_64h | Xcode 11 (10.15 SDK)

Is this just a case of QT getting excluded during the install due to the wrong platform requirements? I notice I see this with other versions of opencv-python (4.3) besides the ones that include QT 4.8.7. I am assuming this is a user requirements issue as this point which is why it works in 4.1.1.26

@skvark
Copy link
Member Author

skvark commented Aug 27, 2020

Qt is always included in the pre-built non-headless packages (pip install opencv-python or pip install opencv-contrib-python), it does not get magically excluded during install from those packages.

The latest pre-built wheels have minimum requirement of macOS 10.13 (see: https://github.com/skvark/opencv-python#backward-compatibility).

Therefore, I believe that your problem is that pip has compiled OpenCV manually from the newly introduced source distribution because your system is not compatible with the pre-built packages. CMake just didn't find Qt from your machine, because it probably isn't there or if it is, you need to add additional flags for CMake before the build: https://github.com/skvark/opencv-python#manual-builds

Easiest way to fix this would be of course to upgrade to latest macOS version. Otherwise you will have to learn how to configure OpenCV build system to find your Qt SDK and build OpenCV manually.

@skvark
Copy link
Member Author

skvark commented Jan 2, 2021

The most recent release (4.5.1.48) drops Qt support on macOS. If you need Qt support, consider contributing.

@xgfd
Copy link

xgfd commented Jan 17, 2021

@skvark Just encountered this issue on macOS. As I understand you need to figure out a way copying the Info.plist file to the packages. Could you elaborate and I'll see whether I could help. Thanks.

@skvark
Copy link
Member Author

skvark commented Jan 18, 2021

4.4.0.40 is the only build that has this issue. The latest packages do not have Qt so you should not encounter the issue anymore. However, if you need Qt and want to help to add it back to the packages, then you can help by looking into the Info.plist file.

I'm not familiar with macOS software packaging requirements, so I can't help much. All I know is that Qt probably has the Info.plist file somewhere and I guess it should be next to the shared libraries inside the wheel packages. You can get Qt 5.15 via Homebrew, find the file and add it to the 4.4.0.40 install location. After that you can try to use imshow. If it crashes, the file is in the wrong place. But again, I'm just making guesses here.

@xgfd
Copy link

xgfd commented Jan 18, 2021

Both Qt5 and PyQt has a lib folder in parallel with the plugins folder, which has the following structure:
image

So I did the obvious thing to copy lib/QtCore.framework (and later the whole lib) to site-packages/cv2/qt to create the following structure:

image

Unfortunately it didn't work. lldb gave the same traces as before. I'll poke around but honestly I don't have a clue right now. Is there a way to tell Qt where to look for those files? Or how does Qt look for these files?

@skvark
Copy link
Member Author

skvark commented Jan 18, 2021

Here you can find the link to relevant parts in Qt's source: opencv/opencv#18079 (comment)

Apple's docs: https://developer.apple.com/documentation/corefoundation/cfbundle?language=objc#overview

@xgfd
Copy link

xgfd commented Jan 18, 2021

Yeah went through those links but it's quite hard to figure out what's going on by just looking at source. I also tried setting os.environ["QT_QPA_PLATFORM_PLUGIN_PATH"] to the system Qt5 installation but got the same error. Will keep looking.

@skvark
Copy link
Member Author

skvark commented Jan 18, 2021

I believe that the QT_QPA_PLATFORM_PLUGIN_PATH and the qt plugins folder is not relevant in this case. QtCore shared lib is trying to load the macOS bundle from somewhere, maybe directly next to the QtCore shared library. On Linux they are located in here: site-packages/opencv_python.libs/, maybe the place is similar on macOS.

@skvark
Copy link
Member Author

skvark commented Jan 18, 2021

Another thing to try is to place the bundle / file directly next to the cv2.so.

@xgfd
Copy link

xgfd commented Jan 18, 2021

On macOS there are only cv2 and opencv_python-4.4.0.40.dist-info inside opencv_python-4.4.0.40-cp38-cp38-macosx_10_13_x86_64.whl and site-packages. I'll try placing it next to the so file.

@xgfd
Copy link

xgfd commented Jan 18, 2021

Tried various locations following macOS bundle structures Applications, Frameworks and Loading Bundles, and Qt docs, but with no luck. The cv2 package doesn't look like any apple bundle (changing the cv2/qt folder to Contents to mimic an application bundle didn't work either), which in the case of PyQt it resembles a Framework.

Then I came across this article Qt is relocatable, where it states that you can turn off this relocation feature
image

Can/Should this be done during the building process of opencv-python?

@skvark
Copy link
Member Author

skvark commented Jan 18, 2021

It requires full rebuild of Qt. It might help, but too much work to maintain separate build and it would take ages to build in the CI pipeline.

This comment in the Qt sources indicates that the Bundle location is relative to the QtCore shared library: https://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/global/qlibraryinfo.cpp#n487

Which means in practice that the Resources/Info.plist should be probably under .dylibs folder which is inside the wheels. I don't know where that folder is placed during install, but I would guess it similar to Linux: site-packages/opencv_python.dylibs/

@xgfd
Copy link

xgfd commented Jan 18, 2021

Placing plist directly under .dylibs or .dylibs/Resources didn't work. Found this link and related discussions. Seemed relevant since we're shipping Qt without frameworks too.

@skvark
Copy link
Member Author

skvark commented Jan 20, 2021

It seems that the only solution is to use a custom Qt build. That would require some custom repository to build and package it as a Homebrew bottle for example. Qt cannot be built as a part of the current build pipelines since it is such a large and heavy project.

@asmorkalov
Copy link
Collaborator

The most recent release (4.5.1.48) drops Qt support on macOS and should fix this issue.

@asmorkalov
Copy link
Collaborator

Related issue: #423

@asmorkalov asmorkalov self-assigned this Feb 9, 2021
@asmorkalov
Copy link
Collaborator

Tested with OpenCV-Python 4.5.1, works without issues.

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

No branches or pull requests

6 participants