I'm developing a multi-threaded application to capture images from four USB web cameras. For simplicity and early development, I'm using Logitech C920 at 640x480 and 30fps.
I have a simple function that opens a camera and sets some parameters, then releases the camera. Because this is a multithreaded app, four threads each are running this when a button is pressed. It works great.
def camParameter(previewName, camID):
#Set camera object and set parameters
start_time = time.time()
cam_test = True
while cam_test:
cam = cv2.VideoCapture(camID)
present_time = time.time()
if present_time - start_time > 2:
print("Could not open camera ", str(camID))
break
if cam.isOpened():
cam_test=False
width = 640
height = 480
fps = 30
test_width = cam.get(3)
test_height = cam.get(4)
test_fps = cam.get(5)
if test_width != width:
cam.set(3,width)
if test_height != height:
cam.set(4,height)
if test_fps != fps:
cam.set(5,fps)
print("Parameters set for camera ", str(camID))
cam.release()
However, if I call the function again, or attempt to open the camera to stream, I get the following error:
VIDEOIO ERROR: V4L2: Pixel format of incoming image is unsupported by OpenCV Unable to stop the stream: Device or resource busy
I can open the camera with GUVCviewer, or unplug/replug the camera to get access back.
Any ideas why a second access of the camera would cause this issue, or how to fix it?
I have verified the camera is in-fact released. I can access the camera
I recompiled openCV with GStreamer - it's much more multithread friendly.