How Should I Properly Use Cv2.waitkey When Wanting To Start/pause A Video?
Solution 1:
According to the OpenCV Documentation:
The function waitKey waits for a key event infinitely (when
delay
<= 0 ) or fordelay
milliseconds, when it is positive. Since the OS has a minimum time between switching threads, the function will not wait exactlydelay
ms, it will wait at leastdelay
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?"