From 372205957b42e7213c592be1fc14f9a2124a3769 Mon Sep 17 00:00:00 2001 From: wakonig_k Date: Tue, 5 Nov 2024 14:51:16 +0100 Subject: [PATCH] refactor: formatting --- csaxs_bec/devices/ids_cameras/ids_camera.py | 150 ++++++++++++-------- 1 file changed, 93 insertions(+), 57 deletions(-) diff --git a/csaxs_bec/devices/ids_cameras/ids_camera.py b/csaxs_bec/devices/ids_cameras/ids_camera.py index 273dc39..243874a 100644 --- a/csaxs_bec/devices/ids_cameras/ids_camera.py +++ b/csaxs_bec/devices/ids_cameras/ids_camera.py @@ -1,38 +1,49 @@ -from ophyd import Device -from ophyd_devices.interfaces.base_classes.psi_detector_base import CustomDetectorMixin, PSIDetectorBase -from pyueye import ueye -from ophyd_devices.sim.sim_signals import SetableSignal -from ophyd import Kind, Component as Cpt import threading -import numpy as np import time +import numpy as np +from ophyd import Component as Cpt +from ophyd import Device, Kind +from ophyd_devices.interfaces.base_classes.psi_detector_base import ( + CustomDetectorMixin, + PSIDetectorBase, +) +from ophyd_devices.sim.sim_signals import SetableSignal +from pyueye import ueye + + class IDSCustomPrepare(CustomDetectorMixin): USER_ACCESS = ["pyueye"] pyueye = ueye - + def __init__(self, *_args, parent: Device = None, **_kwargs) -> None: super().__init__(*_args, parent=parent, **_kwargs) self.ueye = ueye self.h_cam = None - self.s_info =None + self.s_info = None self.data_thread = None - self.thread_event = None - - + self.thread_event = None def on_connection_established(self): - self.hCam = self.ueye.HIDS(self.parent.camera_ID) #0: first available camera; 1-254: The camera with the specified camera ID + self.hCam = self.ueye.HIDS( + self.parent.camera_ID + ) # 0: first available camera; 1-254: The camera with the specified camera ID self.sInfo = self.ueye.SENSORINFO() self.cInfo = self.ueye.CAMINFO() self.pcImageMemory = self.ueye.c_mem_p() self.MemID = self.ueye.int() self.rectAOI = self.ueye.IS_RECT() self.pitch = self.ueye.INT() - self.nBitsPerPixel = self.ueye.INT(self.parent.bits_per_pixel) #24: bits per pixel for color mode; take 8 bits per pixel for monochrome - self.channels = self.parent.channels #3: channels for color mode(RGB); take 1 channel for monochrome - self.m_nColorMode = self.ueye.INT(self.parent.m_n_colormode) # Y8/RGB16/RGB24/REG32 (1 for our color cameras) + self.nBitsPerPixel = self.ueye.INT( + self.parent.bits_per_pixel + ) # 24: bits per pixel for color mode; take 8 bits per pixel for monochrome + self.channels = ( + self.parent.channels + ) # 3: channels for color mode(RGB); take 1 channel for monochrome + self.m_nColorMode = self.ueye.INT( + self.parent.m_n_colormode + ) # Y8/RGB16/RGB24/REG32 (1 for our color cameras) self.bytes_per_pixel = int(self.nBitsPerPixel / 8) # Starts the driver and establishes the connection to the camera @@ -57,36 +68,43 @@ class IDSCustomPrepare(CustomDetectorMixin): # Set display mode to DIB nRet = self.ueye.is_SetDisplayMode(self.hCam, self.ueye.IS_SET_DM_DIB) - - # Set the right color mode - if int.from_bytes(self.sInfo.nColorMode.value, byteorder='big') == self.ueye.IS_COLORMODE_BAYER: + if ( + int.from_bytes(self.sInfo.nColorMode.value, byteorder="big") + == self.ueye.IS_COLORMODE_BAYER + ): # setup the color depth to the current windows setting self.ueye.is_GetColorDepth(self.hCam, self.nBitsPerPixel, self.m_nColorMode) bytes_per_pixel = int(self.nBitsPerPixel / 8) - print("IS_COLORMODE_BAYER: ", ) + print("IS_COLORMODE_BAYER: ") print("\tm_nColorMode: \t\t", self.m_nColorMode) print("\tnBitsPerPixel: \t\t", self.nBitsPerPixel) print("\tbytes_per_pixel: \t\t", bytes_per_pixel) print() - elif int.from_bytes(self.sInfo.nColorMode.value, byteorder='big') == self.ueye.IS_COLORMODE_CBYCRY: + elif ( + int.from_bytes(self.sInfo.nColorMode.value, byteorder="big") + == self.ueye.IS_COLORMODE_CBYCRY + ): # for color camera models use RGB32 mode m_nColorMode = ueye.IS_CM_BGRA8_PACKED nBitsPerPixel = ueye.INT(32) bytes_per_pixel = int(self.nBitsPerPixel / 8) - print("IS_COLORMODE_CBYCRY: ", ) + print("IS_COLORMODE_CBYCRY: ") print("\tm_nColorMode: \t\t", m_nColorMode) print("\tnBitsPerPixel: \t\t", nBitsPerPixel) print("\tbytes_per_pixel: \t\t", bytes_per_pixel) print() - elif int.from_bytes(self.sInfo.nColorMode.value, byteorder='big') == self.ueye.IS_COLORMODE_MONOCHROME: + elif ( + int.from_bytes(self.sInfo.nColorMode.value, byteorder="big") + == self.ueye.IS_COLORMODE_MONOCHROME + ): # for color camera models use RGB32 mode m_nColorMode = self.ueye.IS_CM_MONO8 nBitsPerPixel = self.ueye.INT(8) bytes_per_pixel = int(nBitsPerPixel / 8) - print("IS_COLORMODE_MONOCHROME: ", ) + print("IS_COLORMODE_MONOCHROME: ") print("\tm_nColorMode: \t\t", m_nColorMode) print("\tnBitsPerPixel: \t\t", nBitsPerPixel) print("\tbytes_per_pixel: \t\t", bytes_per_pixel) @@ -100,7 +118,9 @@ class IDSCustomPrepare(CustomDetectorMixin): print("else") # Can be used to set the size and position of an "area of interest"(AOI) within an image - nRet = self.ueye.is_AOI(self.hCam, ueye.IS_AOI_IMAGE_GET_AOI, self.rectAOI, self.ueye.sizeof(self.rectAOI)) + nRet = self.ueye.is_AOI( + self.hCam, ueye.IS_AOI_IMAGE_GET_AOI, self.rectAOI, self.ueye.sizeof(self.rectAOI) + ) if nRet != self.ueye.IS_SUCCESS: print("is_AOI ERROR") @@ -108,16 +128,18 @@ class IDSCustomPrepare(CustomDetectorMixin): self.height = self.rectAOI.s32Height # Prints out some information about the camera and the sensor - print("Camera model:\t\t", self.sInfo.strSensorName.decode('utf-8')) - print("Camera serial no.:\t", self.cInfo.SerNo.decode('utf-8')) + print("Camera model:\t\t", self.sInfo.strSensorName.decode("utf-8")) + print("Camera serial no.:\t", self.cInfo.SerNo.decode("utf-8")) print("Maximum image width:\t", self.width) print("Maximum image height:\t", self.height) print() - #--------------------------------------------------------------------------------------------------------------------------------------- + # --------------------------------------------------------------------------------------------------------------------------------------- # Allocates an image memory for an image having its dimensions defined by width and height and its color depth defined by nBitsPerPixel - nRet = self.ueye.is_AllocImageMem(self.hCam, self.width, self.height, self.nBitsPerPixel, self.pcImageMemory, self.MemID) + nRet = self.ueye.is_AllocImageMem( + self.hCam, self.width, self.height, self.nBitsPerPixel, self.pcImageMemory, self.MemID + ) if nRet != self.ueye.IS_SUCCESS: print("is_AllocImageMem ERROR") else: @@ -129,29 +151,34 @@ class IDSCustomPrepare(CustomDetectorMixin): # Set the desired color mode nRet = self.ueye.is_SetColorMode(self.hCam, self.m_nColorMode) - - # Activates the camera's live video mode (free run mode) nRet = self.ueye.is_CaptureVideo(self.hCam, self.ueye.IS_DONT_WAIT) if nRet != self.ueye.IS_SUCCESS: print("is_CaptureVideo ERROR") # Enables the queue mode for existing image memory sequences - nRet = self.ueye.is_InquireImageMem(self.hCam, self.pcImageMemory, self.MemID, self.width, self.height, self.nBitsPerPixel, self.pitch) + nRet = self.ueye.is_InquireImageMem( + self.hCam, + self.pcImageMemory, + self.MemID, + self.width, + self.height, + self.nBitsPerPixel, + self.pitch, + ) if nRet != self.ueye.IS_SUCCESS: print("is_InquireImageMem ERROR") else: print("Press q to leave the programm") - startmeasureframerate=True + startmeasureframerate = True Gain = False - #Start live mode of camera immediately + # Start live mode of camera immediately self.parent.start_live_mode() - def _start_data_thread(self): self.thread_event = threading.Event() - self.data_thread = threading.Thread(target=self._receive_data_from_camera, daemon = True) + self.data_thread = threading.Thread(target=self._receive_data_from_camera, daemon=True) self.data_thread.start() def _receive_data_from_camera(self): @@ -159,12 +186,19 @@ class IDSCustomPrepare(CustomDetectorMixin): # In order to display the image in an OpenCV window we need to... # ...extract the data of our image memory - array = ueye.get_data(self.pcImageMemory, self.width, self.height, self.nBitsPerPixel, self.pitch, copy=False) + array = ueye.get_data( + self.pcImageMemory, + self.width, + self.height, + self.nBitsPerPixel, + self.pitch, + copy=False, + ) # bytes_per_pixel = int(nBitsPerPixel / 8) # ...reshape it in an numpy array... - frame = np.reshape(array,(self.height.value, self.width.value, self.bytes_per_pixel)) + frame = np.reshape(array, (self.height.value, self.width.value, self.bytes_per_pixel)) self.parent.image_data.put(frame) self.parent._run_subs(sub_type=self.parent.SUB_MONITOR, value=frame) @@ -174,31 +208,34 @@ class IDSCustomPrepare(CustomDetectorMixin): pass # self.parent._run_subs(sub_type=self.parent.SUB_MONITOR, value=self.parent.image_data.get()) - class IDSCamera(PSIDetectorBase): USER_ACCESS = ["start_live_mode", "stop_live_mode"] - + custom_prepare_cls = IDSCustomPrepare - image_data = Cpt(SetableSignal, value=np.empty((100,100)), kind=Kind.omitted) + image_data = Cpt(SetableSignal, value=np.empty((100, 100)), kind=Kind.omitted) SUB_MONITOR = "device_monitor_2d" _default_sub = SUB_MONITOR - def __init__(self, - prefix="", - *, - name:str, - camera_ID: int, - bits_per_pixel: int, - channels: int, - m_n_colormode: int, - kind=None, - parent=None, - device_manager=None, - **kwargs): - super().__init__(prefix, name=name, kind=kind, parent=parent, device_manager=device_manager, **kwargs) + def __init__( + self, + prefix="", + *, + name: str, + camera_ID: int, + bits_per_pixel: int, + channels: int, + m_n_colormode: int, + kind=None, + parent=None, + device_manager=None, + **kwargs, + ): + super().__init__( + prefix, name=name, kind=kind, parent=parent, device_manager=device_manager, **kwargs + ) self.camera_ID = camera_ID self.bits_per_pixel = bits_per_pixel self.channels = channels @@ -218,10 +255,9 @@ class IDSCamera(PSIDetectorBase): if self.custom_prepare.data_thread is not None: self.stop_live_mode() self.custom_prepare._start_data_thread() - + def stop_live_mode(self): - """ Stopping the camera live mode. - """ + """Stopping the camera live mode.""" if self.custom_prepare.thread_event is not None: self.custom_prepare.thread_event.set() if self.custom_prepare.data_thread is not None: @@ -430,4 +466,4 @@ cv2.destroyAllWindows() print() print("END") -""" \ No newline at end of file +"""