USB Camera Timeout/Busy in Long Term Operation

2.1k Views Asked by At

TLDR: USB Camera stops returning images with continued usage.

I am running a continuous application using Raspberry Pi4, Ubuntu, OpenCV where I am capturing an image once every 5 seconds and sending that to an API. I'm having an issue where I am getting None frames after some time and the camera becomes unresponsive.

I've tried using V4L2 and fswebcam directly in the command line as tests, but I keep getting the same issue of the camera either being completely unresponsive or taking several minutes to capture an image.

Simplified Code

import cv2
import time

cam=cv2.VideoCapture(0)
time.sleep(2)
cam.set(3,1280)
cam.set(4,720)
cam.set(cv2.CAP_PROP_FOURCC,cv2.VideoWriter_fourcc('M','J','P','G'))

while True:
     time.sleep(5)
     ret , frame = cam.read()

This runs fine at first, but the images start coming in slower and slower over time. Eventually, it returns None frame and becomes unresponsive. Subsequent attempts to release and select the camera result in camera select timeout errors.

VIDEOIO(V4L2:/dev/video0): select() timeout

Any idea what would cause the camera to start taking longer and longer to capture the images and why the camera becomes unresponsive?

2

There are 2 best solutions below

5
On

Have you tried this implementation to avoid having the videocapture resource tied up while you are waiting for 5 seconds?

import cv2
import time

while True:
    time.sleep(5)
    cam=cv2.VideoCapture(0)
    time.sleep(2)
    cam.set(3,1280)
    cam.set(4,720)
    cam.set(cv2.CAP_PROP_FOURCC,cv2.VideoWriter_fourcc('M','J','P','G'))
    ret , frame = cam.read()
    cam.release()
0
On

I had the same problem and fixed it by moving my USB camera over to one of the USB 2.0 ports (the black ones). I only saw the problem when I plugged the camera into the USB 3.0 ports (the blue ones)