1

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
2
  • Your identation doesn't seem right. But more shouldn't the wait be before the imshow? EDIT: I would expect an else: continue that catches all other clicks and continues to the next round of your while loop. Commented Mar 9, 2017 at 12:28
  • Apologies, the indentation got lost when pasting into SO - I'm not sure I understand what you mean - the waitKey(0) is before the imshow in my loop? Commented Mar 9, 2017 at 13:27

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Cheers for the input, that's helped to clear things up a bit
0

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

3 Comments

Hey, thanks for the reply - I've tried to implement the above code (putting the 'break' after 'if ret == False:' onto the correct indented next line) and I'm still getting the same problem
See edit for extra bit. Also, I did experience Ubuntu messing up keyboard code for some unknown reason. It's safer if you print your frameclick to stdout.
cheers for the additional info - even with the update I'm still getting the same issue - in the meantime I'm using else: break to break if the wrong key is pressed, but I'm trying to find a workaround in which another keypress just doesn't read and show a new frame

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.