OpenCV Python Video Playback - How to set correct delay for cv2.waitKey ()

I used the following code to capture a video file, flip it and save it.

#To save a Video File

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:
        frame = cv2.flip(frame,0)

        # write the flipped frame
        out.write(frame)

        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()

      

This program saves the output file as output.avi

Now, to play the video file, I used the following program

#Playing Video from File

import numpy as np
import cv2

cap = cv2.VideoCapture('output.avi')

print cap.get(5) #to display frame rate of video
#print cap.get(cv2.cv.CV_CAP_PROP_FPS)

while(cap.isOpened()): 
    ret, frame = cap.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #convert to grayscale

    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'): 
        break

cap.release()
cv2.destroyAllWindows()

      

This program plays the output of the .avi video file saved from the first program. The point is that this video appears fast forward. So, I tried to change the delay value for cv2.waitKey (). The video looked great when I put in 100. How do I know what value to put there? Should it be related to frame rate? I checked the frame rate of output.avi (see the cap.get (5) line in the second program) and got 20. But if I use 20 as the delay for cv2.waitKey () the video is still too fast.

Any help would be appreciated.

+3


source to share


3 answers


The function waitKey()

waits for an infinite key event (on delay <= 0) or for a delay of milliseconds when positive.



If the FPS is 20, you must wait 0.05 seconds between consecutive frames. So put waitKey(50)

after imshow()

and it will be displayed at normal speed.

+7


source


For what it's worth, I've tried all sorts of tricks with setting the cv2.waitKey () delay time and they all worked. What I found to work is to try something like: key = cv2.waitKey(1)

inside your time (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.

0


source


waitKey(60)

after imshow()

and it will be displayed at normal speed.

0


source







All Articles