diff options
| author | morefigs <morefigs@gmail.com> | 2019-01-27 13:43:42 +1100 |
|---|---|---|
| committer | morefigs <morefigs@gmail.com> | 2019-01-27 13:43:42 +1100 |
| commit | f2477b9f6a19fd21df6955e4818cf1274ce34a65 (patch) | |
| tree | f86e88dff9ce84f042b22d04a95eed65f9e1c336 | |
| parent | 9b2c7bc2150a5945b06888cd24438fdb6fddbfb1 (diff) | |
| download | pymba-f2477b9f6a19fd21df6955e4818cf1274ce34a65.tar.gz pymba-f2477b9f6a19fd21df6955e4818cf1274ce34a65.zip | |
refactor out camera and interface funcs
| -rw-r--r-- | pymba/vimba.py | 180 |
1 files changed, 40 insertions, 140 deletions
diff --git a/pymba/vimba.py b/pymba/vimba.py index 1174c3d..a943083 100644 --- a/pymba/vimba.py +++ b/pymba/vimba.py @@ -1,14 +1,14 @@ -from ctypes import byref, sizeof, c_uint32 -from typing import List +from ctypes import sizeof +from typing import List, Union from .vimba_exception import VimbaException from .system import System -from .camera import Camera -from .interface import Interface +from .interface import Interface, interface_ids +from .camera import Camera, camera_ids from . import vimba_c -class Vimba(object): +class Vimba: """ Python wrapper for Allied Vision's Vimba C API. """ @@ -18,7 +18,6 @@ class Vimba(object): def __init__(self): # create own system singleton object self._system = System() - self._vmb_interface_infos = None self._interfaces = {} self._cameras = {} @@ -38,13 +37,6 @@ class Vimba(object): self.shutdown() @property - def system(self) -> System: - """ - Get the system object. - """ - return self._system - - @property def version(self) -> str: """ Retrieve the version number of the Vimba C API. @@ -58,7 +50,8 @@ class Vimba(object): vmb_version_info.minor, vmb_version_info.patch)) - def startup(self): + @staticmethod + def startup(): """ Initialize the Vimba C API. """ @@ -66,147 +59,54 @@ class Vimba(object): if error: raise VimbaException(error) - def shutdown(self): + @staticmethod + def shutdown(): """ Perform a shutdown on the API. """ vimba_c.vmb_shutdown() - def _get_interface_infos(self) -> List[vimba_c.VmbInterfaceInfo]: - """ - Gets interface info of all available interfaces. - """ - - # todo is this caching required/harmful? - - if self._vmb_interface_infos is None: - num_found = c_uint32(-1) - - # call once just to get the number of interfaces - error = vimba_c.vmb_interfaces_list(None, - 0, - byref(num_found), - sizeof(vimba_c.VmbInterfaceInfo)) - if error: - raise VimbaException(error) - - # call again to get the features - num_interfaces = num_found.value - vmb_interface_infos = (vimba_c.VmbInterfaceInfo * num_interfaces)() - error = vimba_c.vmb_interfaces_list(vmb_interface_infos, - num_interfaces, - byref(num_found), - sizeof(vimba_c.VmbInterfaceInfo)) - if error: - raise VimbaException(error) - - self._vmb_interface_infos = list(vmb_interface_info for vmb_interface_info in vmb_interface_infos) - - return self._vmb_interface_infos - - def _get_camera_infos(self) -> List[vimba_c.VmbCameraInfo]: - """ - Gets camera info of all attached cameras. - """ - vmb_camera_info = vimba_c.VmbCameraInfo() - num_found = c_uint32(-1) - - # call once just to get the number of cameras - error = vimba_c.vmb_cameras_list(byref(vmb_camera_info), - 0, - byref(num_found), - sizeof(vmb_camera_info)) - if error and error != -9: - raise VimbaException(error) - - num_cameras = num_found.value - - # call again to get the features - vmb_camera_infos = (vimba_c.VmbCameraInfo * num_cameras)() - error = vimba_c.vmb_cameras_list(vmb_camera_infos, - num_cameras, - byref(num_found), - sizeof(vmb_camera_info)) - if error: - raise VimbaException(error) - return list(vmb_camera_info for vmb_camera_info in vmb_camera_infos) - - def get_interface_ids(self) -> List[str]: + def system(self) -> System: """ - Gets IDs of all available interfaces. + Get the system object. """ - return list(vmb_interface_info.id_string for vmb_interface_info in self._get_interface_infos()) + return self._system - def get_camera_ids(self) -> List[str]: - """ - Gets IDs of all available cameras. - """ - return list(camera_info.id_string.decode() for camera_info in self._get_camera_infos()) + @staticmethod + def interface_ids(): + return interface_ids() - def _get_interface_info(self, id_string: str) -> vimba_c.VmbInterfaceInfo: + def interface(self, interface_id: Union[str, int]) -> Interface: """ - Gets interface info object of specified interface. - :param id_string: the ID of the interface object to get. + Gets interface object based on interface ID string or index. Will not recreate interface object if it already exists. + :param interface_id: the ID or the index of the interface. """ - # don't do this live as we already have this info - # return info object if it exists - for vmb_interface_info in self._get_interface_infos(): + # if index is provided, look up the camera id using the index + if isinstance(interface_id, int): + interface_id = interface_ids()[interface_id] - # todo broken lookup on info objects + if interface_id in self._interfaces: + return self._interfaces[interface_id] + interface = Interface(interface_id) + self._interfaces[interface_id] = interface + return interface - if vmb_interface_info.id_string == id_string: - return vmb_interface_info - raise VimbaException(VimbaException.ERR_INTERFACE_NOT_FOUND) + @staticmethod + def camera_ids() -> List[str]: + return camera_ids() - def _get_camera_info(self, id_string: str) -> vimba_c.VmbCameraInfo: + def camera(self, camera_id: Union[str, int]) -> Camera: """ - Gets camera info object of specified camera. - :param cameraId: the ID of the camera object to get. This can be an ID or e.g. a serial number. Check the Vimba - documentation for other possible values. + Gets camera object based on camera ID string or index. Will not recreate camera object if it already exists. + :param camera_id: the ID or the index of the camera object to get. This can be an ID or e.g. a serial number. + Check the Vimba documentation for other possible values. """ - vmb_camera_info = vimba_c.VmbCameraInfo() - error = vimba_c.vmb_camera_info_query(id_string.encode(), - vmb_camera_info, - sizeof(vmb_camera_info)) - if error: - raise VimbaException(error) - return vmb_camera_info + # if index is provided, look up the camera id using the index + if isinstance(camera_id, int): + camera_id = camera_ids()[camera_id] - def get_interface(self, id_string) -> Interface: - """ - Gets interface object based on interface ID string. Will not recreate interface object if it already exists. - :param id_string: the ID of the interface. - """ - # check ID is valid - if id_string in self.get_interface_ids(): - # create it if it doesn't exist - if id_string not in self._interfaces: - self._interfaces[id_string] = Interface(id_string) - return self._interfaces[id_string] - raise VimbaException(VimbaException.ERR_INTERFACE_NOT_FOUND) - - def get_camera(self, camera_id: str) -> Camera: - """ - Gets camera object based on camera ID string. Will not recreate camera object if it already exists. - :param camera_id: the ID of the camera object to get. This can be an ID or e.g. a serial number. Check the Vimba - documentation for other possible values. - """ - # check ID is valid - if camera_id in self.get_camera_ids(): - # create it if it doesn't exist - if camera_id not in self._cameras: - self._cameras[camera_id] = Camera(camera_id) + if camera_id in self._cameras: return self._cameras[camera_id] - else: - # the given string might not be a camera ID -> check for other IDs - vmb_camera_info = vimba_c.VmbCameraInfo() - error = vimba_c.vmb_camera_info_query(camera_id.encode(), - vmb_camera_info, - sizeof(vmb_camera_info)) - if error: - raise VimbaException(error) - - camera_id_string = vmb_camera_info.id_string.decode() - if camera_id_string not in self._cameras: - self._cameras[camera_id_string] = Camera(camera_id_string) - return self._cameras[camera_id_string] + camera = Camera(camera_id) + self._cameras[camera_id] = camera + return camera |
