Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cv2.waitKey returns 255 for all keys

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'.

like image 971
janu777 Avatar asked Oct 27 '17 05:10

janu777


1 Answers

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()
like image 62
janu777 Avatar answered Nov 15 '22 04:11

janu777