alliedvision / VimbaPython

Old Allied Vision Vimba Python API. The successor to this API is VmbPy
BSD 2-Clause "Simplified" License
93 stars 40 forks source link

Unable to change PixelFormat with AccessMode.Full #178

Open nparker2020 opened 8 months ago

nparker2020 commented 8 months ago

Hello,

I am modifying the asynchronous_grab_opencv.py example to set the camera's configuration from an XML file that I have, which contains exposure / color balance configuration that I like.

However, when I try to call cam.load_settings() I get an exception: vmbpy.error.VmbFeatureError: Invalid access while calling 'set()' of Feature 'PixelFormat'. Read access: allowed. Write access: not allowed.

I have added a call to set the access mode of the camera to "Full", in the get_camera method (it's the only place where the cam object is not in a with context block)

I have checked that the access mode is still set to Full later when I call load_settings. I am at a loss as to why this doesn't work, I can change the PixelFormat no problem in VimbaXViewr, that's how I generated this XML file in the first place.

Any help would be appreciated. Thanks in advance.

Here is the python script in its entirety:

"""BSD 2-Clause License

Copyright (c) 2023, Allied Vision Technologies GmbH
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
import sys
from typing import Optional
from queue import Queue

from vmbpy import *

# All frames will either be recorded in this format, or transformed to it before being displayed
opencv_display_format = PixelFormat.Bgr8

def print_preamble():
    print('///////////////////////////////////////////////////')
    print('/// VmbPy Asynchronous Grab with OpenCV Example ///')
    print('///////////////////////////////////////////////////\n')

def print_usage():
    print('Usage:')
    print('    python asynchronous_grab_opencv.py [camera_id]')
    print('    python asynchronous_grab_opencv.py [/h] [-h]')
    print()
    print('Parameters:')
    print('    camera_id   ID of the camera to use (using first camera if not specified)')
    print()

def abort(reason: str, return_code: int = 1, usage: bool = False):
    print(reason + '\n')

    if usage:
        print_usage()

    sys.exit(return_code)

def parse_args() -> Optional[str]:
    args = sys.argv[1:]
    argc = len(args)

    for arg in args:
        if arg in ('/h', '-h'):
            print_usage()
            sys.exit(0)

    if argc > 1:
        abort(reason="Invalid number of arguments. Abort.", return_code=2, usage=True)

    return None if argc == 0 else args[0]

def get_camera(camera_id: Optional[str]) -> Camera:
    with VmbSystem.get_instance() as vmb:
        if camera_id:
            try:
                return vmb.get_camera_by_id(camera_id)

            except VmbCameraError:
                abort('Failed to access Camera \'{}\'. Abort.'.format(camera_id))

        else:
            cams = vmb.get_all_cameras()
            if not cams:
                abort('No Cameras accessible. Abort.')
            cam = cams[0]
            cam.set_access_mode(AccessMode.Full)

            return cams[0]

def setup_camera(cam: Camera):
    #cam.set_access_mode(AccessMode.Full)

    with cam:
        #try to set the camera's config from an XML file
        try:
            print(cam.get_access_mode())
            print("LOADING SETTINGS FROM XML")
            cam.load_settings("my_camera_settings.xml", PersistType.All)
        except (AttributeError, VmbFeatureError):
            print("Failed to load settings from XML file")
            pass

        # # Enable auto exposure time setting if camera supports it

        # try:
        #     cam.ExposureAuto.set('Continuous')

        # except (AttributeError, VmbFeatureError):
        #     pass

        # # Enable white balancing if camera supports it
        # try:
        #     cam.BalanceWhiteAuto.set('Continuous')

        # except (AttributeError, VmbFeatureError):
        #     pass

        # Try to adjust GeV packet size. This Feature is only available for GigE - Cameras.
        try:
            stream = cam.get_streams()[0]
            stream.GVSPAdjustPacketSize.run()
            while not stream.GVSPAdjustPacketSize.is_done():
                pass

        except (AttributeError, VmbFeatureError):
            pass

def setup_pixel_format(cam: Camera):
    # Query available pixel formats. Prefer color formats over monochrome formats
    cam_formats = cam.get_pixel_formats()
    cam_color_formats = intersect_pixel_formats(cam_formats, COLOR_PIXEL_FORMATS)
    convertible_color_formats = tuple(f for f in cam_color_formats
                                      if opencv_display_format in f.get_convertible_formats())

    cam_mono_formats = intersect_pixel_formats(cam_formats, MONO_PIXEL_FORMATS)
    convertible_mono_formats = tuple(f for f in cam_mono_formats
                                     if opencv_display_format in f.get_convertible_formats())

    # if OpenCV compatible color format is supported directly, use that
    if opencv_display_format in cam_formats:
        cam.set_pixel_format(opencv_display_format)

    # else if existing color format can be converted to OpenCV format do that
    elif convertible_color_formats:
        cam.set_pixel_format(convertible_color_formats[0])

    # fall back to a mono format that can be converted
    elif convertible_mono_formats:
        cam.set_pixel_format(convertible_mono_formats[0])

    else:
        abort('Camera does not support an OpenCV compatible format. Abort.')

class Handler:
    def __init__(self):
        self.display_queue = Queue(10)

    def get_image(self):
        return self.display_queue.get(True)

    def __call__(self, cam: Camera, stream: Stream, frame: Frame):
        if frame.get_status() == FrameStatus.Complete:
            print('{} acquired {}'.format(cam, frame), flush=True)

            # Convert frame if it is not already the correct format
            if frame.get_pixel_format() == opencv_display_format:
                display = frame
            else:
                # This creates a copy of the frame. The original `frame` object can be requeued
                # safely while `display` is used
                display = frame.convert_pixel_format(opencv_display_format)

            self.display_queue.put(display.as_opencv_image(), True)

        cam.queue_frame(frame)

def main():
    print_preamble()
    cam_id = parse_args()

    with VmbSystem.get_instance():
        with get_camera(cam_id) as cam:
            # setup general camera settings and the pixel format in which frames are recorded
            setup_camera(cam)
            setup_pixel_format(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)

                msg = 'Stream from \'{}\'. Press <Enter> to stop stream.'
                import cv2
                ENTER_KEY_CODE = 13
                while True:
                    key = cv2.waitKey(1)
                    if key == ENTER_KEY_CODE:
                        cv2.destroyWindow(msg.format(cam.get_name()))
                        break

                    display = handler.get_image()
                    #resize display to 1080p
                    display = cv2.resize(display, (1920, 1080))
                    cv2.imshow(msg.format(cam.get_name()), display)

            finally:
                cam.stop_streaming()

if __name__ == '__main__':
    main()
nparker2020 commented 8 months ago

I found the root of the issue. The call of setup_pixel_format AFTER the XML is loaded is the true cause of the exception.

While my issue is technically resolved if I comment that line out, can somebody explain why setting the pixel format after loading settings from XML causes an exception?

Thank you.

Teresa-AlliedVision commented 8 months ago

I was unable to reproduce the issue, can you let me know the following: -camera model -camera firmware -OS -VimbaX version

nparker2020 commented 8 months ago

Teresa,

Thanks for looking into this. FYI I just re-tried this today and I am no longer getting this same issue. Everyone works fine now even if I set the PixelFormat after loading from XML. Not sure what has changed since I was experiencing this issue.

Still though, here's my system info:

Camera Model: Allied Vision 1800 u-1240c Camera Firmware: 12.0.00611A22 OS: Ubuntu 22.04 VimbaX Version: 2023-2