// 合成帧取流源(`source.type=synth`)—— 零依赖、逐像素可复现。 // // 用途(不是"演示",是**验收与回归**): // * 现场没有相机时先把"取流 → 证据 → 时序 → 寄存器 → PLC"整条链跑通(docs/deployment.md §1); // * 台架验收:`synth_shift_px > 0` 制造帧间位移,验证运动门控"运动中不改变判定"; // * CI:固定 seed → 同一份代码任何机器渲染出**同一批像素**,配合 `--dump-frames` 可做黄金语料回归 // (scripts/replay_check.py)。 #include #include #include #include "portcv/capture.hpp" #include "portcv/edge_synth.hpp" namespace portcv { namespace { /// 合成场景名 → 场景参数(取值与 cpp/tests/selftest_edge.cpp 的验收场景一致)。 bool scene_by_name(const std::string& name, SynthScene* out, std::string* error) { if (name == "gondola") { // 敞车(C70)落箱:围挡上缘 ≈0.70 箱高 *out = synth_gondola_scene(1.95); return true; } if (name == "flatbed") { // 平板车(X70)+ 40 尺箱 *out = synth_flatbed_scene(6.06); return true; } if (name == "ground") { // 箱直接在地面(堆场),无钢轨 *out = synth_ground_scene(); out->ground_rails = false; return true; } if (name == "empty") { // 空画面(无车无箱)→ 证据后端应弃权 → 255 *out = SynthScene(); out->wagon = false; out->gondola = false; out->has_box = false; out->ground_rails = false; return true; } if (error != nullptr) { *error = "source.uri=\"" + name + "\" 不是合成场景名;可用:gondola(敞车落箱)/ flatbed(平车落箱)/ " "ground(地面堆场)/ empty(空画面)"; } return false; } class SyntheticFrameSource : public FrameSource { public: explicit SyntheticFrameSource(const SourceConfig& cfg) : cfg_(cfg) {} std::string name() const override { return "synth"; } std::string describe() const override { char buf[256]; std::snprintf(buf, sizeof(buf), "synth scene=%s %dx%d shift=%.1fpx seed=%u", scene_name_.c_str(), cam_.width, cam_.height, cfg_.synth_shift_px, static_cast(cfg_.synth_seed)); return std::string(buf); } bool open(std::string* error) override { if (cfg_.uri.empty()) { cfg_.uri = "gondola"; } // 空 uri → 敞车(台架最常用) scene_name_ = cfg_.uri; if (!scene_by_name(scene_name_, &scene_, error)) { return false; } scene_.seed = cfg_.synth_seed; cam_.width = cfg_.width > 0 ? cfg_.width : 960; // 默认 = 证据模块的工作长边(不缩放) cam_.height = cfg_.height > 0 ? cfg_.height : 540; cam_.f_px = 900.0; base_ = render_synthetic(cam_, scene_); // 只渲染一次:逐帧平移即可 opened_ = !base_.empty(); if (!opened_ && error != nullptr) { *error = "合成场景渲染失败(尺寸非法)"; } return opened_; } bool next(CapturedFrame* out, std::string* error) override { if (out == nullptr) { return false; } if (!opened_ && !open(error)) { return false; } const int shift = static_cast(cfg_.synth_shift_px < 0.0 ? 0.0 : cfg_.synth_shift_px); if (shift > 0) { // 位移按 8 帧一个周期回位:既能被运动门控稳定识别为"运动中", // 又不会把画面推出视野(台架验收用,不追求物理真实性)。 const long phase = index_ % 8; out->gray = translate_image(base_, static_cast(phase) * shift, 0); } else { out->gray = base_; } out->index = index_; out->ts = capture_now_seconds(); out->valid = true; ++index_; return true; } void close() override { base_ = GrayImage(); opened_ = false; } private: SourceConfig cfg_; SynthCamera cam_; SynthScene scene_; std::string scene_name_; GrayImage base_; long index_ = 0; bool opened_ = false; }; } // namespace std::unique_ptr make_synth_source(const SourceConfig& cfg, std::string* error) { std::unique_ptr src(new SyntheticFrameSource(cfg)); if (!src->open(error)) { return nullptr; } return src; } bool synth_scene_known(const std::string& name) { if (name.empty()) { return true; } // 空 = 默认 gondola SynthScene ignored; std::string error; return scene_by_name(name, &ignored, &error); } } // namespace portcv