Reputation: 73
So I've got a program where I assign different keypresses to different functions. I'm using cv2.waitKey(0) to go through frames one by one. However, when a key that isn't assigned a function is pressed, the next frame is still loaded. How do I prevent a non-assigned keypress from loading the next frame in my loop?
Thanks!
while (cap.isOpened()):
frameclick = cv2.waitKey(0)
ret, frame = cap.read()
cv2.imshow('frame',frame)
if frameclick == ord('a'):
swingTag()
elif frameclick == ord('r'):
rewindFrames()
elif frameclick == ord('s'):
stanceTag()
elif frameclick == ord('d'):
unsureTag()
elif frameclick == ord('q'):
with open((selectedvideostring + '.txt'), 'w') as textfile:
for item in framevalues:
textfile.write("{}\n".format(item))
break
Upvotes: 1
Views: 1392
Reputation: 150825
As mentioned in @Ervin's answer, try:
while (cap.isOpened()):
ret, frame = cap.read()
# check if read frame was successful
if ret == False: break;
# show frame first
cv2.imshow('frame',frame)
# then waitKey -- and make it <= 255
frameclick = cv2.waitKey(0) & 0xFF
if frameclick == ord('a'):
swingTag()
elif frameclick == ord('r'):
rewindFrames()
elif frameclick == ord('s'):
stanceTag()
elif frameclick == ord('d'):
unsureTag()
elif frameclick == ord('q'):
with open((selectedvideostring + '.txt'), 'w') as textfile:
for item in framevalues:
textfile.write("{}\n".format(item))
break
Upvotes: 0
Reputation: 16815
The problem is with your logic. Your program enters into the the while loop and waits for a key. Then, if a key is pressed, the next frame is read, but at the moment your program does not care which key was pressed. So, you have your next frame and only then you check which button was pushed, which is to late.
Upvotes: 1