If I run this code on a PC with two webcams attached it runs fast (or fast enough) without any issues:
import cv2
cap0 = cv2.VideoCapture(0)
cap1 = cv2.VideoCapture(1)
while True:
ret0, img0 = cap0.read()
print('read 0')
ret0, img1 = cap1.read()
print('read 1')
When I run the same code on Rpi3B+, I get this output. It cannot read the second camera and even if the first camera is being read, it's so slow it's unusable:
read 0
[ WARN:[email protected]] global /tmp/pip-wheel-efxaz4j7/opencv-python_bedc0fac27944da0921e079da44d32bf/opencv/modules/videoio/src/cap_v4l.cpp (1000) tryIoctl VIDEOIO(V4L2:/dev/video1): select() timeout.
read 1
Somehow OpenCV on RPi cannot properly do cv2.VideoCapture
on two cameras. I've tried using threads and QThreads, but that doesn't help. If you change the loop to capture and release the camera in every loop step, it works: but it's too slow (< 1 FPS):
import cv2
while True:
cap0 = cv2.VideoCapture(0)
ret0, img0 = cap0.read()
cap0.release()
print('read 0')
cap1 = cv2.VideoCapture(1)
ret1, img1 = cap1.read()
print('read 1')
cap1.release()
Are there any tricks to overcome the limitations of OpenCV on RPi?
Update: I degraded resolution from 640x480 (default) to 320x240 per this question and it seems to work. However, that resolution may not be usable. I'm basically just asking for one frame at a time. It's confusing to me why reducing resolution makes such a huge difference.
cap0.set(3,320)
cap0.set(4,240)
cap1.set(3,320)
cap1.set(4,240)
cap0
code from the first script and run it ... then create a second script forcap1
and run it at the same time in a separate terminal windowselect() timeout
.