While you can have multiple Python instances of HDevEngine/Python, they all share the same implementation as a mutable singleton.
This mostly affects configuration. Specific program and procedure call instances are more or less independent from each other.
Here HALCON is queried for the example directory path, based on which the path containing the 'detect_fin' procedure is constructed. Then an HDevEngine instance is initialized and the procedure search path set to it.
example_dir = ha.get_system_s('example_dir') procedure_path = os.path.join(example_dir, 'hdevengine', 'procedures') hdev_engine = ha.HDevEngine() hdev_engine.set_procedure_path(procedure_path)
def set_attribute(self, name: str, value: HTupleType) -> None: def get_attribute(self, name: str) -> HTupleType:
These functions let you set specific attributes. For example, by default, the attribute 'ignore_invalid_results' is set to true. This means that by default, an empty region object or an empty tuple is returned if a variable or parameter is queried that was not previously set by the program or procedure. Think of it like this: Should accessing an uninitialized variable return a default or should it raise an exception?
hdev_engine = ha.HDevEngine() assert hdev_engine.get_attribute('ignore_invalid_results') == 1 hdev_engine.set_attribute('ignore_invalid_results', 0) assert hdev_engine.get_attribute('ignore_invalid_results') == 0
For HDevEngine attributes, 0 maps to false, and 1 to true.
See section “Overview of the Classes” for a list of available attributes.
HDevEngine def start_debug_server(self) -> None: def stop_debug_server(self) -> None:
These functions let you start and stop the server required for debugging the engine execution. You can control the port via the attribute 'debug_port'.
HDevEngine def set_procedure_path(self, path: str) -> None: def add_procedure_path(self, path: str) -> None:
These functions let you set and extend the list of paths searched when trying to load a procedure.
HDevEngine def get_procedure_names(self) -> List[str]: def get_loaded_procedure_names(self) -> List[str]: def get_global_control_var_names(self) -> List[str]: def get_global_iconic_var_dimension(self, name: str) -> int: def get_global_control_var_dimension(self, name: str) -> int:
These functions let you query global metadata, such as which procedures are loaded, the vector dimensions of global variables, and more.
HDevEngine def set_global_iconic_var(self, name: str, value: HObject) -> None: def set_global_iconic_vector_var(self, name: str, value: IconicVectorType) -> None: def set_global_control_var(self, name: str, value: HTupleType) -> None: def set_global_tuple_vector_var(self, name: str, value: TupleVectorType) -> None: def get_global_iconic_var(self , name: str) -> HObject: def get_global_iconic_vector_var(self, name: str) -> TupleVectorType: def get_global_control_var(self, name: str) -> HTupleType: def get_global_tuple_vector_var(self, name: str) -> TupleVectorType:
These functions let you read and write global variables, both iconic and control.
HDevEngine def unload_procedure(self, name: str) -> None: def unload_all_procedures(self) -> None:
These functions let you unload procedures again, once they are loaded. For example, this can be useful to free up unused memory.
The general concept behind calling HDevelop procedures from Python is as follows:
procedure = ha.HDevProcedure.load_external('detect_fin') proc_call = ha.HDevProcedureCall(procedure) proc_call.set_input_iconic_param_by_name('Image', img) proc_call.execute() fin_area = proc_call.get_output_control_param_by_name('FinArea')[0] print(f'Fin Area: {fin_area}')
HDevProcedure @staticmethod def load_external(name: str) -> 'HDevProcedure': @staticmethod def load_local( program: Union[HDevProgram, str], name: str ) -> 'HDevProcedure':
Procedures are loaded via these static methods. Use only these functions to initialize HDevProcedure instances.
hdev_engine.set_procedure_path(proc_dir) external_proc = ha.HDevProcedure.load_external('detect_fin') program = ha.HDevProgram(os.path.join(proc_dir, 'program.hdev')) local_proc_program = ha.HDevProcedure.load_local(program, 'count_nuts') local_proc_name = ha.HDevProcedure.load_local('program.hdev', 'count_nuts')
While Python allows calling static methods on instances, e.g.: haHDevProcedure()load_external this is not recommended as it needlesly wastes resources.
Access procedure metadata via these read-only member variables:
HDevProcedure - name : str - short_description : str - loaded : bool - input_iconic_param_names : List[str] - output_iconic_param_names : List[str] - input_control_param_names : List[str] - output_control_param_names : List[str] - input_iconic_param_dimensions : List[int] - output_iconic_param_dimensions : List[int] - input_control_param_dimensions : List[int] - output_control_param_dimensions : List[int]
HDevProcedure def get_used_procedure_names(self) -> List[str]: def get_info(self, slot: str) -> HTupleType: def get_param_info(self, name: str, slot: str) -> HTupleType: def get_input_iconic_param_info(self, idx: int, slot: str) -> HTupleType: def get_output_iconic_param_info(self, idx: int, slot: str) -> HTupleType: def get_input_control_param_info(self, idx: int, slot: str) -> HTupleType: def get_output_control_param_info(self, idx: int, slot: str) -> HTupleType: def query_slots(self) -> List[str]: def query_param_slots(self) -> List[str]:
Use these functions to query procedure metadata.
HDevProcedure def compile_used_procedures(self) -> bool:
Compile all procedures that are used by the program and that can be compiled with a just-in-time compiler. Procedures that could not be compiled are called by the HDevEngine interpreter in the usual way. To check which procedure could not be compiled and what the reason is for that, start HDevelop and check the compilation states there.
This functions returns whether all used procedures were JIT compiled.
proc_call = ha.HDevProcedureCall(procedure)
The only way to initialize an HDevProcedureCall instance is using a loaded HDevProcedure instance.
HDevProcedureCall def set_input_control_param_by_index(self, idx: int, value: HTupleType) -> None: def set_input_tuple_vector_by_index(self, idx: int, value: TupleVectorType) -> None: def set_input_control_param_by_name(self, name: str, value: HTupleType) -> None: def set_input_tuple_vector_by_name(self, name: str, value: TupleVectorType) -> None: def set_input_iconic_param_by_index(self, idx: int, value: HObject) -> None: def set_input_iconic_vector_by_index(self, idx: int, value: IconicVectorType) -> None: def set_input_iconic_param_by_name(self, name: str, value: HObject) -> None: def set_input_iconic_vector_by_name(self, name: str, value: IconicVectorType) -> None:
These functions let you set input parameters. We recommend always setting all input parameters again every time you want to execute the procedure call. This helps make your code more robust in the face of error conditions.
Indices here start at 1 instead of 0.
HDevProcedureCall def execute(self) -> None:
Use this function when you have set all input parameters to execute the procedure.
HDevProcedureCall def get_output_control_param_by_index(self, idx: int) -> HTupleType: def get_output_tuple_vector_by_index(self, idx: int) -> TupleVectorType: def get_output_control_param_by_name(self, name: str) -> HTupleType: def get_output_tuple_vector_by_name(self, name: str) -> TupleVectorType: def get_output_iconic_param_by_index(self, idx: int) -> HObject: def get_output_iconic_vector_by_index(self, idx: int) -> IconicVectorType: def get_output_iconic_param_by_name(self, name: str) -> HObject: def get_output_iconic_vector_by_name(self, name: str) -> IconicVectorType:
Use these functions to read output parameters after the call to execute has finished successfully.
Indices here start at 1 instead of 0.
HDevProcedureCall def wait_for_debug_connection(self) -> None:
Use this function in conjunction with start_debug_server to debug procedure execution.
HDevProcedureCall def reset(self) -> None:
This is mainly for situations when you want to abort execution from another thread or possibly free native resources even while some instances are still alive.
The general concept behind calling HDevelop programs from Python is as follows:
example_dir = ha.get_system_s('example_dir') program_path = os.path.join( example_dir, 'hdevelop', 'Transformations', 'Poses' ) program = ha.HDevProgram(os.path.join(program_path, 'pose_compose.hdev')) program_call = ha.HDevProgramCall(program) program_call.execute() pose = program_call.get_control_var_by_name('PoseComposeAlternative') rounded_pose = [round(x, 8) for x in pose] assert rounded_pose == [0.3, -0.0498838, 0.33986422, 77.0, 90.0, 0.0, 0]
program = ha.HDevProgram(os.path.join(program_path, 'pose_compose.hdev'))
Programs are loaded via the initializer of HDevProgram, which expects a full path to an HDevelop program in the format of the operating system, including file name.
Access program metadata via these read-only member variables:
HDevProgram - name : str - loaded : bool - inconic_var_names : List[str] - control_var_names : List[str] - inconic_var_dimensions : List[int] - control_var_dimensions : List[int]
HDevProgram def get_used_procedure_names(self) -> List[str]: def get_local_procedure_names(self) -> List[str]:
Use these functions to query program metadata.
HDevProgram def compile_used_procedures(self) -> bool:
Compile all procedures that are used by the program and that can be compiled with a just-in-time compiler. Procedures that could not be compiled are called by the HDevEngine interpreter in the usual way. To check which procedure could not be compiled and what the reason is for that, start HDevelop and check the compilation states there.
Returns whether all used procedures were JIT compiled.
program_call = ha.HDevProgramCall(program)
The only way to initialize an HDevProgramCall instance is using a valid HDevProgram instance.
HDevProgramCall def execute(self) -> None:
Use this function to execute the program.
HDevProgramCall def get_control_var_by_index(self, idx: int) -> HTupleType: def get_tuple_vector_var_by_index(self, idx: int) -> TupleVectorType: def get_control_var_by_name(self, name: str) -> HTupleType: def get_tuple_vector_var_by_name(self, name: str) -> TupleVectorType: def get_iconic_var_by_index(self, idx: int) -> HObject: def get_iconic_vector_var_by_index(self, idx: int) -> IconicVectorType: def get_iconic_var_by_name(self, name: str) -> HObject: def get_iconic_vector_var_by_name(self, name: str) -> IconicVectorType:
Use these functions to read variables after the call to execute has finished successfully.
Indices here start at 1 instead of 0.
HDevProgramCall def wait_for_debug_connection(self) -> None:
Use this function in conjunction with start_debug_server to debug program execution.
HDevProgramCall def reset(self) -> None:
This is mainly for situations when you want to abort execution from another thread or possibly free native resources even while some instances are still alive.
Inside HDevelop, dev_* operators can be used for convenience. When embedding an HDevelop program or procedure inside your application, the potential dev_* operator calls have no straightforward mapping. It might be desirable to for example map dev_* operator calls to visualization within your application. Thus, HDevEngine/Python provides a base class, which you can inherit from and overwrite with logic appropriate for your application.
Take for example this simple HDevelop program, which reads an image and then displays this PCB image in a dev window.
read_image(Image, 'pcb') dev_display(Image)
By default, this program will not open a window when using HDevEngine/Python. However, you can specify your own logic of what should happen when dev_display is called.
For example, this Python code registers a very basic implementation of dev_display.
class DevImpl(ha.HDevOperatorBase): @staticmethod def dev_display(object): print(object) hdev_engine = ha.HDevEngine() hdev_engine.set_hdev_operator_impl(DevImpl())
To get an overview which dev_* operators are available and which signatures each of them expects, take a look at the implementation of HDevOperatorBase.
Note that only one implementation can be registered at a time. This applies to all current and future HDevEngine instances, until changed.
By default no implementation is registered. Once you have registered an implementation you can return to the default behavior by calling:
hdev_engine.unset_hdev_operator_impl()
Note that calling register_dev_operators might have surprising lifetime effects on your Python variables. See the documentation of said function for more details.
Every function that was not overwritten by your implementation of HDevOperatorBase will raise an exception if called.
It is safe to raise exceptions from within Python code implementing dev_* operators. However, only a generic HDevEngineError will be raised and the original exception traceback will be logged to stderr. This is due to technical limitations.
HDevEngine/Python maps HALCON vectors to nested Python lists. There are two separate, incompatible types of vectors: tuple_vector and iconic_vector. The following examples demonstrate the Python representation that is used to read and write HALCON vectors in HDevEngine/Python:
empty_tuple_vector = ha.HDevEmptyVector(dimension=1) 1d_tuple_vector = [[23, 'a'], ['ec', 2.5, 77]] 2d_tuple_vector = [[[8], ['b', 'c']], [], [[]]] empty_iconic_vector = ha.HDevEmptyVector(dimension=1) 1d_iconic_vector = [img1, img2, img3] 2d_iconic_vector = [[img1, img2], [img1, img3, img4], []]
For tuple_vector, the inner most list is treated as HALCON tuple.
All elements of a vector must have the same dimension:
[ [234], [2, 5] ] # ok [ 234, [2, 5] ] # not ok
Because HDevEngine is a mutable singleton, some functionality affects your entire application:
A multi-threaded application must take care when working with this sort of shared mutable state. For example, you cannot safely load multiple procedures in parallel using the global procedure path if they require different path settings, without synchronizing appropriately inside your application. Not applying the required care will lead to undefined behavior, including crashes and worse.
To avoid the aforementioned issues, we recommend configuring HDevEngine, as well as loading procedures and programs, at the beginning of your program before starting additional application threads.
In contrast to HDevEngine, HDevProcedure, and HDevProgram, the call instances HDevProcedureCall and HDevProgramCall are independent from each other.