Files

81 lines
2.9 KiB
Python
Raw Permalink Normal View History

"""Run one HDevelop external procedure (.hdvp) via HDevEngine and print outputs.
This is the generic form of the tested run_compare.py driver. Pass input control
params with --set, name the outputs to read with --get; results print as a JSON
object on stdout.
Example:
/mnt/c/Users/NAURA/.local/bin/uv.exe run python \
.claude/skills/halcon/scripts/run_procedure.py \
--proc-path C:/workspace/agent-studio/halcon-001 \
--proc find_circles \
--set ImageFile=C:/data/a.png \
--set RoiFile=C:/workspace/agent-studio/halcon-001/roi.hobj \
--get FitRow --get FitCol --get MetroRow --get MetroCol
Notes:
- Always use ABSOLUTE paths for images/ROIs (HDevEngine has no cwd notion).
- Numeric --set values are auto-typed; use commas for a tuple (e.g. --set T=10,20).
- Wrap in a loop from your own Python for batch processing.
"""
import argparse
import json
import sys
from _common import parse_set, jsonable
def main() -> int:
ap = argparse.ArgumentParser(description="Run a HALCON .hdvp via HDevEngine")
ap.add_argument("--proc-path", required=True,
help="Directory containing the .hdvp (set_procedure_path)")
ap.add_argument("--proc", required=True,
help="Procedure base name, no extension (e.g. find_circles)")
ap.add_argument("--set", dest="sets", action="append", default=[],
metavar="Name=Value", help="Input control param (repeatable)")
ap.add_argument("--get", dest="gets", action="append", default=[],
metavar="Name", help="Output control param to read (repeatable)")
args = ap.parse_args()
try:
from halcon.hdevengine import (
HDevEngine, HDevProcedure, HDevProcedureCall, HDevEngineError,
)
except Exception as e:
print(f"ERROR: cannot import HDevEngine ({e}). Run via the project venv "
f"using uv.", file=sys.stderr)
return 1
eng = HDevEngine()
eng.set_procedure_path(args.proc_path)
try:
proc = HDevProcedure.load_external(args.proc)
except HDevEngineError as e:
print(f"ERROR: could not load procedure '{args.proc}' from "
f"'{args.proc_path}': {e}", file=sys.stderr)
return 2
call = HDevProcedureCall(proc)
call.reset()
for name, value in parse_set(args.sets):
call.set_input_control_param_by_name(name, value)
try:
call.execute()
except HDevEngineError as e:
print(json.dumps({"ok": False, "error": str(e)}, ensure_ascii=False))
return 3
out = {}
for name in args.gets:
try:
out[name] = jsonable(call.get_output_control_param_by_name(name))
except HDevEngineError as e:
out[name] = {"error": str(e)}
print(json.dumps({"ok": True, "outputs": out}, ensure_ascii=False))
return 0
if __name__ == "__main__":
sys.exit(main())