Python OpenCV - waitKey(0) does not respond?

Viewed 54908

I'm using opencv 2.4.7 on ubuntu 12.04. I'm programming with python and I have a problem when i run this script:

import cv2

img = cv2.imread('347620923614738322_233985812.jpg')
cv2.namedWindow("window")
cv2.imshow("window", img)
cv2.waitKey(0)

The problem is that the script doesn't stop when I close the image. I searched information about waitKey and I found that using cv2.waitKey(0) is correct.

I don't understand, where is the problem?

12 Answers

click on the image window(active) and then press and key, don't write in the terminal window.

Here a minimalistic code for best performance on all platforms:

import cv2

img = cv2.imread("image.jpg")
cv2.imshow("Window", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

And now some observations:

When the user wants to close the window through the press of the 0 key, he/she has to make sure that the 0 key is pressed whilst the window is in focus. Because as stated above, if the terminal is in focus nothing happens and code execution stucks at the cv2.waitKey(0) until the 0 key is pressed properly whilst the window is in focus.

Pressing the 0 key whilst the window is in focus is the right way to close the window and make sure that, once the window is destroyed in line cv2.destroyAllWindows() and the program ends, the user can get back the control of the terminal.

If the window is exited by mouse click, the window will be destroyed yes, but the user will very much end up in the situation of not being able to get back the control of the terminal. In this kind of situation the user may want to shut down the unresponsive terminal and open a new one.

Solved in Spyder under Ubuntu by following [Run]->[Configuration per file]->[Execute in an external system terminal].

Well the use cv2.waitKeyEx(0).

It runs in the same way and waits for your response until you revoke it.

I hope it helps. Nithesh varmma

There is a problem with unix based system running opencv programs from python notebooks.

Check this alternate method My suggestion is to run code in python in terminal. You will not face any kind of problem

Copy the same code and save with filename.py

import cv2
input = cv2.imread('path_to_image.png')
cv2.imshow('Hello World', input)
cv2.waitKey(0)
cv2.destroyAllWindows()

then open specific directory and then open terminal

Steps:

Open Terminal
cd path/to/filename.py
source activate YOURPROFILE 
python filename.py

This will solve problem

https://youtu.be/8O-FW4Wm10s

This will work even if you close the window using the cross button on the window.

import numpy as np
import cv2

img = cv2.imread('arvid.jpg', 0)
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.imshow('image', img)

while True:
    k = cv2.waitKey(30) & 0xFF
    if k == 27:         # wait for ESC key to exit
        cv2.destroyAllWindows()
    elif k == ord('s'):  # wait for 's' key to save and exit
        cv2.imwrite('arvid2.jpg', img)
        cv2.destroyAllWindows()
    if cv2.getWindowProperty("image", 0) == -1:
        break

This problem occurs in some earlier version of opencv. So you just need to update opencv to the latest version. Version 4.0.0.21 worked for me. Use the command below.

update opencv to version 4.0.0.21.

Related