frame_dump.hpp 4.6 KB

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