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:

  • load has trigger start: it runs once when the group starts, before any request is served. It has no run function and no input or output nodes.

  • ModelFile gets the fixed value part.shm, resolved in the script folder.

  • load writes its ModelID output into the store of group line-1 under the key model. A handle output goes to the store even without the binding, and so does every output of a start module; the key is then the parameter name.

  • find reads ModelID from the same key. It gets no node for ModelID; 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 load fails at start, load/status/code is -10002 and its status/text gives the reason. The key stays empty, and every find run 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 -3 and 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 (running true) 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 start modules 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_list integer. Beside a handle label such as shape_model the Script Runner reads it as a handle anyway and logs a warning, see Types from sem_type.