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

cv2.imshow() not working consistently #362

Closed
GokulNarayan opened this issue Jul 16, 2020 · 28 comments
Closed

cv2.imshow() not working consistently #362

GokulNarayan opened this issue Jul 16, 2020 · 28 comments

Comments

@GokulNarayan
Copy link

Expected behaviour

imshow displays image

Actual behaviour

when calling cv2.imshow(), I mostly get a small window with the correct name but containing only a black screen. It does work sometimes however but it is unpredictable. This issue was not encountered in previous versions of opencv-contrib-python.
error

Steps to reproduce

  • example code

import cv2
import os
import imutils

image = cv2.imread("ref_image.png")

cv2.imshow("test", image)
cv2.waitKey(0)

cv2.destroyAllWindows()

  • operating system : Ubuntu 20.04
  • architecture (e.g. x86): x64
  • python version: 3.8
  • opencv-python version: opencv-contrib-python 4.3.36
@skvark
Copy link
Member

skvark commented Jul 16, 2020

Yeah, probably same issue as here: #356

I have no idea what causes that behavior. Qt4 version is the same, only the base image where the wheels and Qt4 are built was changed from manylinux1 to manylinux2014.

@ghost
Copy link

ghost commented Jul 18, 2020

@skvark Do you know any workaround to solve this? it happens on any version of opencv-contrib-python using ubuntu 20

@skvark
Copy link
Member

skvark commented Jul 18, 2020

I had a look at the linked OpenCV issue and it seems that this issue is related to Qt conflicts. There is no proper workaround for the latest wheels (see next message for some options). Best option would be to bundle Qt5 to the wheels (Qt4 is very old and does not behave well with other global Qt system installations) but there were major issues when I tried that during the manylinux version upgrade (#309). Auditwheel might as well cause the issue, it bundles the dependencies into the wheels and it might be doing something wrong.

@skvark
Copy link
Member

skvark commented Jul 18, 2020

How to avoid this issue until it's solved:

  • downgrade to the previous release which uses manylinux1 (4.2.0 and 3.4.9)
  • use some proper GUI framework to display the images such as PyQt5
  • build OpenCV wheel locally and use it, instructions are in the readme: https://github.com/skvark/opencv-python#manual-builds

@skvark
Copy link
Member

skvark commented Jul 20, 2020

I will rebuild the manylinux2014 images and check if that fixes this issue, auditwheel has had some updates since the last build.

@skvark
Copy link
Member

skvark commented Jul 21, 2020

That didn't help, I'll need to probably start looking into Qt5 again...

@skvark
Copy link
Member

skvark commented Jul 29, 2020

Looks like I got the Qt5 builds working (at least Travis tests are passing), but I need to still check that GUI functions actually work.

Edit: still segfaulting...

@skvark
Copy link
Member

skvark commented Aug 6, 2020

I patched auditwheel and got Qt 5.15 working. Qt is also built in a namespace so it should not conflict with other systems Qt installations in the future. There is still some font related finetuning to do but it's far more easier to fix than the segmentation fault issue.

@skvark
Copy link
Member

skvark commented Aug 9, 2020

This issue should be now fixed. Fixed wheels (3.4.10.37 and 4.3.0.38) will be soon up in PyPI. It will take probably about ~24 hours for all builds to finish.

Please open a new issue if there are still problems related to GUI functions.

@skvark skvark closed this as completed Aug 9, 2020
@ParthikB
Copy link

What's the solution finally?

@skvark
Copy link
Member

skvark commented Aug 12, 2020

Solution to this issue? Upgrade your opencv-python to version 4.3.0.38 or greater.

@SiddhantKumar14
Copy link

upgrading to 4.3.0.38 did not work, still facing the same issue

@skvark
Copy link
Member

skvark commented Sep 15, 2020

Please post exact install steps and how you are using opencv-python and please upgrade to 4.4.0. This issue has been fixed, the issue is very likely in your environment.

@SiddhantKumar14
Copy link

upgrading to 4.4.0 fixed it, thank you!

@ahmad-sulehari
Copy link

I am using the latest conda version for Debian on parrot OS security(updated to latest version)
I'm running my script in a venv with python3.7, qt5.9, OpenCV-python4.4.0.44
Screenshot spider cv output at 2020-10-03 13-12-00
with this: QObject::moveToThread: Current thread (0x55e37f4716b0) is not the object's thread (0x55e37f6733f0).
Cannot move to target thread (0x55e37f4716b0)

@ganler
Copy link

ganler commented Nov 15, 2020

OpenCV 4.4 worked to show the image. But the warning is still there.

@ikram-hamizi
Copy link

I am using the latest conda version for Debian on parrot OS security(updated to latest version)
I'm running my script in a venv with python3.7, qt5.9, OpenCV-python4.4.0.44
Screenshot spider cv output at 2020-10-03 13-12-00
with this: QObject::moveToThread: Current thread (0x55e37f4716b0) is not the object's thread (0x55e37f6733f0).
Cannot move to target thread (0x55e37f4716b0)

@ahmad-sulehari Upgrading to 38+ resulted in the same issue as you.
To solve the QObject::moveToThread: problem, only opencv==4.3.0.36 worked for me (following the comment here: #Yuliang-Liu/Curve-Text-Detector#11 (comment))

@matanox
Copy link

matanox commented May 24, 2021

With 4.3.0.36, and only that version, I no longer get the original warning, but multiple windows open up when reusing imshow(), which was not the case before. Whereas installing 4.4.0.42 reverts back to the original problem of showing that warning message about thread switching that is mentioned above. This seems to be a known issue as described here. So I will really try to get to fix the underlying qt library conflict somehow.

(One thing I do notice is that it's easy to end up with an opencv-contrib-python package that is not the same version as opencv-python itself, which may add to some people's confusion in fiddling version installs).

@skvark
Copy link
Member

skvark commented May 24, 2021

With 4.3.0.36, and only that version, I no longer get the original warning, but multiple windows open up when reusing imshow(), which was not the case before. Whereas installing 4.4.0.42 reverts back to the original problem of showing that warning message about thread switching that is mentioned above. This seems to be a known issue as described here. So I will really try to get to fix the underlying qt library conflict somehow.

4.3.0.36 is known to have this broken behavior. These packages ship with their own copy of Qt. The Qt issues are always related to some other Qt installation on your system. Unfortunately, the only way to avoid conflicts between other Qt installations completely on Linux is to build this package from sources against your system Qt.

(One thing I do notice is that it's easy to end up with an opencv-contrib-python package that is not the same version as opencv-python itself, which may add to some people's confusion in fiddling version installs).

You should never install both opencv-python and opencv-contrib-python. This will break your environment and may as well cause issues you are seeing. Follow the install instructions in the README and install only one package.

@matanox
Copy link

matanox commented May 24, 2021

Thanks a lot. Would you happen to recall how to build OpenCV from source in a way that gets picked up within a conda environment?

@skvark
Copy link
Member

skvark commented May 24, 2021

I don't know about conda, these packages are for PyPI and pip. If you use pip, follow the manual build instructions in the README. Conda has also their own OpenCV packages here: https://github.com/conda-forge/opencv-feedstock

@matanox
Copy link

matanox commented May 25, 2021

In case it ever helps anyone for that qt compatibility situation, here's how to manage building from source for a conda environment, which I didn't see documented anywhere else.

@pk16514
Copy link

pk16514 commented Jul 29, 2021

Use pip install opencv-python==4.2.0.34 . It worked for me. No warning and working consistently without any black image.

@leosongwei
Copy link

got the same problem on 4.5.3.56, OS is Debian 11

@camilocamachour
Copy link

Same problem with 4.5.3.56 on Ubuntu 18.04 and Python 3.8.

@vocdex
Copy link

vocdex commented Apr 21, 2022

Using the latest OpenCV at the moment(4.5.5.64) Ubuntu 18.04 and Python 3.8, but this behaviour is still happening. Any suggestions?

@angeliquemccl
Copy link

If it everything is update per the previous comments. I found out it could be the image size, try using:

def rescaleFram(frame, scale=0.75):

14.23 seconds in the video

https://www.youtube.com/watch?v=oXlwWbU8l2o

@apolloo43
Copy link

it same as me.. i using python 3.8 , ubuntu 20.04 and opencv version 4.8.0.76 and same problem happen.. are there any solution???

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

No branches or pull requests