Gaining high frame rate from ELP camera (python)

asked 2017-07-05 04:26:29 -0500

jj364 gravatar image

updated 2017-07-05 04:44:34 -0500

I'm using an ELP USB camera that is supposedly rated at 100fps at 640x480 quality but I don't seem to be getting anywhere near that frame rate. I was wondering if anyone has used an ELP camera with openCV and managed to achieve a frame rate close to 100 fps? Or if anyone has any advice to help increase the frame rate? I am using Ubuntu 14.04 with openCV version 2.4.8.

I have already tested that the output is MJPEG at 640x420 and I am running cv2.VideoCapture in one thread and placing the frame on a queue. From the main thread I repeatedly ask for frames but only return when the results is not None. This gives a frame rate hovering around 30fps. One I have the image I am performing contour detection but essentially I would like to get the frame rate up as high as possible. Below is the code I'm using just to test the frame rate.

import cv2
import time
import numpy as np
from datetime import datetime
from threading import Thread, Lock, Condition
import time
from Queue import Queue

class WebcamVideoStream:

def __init__(self, src=0):
    # initialize the video camera stream 
    self.stream = cv2.VideoCapture(src)
    # initialize the variable used to indicate if the thread should
    # be stopped
    self.stopped = False
    self.frame = None

def start(self):
    global qt
    self.stopped = False
    qt = Queue(10)
    # start the thread to read frames from the video stream
    thread1 = Thread(target=self.update, args=())
    thread1.start()
    return self

def update(self):
    global qt
    # keep looping infinitely until the thread is stopped
    while True:
        if self.stopped:
            return
        _, self.frame = self.stream.read()
        qt.put(self.frame)

def read(self):
    global qt
    if(not qt.empty()):
        self.CurrFrame=qt.get()
        if self.CurrFrame is not None:
            return self.CurrFrame
    if self.stopped:
        return

def stop(self):
    print('Stop')
    # indicate that the thread should be stopped
    self.stopped = True
    return self

vs = WebcamVideoStream(src=-1).start()
time.sleep(1)

i = 0
t0 = time.time()
while i < 100:
frame = vs.read()
while frame is None:
    frame = vs.read()
i = i + 1

rate = 100/(time.time()-t0)
print(rate)

cv2.destroyAllWindows()
vs.stop()

Thanks in advance

edit retag flag offensive close merge delete

Comments

I'm struggling to get the embedded code to format, sorry.

jj364 gravatar imagejj364 ( 2017-07-05 04:19:10 -0500 )edit

I have already tested that the output is MJPEG at 640x420

os ? opencv version ?

(i.e. on win, dshow will force that into bgr, which takes time, but what would you want to do even with a mjpeg image, if not uncompress it ?)

then, this is basically an io bottleneck, you won't gain anything with multithreading

berak gravatar imageberak ( 2017-07-05 04:29:59 -0500 )edit

In the demo I'm doing absolutely nothing with them. I had assumed that because (most of the time) when a frame is requested None is returned that the requests are coming in far faster than the camera can output them and therefore the queue never has more than one frame on it.

jj364 gravatar imagejj364 ( 2017-07-05 04:48:20 -0500 )edit

apologies, i removed a comment, you the other, so the missing information:

ubuntu 14, opencv2.4.8 (stone-age !)
berak gravatar imageberak ( 2017-07-05 04:58:07 -0500 )edit

again, i'm quite sure, that cv2.VideoCapture will have to uncompress your mjpg image in read()

(else you could not use it for contour detection)

berak gravatar imageberak ( 2017-07-05 05:00:08 -0500 )edit

Ah ok. So this is causing the bottleneck? Is there any method to use to improve the frame rate?

jj364 gravatar imagejj364 ( 2017-07-05 05:07:07 -0500 )edit

i don't think, there's much you can do from python . if it was c++, i'd say: try to use libv4l directly, without using VideoCapture (there's also a hidden fifo queue, threads & locks & whatnot in the v4l wrapper)

berak gravatar imageberak ( 2017-07-05 05:13:23 -0500 )edit

I decided to upgrade to OpenCV 3 and this has made all the difference. Frame rate immediately rose greatly. Thanks for all your help, Berak.

jj364 gravatar imagejj364 ( 2017-07-06 03:03:31 -0500 )edit