Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

low fps by using cv2.VideoCapture

I have low FPB ~5, I have checked this code on different cameras logitech c270 and logitech 9000, same situation.

I completed all the tips about turn off right light etc.

import urllib.request as urllib
import cv2
import numpy as np
import time

while True:

    # Use urllib to get the image and convert into a cv2 usable format
    cap = cv2.VideoCapture(0)

    width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    hiegh = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

    ret, frame = cap.read()


    # put the image on screen
    cv2.imshow('Webcam', frame)


    if cv2.waitKey(1) & 0xFF == 27:
        break

cap.release()        
cv2.destroyAllWindows()

What should I do for increasing FPS ?

like image 639
Oleksii Avatar asked Feb 12 '26 06:02

Oleksii


2 Answers

You need to move this line up, outside your acquisition loop:

 cap = cv2.VideoCapture(0)

It does a one-time only initialisation.

like image 155
Mark Setchell Avatar answered Feb 17 '26 03:02

Mark Setchell


I know I'm late but, if anyone else is facing this...

Check if you're using the right codec. In my case for a Logitech OrbiCam I had to set it to MJPG:

For C++:

cv::VideoCapture camera;
camera.open(0);
auto codec = cv::VideoWriter::fourcc('M','J','P','G');
camera.set(cv::CAP_PROP_FOURCC, codec);

In Python:

import cv2
camera = cv2.VideoCapture(0)
camera.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter.fourcc('m','j','p','g'))

For Kotlin:

var capture: VideoCapture = VideoCapture()
camera.open(deviceId)
val mjpg = VideoWriter.fourcc('M', 'J', 'P', 'G')
capture.set(Videoio.CAP_PROP_FOURCC, mjpg.toDouble())

etc...

...and then set your target resolution

like image 43
Luis González Avatar answered Feb 17 '26 03:02

Luis González