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:
-
In HDevelop select the lines of your logic and choose Create New Procedure.
-
Declare the interface: which iconic objects and control values go in and come out.
-
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:
MinScorebecomesmin-score,ROIRowbecomesroi-row. Two parameters must not give the same node name. -
The output names
status-code,status-textandpackage-identifierare 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 |
|---|---|---|
You can edit the procedure in HDevelop. This is the recommended way. |
Inside the |
|
The procedure is protected, you do not want to change it, or its documentation is incomplete. |
In |
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).
-
Open the procedure in HDevelop and choose Procedures > Edit Procedure Interface (Ctrl+Shift+P, I).
-
Open the documentation part of the dialog. It has a tab card for each parameter.
-
For each control parameter (value), fill in:
-
Semantics, for example
integer,real,string,number,angle.radorfilename.read, or a handle type such asshape_model; -
Type List:
integer,real,stringorhandle; -
Multi Value:
falsefor one value,trueoroptionalfor several; -
Mixed Types: leave it at
false. A parameter that mixes strings and numbers is refused.
-
-
For each iconic parameter (image), fill in:
-
Semantics
image; -
Pixel Types: the pixel types the procedure accepts (
byte,uint2, …); -
Multi Channel:
falsefor one channel,truefor colour.
-
-
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.
-
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 |
|
Gives the type when |
Type List |
|
Gives the type of a control parameter; the accepted pixel types of an image. |
Multi Value |
|
One value gives a scalar node, several values an array node. |
Mixed Types |
|
|
Multi Channel |
|
One channel or colour. |
Default Value |
|
The input node’s first value. |
Value Min, Value Max |
|
The range an input is checked against. |
Values |
|
The suggested values. |
Description |
|
The node’s description. |
Which slot decides the type of a control parameter:
-
type_listdecides when it is filled in. -
Only when
type_listis empty does the Script Runner read the type fromsem_type, and only from these labels:integer;real,number,angle.,point.;string,filename*; the handle labelshandle,dict,window,message,message_queue,serial,socketand any*_model. Any othersem_typegives no type. -
One exception: a
type_listofintegeralone 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.
-
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 (
-10002or-10005), but itsscript/nodes are there. -
Read
v1/<module>/script/interface. It lists every parameter withgroup(input_iconic,output_iconic,input_ctrl,output_ctrl),name,positionanddimension; each parameter without a type is marked"typeSource": "missing". -
Save that JSON as
<procedure>.interface.jsonnext to the procedure. Delete everytypeSourceand the top-levelinterfaceHash, 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,maxandvalues. All fields are listed in Interface File.
-
-
Restart the app. The types changed the interface, so the module reports
-10005, and itsstatus/textgives the new hash (… now sha256:…). Copy that hash into the module’sacceptedInterfaceHashin the application file and restart again. -
Read
v1/<module>/status/codeandscript/interface-source. Expected:0andinterface-file(ormixed, 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 (
multivaluetrueoroptional) 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 exampleregion_to_binorpaint_regionfor an image,area_centerorget_region_pointsfor coordinates,paint_xldfor 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
realvalues, ahom_mat2d6.
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
-
Test the procedure in HDevelop with typical and with bad inputs.
-
For a bad input, raise an error yourself with
throw: the text arrives in the module’srun/output/status-textwith code-10001. -
Make sure every output is set on every path through the procedure: an output the procedure never set fails the run with
-10003. -
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.