Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use waitKey in order pause and play video

Tags:

c++

opencv

I have a VideoCapture in OpenCV, I can successfully display a given video. What I want to do now is to pause and play by pressing a key (optional which one as long as it works). I have been reading about waitKey but there is something about this whole thing I don't get (ASCII) and how to bind keys. What I understand it is used to let highgui process but can also be used for other purposes?

If it is hard/impossible to pause a video and start it again I would be happy with just a delay when key is pressed.

Help is much appreciated!

like image 517
J.Smith Avatar asked Jun 27 '16 23:06

J.Smith


People also ask

What happens if we pass 0 to waitKey function for playing video?

It takes time in milliseconds as a parameter and waits for the given time to destroy the window, if 0 is passed in the argument it waits till any key is pressed.

How do I pause a video in OpenCV?

Notice that you also use the waitKey() function to pause for 20ms between video frames. Calling the waitKey() function lets you monitor the keyboard for user input. In this case, for example, if the user presses the ' q ' key, you exit the loop.

How do you pause a video in Python?

waitKey() . To pause the video, you can pass no parameter (or 0) to cv2. waitKey() which will wait indefinitely until there is a key press then it will resume the video.

What does cv2 waitKey 1 mean?

cv2 waitkey() allows you to wait for a specific time in milliseconds until you press any button on the keyword. It accepts time in milliseconds as an argument.


1 Answers

With reference to OpenCV Documentation for cv::waitKey(delay), when delay <= 0 will cause to the function to wait infinitely for a key event.

Here is a sample Python script to display frames captured from computer's webcam. When q is pressed, the loop is exited. However, if p is pressed, the display pauses until any other key is pressed:

import cv2
cap = cv2.VideoCapture(0) # getting video from webcam
while cap.isOpened():
    ret, img = cap.read()

    cv2.imshow("Frame",img)

    key = cv2.waitKey(1)
    if key == ord('q'):
        break
    if key == ord('p'):
        cv2.waitKey(-1) #wait until any key is pressed
cap.release()
cv2.destroyAllWindows()
like image 69
elcymon Avatar answered Oct 03 '22 21:10

elcymon