sliders_alpha
sliders_alpha

Reputation: 2454

OpenCv webcam reading official code is very slow

I just got a highend 1080p webcam, opening it in the "camera" app of windows 10 display it flawlessly, at 25 or 30fps, however when using opencv it's very laggy, I put a timer in the loop while disabling the display and I have around 200ms between each frame.

Why?

import numpy as np
import cv2
import time

def getAvailableCameraIds(max_to_test):
    available_ids = []
    for i in range(max_to_test):
        temp_camera = cv2.VideoCapture(i)
        if temp_camera.isOpened():
            temp_camera.release()
            print "found camera with id {}".format(i)
            available_ids.append(i)
    return available_ids

def displayCameraFeed(cameraId, width, height):
    cap = cv2.VideoCapture(cameraId)
    cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)

    while(True):
        start = time.time()
        ret, frame = cap.read()
        rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        end = time.time()
        print "time to read a frame : {} seconds".format(end-start)

        #DISABLED
        #cv2.imshow('frame', frame)
        #if cv2.waitKey(1) & 0xFF == ord('q'):
            #break

    cap.release()
    cv2.destroyAllWindows()

#print getAvailableCameraIds(100)
displayCameraFeed(0, 1920, 1080)

Thanks

Opencv 3.1 on a windows 10 x64, with python 2.7 x64

Upvotes: 4

Views: 10396

Answers (2)

Dr.Bob
Dr.Bob

Reputation: 108

for me this did the trick on Windows 10 with a Logitech c922. The order in which the methods a called seem to have an impact. (i have 'import cv' instead of 'import cv2')

cap = cv.VideoCapture(camera_index + cv.CAP_DSHOW)
cap.set(cv.CAP_PROP_FRAME_WIDTH, 1920)
cap.set(cv.CAP_PROP_FRAME_HEIGHT, 1080)
cap.set(cv.CAP_PROP_FPS, 30)
cap.set(cv.CAP_PROP_FOURCC, cv.VideoWriter_fourcc(*'MJPG'))

Upvotes: 4

JaqenTheMan
JaqenTheMan

Reputation: 41

I've faced the same problem on my linux system where I had 150ms delay between frames. In my case, the problem was that the Auto Exposure feature of the camera was ON, which increased exposure times, causing the delay.

Turning OFF auto exposure reduced delay to 49~51 ms

Here is a link from OBSProject that talks about it https://obsproject.com/forum/threads/getting-the-most-out-of-your-webcam.1036/

I'm not sure how you'd do this on a windows machine, a Google search revealed that changing it on your Skype settings changes it globally. (If you have bundled software with your camera, you could probably change it there as well.)

As for a linux machine, running v4l2-ctl --list-ctrls lists the features of your camera that you can modify.

I set exposure_auto_priority (bool) to 0 which turns OFF Auto Exposure.

Upvotes: 3

Related Questions