how can i control webcam fps

asked 2019-02-28 22:37:26 -0500

hiyong gravatar image

updated 2019-03-02 00:59:15 -0500

I have difficulty handling fps in real-time webcam, not video file.

Currently my code always give 30 fps in any resolution like 640 x 480 or 1280 x 720. And I'm using logitech c922 which says it supports 60 fps in 720p (1280 x 720)

And I tried 'cap.set(cv2.CAP_PROP_FPS, 60)' where cap is, for example, 'cv2.VideoCapture(0)'. (it's written in python by the way). But It didn't work for me and someone even said CAP_PROP_FPS can be only used in video.

Thus, I don't know how i can figure this out. Please help me.

my codes as follows.

import cv2
import time
cap = cv2.VideoCapture(1)

frame_size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
              int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))

print(frame_size)
print(cv2.CAP_PROP_FPS)

prevTime = 0

while True:
    retval, frame = cap.read()
    if not retval:
        break

    curTime = time.time()
    sec = curTime - prevTime
    prevTime = curTime

    fps = 1/(sec)

    str = "FPS : %0.1f" % fps

    cv2.putText(frame, str, (0, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0))

    cv2.imshow('frame', frame)

    key = cv2.waitKey(1)
    if (key == 27):
        break

if cap.isOpened():
    cap.release()

cv2.destroyAllWindows()

thanks you in advance.

edit retag flag offensive close merge delete

Comments

Use timing in your loop to get image every x second.

LBerger gravatar imageLBerger ( 2019-03-01 01:53:35 -0500 )edit
  • you're measuring a) wall time, not cpu / process cycles b) the time of the whole loop (including waitKey(), drawing, etc.

  • since you're using python, you won't have any control about it, you're at the mercy of your os (which is ?) your camera driver (which is ?) and the opencv backend used (which is ?)

berak gravatar imageberak ( 2019-03-01 02:24:40 -0500 )edit

So your point is that there is no way to control fps using python opencv so I should use other language(like c++) or other library(i don't know in this case) to control webcam, right? I'm not really experienced with programming in this field and it's first time for me to use webcam in this way, so would be appreciated if you give me some details. thanks :P

hiyong gravatar imagehiyong ( 2019-03-01 03:14:09 -0500 )edit

How do you take a picture with a camera ? press the button. Instead of press the button ask an image when you need it using cap.read()

LBerger gravatar imageLBerger ( 2019-03-01 03:34:43 -0500 )edit

[...] someone even said CAP_PROP_FPS can be only used in video.

No, it's the contrary: you can use set on CAP_PROP_FPSonly on cameras. Video files have fixed FPS.

Anyway, it's better to set the FPS value; timed capture causes lot of problems (for lower framerates jitter, and you cannot increase the framerate)

Try other FPS and resolution values too; maybe the camera doesn't support 60fps, or it won't work on the default resolution.

In the meantime I join berak's question: what OS and camera driver do you use?

kbarni gravatar imagekbarni ( 2019-03-01 05:14:31 -0500 )edit

I merely agreed withh @LBerger.

supra56 gravatar imagesupra56 ( 2019-03-01 07:05:57 -0500 )edit

You have typo error. Change this:

inport cv2

to

import cv2

Change this too: cap = cv2.VideoCapture(1) to cap = cv2.VideoCapture(0)

supra56 gravatar imagesupra56 ( 2019-03-01 07:13:35 -0500 )edit

Ok! Here is answer, but the moderator(s) may help you tranlsated to c++ to python using pc system. logitech c922

supra56 gravatar imagesupra56 ( 2019-03-01 08:02:17 -0500 )edit

I'm currently using 'window 10' and, i don't know whether it's right or not but, am using 'C922 Pro Stream Webcam' driver provided by Microsoft

hiyong gravatar imagehiyong ( 2019-03-02 00:52:09 -0500 )edit

thanks for telling me typo error. And the reason why i used cap = cv2.VideoCapture(1) is that I used second webcam which is legitech c922 not a built-in camera by the way. thanks for your opinion though.

hiyong gravatar imagehiyong ( 2019-03-02 00:58:58 -0500 )edit