| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- """P0-5 标定记录生成/校验(现场用)。
- 生成(自动写入 sha256)::
- python scripts/calib_record.py create --site CT01 --camera front \\
- --reviewer "张工" --px-per-m 115.6 --box-height 2.591 \\
- --out configs/calib/ct01-front.json
- 校验(过期/哈希/复核/漂移;退出码 1 = CALIB_STALE)::
- python scripts/calib_record.py check configs/calib/ct01-front.json \\
- --max-age-days 180 --px-per-m 115.6 --box-height 2.591
- 说明:
- * 两份实现(Python `portcv/calibration.py` 与 C++ `cpp/src/calibration.cpp`)用**同一个**
- canonical JSON + sha256 口径;本脚本生成的记录 C++ 运行期同样能校验通过
- (跨语言契约由 `cpp/tests/selftest_calib.cpp` 与 `tests/test_calibration.py` 双向锁定)。
- * 放在 `scripts/` 而不是 `build/`:`build/` 在 `.gitignore` 里,交付版本拿不到脚本
- 就等于"记录无法生成/无法校验"。
- """
- from __future__ import annotations
- import argparse
- import json
- import os
- import sys
- from datetime import datetime, timezone
- ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
- if ROOT not in sys.path:
- sys.path.insert(0, ROOT)
- from portcv.calibration import evaluate, seal # noqa: E402
- from portcv.config import Config # noqa: E402
- def _force_utf8() -> None:
- """Windows 默认 GBK 控制台打印中文/符号会崩(UnicodeEncodeError),统一切 UTF-8。"""
- for stream in (sys.stdout, sys.stderr):
- try:
- stream.reconfigure(encoding="utf-8", errors="replace")
- except Exception: # pragma: no cover - 老 Python / 被重定向的流
- pass
- def cmd_create(args: argparse.Namespace) -> int:
- record = {
- "schema": "portcv.calib/1",
- "site": args.site,
- "camera_id": args.camera,
- "calibrated_at": args.date or datetime.now(timezone.utc).isoformat(timespec="seconds"),
- "reviewer": args.reviewer,
- "method": args.method,
- "px_per_m": float(args.px_per_m),
- "box_height_m": float(args.box_height),
- "intrinsics": {},
- "extrinsics": {},
- "roi": [],
- }
- record = seal(record)
- os.makedirs(os.path.dirname(os.path.abspath(args.out)), exist_ok=True)
- with open(args.out, "w", encoding="utf-8") as fh:
- json.dump(record, fh, ensure_ascii=False, indent=2)
- fh.write("\n")
- print(json.dumps({"written": args.out, "sha256": record["sha256"]}, ensure_ascii=False, indent=2))
- return 0
- def cmd_check(args: argparse.Namespace) -> int:
- cfg = Config(
- {
- "calibration": {
- "record": args.record,
- "max_age_days": args.max_age_days,
- },
- "evidence_v2": {
- "px_per_m": args.px_per_m or 0.0,
- "box_height_m": args.box_height or 0.0,
- },
- }
- )
- status = evaluate(cfg)
- print(json.dumps(status.to_dict(), ensure_ascii=False, indent=2))
- return 0 if not status.stale else 1
- def main(argv=None) -> int:
- _force_utf8()
- parser = argparse.ArgumentParser(description="P0-5 标定记录")
- sub = parser.add_subparsers(dest="cmd", required=True)
- create = sub.add_parser("create", help="生成并封存记录")
- create.add_argument("--site", required=True)
- create.add_argument("--camera", required=True)
- create.add_argument("--reviewer", required=True)
- create.add_argument("--method", default="")
- create.add_argument("--px-per-m", type=float, default=0.0)
- create.add_argument("--box-height", type=float, default=2.591)
- create.add_argument("--date", default="", help="ISO-8601;默认当前时间")
- create.add_argument("--out", required=True)
- create.set_defaults(func=cmd_create)
- check = sub.add_parser("check", help="校验记录(只读)")
- check.add_argument("record")
- check.add_argument("--max-age-days", type=float, default=180.0)
- check.add_argument("--px-per-m", type=float, default=0.0)
- check.add_argument("--box-height", type=float, default=0.0)
- check.set_defaults(func=cmd_check)
- args = parser.parse_args(argv)
- return int(args.func(args))
- if __name__ == "__main__":
- sys.exit(main())
|