From 78800c135104cd5d2ed8fc28bc32a21f3db5a083 Mon Sep 17 00:00:00 2001 From: mhoeschle <mhoeschle> Date: Wed, 19 Jul 2023 15:22:20 +0200 Subject: [PATCH] * bigger changes in code. Implemented arguments to the function to run them for each camera in a loop. * Adding better help for listcams(). * Code can be exectuted now in shell / batch and visualCode. --- BaslerChangeParameter.py | 211 ++++++++++++++++++++++++--------------- 1 file changed, 132 insertions(+), 79 deletions(-) diff --git a/BaslerChangeParameter.py b/BaslerChangeParameter.py index dd27b74..87f96e4 100644 --- a/BaslerChangeParameter.py +++ b/BaslerChangeParameter.py @@ -15,19 +15,18 @@ import os MAX_CAMERAS_TO_USE = 2 -# Get the transport layer factory. -tlFactory = pylon.TlFactory.GetInstance() +# get instance of the pylon TransportLayerFactory +TLF = pylon.TlFactory.GetInstance() -# Get all attached devices and exit application if no device is found. -devices = tlFactory.EnumerateDevices() -if len(devices) == 0: +# LIST all connected devices (Cameras) in DEVICES (CDeviceInfo) and exit application if no device is found. +DEVICES = TLF.EnumerateDevices() +if len(DEVICES) == 0: raise pylon.RuntimeException("No camera present.") # Create an array of instant cameras for the found devices and avoid exceeding a maximum number of devices. -> Output-> Pylon::CInstantCameraArray -CAMERAS_ARRAY = pylon.InstantCameraArray(min(len(devices), MAX_CAMERAS_TO_USE)) -# print(f'cameras: {CAMERAS_ARRAY}') # for debug - -l = CAMERAS_ARRAY.GetSize() +# CAMERAS_ARRAY = pylon.InstantCameraArray(min(len(DEVICES), MAX_CAMERAS_TO_USE)) +# print(f'cameras: {CAMERAS_ARRAY}') # for debug +# l = CAMERAS_ARRAY.GetSize() # # Funktions: @@ -38,73 +37,118 @@ l = CAMERAS_ARRAY.GetSize() # -def listcams(): +def listcams(method_name): ''' - shows all connected / founded cameras. Number of cameras is defined by CAMERAS_ARRAY + Iterating through all cameras. By adding an argument you can read or write values to the camera. + --------------------------- + Following arguments usable: + --------------------------- + :gain: increase the brightness of the image output + :delay: allows to determine the period of time between the detection of the trigger signal and the actual start of the exposure + :exposure: specifies how long the image sensor is exposed to light during image acquisition. + :read: reads out all the parameters that can be used as arguments + :fps: allows you to determine the estimated frame rate with the current camera settings. + ''' - # Create and attach all Pylon Devices. - for i, camera_instantz in enumerate(CAMERAS_ARRAY): - # cam is an instance Swig object of type Pylon::CInstantCamera on cluster on HD - camera_instantz.Attach(tlFactory.CreateDevice(devices[i])) - print(f'camera_instantz: {type(camera_instantz)}') + # save the camera information into list and dictionary - needed?? + cam_instances = [] + cam_instances_dic = {} + + # itterates through all connected cameras and select them step by step after each other + # camera_device is only + + for i, camera_device in enumerate(DEVICES): + print('----------- Start ------------------') + # print model name and id of the camera + print(f'Camera Model: {camera_device.GetModelName()}') + print(f'Serial numer: {camera_device.GetSerialNumber()}', "\n") + + # the active camera will be an InstantCamera (CInstantCamera) based on a device to access the parameter + # created with the corresponding DeviceInfo + # this can be from a list of previously enumerated + cam = py.InstantCamera(TLF.CreateDevice(DEVICES[i])) + # print(f'CAM: {cam}') # - Debug + + # print(f'Nr: {i} - cam: {cam} ') # - Debug + + # open camera -> :cam: is the camera instance to access camera, :camera_device: the camera info like serial number, ... + opencam(cam, camera_device) + + # choose the method you want to make changes + if method_name == 'gain': + gaincam(cam, camera_device) + elif method_name == 'read': + readcam(cam, camera_device) + elif method_name == 'delay': + triggerdelaycam(cam, camera_device) + elif method_name == 'exposure': + exposurecam(cam, camera_device) + else: + print('wrong argumnets. Please choose: gain, read, delay, exposure, fps') + + # close cam + closecam(cam, camera_device) - # Print the serial number of the model of the camera. - by replacing .GetSerialNumber() with .GetModelName() outputs the Modelname - cam_ids = camera_instantz.GetDeviceInfo().GetSerialNumber() - print("Using device ", cam_ids) + # Listing of Instance and camera info is not required - maybe for later usage!! - return cam_ids, camera_instantz + # add cameras Instances to list + cam_instances.append(cam) + # add camera Instances and serial id to a list + cam_instances_dic[camera_device.GetSerialNumber()] = cam -# Open cameras: + # print(f'Cam_dict: {cam_instances_dic}') # - debug + print('------------- Stop ---------------') +# CAM = testcam() -def opencams(): + +def opencam(cam, camera_device): ''' open camera to access camera and read or write values ''' - CAM_INSTANT.Open() - print(f'Cam open: {CAM_ID}', '\n') + cam.Open() + print(f'Cam open: {camera_device.GetSerialNumber()}', '\n') # Close cameras: -def closecams(): +def closecam(cam, camera_device): ''' close camera after read or write values ''' - CAM_INSTANT.Close() - print(f'Cam close: {CAM_ID}', '\n') + cam.Close() + print(f'Cam closed: {camera_device.GetSerialNumber()}', '\n') # read camera parameters to get an current overiew -def readcams(): +def readcam(cam, camera_device): """ - read out current settings from camera via interface + reads out current settings from camera via interface """ - opencams() - # Read Gain value in Camera: - print("Gain in (dB):") - # Gain value - print(CAM_INSTANT.Gain.GetValue(), "\n") + # Read Gain value in Camera and round to two decimal points : + gain = cam.Gain.GetValue() + gain = round(gain, 2) + print( + f'Gain in (dB): {gain} from camera: {camera_device.GetSerialNumber()}') # Read Trigger Delay value in Camera: - print("Delay in (microseconds):") - # TriggerDelay value - print(CAM_INSTANT.TriggerDelay.GetValue(), "\n") + print( + f'trigger delay from camera (in Microseconds) {camera_device.GetSerialNumber()} is: {cam.TriggerDelay.GetValue()}') # Exposure parameter - oldexposure = CAM_INSTANT.ExposureTime.GetValue() - print(f'exposure: {oldexposure}', "\n") + print( + f'old exposure in microseconds: {cam.ExposureTime.GetValue()} from camera: {camera_device.GetSerialNumber()}') - oldfps = CAM_INSTANT.AcquisitionFrameRate.GetValue() - print(f'fps: {oldfps}') + # fps + oldfps = cam.AcquisitionFrameRate.GetValue() + print(f'fps: {oldfps} from camera: {camera_device.GetSerialNumber()}') # Gamma value: print("Camera gamma value: ") - print(CAM_INSTANT.Gamma.GetValue(), "\n") + print(cam.Gamma.GetValue(), "\n") - closecams() # readcam() @@ -113,22 +157,25 @@ def readcams(): # -def gaincam(): +def gaincam(cam, camera_device=None): """ changes the gain in (dB). Entering float numbers for. e.g: 5.0 """ - # Read Gain value in Camera: - print(" Gain in (dB):") - # Gain value - print(CAM_INSTANT.Gain.GetValue(), "\n") + print( + f'Gain in (dB): {cam.Gain.GetValue()} from camera: {camera_device.GetSerialNumber()}') + # Input new gain value gainvalue = float(input("Please enter the new gain in (dB): ")) - CAM_INSTANT.Gain.SetValue(gainvalue) + # Set new value to camera + cam.Gain.SetValue(gainvalue) + # Read Gain value in Camera: - print("New Gain in (dB):") - # Gain value - print(CAM_INSTANT.Gain.GetValue(), "\n") + new_gain = cam.Gain.GetValue() + new_gain = round(new_gain, 2) + print( + f'New Gain in (dB): {new_gain} from camera: {camera_device.GetSerialNumber()}') + # gaincam() @@ -137,22 +184,24 @@ def gaincam(): # -def exposurecam(): +def exposurecam(cam, camera_device=None): """ changes the exposure time in microseconds (ms) """ - old_exposure = CAM_INSTANT.ExposureTime.GetValue() - print(f'old exposure: {old_exposure}') - + # read current value + old_exposure = cam.ExposureTime.GetValue() + print( + f'old exposure in microseconds: {old_exposure} from camera: {camera_device.GetSerialNumber()}') + # ask user to add new value new_exposure = input("Please enter new exposure value (microseconds): ") new_exposure = float(new_exposure) - CAM_INSTANT.ExposureTime.SetValue(new_exposure) + cam.ExposureTime.SetValue(new_exposure) # Read new exosure value in Camera: - print("New exposure in (ms):") - # print new exposure value - print(CAM_INSTANT.ExposureTime.GetValue(), "\n") + print( + f'New exposure: {cam.ExposureTime.GetValue()} from camera: {camera_device.GetSerialNumber()}') + # exposurecam() @@ -161,30 +210,31 @@ def exposurecam(): # -def aquisationfps(): +def aquisationfps(cam, ): """ enable / dissable auto or manual fps and changes the fps (frame per seconds) """ - status = CAM_INSTANT.AcquisitionFrameRateEnable.GetValue() + # read current value + status = cam.AcquisitionFrameRateEnable.GetValue() print(f'fps enable? {status}') - + # ask user to change or not statusenable = str(input("Do you want to change fps, (y/n)? ")) if statusenable.lower() == 'y': - CAM_INSTANT.AcquisitionFrameRateEnable.SetValue(True) - oldfps = CAM_INSTANT.AcquisitionFrameRate.GetValue() + cam.AcquisitionFrameRateEnable.SetValue(True) + oldfps = cam.AcquisitionFrameRate.GetValue() print(f'Old fps: {oldfps}') newfps = input("Choose your fps: ") - CAM_INSTANT.AcquisitionFrameRate.SetValue(float(newfps)) + cam.AcquisitionFrameRate.SetValue(float(newfps)) print(f"New fps is: {newfps}", '\n') else: print("disabled Aquisation Frame rate!") - CAM_INSTANT.AcquisitionFrameRateEnable.SetValue(False) + cam.AcquisitionFrameRateEnable.SetValue(False) # aquisationfps() @@ -194,19 +244,21 @@ def aquisationfps(): # -def triggerdelaycam(): +def triggerdelaycam(cam, camera_device=None): """ changes the trigger delay in microseconds (ms) """ - print(f'trigger delay is: {CAM_INSTANT.TriggerDelay.GetValue()}') + print( + f'trigger delay from camera (in Microseconds) {camera_device.GetSerialNumber()} is: {cam.TriggerDelay.GetValue()}') newtriggerdelay = input( "Please enter new trigger delay in (microseconds): ") - CAM_INSTANT.TriggerDelay.SetValue(float(newtriggerdelay)) + cam.TriggerDelay.SetValue(float(newtriggerdelay)) - print(f'New trigger delay is: {CAM_INSTANT.TriggerDelay.GetValue()}') + print( + f'New trigger delay from camera {camera_device.GetSerialNumber()} is: {cam.TriggerDelay.GetValue()}') # triggerdelaycam() @@ -215,23 +267,23 @@ def triggerdelaycam(): # start canera in free run mode: -def freeruncam(): +def freeruncam(cam): ''' # grabbing images in free run ''' # camera start free run mode: VIDEO 37:32 - CAM_INSTANT.StartGrabbing(pylon.GrabStrategy_OneByOne) + cam.StartGrabbing(pylon.GrabStrategy_OneByOne) i = 0 print('Starting to acquire') t0 = time.time() - while CAM_INSTANT.IsGrabbing(): - grab = CAM_INSTANT.RetrieveResult( + while cam.IsGrabbing(): + grab = cam.RetrieveResult( 100, pylon.TimeoutHandling_ThrowException) if grab.GrabSucceeded(): i += 1 if i == 100: - CAM_INSTANT.StopGrabbing() + cam.StopGrabbing() break print(f'Acquired {i} frames in {time.time()-t0:.0f} seconds') @@ -239,8 +291,9 @@ def freeruncam(): # ---> ongoing, not yet in usage. -CAM_ID, CAM_INSTANT = listcams() +# CAM_ID, CAM_INSTANT = listcams() if __name__ == '__main__': - CAM_ID, CAM_INSTANT = listcams() - # readcams() + import sys + method_name_input = sys.argv[1] # 'read' # sys.argv[1] + listcams(method_name=method_name_input) -- GitLab