Second camera is not opening until the first camera is released

asked 2017-10-18 11:44:43 -0500

Vijay Nirmal gravatar image

updated 2017-10-18 13:47:11 -0500

I want to display the video inputs from two cameras at the same time. I used below code.

capL = cv.VideoCapture(0)
capR = cv.VideoCapture(1)

if capL.isOpened() & capR.isOpened():
    while True:
        retL, frameL = capL.read()
        retR, frameR = capR.read()

        leftImage = cv.cvtColor(frameL, cv.COLOR_BGR2GRAY)
        rightImage = cv.cvtColor(frameR, cv.COLOR_BGR2GRAY)

        cv.imshow('Left Camera', leftImage)
        cv.imshow('Right Camera', rightImage)

        if cv.waitKey(25) & 0xFF == ord('q'):
            break
else:
    print("unable to open camera")

But the output always shows

unable to open camera

To debug it, I used below code

capL = cv.VideoCapture(0)
while not capL.isOpened():
    print('L not open')

capR = cv.VideoCapture(1)
while not capR.isOpened():
    print('R not open')

And I found out the second camera is not opening. But if I release the first camera then the second camera is opening.

capL = cv.VideoCapture(0)
while not capL.isOpened():
    print('L not open')
capL.release()

capR = cv.VideoCapture(1)
while not capR.isOpened():
    print('R not open')

Am I doing something wrong?

edit retag flag offensive close merge delete

Comments

Is current sufficient on usb bus for both camera?

LBerger gravatar imageLBerger ( 2017-10-18 11:55:11 -0500 )edit

@LBerger I have tried with one USB cam and a built-in webcam. same problem

Vijay Nirmal gravatar imageVijay Nirmal ( 2017-10-18 12:59:55 -0500 )edit

Do you use a raspberry card?

LBerger gravatar imageLBerger ( 2017-10-18 13:27:15 -0500 )edit

@LBerger Not at this moment but will use it in near future.

Vijay Nirmal gravatar imageVijay Nirmal ( 2017-10-18 13:46:40 -0500 )edit

I don't know python but in capL.isOpened() & capR.isOpened() the & is logical "and" or a binary operator

LBerger gravatar imageLBerger ( 2017-10-18 14:13:50 -0500 )edit

@LBerger It is correct. In python, it is single & or I can use and. I have tested, capR.isOpened() returns false

Vijay Nirmal gravatar imageVijay Nirmal ( 2017-10-18 15:09:46 -0500 )edit

Can you run two programs first one openning first camera and second program the second camera?

LBerger gravatar imageLBerger ( 2017-10-19 12:47:36 -0500 )edit

@LBerger After restarting my computer, my code works. I don't understand what is the problem but it works now. Thanks for helping.

Vijay Nirmal gravatar imageVijay Nirmal ( 2017-10-20 06:06:52 -0500 )edit