// 黄金语料导出(B-lite 的一半):把"逐帧灰度图 + 逐项证据 + 结论"落盘,供 Python 侧回放断言。 // // 为什么需要它(堵住"12 项阈值未整定"的整定通路): // 现在整定阈值只有一条路 —— 改参数 → 上板卡 → 重编译 → 跑一遍 → 肉眼看日志。 // 一帧现场画面的"证据明细"应当只采集**一次**,之后所有阈值迭代都在 PC 上秒级跑完, // 而且能被自动回归(scripts/replay_check.py 对着基线 JSONL 逐帧比对)。 // // 与 Python 侧的契约(`scripts/replay_check.py` 依赖,改动需同步两侧): // * ``:每帧一行 JSON(UTF-8,无缩进),字段见 frame_dump.cpp 的注释; // * `/frame_%06ld.pgm`:P5 二进制灰度图(maxval 255,无注释、单空格分隔); // * 行内 `gray_crc32` = 该 PGM **像素区字节**的 CRC-32(IEEE,与 zlib.crc32 同算法), // 于是 Python 侧能断言"这一帧的像素逐字节没变"。 // // 注意:导出是**只读旁路**,不改判决、不改时序;写失败只记一次告警并置 `failed()`, // 绝不因为"存图盘满了"影响 PLC 拿结果(现场安全优先)。 #pragma once #include #include #include #include "portcv/edge_evidence.hpp" #include "portcv/edge_image.hpp" #include "portcv/schema.hpp" namespace portcv { /// 导出配置(全部来自命令行;不属于配置文件契约,避免现场误开)。 struct DumpConfig { std::string jsonl_path; ///< 空 = 关闭导出 std::string image_dir; ///< 空 = 只写 JSONL,不存 PGM int every = 1; ///< 每 N 帧导出一帧(>=1;整定时用 1,长跑复盘用 10~30) }; /// 一帧的导出内容(各指针可空;缺什么就少写什么,但字段名不省略)。 /// /// 两个状态**不是一回事**,导出必须都给(否则回放分不清"这一帧真弃权"与 /// "这一帧没结论、寄存器里是上一稳定值"): /// * `snap->state` → JSON 的 `state`:**寄存器口径**(PublishGate 之后的结果, /// 可能是保持值;PLC 实际读到的就是它); /// * `vote_state`:**TemporalVoter 之后、PublishGate 之前**的内部结论 /// (255 = 本帧不足以投票 / 弃权); /// * `edge_state`:**本帧证据结论**(255 = 本帧弃权 / 看不清 / 运动中不改变判定)。 struct DumpFrame { long index = 0; double ts = 0.0; const GrayImage* gray = nullptr; ///< 像素(空 → 不存图、不写 gray_crc32) const VisionSnapshot* snap = nullptr; ///< 寄存器口径(空 → 不写发布字段) const Evidence* evidence = nullptr; ///< 证据明细(mock 后端没有 → 空) std::string summary; ///< evidence_summary()(可空) std::string reason; ///< 判决原因(可空) int edge_state = -1; ///< 本帧证据结论(-1 = 该后端不产出/未填) double edge_conf = 0.0; ///< 本帧证据结论的置信度(0..1) int vote_state = -1; ///< 投票后、发布门之前的内部结论 }; /// 写一张 P5 PGM(失败返回 false 并填写 error)。 bool write_pgm(const std::string& path, const GrayImage& img, std::string* error); /// 逐帧导出器(单线程使用:推理线程写)。 class FrameDumper { public: FrameDumper() = default; ~FrameDumper() { close(); } FrameDumper(const FrameDumper&) = delete; FrameDumper& operator=(const FrameDumper&) = delete; /// 打开输出(`jsonl_path` 为空 → 返回 true 且保持关闭 = 不导出)。 bool open(const DumpConfig& cfg, std::string* error); void close(); bool enabled() const { return file_ != nullptr; } bool failed() const { return failed_; } const std::string& last_error() const { return last_error_; } uint64_t written() const { return written_; } /// 已经导出过的帧数对应的"每 N 帧"判断(`every` 在 open 时定下)。 bool wants(long index) const { return every_ > 0 && (index % every_) == 0; } /// 导出(或跳过)一帧。返回 true = 这一帧已按契约写出(或按 every 跳过)。 bool write(const DumpFrame& frame, std::string* error); private: std::FILE* file_ = nullptr; std::string jsonl_path_; std::string image_dir_; int every_ = 1; uint64_t written_ = 0; bool failed_ = false; std::string last_error_; }; /// 递归建目录(已存在视为成功);`--dump-dir` 首次使用时自动创建。 bool ensure_dir(const std::string& path, std::string* error); } // namespace portcv