Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • mhoeschle/baslerpypylontools
1 result
Show changes
Commits on Source (10)
......@@ -7,6 +7,7 @@ import sys
import time
import os
#
# Variable:
#
......@@ -75,9 +76,12 @@ def listcams(method_name):
# open camera -> :cam: is the camera instance to access camera, :camera_device: the camera info like serial number, ...
opencam(cam, camera_device)
# Load toml as a dictionary
# config = toml.load ...
# choose the method you want to make changes
if method_name == 'gain':
gaincam(cam, camera_device)
gaincam(cam, camera_device, config['gainvalue'])
elif method_name == 'read':
readcam(cam, camera_device)
elif method_name == 'delay':
......@@ -157,7 +161,7 @@ def readcam(cam, camera_device):
#
def gaincam(cam, camera_device=None):
def gaincam(cam, camera_device=None, gainvalue=None):
"""
changes the gain in (dB). Entering float numbers for. e.g: 5.0
"""
......@@ -166,7 +170,7 @@ def gaincam(cam, camera_device=None):
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): "))
# gainvalue = float(input("Please enter the new gain in (dB): "))
# Set new value to camera
cam.Gain.SetValue(gainvalue)
......
import pypylon.pylon as py
from pypylon import pylon
import numpy as np
import matplotlib.pyplot as plt
from pypylon import genicam
import sys
import time
import os
import tomli as tomllib
import toml
import camera_Initialization as ci
import toml_handling as th
import camera_controler as cam_control
# connect camera and store connected devices in DEVICES
DEVICES, TLF = ci.get_instance_tlf(pylon)
# READ the the serial and swig from the connected cameras and write it to lists:
cam_serial_swig_adress = ci.find_camera_serial_swig(DEVICES, TLF)
# -------- for toml file -------------
# If you run via cmd choose the correct file, if you run with visual code - takes default toml file
# only running without a specific toml file it will use the default one, adding a toml file after the script name it will choose the entered one!
config_filename = th.checktoml(sys.argv)
# print(f'Config_filename: {config_filename}') # for debugging
cam_data_update = th.access_toml_file(cam_serial_swig_adress)
print()
print("----------cameras found and access parameters saved, read from updated toml file, choose the correct cameras. --------------", '\n')
# get the camera id's from the camera you want to make the changes from the camera_parameters_config.toml file:
for camera_serial_toml in cam_data_update.get('camera_ids').values():
# print(camera_serials) # for debuging
# from the camera_parameters_config.toml take the camera id's you want to change and take the swig of the camera from
for cam_swig, cam_serial in cam_serial_swig_adress.items():
if int(camera_serial_toml) == int(cam_serial):
print(
f'YES, camera found: {camera_serial_toml} = {cam_serial} + {cam_swig}')
print(type(cam_swig))
# Add camera controler here to exectue on the camera!
# open camera
cam_control.opencam(cam_swig, cam_serial)
# read Gain, Trigger delay, exposure, fps and gamma from camera
cam_control.readcam(cam_swig, cam_serial)
# close camera
cam_control.closecam(cam_swig, cam_serial)
else:
print(f'{camera_serial_toml}: NO, not found')
# readcam(swig, camera_serials)
# print('------------- Stop ---------------')
# if __name__ == '__main__':
# :'read': run on cmd # :sys.argv[1]: to run in visual code
# check_cmd_input_file(argv_1, default_config_filename)
import pypylon.pylon as py
from pypylon import pylon
import numpy as np
import matplotlib.pyplot as plt
from pypylon import genicam
###
#
# All functions to read and write the toml files
# All functions are executed in basler_camera_access.py file via import
#
###
def get_instance_tlf(pylon):
'''
get the TransportLayerFactory in TLF instances. List all connected devices (Cameras) in DEVICES
'''
# get instance of the pylon TransportLayerFactory
TLF = pylon.TlFactory.GetInstance()
# 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.")
# return TransportLayerFactory
return DEVICES, TLF
#
# connect camera and store connected devices in DEVICES
# DEVICES, TLF = get_instance_tlf(pylon)
# READ the the serial and swig from the connected cameras and write it to lists:
def find_camera_serial_swig(DEVICES, TLF):
'''
Find the the serial and the SWIG adress of each connected cameras and write it into a list and a dict
'''
# save the camera information into list and dictionary - needed??
cam_instances = [] # swig - Pylon::CInstantCamera at address 0x0.....
cam_serials = []
cam_serial_swig_adress = {}
# iterate and find all connected cameras:
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_SWIG: {cam}') # - Debug
print(type(cam), '\n') # - Debug
# read out all camera information
pylon.FeaturePersistence.Save("test.txt", cam.GetNodeMap())
# print(f'Nr: {i} - cam: {cam} ') # - Debug
# add cameras Instances as swig to empty list
cam_instances.append(cam)
# add camera Instances and serial id to empty list
cam_serials.append(camera_device.GetSerialNumber())
# create dict with swig and camera serial id
cam_serial_swig_adress[cam] = camera_device.GetSerialNumber()
# return dict with camera serials and their swig adress
return cam_serial_swig_adress
# cam_serial_swig_adress = find_camera_serial_swig(DEVICES, TLF)
###
#
# All functions to read and write the basler camera access file
# All functions are executed in basler_camera_access.py file via import
#
###
# open cam
def opencam(cam_swig, cam_serial):
'''
open camera to access camera and read or write values
'''
cam_swig.Open()
print(f'Cam open: {cam_serial}', '\n')
# opencam(cam_swig, cam_serial)
# close cam
def closecam(cam_swig, cam_serial):
'''
close camera after read or write values
'''
cam_swig.Close()
print(f'Cam closed: {cam_serial}', '\n')
# closecam(cam_swig, cam_serial)
def readcam(cam_swig, cam_serial):
"""
reads out current settings from camera via interface
"""
# Read Gain value in Camera and round to two decimal points :
gain = cam_swig.Gain.GetValue()
gain = round(gain, 2)
print(
f'Gain in (dB): {gain} from camera: {cam_serial}')
# Read Trigger Delay value in Camera:
print(
f'trigger delay from camera (in Microseconds) {cam_serial} is: {cam_swig.TriggerDelay.GetValue()}')
# Exposure parameter
print(
f'old exposure in microseconds: {cam_swig.ExposureTime.GetValue()} from camera: {cam_serial}')
# fps
oldfps = cam_swig.AcquisitionFrameRate.GetValue()
print(f'fps: {oldfps} from camera: {cam_serial}')
# Gamma value:
print("Camera gamma value: ")
print(cam_swig.Gamma.GetValue(), "\n")
# readcam(cam_swig, cam_serial)
camera_model = "acA2000-165uc"
[camera_ids]
camera_id_1 = 23093107
camera_id_2 = 22320672
camera_id_3 = 2234
[connected_cameras]
23093107 = "<pypylon.pylon.InstantCamera; proxy of <Swig Object of type 'Pylon::CInstantCamera *' at 0x000002A58FD89020> >"
[camera_swig]
[camera_parameters]
delaycam = 1000
exposurecam = 1000
gaincam = 1.0
[connected_cameras]
22320672 = "<pypylon.pylon.InstantCamera; proxy of <Swig Object of type 'Pylon::CInstantCamera *' at 0x000001B6EDEF8F60> >"
23093107 = "<pypylon.pylon.InstantCamera; proxy of <Swig Object of type 'Pylon::CInstantCamera *' at 0x000001B6EDEF9470> >"
......@@ -18,7 +18,13 @@ pyspnego==0.9.1
python-dateutil==2.8.2
six==1.16.0
smbprotocol==1.10.1
toml==0.10.2
tomli==2.0.1
# check packages installed ni virtualenv
# pip freeze --local
\ No newline at end of file
# check packages installed in virtualenv
# pip freeze --local
# py -m pip freeze
# install requierements:
# py -m pip install -r requirements.txt
\ No newline at end of file
# {05D8C294-F295-4dfb-9D01-096BD04049F4}
# GenApi persistence file (version 3.1.0)
# Device = Basler::UsbCameraParams -- Basler USB3Vision camera interface -- Device version = 1.0.0 -- Product GUID = 5a809e15-f447-480f-a21f-0fb7256b8400 -- Product version GUID = BD8DAF13-291A-4A6B-A82A-EAC119712405
TimerSelector Timer1
TimerTriggerSource ExposureStart
TimerSelector Timer1
import sys
import tomli as tomllib
import toml
###
#
# All functions to read and write the toml files
# All functions are executed in basler_camera_access.py file via import
#
###
# -------- for toml file -------------
# If you run via cmd choose the correct file, if you run with visual code - takes default toml file
# only running without a specific toml file it will use the default one, adding a toml file after the script name it will choose the entered one!
def checktoml(cmd_input):
'''
Running script via command line, gives you the possability to choose different configfiles with different parameters
Sys.argv[0] = scipt name, sys.argv[1] = config file name
Example how to run with Command line:. python scriptname.py configfilename.toml
'''
default_file = (r'config_files\camera_parameters_config.toml')
# chosen_file = tomlfilename[1]
if len(cmd_input) < 1:
# print(f'tomlfilename: {tomlfilename}') # for debugging
# exit(1)
return default_file
elif len(cmd_input) > 1:
tomlfile = cmd_input[1]
# print(f'tomlfile: {tomlfile}') # for debugging
return tomlfile
else:
# print(f'default_File: {default_file}') # for debugging
return default_file
config_filename = checktoml(sys.argv)
# print(f'Config_filename: {config_filename}') # for debugging
#
# open toml file:
#
def access_toml_file(cam_serial_swig_adress):
'''
open toml file for read and write the config parameter from or to the camera
'''
#
# open the toml file
#
with open(config_filename, "rb") as f:
data = tomllib.load(f)
#
# read toml files:
#
for parameter in data.items():
print(f'Parameter_Initialisation: {parameter}') # for debug
#
# delete connected cameras to refresh each time new:
#
data['connected_cameras'] = {}
#
# Modify values in the toml config under connected_cameras with explored cameras infos from the script:
#
for cam_instance, serial in cam_serial_swig_adress.items():
data['connected_cameras'][serial] = cam_instance
# for parameter in data.items():
# print(f'Parameter_after_updating: {parameter}')
#
# Write to modified config:
#
with open(config_filename, "w") as f:
toml.dump(data, f)
#
# open toml file to use updated camera input and SWIG:
#
with open(config_filename, "rb") as f:
cam_data_update = tomllib.load(f)
for new_parameter in cam_data_update.items():
print(f'Updated_Parameter: {new_parameter}') # for debug
return cam_data_update
# access_toml_file(cam_serial_swig_adress)
# debug:
# test = cam_data_update.get('camera_ids').get('camera_id_2')
# print(f'Camera_ID: {test}')
# test2 = list(cam_data_update.get('connected_cameras').keys())[0]
# print(f'Camera_ID_Updated: {test2}')