capture_synth.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // 合成帧取流源(`source.type=synth`)—— 零依赖、逐像素可复现。
  2. //
  3. // 用途(不是"演示",是**验收与回归**):
  4. // * 现场没有相机时先把"取流 → 证据 → 时序 → 寄存器 → PLC"整条链跑通(docs/deployment.md §1);
  5. // * 台架验收:`synth_shift_px > 0` 制造帧间位移,验证运动门控"运动中不改变判定";
  6. // * CI:固定 seed → 同一份代码任何机器渲染出**同一批像素**,配合 `--dump-frames` 可做黄金语料回归
  7. // (scripts/replay_check.py)。
  8. #include <cmath>
  9. #include <cstdio>
  10. #include <string>
  11. #include "portcv/capture.hpp"
  12. #include "portcv/edge_synth.hpp"
  13. namespace portcv {
  14. namespace {
  15. /// 合成场景名 → 场景参数(取值与 cpp/tests/selftest_edge.cpp 的验收场景一致)。
  16. bool scene_by_name(const std::string& name, SynthScene* out, std::string* error) {
  17. if (name == "gondola") { // 敞车(C70)落箱:围挡上缘 ≈0.70 箱高
  18. *out = synth_gondola_scene(1.95);
  19. return true;
  20. }
  21. if (name == "flatbed") { // 平板车(X70)+ 40 尺箱
  22. *out = synth_flatbed_scene(6.06);
  23. return true;
  24. }
  25. if (name == "ground") { // 箱直接在地面(堆场),无钢轨
  26. *out = synth_ground_scene();
  27. out->ground_rails = false;
  28. return true;
  29. }
  30. if (name == "empty") { // 空画面(无车无箱)→ 证据后端应弃权 → 255
  31. *out = SynthScene();
  32. out->wagon = false;
  33. out->gondola = false;
  34. out->has_box = false;
  35. out->ground_rails = false;
  36. return true;
  37. }
  38. if (error != nullptr) {
  39. *error = "source.uri=\"" + name +
  40. "\" 不是合成场景名;可用:gondola(敞车落箱)/ flatbed(平车落箱)/ "
  41. "ground(地面堆场)/ empty(空画面)";
  42. }
  43. return false;
  44. }
  45. class SyntheticFrameSource : public FrameSource {
  46. public:
  47. explicit SyntheticFrameSource(const SourceConfig& cfg) : cfg_(cfg) {}
  48. std::string name() const override { return "synth"; }
  49. std::string describe() const override {
  50. char buf[256];
  51. std::snprintf(buf, sizeof(buf), "synth scene=%s %dx%d shift=%.1fpx seed=%u",
  52. scene_name_.c_str(), cam_.width, cam_.height, cfg_.synth_shift_px,
  53. static_cast<unsigned>(cfg_.synth_seed));
  54. return std::string(buf);
  55. }
  56. bool open(std::string* error) override {
  57. if (cfg_.uri.empty()) { cfg_.uri = "gondola"; } // 空 uri → 敞车(台架最常用)
  58. scene_name_ = cfg_.uri;
  59. if (!scene_by_name(scene_name_, &scene_, error)) { return false; }
  60. scene_.seed = cfg_.synth_seed;
  61. cam_.width = cfg_.width > 0 ? cfg_.width : 960; // 默认 = 证据模块的工作长边(不缩放)
  62. cam_.height = cfg_.height > 0 ? cfg_.height : 540;
  63. cam_.f_px = 900.0;
  64. base_ = render_synthetic(cam_, scene_); // 只渲染一次:逐帧平移即可
  65. opened_ = !base_.empty();
  66. if (!opened_ && error != nullptr) { *error = "合成场景渲染失败(尺寸非法)"; }
  67. return opened_;
  68. }
  69. bool next(CapturedFrame* out, std::string* error) override {
  70. if (out == nullptr) { return false; }
  71. if (!opened_ && !open(error)) { return false; }
  72. const int shift = static_cast<int>(cfg_.synth_shift_px < 0.0 ? 0.0 : cfg_.synth_shift_px);
  73. if (shift > 0) {
  74. // 位移按 8 帧一个周期回位:既能被运动门控稳定识别为"运动中",
  75. // 又不会把画面推出视野(台架验收用,不追求物理真实性)。
  76. const long phase = index_ % 8;
  77. out->gray = translate_image(base_, static_cast<int>(phase) * shift, 0);
  78. } else {
  79. out->gray = base_;
  80. }
  81. out->index = index_;
  82. out->ts = capture_now_seconds();
  83. out->valid = true;
  84. ++index_;
  85. return true;
  86. }
  87. void close() override {
  88. base_ = GrayImage();
  89. opened_ = false;
  90. }
  91. private:
  92. SourceConfig cfg_;
  93. SynthCamera cam_;
  94. SynthScene scene_;
  95. std::string scene_name_;
  96. GrayImage base_;
  97. long index_ = 0;
  98. bool opened_ = false;
  99. };
  100. } // namespace
  101. std::unique_ptr<FrameSource> make_synth_source(const SourceConfig& cfg, std::string* error) {
  102. std::unique_ptr<FrameSource> src(new SyntheticFrameSource(cfg));
  103. if (!src->open(error)) { return nullptr; }
  104. return src;
  105. }
  106. bool synth_scene_known(const std::string& name) {
  107. if (name.empty()) { return true; } // 空 = 默认 gondola
  108. SynthScene ignored;
  109. std::string error;
  110. return scene_by_name(name, &ignored, &error);
  111. }
  112. } // namespace portcv