calibration.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // 标定记录(P0-5):记录解析 / sha256 校验 / 过期与漂移判定 —— 与
  2. // `portcv/calibration.py` **同字段、同判据、同原因名**。
  3. //
  4. // 为什么 C++ 侧必须自己实现一份:
  5. // 现场走 C++(`portcv_run`)时若只读顶层 `calibration.stale`,就等于"标定记录形同虚设"
  6. // —— 文件被改一个数、超期半年、运行期 `px_per_m` 悄悄改大都无人知晓。本模块把
  7. // Python 的判定原样搬到运行期,`--check-config` 与启动日志都会给出**原因串**。
  8. //
  9. // 关键一致性(这是整份实现里最容易被忽略的地方):
  10. // * `sha256` 覆盖"除自身外全部字段"的 **canonical JSON**:键排序、无空格、
  11. // UTF-8 原样(`ensure_ascii=False`)。C++ 的浮点/整数渲染必须与 Python 的
  12. // `json.dumps` 逐字相同,否则永远校验不过 —— 见 `canonical_json()` 的说明与
  13. // `cpp/tests/selftest_calib.cpp` 里由 Python 生成的黄金向量。
  14. // * 判定顺序与原因串:`manual_stale` / `missing` / `invalid:json` / `invalid:fields`
  15. // / `hash_mismatch` / `expired` / `unreviewed` / `px_per_m_drift` / `box_height_drift`。
  16. // 任一条成立即 `CALIB_STALE`(bit11),不做"加权"也不自动改阈值。
  17. #pragma once
  18. #include <cstdint>
  19. #include <string>
  20. #include <vector>
  21. #include "portcv/json_lite.hpp"
  22. namespace portcv {
  23. /// `calibration.*` 配置(键名与 Python `portcv/config.py` 的 DEFAULTS 相同)。
  24. struct CalibrationConfig {
  25. bool stale = false; ///< calibration.stale:手工置过期(覆盖自动判定)
  26. std::string record; ///< calibration.record:记录文件路径(空 = 不做判定)
  27. double max_age_days = 180.0; ///< calibration.max_age_days(0 = 不判过期)
  28. double px_per_m_tolerance = 0.10; ///< calibration.px_per_m_tolerance(相对容差)
  29. double box_height_tolerance_m = 0.02; ///< calibration.box_height_tolerance_m(绝对容差)
  30. /// calibration.hold_on_stale(C++/Python 同键):标定不可信时**是否清 RESULT_VALID/STABLE**。
  31. /// true(默认)= 只发布 state 与 CALIB_STALE,PLC 不得据此自动动作;
  32. /// false = 仅告警(等价于历史行为,留给"现场已有人工联锁"的机位)。
  33. bool hold_on_stale = true;
  34. };
  35. /// 记录里运行期真正用到的字段(`intrinsics` / `extrinsics` / `roi` 只参与哈希,不解析)。
  36. struct CalibrationRecord {
  37. std::string site;
  38. std::string camera_id;
  39. std::string calibrated_at; ///< 原样文本(日志用)
  40. long long calibrated_at_s = 0; ///< UTC epoch 秒(秒级精度,与 Python 的年龄判定等价)
  41. std::string reviewer;
  42. std::string method;
  43. double px_per_m = 0.0;
  44. double box_height_m = 0.0;
  45. };
  46. /// 一次判定的结果(`stale=false` 且 `reasons` 为空 = 标定可用)。
  47. struct CalibrationStatus {
  48. bool stale = false;
  49. bool hold = true; ///< 生效的 hold_on_stale
  50. bool hash_ok = false; ///< 哈希校验通过(无记录/文件缺失时保持 false)
  51. bool has_record = false; ///< 解析出了记录(字段齐全)
  52. bool has_age = false; ///< 年龄可计算
  53. double age_days = 0.0;
  54. std::string path;
  55. std::vector<std::string> reasons;
  56. CalibrationRecord record;
  57. /// 原因串 `"expired,unreviewed"`;无原因 → `""`(与 Python `",".join(reasons)` 同形)。
  58. std::string reasons_text() const;
  59. };
  60. // ---------------------------------------------------------------- 哈希口径
  61. /// canonical JSON:键排序(按 UTF-8 字节 = Unicode 码点序)、无空格、UTF-8 原样、
  62. /// 控制字符按 `\u00XX` 转义 —— 与 `json.dumps(obj, ensure_ascii=False,
  63. /// sort_keys=True, separators=(",", ":"))` 逐字相同(顶层 `sha256` 成员被剔除)。
  64. std::string canonical_json(const json::Value& record);
  65. /// 记录内容的 sha256(十六进制小写)。等价 Python `compute_hash()`。
  66. std::string compute_hash(const json::Value& record);
  67. /// 记录自带 `sha256` 是否与重算值一致。等价 Python `verify()`。
  68. bool verify_hash(const json::Value& record);
  69. // ---------------------------------------------------------------- 记录解析
  70. /// ISO-8601 → UTC epoch 秒(接受 `Z` / `+08:00` / `+0800` / 无时区=UTC / 纯日期 / 小数秒)。
  71. /// 失败返回 false(不抛异常,现场错误只走日志)。
  72. bool parse_iso8601(const std::string& text, long long* epoch_s);
  73. /// 必填字段(`site` / `camera_id` / `calibrated_at`)校验并取值;失败时 `*error` 写原因。
  74. bool record_from_json(const json::Value& data, CalibrationRecord* out, std::string* error);
  75. // ---------------------------------------------------------------- 判定
  76. /// 判定的纯函数输入(文件读/解析结果由调用方给,便于自检不碰磁盘)。
  77. struct CalibrationInputs {
  78. bool file_exists = false; ///< `record` 指向的文件是否可读
  79. bool json_ok = false; ///< 内容是否为 JSON 对象
  80. json::Value record; ///< 已解析内容(file_exists && json_ok 时有效)
  81. double run_px_per_m = 0.0; ///< evidence_v2.px_per_m(运行期)
  82. double run_box_height_m = 0.0; ///< evidence_v2.box_height_m(运行期)
  83. };
  84. /// 纯函数判定(等价 Python `evaluate()`;不做 I/O)。
  85. CalibrationStatus evaluate_calibration_values(const CalibrationConfig& cfg, const CalibrationInputs& in,
  86. long long now_s);
  87. /// 读文件 + 判定(运行期入口;文件缺失/损坏不抛错,只变成原因串)。
  88. CalibrationStatus evaluate_calibration(const CalibrationConfig& cfg, double run_px_per_m,
  89. double run_box_height_m, long long now_s);
  90. /// 当前 UTC epoch 秒。
  91. long long epoch_now();
  92. /// 一行状态(启动日志 / `--check-config`)。不含换行符结尾之外的格式,调用方直接 printf。
  93. std::string calibration_status_line(const CalibrationStatus& status);
  94. } // namespace portcv