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