OpenCV Python Video playback - How to set the right delay for cv2.waitKey()
put waitKey(60)
after imshow()
and it will be displayed at normal speed.
For what it is worth, I have tried all sorts of tricks with setting the cv2.waitKey() delay time and they have all failed. What I have found to work is to try something like:key = cv2.waitKey(1)
inside of your while(cap.isOpened()) like so:
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
# Define the codec and create VideoWriter object
fourcc = cv2.cv.CV_FOURCC(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
key = cv2.waitKey(1)
frame = cv2.flip(frame,0)
# write the flipped frame
out.write(frame)
cv2.imshow('frame',frame)
if key & 0xFF == ord('q'):
break
else:
break
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
I hope this helps someone out there.
From the OpenCV documentation:
The function
cv.waitKey([, delay])
waits for a key event infinitely (whendelay <= 0
) or fordelay
milliseconds, when it is positive.
If the FPS is equal to 20, then you should wait 0,05 seconds between displaying the consecutive frames. So just put waitKey(50)
after imshow()
in order to have the desired speed for the playback.