Prepare a HALCON Procedure

The Script Runner runs HALCON procedures. It reads a procedure’s declared interface and builds one node per parameter, so the interface must say what every parameter holds. This guide shows how to get there in HDevelop.

1. Make it a procedure

The Script Runner can pass inputs only to a procedure and read only a procedure’s outputs. If your vision logic is an HDevelop program (.hdev) with a main:

  1. In HDevelop select the lines of your logic and choose Create New Procedure.

  2. Declare the interface: which iconic objects and control values go in and come out.

  3. Save the procedure as an external procedure (<name>.hdvp). The file name is the procedure name.

Rules for the interface:

  • Input or output is what the interface declares. The Script Runner never guesses it from the body.

  • Every parameter name must be unique within the procedure. A procedure cannot use one name for an input and an output; to carry a value from one run to the next, see A value carried from run to run.

  • Parameter names become node names in kebab case: MinScore becomes min-score, ROIRow becomes roi-row. Two parameters must not give the same node name.

  • The output names status-code, status-text and package-identifier are reserved: no output parameter may give one of them.

2. Describe every input and output

To run a procedure, the Script Runner has to know each of its arguments. It needs two kinds of information:

  • The shape: which parameters are inputs and which are outputs, which are images (iconic) and which are values (control), their names and their order. The Script Runner reads the shape from the procedure itself; the interface you declared in step 1 is enough.

  • The types: what each parameter holds. For a value parameter that is integer, real, string or handle, one value or several. For an image parameter it is the pixel types and the channels. HALCON does not store this in the declared interface: a control parameter is a tuple that can hold anything. Without the type, the Script Runner cannot build a node of the right type, check a value the PLC writes, or check an output after a run.

You give the types in one of two ways:

Approach Use it when Where the types are kept

1. Parameter documentation

You can edit the procedure in HDevelop. This is the recommended way.

Inside the .hdvp file. Nothing else has to be copied with it.

2. Interface file

The procedure is protected, you do not want to change it, or its documentation is incomplete.

In <procedure>.interface.json next to the procedure. Where the file and the documentation disagree, the file wins.

Both can be mixed: the file only has to give the types the documentation lacks. A parameter that has no type from either refuses the module (-10002); the status text names the parameter.

Approach 1: Export the procedure with parameter documentation

HDevelop documents a procedure’s parameters the same way as HALCON’s own operators. The fields are described in the HDevelop User’s Guide of HALCON 22.11, section 6.17.8.2 "Parameter Documentation" (in the HALCON help, or doc folder of the HALCON installation).

  1. Open the procedure in HDevelop and choose Procedures > Edit Procedure Interface (Ctrl+Shift+P, I).

  2. Open the documentation part of the dialog. It has a tab card for each parameter.

  3. For each control parameter (value), fill in:

    • Semantics, for example integer, real, string, number, angle.rad or filename.read, or a handle type such as shape_model;

    • Type List: integer, real, string or handle;

    • Multi Value: false for one value, true or optional for several;

    • Mixed Types: leave it at false. A parameter that mixes strings and numbers is refused.

  4. For each iconic parameter (image), fill in:

    • Semantics image;

    • Pixel Types: the pixel types the procedure accepts (byte, uint2, …​);

    • Multi Channel: false for one channel, true for colour.

  5. Optionally fill in Default Value, Value Min, Value Max, Values and Description of each input. The default is the node’s first value, the description goes into the node’s description, and the Script Runner refuses an input outside Value Min and Value Max before the procedure runs.

  6. Save the procedure as an external procedure (.hdvp).

HDevelop saves the documentation in the <docu> part of the .hdvp file, one slot per field:

Dialog field Slot What the Script Runner does with it

Semantics

sem_type

Gives the type when type_list is empty (see below); image, region or xld for an iconic parameter; angle.rad and angle.deg give the unit.

Type List

type_list

Gives the type of a control parameter; the accepted pixel types of an image.

Multi Value

multivalue

One value gives a scalar node, several values an array node.

Mixed Types

mixed_type

true or optional refuses the parameter.

Multi Channel

multichannel

One channel or colour.

Default Value

default_value

The input node’s first value.

Value Min, Value Max

valuemin, valuemax

The range an input is checked against.

Values

value_list

The suggested values.

Description

description

The node’s description.

Which slot decides the type of a control parameter:

  • type_list decides when it is filled in.

  • Only when type_list is empty does the Script Runner read the type from sem_type, and only from these labels: integer; real, number, angle., point.; string, filename*; the handle labels handle, dict, window, message, message_queue, serial, socket and any *_model. Any other sem_type gives no type.

  • One exception: a type_list of integer alone beside a handle label is read as a handle, with a warning in the log. That is how documentation from before HALCON 18, when handles were integers, describes a model; some of MVTec’s own procedures still do. See Types from sem_type.

So a procedure needs no interface file when every control parameter has a type_list, or one of the sem_type labels above, and every image parameter has the sem_type image (or pixel types in its type_list).

This is one parameter of the example convert_length.hdvp, which needs no interface file:

<parameter id="Length">
<default_type>real</default_type>
<default_value>25.4</default_value>
<description lang="en_US">The length to convert.</description>
<mixed_type>false</mixed_type>
<multivalue>false</multivalue>
<sem_type>real</sem_type>
<type_list>
<item>real</item>
</type_list>
<valuemin>0</valuemin>
</parameter>

A procedure whose parameters were never documented has only <parameter id="Length"/> there. No type can be read from it, and its module is refused until approach 2 gives the types.

Approach 2: Configure the interface by hand in an interface file

An interface file is a JSON file named <procedure>.interface.json in the script folder, next to the procedure. It lists every parameter of the procedure with its type. You do not have to type it from scratch: the Script Runner shows the procedure’s shape in the format of an interface file, and you add the types. The Script Runner can also build the file for you over the Data Layer, see Building one over the Data Layer.

  1. Copy the procedure into the script folder, add a module for it to the application file and restart the app, see Add a Module. The module is refused (-10002 or -10005), but its script/ nodes are there.

  2. Read v1/<module>/script/interface. It lists every parameter with group (input_iconic, output_iconic, input_ctrl, output_ctrl), name, position and dimension; each parameter without a type is marked "typeSource": "missing".

  3. Save that JSON as <procedure>.interface.json next to the procedure. Delete every typeSource and the top-level interfaceHash, and add the type of each parameter:

    • a control parameter gets elementTypes (["integer"], ["real"], ["string"] or ["handle"]) and, for several values, "multivalue": "true";

    • an iconic parameter gets "objectClass": "image", pixelTypes (for example ["byte"]) and, for colour, "multichannel": "true".

      Optionally add description, unit, default, min, max and values. All fields are listed in Interface File.

  4. Restart the app. The types changed the interface, so the module reports -10005, and its status/text gives the new hash (… now sha256:…). Copy that hash into the module’s acceptedInterfaceHash in the application file and restart again.

  5. Read v1/<module>/status/code and script/interface-source. Expected: 0 and interface-file (or mixed, when some types still come from the documentation).

When it loads the file, the Script Runner checks it against the procedure, so a file that does not fit is refused (-10002) instead of being half applied:

  • Every parameter of the procedure must be in the file, and every parameter in the file must be in the procedure. The status text names each one that is missing on either side.

  • Each parameter must be in the same group (input or output, iconic or control) and have the same dimension as in the procedure.

  • A parameter must not be listed twice.

  • Each type must be in the closed type set.

The file matches parameters by name, not by position: reordering the parameters in HDevelop needs no change to the file, but renaming one does. A worked example with a complete file is in Writing one from the procedure’s shape.

3. Use types the Data Layer can carry

See Type Mapping for the complete table. In short:

  • Numbers and strings become nodes. One value is a scalar node, several values (multivalue true or optional) an array node.

  • Do not mix strings and numbers in one parameter: such a parameter is refused.

  • Images become image groups. Mono8, Mono12, Mono16, RGB8 and BGR8 are carried.

  • Regions and contours (XLD) are not carried. A region or contour input refuses the module. A region or contour output gets no node: convert it before return (), for example region_to_bin or paint_region for an image, area_center or get_region_points for coordinates, paint_xld for a contour.

  • Handles (shape_model, dict, any model) never cross the Data Layer. A handle input must be bound to the group’s store; a handle output goes to the store by default. See Share a Model Between Scripts.

  • A structured value is declared with the plain types: a pose is 7 real values, a hom_mat2d 6.

4. Files the procedure reads

A string input of the semantic type filename (filename.read, …​) is resolved inside the script folder: the client writes blobs.png, the procedure gets the full path. Put model files, images and other data your procedure reads into the script folder. See The script folder.

5. Leave out the display

The Script Runner runs without a display. By default the display operators (dev_display, dev_set_color, …​) do nothing, and dev_open_window and dev_get_window fail. Remove window operators from the procedure, or guard them. See ignoreDisplay in engine.

6. Test it

  1. Test the procedure in HDevelop with typical and with bad inputs.

  2. For a bad input, raise an error yourself with throw: the text arrives in the module’s run/output/status-text with code -10001.

  3. Make sure every output is set on every path through the procedure: an output the procedure never set fails the run with -10003.

  4. Every output must match its declared type. An integer where a real is declared is converted; anything else fails the run with -10003.

7. Protect it (optional)

You can lock the procedure with a password in HDevelop. The Script Runner still reads its interface and runs it; the password never reaches the device. Keep an unlocked copy: only the password holder can view or change the body.

If you use an interface file, write it before you protect the procedure, from the open procedure.

Next

Copy the procedure (and its interface file) into the script folder, then Add a Module.