Reputation: 1978
I am trying to read the key value using cv2.waitKey(0) but it doesn't work. It waits forever. I used cv2.waitKey(1) to check what it returns and it was always 255 no matter which key I pressed.
while True:
key = cv2.waitKey(0)
print(key)
The above code does nothing, no matter whichever key I press.
while True:
key = cv2.waitKey(1) & 0xFF
print(key)
if key == ord('q'):
break
keeps printing 255 and doesn't break if I press 'q'.
Upvotes: 5
Views: 3072
Reputation: 1978
I figured out the solution. Looks like it requires a named window to be open for it to read the key values. So I tried the following and it worked.
cap = cv2.VideoCapture(0)
cv2.namedWindow('frame', cv2.WINDOW_NORMAL)
while(True):
ret, frame = cap.read()
cv2.imshow('frame',frame)
key=cv2.waitKey(0) & 0xFF
print(key)
if key == ord('q'):
break
cv2.destroyAllWindows()
Upvotes: 12