How to read videos from three cameras in the same time?

asked 2019-05-02 10:31:56 -0500

Laurentiu gravatar image


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 =
    test2, frame2 =
    test3, frame3 =
    gray1 = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
    gray2 = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)
    gray3 = cv2.cvtColor(frame3, cv2.COLOR_BGR2GRAY)

         cv2.imshow('frame1', gray1)
         print("Frame 1")
         cv2.imshow('frame2', gray2)
         print("Frame 2")
         cv2.imshow('frame3', gray3)
         print("Frame 3")

     if cv2.waitKey(1) & 0xFF == ord('q'):


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 "", 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! :)

edit retag flag offensive close merge delete



can you try running it with just the first camera and see if it works? If not, change cap1 = cv2.VideoCapture(0) to cap1 = cv2.VideoCapture(0+cv2.CAP_DSHOW) and try again.

eshirima gravatar imageeshirima ( 2019-05-02 10:39:16 -0500 )edit

With one or two cameras it works, only with three cameras not working :( I tried with your method but not working :(

Laurentiu gravatar imageLaurentiu ( 2019-05-02 10:59:09 -0500 )edit

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

eshirima gravatar imageeshirima ( 2019-05-02 11:17:19 -0500 )edit

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

Laurentiu gravatar imageLaurentiu ( 2019-05-02 11:37:57 -0500 )edit

I discovered the problem. I had two cameras connected at the same USB port using a USB hub.

Laurentiu gravatar imageLaurentiu ( 2019-05-02 12:00:34 -0500 )edit
LBerger gravatar imageLBerger ( 2019-05-02 12:00:58 -0500 )edit

@LBerger: thank you! :)

Laurentiu gravatar imageLaurentiu ( 2019-05-02 12:02:58 -0500 )edit