Python Opencv - Waitkey(0) Does Not Respond?
Solution 1:
I found that it works if i press the key whilst the window is in focus. If the command line is in focus then nothing happens
Solution 2:
Adding a cv2.waitKey(1) after you destroy the window should work in this case.
cv2.imshow('imgae',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.waitKey(1)
Solution 3:
This code works for me from IDLE:
# -*- coding: utf-8 -*-# Objectif : découvrir le fonctionnement d'opencv-python# http://opencv-python-tutroals.readthedocs.org/en/latest/index.htmlimport numpy as np
import cv2
# Load an color image in grayscale
img = cv2.imread('Lena.tiff',0)
WINDOW_NAME = 'Image de Lena'
cv2.namedWindow(WINDOW_NAME, cv2.CV_WINDOW_AUTOSIZE)
cv2.startWindowThread()
# Display an image
cv2.imshow(WINDOW_NAME,img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Hope this helps for future readers.
Solution 4:
click on the image window(active) and then press and key, don't write in the terminal window.
Solution 5:
Here a minimalistic code for best performance on all platforms:
importcv2img= 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 the0
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.
Post a Comment for "Python Opencv - Waitkey(0) Does Not Respond?"