Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV Python Video playback - How to set the right 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 as output.avi

Now, to playback 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 video file output.avi saved from the first program. The thing is, this video appears fast forward. So, I tried changing the delay value for cv2.waitKey(). The video looked fine when I put 100. How do I know which value to put there? Should it be related to the frame rate? I checked the frame rate of output.avi (see line cap.get(5) in second program) and got 20. But if I use 20 as delay for cv2.waitKey() the video is still too fast.

Any help would be appreciated.

like image 988
Clive Avatar asked Aug 07 '14 12:08

Clive


People also ask

How do I add a delay in OpenCV?

waitKey([, delay]) waits for a key event infinitely (when delay <= 0 ) or for delay 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.

What is cv2 waitKey in Python?

Python OpenCV – waitKey() Function waitkey() function of Python OpenCV allows users to display a window for given milliseconds or until any key is pressed. 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.

What does cv2 waitKey 1 mean?

cv2. waitkey(1) is being used in while loop.It shows the output for 1msec but because of infinite while loop it is the sequence of images that are perceived by our brain as a single continuos video.


3 Answers

From the OpenCV documentation:

The function cv.waitKey([, delay]) waits for a key event infinitely (when delay <= 0) or for delay 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.

like image 154
Kornel Avatar answered Oct 22 '22 11:10

Kornel


put waitKey(60) after imshow() and it will be displayed at normal speed.

like image 31
Pravin Desai Avatar answered Oct 22 '22 11:10

Pravin Desai


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.

like image 2
jpxrc Avatar answered Oct 22 '22 11:10

jpxrc