VimbaPython icon indicating copy to clipboard operation
VimbaPython copied to clipboard

How to handle USB camera disconnection

Open edoardopareti opened this issue 2 years ago • 1 comments

Hi all.

I'm currently working with USB ALVIUM AVT cameras, and I'm having issues in understanding how to handle physical disconnection of the devices. It can be also explained by taking as an example the asynchronous_grab_opencv.py example in the Vimba Examples Folder. If I run the above mentioned script, and then disconnect the camera from its port, the execution will simply freeze. I need to handle the exception though. Can you give me some hints?

EDIT: Thought it could be better to include some additional info: Camera Model: Alvium 1800 U-319c Operating sys: Windows 11

Snippet code from Vimba Python Examples folder:

with Vimba.get_instance():
    with get_camera(cam_id) as cam:

        # Start Streaming, wait for five seconds, stop streaming
        setup_camera(cam)
        handler = Handler()

        try:
            # Start Streaming with a custom a buffer of 10 Frames (defaults to 5)
            cam.start_streaming(handler=handler, buffer_count=10)
            handler.shutdown_event.wait()

        finally:
            cam.stop_streaming()

Thanks in advance, EP

edoardopareti avatar May 03 '23 17:05 edoardopareti

You can try the following example for camera handling

import time

# This function is called for every camera event that is detected. The callback must take two
# parameters for VimbaPython. The first parameter is the `Camera` instance that changed, the second
# parameter es the type of event that occurred. Events can be `Detected`, `Reachable`,
# `Unreachable`, and `Missing`.
def camera_change_callback(cam, event):
    print(f'Executed user camera_change_callback: {cam}, {str(event)}')


# Get the `Vimba` singleton to register our callback function and to list our available cameras
vmb = vimba.Vimba.get_instance()
# This registers the change handler. To remove the callback, the
# vmb.unregister_all_camera_change_handlers and vmb.unregister_camera_change_handler are available.
vmb.register_camera_change_handler(camera_change_callback)

with vmb:
    while True:
        # Simple infinite loop, that prints available access modes for all detected cameras. If the
        # access mode for our cameras changes or the number of detected cameras changes, the
        # registered callback will be executed automatically. We do not need to do anything here.
        cams = vmb.get_all_cameras()
        for cam in cams:
            print(f'Camera {cam}: {cam.get_permitted_access_modes()}')
        # Delay execution so we do not spam the output too much.
        time.sleep(1)

BernardoLuck avatar May 04 '23 07:05 BernardoLuck