Ask Your Question
0

USB camera disconnection/reconnection

asked 2015-11-09 07:40:34 -0600

mkp118 gravatar image

I have a project running Python with OpenCV and Pygame under Ubuntu 14.04 that uses two USB cameras, images from which are processed and displayed on the local monitor.

If the system is started with both cameras connected everything runs fine, if I disconnect one camera the associated part of the display freezes as expected. The disconnection is detected and the associated video capture object released. When the camera is reconnected the reconnection is detected and a new video capture object created and the display eventually resumes updating but ONLY if the camera has been allocated the same index, e.g. /dev/video0, as it had before it was disconnected.

I would like to know where I could look to try and find out why the video capture does not start if the device index is different on reconnection. I am new to Ubuntu, Python etc but have many years experience in microcontrollers and Visual Basic.

Thanks

edit retag flag offensive close merge delete

Comments

1

In Linux you can retrieve a list from the system of every coupled devices. Once you deconnect a camera, request that list, once you add a new one, check again. Then look for differences. This could be done in a second thread that keeps looking for changes in the camera's listed.

StevenPuttemans gravatar imageStevenPuttemans ( 2015-11-09 08:26:02 -0600 )edit

You could look here, where the VidoeCapture is defined

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-11-09 08:33:10 -0600 )edit
1

@StevenPuttemans: I am already doing that periodically within my script. The point is that, if the camera has a different index on reconnection, even though it is recognised and a new video capture object created to suit it is never activated and images are not captured.

mkp118 gravatar imagemkp118 ( 2015-11-09 10:03:36 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-09-06 11:51:45 -0600

ippo615 gravatar image

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 )
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-11-09 07:40:34 -0600

Seen: 3,685 times

Last updated: Nov 09 '15