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!
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.
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.
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.
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.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With