0

Here is my code. I wanted to know the frame rate of my webcam. For some reason, the cap.get(5) which gets the fps property does not work for live capture. So I tried to do a work around in order to calculate the number of frames that are taken in each loop. I used the time.time() function to get the time between each frame (using which I can calculate the number of frames in a second). I get the result as around 0.128, but my problem right now is cv2.waitKey(x). Even if I substitute 1 or 10 or 100 for x the result remains the same.

Yes, I know x is in milliseconds. But if I put x as 100, I should get 0.2 right? What's wrong here? Any help would be appreciated. Also if someone can help me out with calculating the fps I would be glad. PS. All this started because the videos I save using OpenCV always appear too fast ie fast forwarded..

Note : If I put x as 1000, then I get 2.128.

import numpy as np
import cv2
import time

cap = cv2.VideoCapture(0)
#print cap.get(5)

# Define the codec and create VideoWriter object
fourcc = cv2.cv.CV_FOURCC(*'XVID') 
out = cv2.VideoWriter('output1.avi',fourcc, 10, (640,480))

while(cap.isOpened()):
    start = time.time()
    ret, frame = cap.read()
    if ret==True:
        frame = cv2.flip(frame,1) 
       
        # write the flipped frame
        out.write(frame)        

        cv2.imshow('frame',frame)

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


    end = (time.time() - start)
    print end

# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
evanhutomo
  • 627
  • 1
  • 11
  • 24
Clive
  • 1,137
  • 7
  • 19
  • 28

1 Answers1

0

I'd try aggrigating them as suggested here: OpenCV: VideoCapture::get(CV_CAP_PROP_FPS) returns 0 FPS

Something like:

import numpy as np
import cv2
import time

cap = cv2.VideoCapture(0)
#print cap.get(5)

# Define the codec and create VideoWriter object
fourcc = cv2.cv.CV_FOURCC(*'XVID')

num_frames = 0

start = time.time()
while(cap.isOpened()):

    ret, frame = cap.read()
    if num_frames < 500:
        frame = cv2.flip(frame,1)
        num_frames = num_frames + 1;
    else:
        break


total_time = (time.time() - start)
fps = (num_frames / total_time)
print str(num_frames) + ' frames in ' + str(total_time) + ' seconds = ' + str(fps) + ' fps'

# Release everything if job is finished
cap.release()
cv2.destroyAllWindows()
Community
  • 1
  • 1
abarry
  • 425
  • 3
  • 16
  • The code you posted gives no output EDIT : Oh. that was because you gave "if num_frames<500". Had to wait a while. Thanks. I got my frame rate as 7.8 fps. So this is the rate at which OpenCV is taking images right? Or is it the frame rate of my webcam? So in out = cv2.VideoWriter('output1.avi',fourcc, 10, (640,480)) in the program for capturing and saving a video file, I should use 7.8 instead of 10 (fps) ? Also, 7.8 fps is a bit slow isn't it? What should I do to speeden up that? It depends on the program execution right? – Clive Aug 07 '14 at 17:20
  • Yes, you should use that framerate if you want other programs to play it back correctly. I agree, 7.8fps is pretty slow. Usually I open cameras up in `guvcview` and change their settings there. Often that will tell you what rates the camera says it can hit at different resolutions. – abarry Aug 07 '14 at 18:02
  • guvcview shows that my webcam frame rate is 25fps at 640x480 resolution. So then, it should be the limitation of the program itself? – Clive Aug 08 '14 at 02:48
  • Does it give other options (like higher resolutions) that OpenCV might be using? I have found that OpenCV is usually OK at capturing fast. – abarry Aug 08 '14 at 02:51
  • Yes guvcview shows options for higher as well as lower resolutions. Also there is an option for 30fps. That's the highest. How do I know what resolution OpenCV is using? In VideoWriter it has been set as 640x480. – Clive Aug 08 '14 at 02:54
  • Take a look at the [capture properties](http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html#videocapture-get) or use `image.rows` / `image.cols` on the images coming back (probably more reliable) – abarry Aug 08 '14 at 03:07
  • UPDATE : I checked a tickbox in guvcview controls window that says "show framerate". Now, the current framerate is displayed on the window that records the video (some bug here. I am not able to record a video though. The video preview window is just black). Although my fps is set as 25fps in the options in guvcview controls, the fps that is shown in the video preview window says only 7.50fps. Is it the limitation of my webcam now? Can someone verify this with a different cam? – Clive Aug 08 '14 at 03:12
  • yes i did check out resolution using cap.get(3) and cap.get(4). It says 640 x 480. – Clive Aug 08 '14 at 03:14
  • Seems likely to be your webcam or your drivers. When I do that with my PS3 eye it responds correctly. You can always check your real framerate with a stopwatch in front of the camera :) – abarry Aug 08 '14 at 03:18
  • It's more likely to be an issue with Ubuntu. Google shows others have had similar low fps problems. Trying to find out whats wrong. – Clive Aug 08 '14 at 03:20
  • Well its my webcam. I tested the fps in windows and it says 7.50 fps. The correct drivers are installed. So it probably is the limitation of my inbuilt webcam. Thanks for the help anyways. – Clive Aug 10 '14 at 06:27