video from webcam is extremly slow

asked 2017-10-10 13:57:05 -0600

SpacePirate gravatar image

Hi,

I just got a highend 1080p webcam, opening it in the "camera" app of windows 10 display it flawlessly, however when using opencv it's very laggy, I put a timer in it 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)

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

    cap.release()
    cv2.destroyAllWindows()

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

Thanks.

edit retag flag offensive close merge delete

Comments

2

what if you comment those lines:

  cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
  cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)

also, which image size is used in the camera app ?

berak gravatar imageberak ( 2017-10-11 03:12:11 -0600 )edit