Ask Your Question
0

Why do I get an interaction between different cameras?

asked 2017-06-10 04:48:34 -0600

Bazmundi gravatar image

updated 2017-06-10 21:26:43 -0600

Code is:

import numpy as np
import cv2

cam1 = cv2.VideoCapture(1)
cam2 = cv2.VideoCapture(2)

cv2.namedWindow( "Left", cv2.WINDOW_AUTOSIZE );
cv2.namedWindow( "Right", cv2.WINDOW_AUTOSIZE );

while(True):
    # Capture frame-by-frame
    ret, frame1 = cam1.read()
    ret, frame2 = cam2.read()



    # Display the resulting frame
    cv2.imshow('Left',frame1)  # code line works
    cv2.imshow('Right',frame2)  # code line hangs



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

# When everything done, release the capture
cam1.release()
cam2.release()
cv2.destroyAllWindows()

Problem is that code hangs on imshow of frame 2 if and only if frame1 was read. That is, if I delete the code line that hangs then code runs. If I delete code line that hangs but change frame used to frame2 then code runs. So, when both camx.read() are called it seems fine, it is only hanging when the two imshow are being called.

Error reported is:

Traceback (most recent call last):
  File "D:\python\webcam.py", line 19, in <module>
    cv2.imshow('Right',frame2)  # hangs
error: C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\highgui\src\window.cpp:304: error: (-215) size.width>0 && size.height>0 in function cv::imshow

Quirk also is that the following works unless I swap the order of the VideoCapture(x):

cv2.imshow('Left',frame1)  # code line works
cv2.imshow('Right',frame1)  # code line still works (but using same buffer)

While the following does not work unless I swap the order of the VideoCapture(x):

cv2.imshow('Left',frame2)  # code line not longer works (using second buffer)
cv2.imshow('Right',frame2)  # ditto

I am using a stereo camera which works as two separate cameras. Camera works with other software. Running OpenCV3.2 on 64bit Python 2.7.9 (stackless).

Another couple of things. Coding up to using only one camera and changing the index for the camera, to select either one or the other camera, works.

I found, by experimenting, that releasing camera 1 allow camera 2 to read a buffer BUT alternatingly creating/then releasing cameras did not work - there didn't seem to be an alternate way to do this with the python.

So, it appears, that something more fiddly than creating cam1 and cam2 is required as the cameras seem to share something that does not allow them to be active at same time.
Cheers, B

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-06-10 21:38:44 -0600

Bazmundi gravatar image

updated 2017-06-10 21:45:27 -0600

Self Answer (given I don't have kudos to add self answer yet) is:

cam1 = cv2.VideoCapture(1)
cam1.set(cv2.CAP_PROP_FRAME_WIDTH,384)
cam1.set(cv2.CAP_PROP_FRAME_HEIGHT,216)

cam2 = cv2.VideoCapture(2)
cam2.set(cv2.CAP_PROP_FRAME_WIDTH,384)
cam2.set(cv2.CAP_PROP_FRAME_HEIGHT,216)

It seems the USB channel gets choked (it is only USB 2.0) so one needs to right size the images so that two cameras can work off that single port. Full code solution tomorrow or next day once my kudos limit expires.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-06-10 04:48:34 -0600

Seen: 374 times

Last updated: Jun 10 '17