Skip to content Skip to sidebar Skip to footer

How Should I Properly Use Cv2.waitkey When Wanting To Start/pause A Video?

I've written a small script that allows be to run/pause a video stream using OpenCV. I don't understand why I needed to use the cv2.waitkey() in the manner I did. The structure of

Solution 1:

According to the OpenCV Documentation:

The function waitKey waits for a key event infinitely (when delay <= 0 ) or for delay milliseconds, when it is positive. Since the OS has a minimum time between switching threads, the function will not wait exactly delay ms, it will wait at least delay ms, depending on what else is running on your computer at that time. It returns the code of the pressed key or -1 if no key was pressed before the specified time had elapsed.

So you can pause by pressing the "p" button of the keyboard using the OpenCV cv2.waitKey(delay) function like this:

importcv2cap= cv2.VideoCapture('your_video.mov')

while(True):
    _, frame = cap.read()

    cv2.imshow("Frame", frame)

    key = cv2.waitKey(1)
    ifkey== ord("p"):
        cv2.waitKey(0)

Solution 2:

waitKey drives the event-loop. Which is essential for things like keyboard or mouse events. Thus you always need to drive it if you expect interactive reactions.

Also, you can pull the waitKey in front of the if, and just issue it once.

Post a Comment for "How Should I Properly Use Cv2.waitkey When Wanting To Start/pause A Video?"