1from __future__
import annotations
7from ctypes
import c_double
8from typing
import Callable, Type, TypeVar, Sequence, Any, MutableSequence, Union
10from ids_peak_common
import Metadata, MetadataKey, Rectangle, Point, Size
12 peak_icv_capture_information)
14 NotPossibleException, OutOfRangeException, MathErrorException,
15 TargetNotFoundException, CorruptedException, IOException, InternalErrorException,
16 ICVException, NullPointerException)
17from numpy.typing
import NDArray
21 array_class: Type[ctypes.Array] = c_type * size
28 if metadata.has_entry_by_key(MetadataKey.DEVICE_TIMESTAMP):
29 c_capture_information.relative_timestamp = metadata.get_value_by_key(MetadataKey.DEVICE_TIMESTAMP)
31 c_capture_information.relative_timestamp = 0
32 if metadata.has_entry_by_key(MetadataKey.BINNING_HORIZONTAL):
33 c_capture_information.binning_factor.x = metadata.get_value_by_key(MetadataKey.BINNING_HORIZONTAL)
35 c_capture_information.binning_factor.x = 1
36 if metadata.has_entry_by_key(MetadataKey.BINNING_VERTICAL):
37 c_capture_information.binning_factor.y = metadata.get_value_by_key(MetadataKey.BINNING_VERTICAL)
39 c_capture_information.binning_factor.y = 1
40 if metadata.has_entry_by_key(MetadataKey.ROI):
41 roi = metadata.get_value_by_key(MetadataKey.ROI)
42 c_capture_information.region_of_interest.x = ctypes.c_uint32(roi.x)
43 c_capture_information.region_of_interest.y = ctypes.c_uint32(roi.y)
44 c_capture_information.region_of_interest.width = ctypes.c_uint32(roi.width)
45 c_capture_information.region_of_interest.height = ctypes.c_uint32(roi.height)
47 c_capture_information.region_of_interest.x = 0
48 c_capture_information.region_of_interest.y = 0
51 c_capture_information.region_of_interest.width = size.width
52 c_capture_information.region_of_interest.height = size.height
54 c_capture_information.region_of_interest.width = 0
55 c_capture_information.region_of_interest.height = 0
56 if metadata.has_entry_by_key(MetadataKey.DEVICE_EXPOSURE_TIME):
57 c_capture_information.exposure_time = metadata.get_value_by_key(MetadataKey.DEVICE_EXPOSURE_TIME)
59 c_capture_information.exposure_time = 0
60 if metadata.has_entry_by_key(MetadataKey.DEVICE_EXPOSURE_TIME_SEQUENCE):
61 exposure_time_sequence = metadata.get_value_by_key(MetadataKey.DEVICE_EXPOSURE_TIME_SEQUENCE)
63 if len(exposure_time_sequence) > 4:
65 exposure_time_sequence_padded = list(exposure_time_sequence) + [0.0] * (4 - len(exposure_time_sequence))
67 c_array = c_double * 4
68 c_capture_information.exposure_time_sequence = c_array(*exposure_time_sequence_padded)
71 c_capture_information.exposure_time_sequence = (0, 0, 0, 0)
73 if metadata.has_entry_by_key(MetadataKey.DEVICE_GAIN):
74 c_capture_information.gain = metadata.get_value_by_key(MetadataKey.DEVICE_GAIN)
76 c_capture_information.gain = 0
77 if metadata.has_entry_by_key(MetadataKey.DEVICE_GAIN_SEQUENCE):
78 gain_sequence = metadata.get_value_by_key(MetadataKey.DEVICE_GAIN_SEQUENCE)
80 if len(gain_sequence) > 4:
82 gain_sequence_padded = list(gain_sequence) + [0.0] * (4 - len(gain_sequence))
84 c_array = c_double * 4
85 c_capture_information.gain_sequence = c_array(*gain_sequence_padded)
88 c_capture_information.gain_sequence = (0, 0, 0, 0)
90 return c_capture_information
94 return add_metadata_from_capture_information_to_metadata(Metadata(), capture_information)
98 capture_information: peak_icv_capture_information) -> Metadata:
99 metadata.set_value_by_key(MetadataKey.DEVICE_TIMESTAMP, capture_information.relative_timestamp)
100 metadata.set_value_by_key(MetadataKey.BINNING_HORIZONTAL, capture_information.binning_factor.x)
101 metadata.set_value_by_key(MetadataKey.BINNING_VERTICAL, capture_information.binning_factor.y)
102 metadata.set_value_by_key(MetadataKey.ROI, Rectangle(Point(capture_information.region_of_interest.x,
103 capture_information.region_of_interest.y),
104 Size(capture_information.region_of_interest.width,
105 capture_information.region_of_interest.height)))
106 if not capture_information.exposure_time == 0:
107 metadata.set_value_by_key(MetadataKey.DEVICE_EXPOSURE_TIME, capture_information.exposure_time)
109 exposure_time_sequence = []
110 for v
in list(capture_information.exposure_time_sequence):
113 exposure_time_sequence.append(v)
114 if exposure_time_sequence:
115 metadata.set_value_by_key(MetadataKey.DEVICE_EXPOSURE_TIME_SEQUENCE,
116 list(capture_information.exposure_time_sequence))
118 if not capture_information.gain == 0:
119 metadata.set_value_by_key(MetadataKey.DEVICE_GAIN, capture_information.gain)
122 for v
in list(capture_information.gain_sequence):
125 gain_sequence.append(v)
127 metadata.set_value_by_key(MetadataKey.DEVICE_GAIN_SEQUENCE,
128 list(capture_information.gain_sequence))
134 return ctypes.c_void_p(int(image_handle))
138 if point_type == peak_icv_point_f:
142 "Point type not supported.")
146 if point_type == peak_icv_point_f:
147 return peak_icv_point_type.PEAK_ICV_POINT_TYPE_XY
150 "Point type not supported.")
154 if point_enum == peak_icv_point_type.PEAK_ICV_POINT_TYPE_XY:
155 return peak_icv_point_f
158 "point_enum not supported " + str(point_enum.value))
161T = TypeVar(
'T', bound=Any)
162InputType = Union[Sequence[T], NDArray[T], ctypes.Array[Any]]
163OutputType = Union[MutableSequence[T], NDArray[T], ctypes.Array[Any]]
166def transform(input_: InputType, output: OutputType, func: Callable) ->
None:
167 for i
in range(len(input_)):
168 output[i] = func(input_[i])
172 def execute_and_map_return_codes_light(func_light: Callable, *args_light: Any, **kwargs_light: Any) ->
None:
173 ec = func_light(*args_light, **kwargs_light)
176 if ec != peak_icv_status.PEAK_ICV_STATUS_SUCCESS:
177 raise ICVException(
"Could not query the last error!", ec)
179 error_code = func(*args, **kwargs)
187 if error_code != peak_icv_status.PEAK_ICV_STATUS_SUCCESS:
189 c_count = ctypes.c_size_t()
190 execute_and_map_return_codes_light(
191 Loader().dll().peak_icv_GetLastErrorMessage_GetCount,
192 ctypes.pointer(c_count))
194 if c_count.value == 0:
197 c_array = create_array(ctypes.c_char, c_count.value)
198 execute_and_map_return_codes_light(
Loader().dll().peak_icv_GetLastErrorMessage,
200 exception_msg = c_array.value
203 peak_icv_status.PEAK_ICV_STATUS_LIBRARY_NOT_INITIALIZED,
204 peak_icv_status.PEAK_ICV_STATUS_DYNAMIC_DEPENDENCY_MISSING,
208 elif error_code == peak_icv_status.PEAK_ICV_STATUS_NULL_POINTER:
211 elif error_code == peak_icv_status.PEAK_ICV_STATUS_MISMATCH:
214 elif error_code == peak_icv_status.PEAK_ICV_STATUS_NOT_SUPPORTED:
217 elif error_code == peak_icv_status.PEAK_ICV_STATUS_NOT_POSSIBLE:
220 elif error_code == peak_icv_status.PEAK_ICV_STATUS_OUT_OF_RANGE:
223 elif error_code == peak_icv_status.PEAK_ICV_STATUS_MATH_ERROR:
226 elif error_code == peak_icv_status.PEAK_ICV_STATUS_TARGET_NOT_FOUND:
229 elif error_code == peak_icv_status.PEAK_ICV_STATUS_CORRUPTED:
232 elif error_code == peak_icv_status.PEAK_ICV_STATUS_IO_ERROR:
236 peak_icv_status.PEAK_ICV_STATUS_INTERNAL_ERROR,
237 peak_icv_status.PEAK_ICV_STATUS_INVALID_BUFFER_SIZE,
238 peak_icv_status.PEAK_ICV_STATUS_INVALID_HANDLE,
246 for path
in site.getsitepackages():
247 package_path = os.path.join(path,
"ids_peak_ipl")
248 if os.path.isdir(package_path):
251 peak_icv_status.PEAK_ICV_STATUS_DYNAMIC_DEPENDENCY_MISSING)
256 _dll: ctypes.CDLL |
None =
None
261 file_dir = os.path.dirname(os.path.realpath(__file__))
263 if sys.platform.startswith(
'win'):
264 lib_name =
"ids_peak_icv.dll"
265 dll_path = os.path.join(file_dir,
"..")
266 os.add_dll_directory(dll_path)
268 os.add_dll_directory(ids_peak_ipl_dll_path)
270 lib_name =
"libids_peak_icv.so"
272 lib_path = os.path.abspath(os.path.join(file_dir,
"..", lib_name))
274 cls.
_instance._dll = ctypes.cdll.LoadLibrary(lib_path)
276 execute_and_map_return_codes(cls.
_instance._dll.peak_icv_Init)
278 atexit.register(Loader._library_exit)
282 def dll(self) -> ctypes.CDLL:
283 if self.
_dll is None:
291 execute_and_map_return_codes(
Loader().
dll().peak_icv_Exit)
294ReturnType = TypeVar(
'ReturnType', bound=ctypes.c_void_p)
298 *args: Any, **kwargs: Any) -> ReturnType:
300 handle = kwargs[
"_handle"]
301 if type(handle)
is not return_type:
307 f
"Cannot initialize {class_name} with __init__. Please use a classmethod.")
Occurs when reading from a file or stream that is incomplete, damaged, or in an unexpected format.
Base class for all exceptions thrown by the library.
Occurs when a file operation fails (e.g., missing file, insufficient permissions, or inaccessible dir...
Occurs when a failure happens during processing due to an unhandled internal routine condition.
Occurs when a required dependency is missing, the library is not initialized, or a misconfiguration a...
Occurs when a mathematical computation fails (e.g., division by zero or invalid matrix operations).
Occurs when input parameters do not match expected conditions (e.g., two input images have different ...
Occurs when an operation cannot be performed due to logical constraints (e.g., camera calibration wit...
Occurs when a requested operation is not implemented or supported (e.g., exporting to an unsupported ...
Occurs when attempting to access an object or value via an uninitialized or null pointer.
Occurs when a provided value falls outside the allowed valid range.
Occurs when an expected target object (e.g., calibration plate or marker) is not detected in the inpu...
ctypes.Array create_array(Any c_type, int size)
str find_ids_peak_ipl_dll()
Metadata metadata_from_capture_information(peak_icv_capture_information capture_information)
Type map_enum_to_point_type(peak_icv_point_type point_enum)
ctypes.c_void_p image_handle_to_ctypes(int image_handle)
Metadata add_metadata_from_capture_information_to_metadata(Metadata metadata, peak_icv_capture_information capture_information)
ReturnType check_init_for_classes_with_classmethods_only(str class_name, Type[ReturnType] return_type, *Any args, **Any kwargs)
Type map_point_type_to_native_type(Type point_type)
None transform(InputType input_, OutputType output, Callable func)
None execute_and_map_return_codes(Callable func, *Any args, **Any kwargs)
peak_icv_point_type map_point_type_to_enum(Type point_type)
peak_icv_capture_information metadata_to_capture_information(Metadata metadata, Size|None size=None)