24.1   Introduction

The HDevEngine/Python interface is similar to the HDevEngine/.NET and HDevEngine/C++ interface. Most concepts and functions have a one-to-one mapping.

A short reference of the C++ classes for the HDevEngine can be found in section “Overview of the Classes”. The Python classes are similar.

This chapter only describes HDevEngine/Python specifics. If a topic e.g., error handling is not mentioned, the relevant part in Part “Programming With HALCON/Python” applies.

24.1.1   A First Example

This section explains how to create a simple HDevEngine/Python applicaton. For a more comprehensive description, read section “HDevEngine/Python Interface”.

  1. Install HALCON 21.05.
  2. Install Python 3.8 or newer on your system.
  3. Set up your Python enviroment of choice, e.g., using python -m venv`
  4. Run the following commands in a shell:

    
    mkdir hdevengine_example
    cd hdevengine_example
    pip install mvtec-halcon==21050
    

  5. Create a file named hdevengine_example.py and change the content to:

    import os
    
    import halcon as ha
    
    if __name__ == '__main__':
        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)
    
        img = ha.read_image('fin2')
    
        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}')
    

  6. To run the application, type the following command in the same shell:

    python hdevengine_example.py
    

As as result, you should see the following output 'Fin Area: 1634'.