Share a Model Between Scripts
A HALCON handle, such as a shape model, lives in the Script Runner’s memory and never crosses the Data Layer. Procedures that use the same handle run in one group and pass it through the group’s store. This guide reads a shape model from a file once at start and uses it in every run of a search procedure.
The two procedures:
load_model ( : : ModelFile : ModelID) find_pattern (Image : Overlay : ModelID, MinScore, MaxMatches : Row, Column, Angle, Score)
ModelID is a handle in both (type_list handle, sem_type shape_model).
1. Put both modules into one group
"modules": [
{
"name": "load",
"procedure": "load_model",
"group": "line-1",
"trigger": "start",
"inputs": { "ModelFile": { "from": "value", "value": "part.shm" } },
"outputs": { "ModelID": { "to": "store", "key": "model" } },
"acceptedInterfaceHash": "sha256:…"
},
{
"name": "find",
"procedure": "find_pattern",
"group": "line-1",
"trigger": "handshake",
"inputs": { "ModelID": { "from": "store", "key": "model" } },
"acceptedInterfaceHash": "sha256:…"
}
]
What happens:
-
loadhastriggerstart: it runs once when the group starts, before any request is served. It has norunfunction and no input or output nodes. -
ModelFilegets the fixed valuepart.shm, resolved in the script folder. -
loadwrites itsModelIDoutput into the store of groupline-1under the keymodel. A handle output goes to the store even without the binding, and so does every output of astartmodule; the key is then the parameter name. -
findreadsModelIDfrom the same key. It gets no node forModelID; its other parameters get nodes:run/input/image/…,run/input/min-score,run/input/max-matches,run/output/overlay/…,run/output/row,column,angle,score.
Put part.shm into the script folder, accept both interface hashes (see Add a Module) and restart the app.
2. Use it
The PLC calls find/run as often as it needs; every run uses the model load read at start.
-
If
loadfails at start,load/status/codeis-10002and itsstatus/textgives the reason. The key stays empty, and everyfindrun ends with-10004(store value missing); the status text names the key and the module that writes it. -
Before each run the Script Runner checks that the handle taken from the store is still valid and, where the interface gives a semantic type, that it is the right kind of handle. If not, the run ends with
-3and the procedure does not run.
Variant: train the model at run time
Replace the start module by a handshake module that trains the model from a camera image:
{
"name": "train",
"procedure": "train_model",
"group": "line-1",
"outputs": { "ModelID": { "to": "store", "key": "model" } },
"acceptedInterfaceHash": "sha256:…"
}
The PLC calls train/run once, then find/run as often as it likes. The next find after a new train uses the new model. A find before any train ends with -10004. The store is written only after a successful run whose outputs passed the output check, so a failed train keeps the old model.
Good to know
-
A group has one worker thread. Its modules run one after the other, never at the same time, so a handle is never used by two threads. A request to a module whose group is busy is acknowledged (
runningtrue) and waits in the group’s queue, first in, first out. -
Modules without shared state should not share a group: put them into groups of their own (or leave out
group), and they run in parallel. -
The store lives in memory only. After a restart of the app the
startmodules fill it again. To keep a trained model across restarts, write it to a file in the procedure (write_shape_model) and read it in a start module (read_shape_model). -
Procedures written before HALCON 18, and some of MVTec’s own, document a model ID with
type_listinteger. Beside a handle label such asshape_modelthe Script Runner reads it as a handle anyway and logs a warning, see Types from sem_type.