Home > Back-end >  cv2.waitKey(0) blocks the program
cv2.waitKey(0) blocks the program

Time:09-06

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.

CodePudding user response:

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

CodePudding user response:

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