Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You can specify the path to the hardware device that you want to use. By doing that it'll reconnect and let you capture pictures. So all you need to do is keep track of which camera is using which device and reconnect accordingly. I've always seen Ubuntu enumerate cameras as /dev/video0, /dev/video1, etc... Using OpenCV 3.4.1 on Ubuntu 16.04.3, (note: this may work on older versions but I haven't tested it):

import glob
import cv2

if __name__ == '__main__':

    # Lists which video devices are available
    device_paths = glob.glob( '/dev/video*' )

    # Connect to each camera initially
    camera_a_path = device_paths[0]
    camera_a_camera = cv2.VideoCapture( camera_a_path )
    camera_b_path = device_paths[1]
    camera_b_camera = cv2.VideoCapture( camera_b_path )

    # # # Assume a disconnect of camera_b happens here # # #
    # If disconnected, `img` is `None` in the line below
    # rv, img = camera_b_camera.read()

    # Reconnect to the disconnected camera
    new_paths = glob.glob( '/dev/video*' )
    new_paths.remove(camera_a_path)
    camera_b_path = new_paths[0]
    camera_b_camera = cv2.VideoCapture( camera_b_path )