cv2.waitKey(0) blocks the program - python

Here is a simple script:
import cv2
img = cv2.imread("img.png", 0)
cv2.imshow("Test", img)
print("Before")
cv2.waitKey(0)
print("After")
After closing the Test window, the output of terminal was like:
$ python test.py
Before
It seems that cv2.waitKey(0) blocks the program and I cannot even kill the process by typing ctrl+c in the terminal. What's wrong with it?
PS: It works properly before but today there is some system upgrade being done and this kind of issue happened. I'm using ubuntu 18.04.

waitKey() is supposed to break/return once all windows have been closed, and also return immediately if no windows are currently open. This is regardless of the desired amount of delay.
On Windows 10, I can't reproduce what you describe. That is to say, it's probably specific to your environment.
Your described behavior is probably a bug due to bad interaction with a window manager/GUI toolkit.
Please check https://github.com/opencv/opencv/labels/category%3A%20highgui-gui for any obvious previous reporting.
Before opening a new bug, make sure it's reproducible and comes with a description of the environment that is required to reproduce it. That includes all versions of everything you think is relevant, including OpenCV's version.

After checking issues of opencv, I do find the same problem that has already been posted:https://github.com/opencv/opencv/issues/20822.
It is a bug due to the Qt Gui backend and the issue is still open.

Related

How do I get Pillow ImageOps.contain to work when inside VS Code?

I have VS Code on a Mac.
Pillow is installed and version verified as 8.3.2 via Pip list in the terminal window of VS Code. I have confirmed via the pillow docs that the ImageOps.contain() is part of 8.3.
My problem is that when I use the terminal, type python and run the following, it works perfectly:
from PIL import Image, ImageOps
im = Image.open("images/Barcelona.jpg")
print(im.format, im.size, im.mode)
im = ImageOps.contain(im, (800, 800), method=3)
im.show()
Preview pops right up and shows me the picture.
When I put the exact code into VS Code or build a .py file with Nano, I get an error message which is shown in this image:
I've verified the right version of Python, Pillow, and such. Any help or pointers would be greatly appreciated.
This turns out to be a problem with what I can only call nested environments and/or a conflict between Anaconda and Workspaces. I'm not really sure but when I used the _version import to figure out a) what VS Code thought and then b) figured out the environment Pip was reporting on, I deactivated up one level, upgraded the (base) to the 8.3 version and all ran fine. The knowledge on importing the version variable to see precisely what the code is importing came from the question asked below and was invaluable.

np.convolve and signal.convolve hard fault after freezing using pyinstaller

So I have been debugging this bug for the last 10 hours. The problem is that I wrote a program that get's data from a usb scope and processes it, but after I frooze it using pyinstaller it would crash without any error. I made test program and by adding print statements throughout my code I traced down the bug to this one line:
y = signal.convolve(prev_values,filter_b,mode ='valid')
I tried replacing this line with:
y = np.convolve(prev_values,filter_b,mode ='valid')
but that also crashes. I tried replacing the line with
y = np.ones(len(prev_values)-len(filter_b)+1)
and everything is fine. The command I use to freeze my app is:
pyinstaller --log-level=DEBUG --add-binary "./dlls/*;./" --add-binary "./interface.ui;./" --add-binary "./settings.yaml;./" "app.py"
The problem now is that I have now idea where this goes wrong. Is it python having some memory issues or something? Or could both numpy and scipy have a bug that causes this? Or is it something with pyinstaller? Any tips are welcome. Also if you know a good alternative to do a convolution for numpy and signal.
this np.convolve function was also essencial for my app and also was giving me some problems while using pyinstaller. What happened is that my numpy library was up to date while my pyinstaller wasn't, so i think all the hooks, that called the aproppriate dlls and etc to make everything work, were wrong. I've tried to manually fix it adding all the missing stuff manually, but in the end updating pyinstaller made everything work without any further issue.
No python expert here, but I hope updating everything helps you.

Problem displaying image opencv+python using imshow + waitKey

I have recently upgraded my python/opencv for a project to python 3.7 + opencv 4.3.0 and now I have an issue with opencvs imshow. I am running Ubuntu 18.04 and am using conda venvs.
I tried to rerun this piece of code multiple times and half the time it correctly displays the white image, and half the time it displays the image below (1). The printed output (2) is always the same. I tried changing it from waitKey(0) to waitKey(1000) but that doesn't make a difference. Still about half the time a tiny black image is all I see.
Does anybody know how to debug this? I tried looking at Pycharms log files but they dont have any more details. I also tried running it straight from the command line but that gives the same issue. Also tried to remove the environment and created a fresh one, reinstalled opencv and got the same issues. When I create a 3.6 environment I don't have the issue, but that's no longer an option. I need python3.7 for some other packages that don't support certain features in 3.6.
I received a warning that libcanberra gtk was missing, and found that in another post that it could cause issues. So I installed it using sudo apt install libcanberra-gtk-module libcanberra-gtk3-module and the warning went away. Sadly the issue did not...
import numpy as np
import cv2
if __name__ == '__main__':
img = np.ones((255, 255, 3), dtype=np.uint8)*255
print(img.shape)
print(img.dtype)
print(img.min())
print(img.max())
cv2.imshow("i", img)
cv2.waitKey(0)
screenshot of the code + result
console output
I think there is no problem. The left-upper part of the image is white as it is supposed to be. The rest is undefined. I recommend equating green and blue channels to zero and leaving only red channel as 255 in order to make sure that is the case.
I solved it in this way:
if cv2.waitKey(0):
pass

Tkinter keeps crashing on Tk() on Mac

I've encountered a problem while attempting to create a Tkinter window using root = tk.Tk() . Every time I get to that point, the program crashes, and "Python quit unexpectedly" message is displayed.
I get no tracebacks at all, so I assume that is the ActiveTcl bug. However, I have the new distribution from ActiveTcl Website installed, which is supposed to take care of the problem (obviously, it doesn't).
Interestingly enough, it only crashes when it is executed in Python 2.7. It works fine in Python 3.6. However, I need to use 2.7.
My MacOS version is 10.12.5.
Any ideas / suggestions about fixing the issue are welcome.
P.S. I have read a good dozen of similar posts before posting this, and not any of the proposed solutions worked for me. Please consider this before marking this post as a duplicate.
I don't know what is meant by "new distribution" for ActiveTcl is but if you're using 8.6, it needs to be downgraded to 8.5.
Also, if you run IDLE which uses Tkinter, do you see any messages warning of "instability"? If you see that then, it means you need to downgrade Tcl to 8.5.
Are you running Python 3 through Anaconda? Tkinter was acting all sorts of funky on me and then I uninstalled Anaconda and now it works fine.
Interestingly enough, I am running a PyCharm Professional / Anaconda combo on a Windows 10 VM on my Mac, and I have issues with Tkinter on it as well. I have absolutely no issues however on my Linux Mint box.
To reiterate, I was able to remove the issue by completely removing Anaconda. (How to uninstall Anaconda completely from macOS)
Tkinter needs to be imported like this in order to work with both python 2 and 3:
try:
import tkinter
except ImportError: # python 2
import Tkinter as tkinter

OpenCV error on spyder

I want to use OpenCV with python on spyder.
But error occured when I run a simple code.
import cv2
img = cv2.imread('pi.png',0)
cv2.imshow('image', img)
cv2.waitKey(0)
dcv2.destroyAllwindows()
Error
This application failed to start because it could not find or load the Qt platform plugin "xcb".
Reinstalling the application may fix this problem.
The error is occured at this line.
cv2.imshow('image', img)
Details: Ubuntu 14.04(LTS), OpenCV 2.4.13, Spyder 2.3.9(Python 2.7)
Please Tell me in detail what should I do.
I didn't understand the answer.( stackoverflow.com/questions/30483753/python-app-xcb-plugin-fail )
But, the problem is solved from changing condition when I reinstall OpenCV
Installation is referred to this page.
The key of the solution in this method is simple!
Change the condition from 'WITH_QT=ON' to 'WITH_QT=OFF' in the script. (opencv.sh)
I don't have any solution without reinstalling because I'm inexperienced.
If you know about solution that change the condition without reintalling or reason why the problem is occured, give me a feedback please.
Thank you.

Categories