How to read videos from three cameras in the same time?
Hello,
I tried to getting videos from three cameras in the same time, but I have the following error, do you have any idea how to read videos from three cameras in python with opencv? It's possible?
This is my code:
import cv2
cap1 = cv2.VideoCapture(0)
cap2 = cv2.VideoCapture(1)
cap3 = cv2.VideoCapture(2)
while True:
print("In while")
test1, frame1 = cap1.read()
test2, frame2 = cap2.read()
test3, frame3 = cap3.read()
gray1 = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)
gray3 = cv2.cvtColor(frame3, cv2.COLOR_BGR2GRAY)
if(test1):
cv2.imshow('frame1', gray1)
print("Frame 1")
if(test2):
cv2.imshow('frame2', gray2)
print("Frame 2")
if(test3):
cv2.imshow('frame3', gray3)
print("Frame 3")
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap1.release()
cap2.release()
cap3.release()
cv2.destroyAllWindows()
And this is the error:
[ WARN:0] videoio(MSMF): OnReadSample() is called with error status: -1072875772 [ WARN:0] videoio(MSMF): async ReadSample() call is failed with error status: -1072875772 [ WARN:1] videoio(MSMF): can't grab frame. Error: -1072875772
Traceback (most recent call last):
File "main.py", line 21, in <module> gray3 = cv2.cvtColor(frame3, cv2.COLOR_BGR2GRAY) cv2.error: OpenCV(3.4.5) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:181: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'[ WARN:1] terminating async callback [ WARN:1] terminating async callback
Can you help me, please, to clarify this topic? Thank you in advance! :)
can you try running it with just the first camera and see if it works? If not, change
cap1 = cv2.VideoCapture(0)
tocap1 = cv2.VideoCapture(0+cv2.CAP_DSHOW)
and try again.With one or two cameras it works, only with three cameras not working :( I tried with your method but not working :(
do all three cameras work individually? if they do, then you have three options.
1: resize the frames.
cap1.set(3,160) cap1.set(4,120) cap2.set(3,160) cap2.set(4,120) cap3.set(3,160) cap3.set(4,120)
2: use multithreading
3: a combination of the two
Yes, individually camera works and if I have only two cameras all is ok, but only with three cameras it's a problem. I tried all possible methods but not working. I will try with multithreading
I discovered the problem. I had two cameras connected at the same USB port using a USB hub.
check https://en.wikipedia.org/w/index.php?...
@LBerger: thank you! :)