Focus and FPS settings in opencv python

Hi,

  1. I am processing real time video from logitech c922 webcam in python. I can set width and hight using the below functions successfully:
    camera.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
    camera.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)

However, I can’t able to set the focus type(manual) and FPS using below functions:
camera.set(cv2.CAP_PROP_AUTOFOCUS, 0)
camera.set(cv2.CAP_PROP_FOCUS, 30)
camera.set(cv2.CAP_PROP_FPS, 50)

How to do it?

  1. I have attached python code which displays realtime video with FPS.
import cv2
import time

font = cv2.FONT_HERSHEY_SIMPLEX

camera = cv2.VideoCapture(0)
camera.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080) #1080, 720, 360
camera.set(cv2.CAP_PROP_FRAME_WIDTH, 1920) #1920, 1280, 640

while True:
     start = time.time()
     ret, image = camera.read()
     [h, w, d] = image.shape
    
     if cv2.waitKey(1)& 0xFF == ord('q'):
         break;

     end = time.time()
     fps = "FPS: %.1f" % (1/(end - start))
     cv2.putText(image, fps, (0, 25), font, 1, (0, 0, 255), 2)
     cv2.imshow('Image', image)

 camera.release()
 cv2.destroyAllWindows()

Actual problem is for 1080p resolution FPS is 30. But i am getting 46 fps(oscillating values) sometimes as per the above calculation, while using logitech c922 webcam.

how to calculate FPS exactly? Is there any other formula to find it? or Is there any explanation behind it? Please help to let me know about it…

that may have a resolution of ~16 milliseconds, or 10 ms, or anything really. do you see why that’d give you wildly varying FPS numbers?

let’s make it simple. count 100 frames, measure the time for those frames. that’s an average.

Thanks for reply @crackwitz. I can get around 35 FPS by averaging FPS of 100 frames (That’s too greater than 30).

But why FPS is oscillating?