// 检测器接口 + Mock 实现。 // // 真机上 Box 来自 TensorRT/ONNXRuntime/RKNN 后端(见 docs/technical_route.md §5); // 这个骨架里用 MockDetector 走"脚本化的检测结果",目的是**在没有模型、没有相机的情况下 // 也能把 判决 → 时序滤波 → Modbus/TCP → PLC 整条链路联调通**(现场先对寄存器再上模型)。 #pragma once #include #include #include #include #include "portcv/edge_image.hpp" #include "portcv/schema.hpp" namespace portcv { /// 一帧图像:骨架里只保留算法需要的元信息;真机上换成 GPU 缓冲 / cv::Mat。 struct Frame { int width = 1920; int height = 1080; long index = 0; double ts = 0.0; bool valid = true; ///< false 表示取帧失败(相机掉线/超时) /// 可选的整帧灰度图:**只有** `edge` 后端(2D 边缘几何证据)需要; /// 真机上由取流线程填(硬件解码 → 灰度转换),其它后端留 nullptr。 /// 加这一个可选字段,就能在不改动既有 Detector 契约的前提下接入证据后端(§10)。 const GrayImage* gray = nullptr; }; /// 检测器接口。 class Detector { public: virtual ~Detector() = default; /// 后端名(日志用)。 virtual std::string name() const = 0; /// 单帧推理,返回像素坐标框。 virtual std::vector infer(const Frame& frame) = 0; /// 加载/预热(真机后端在这里建 engine、跑一次空推理);失败返回 false。 virtual bool warmup(std::string* error = nullptr) { if (error) { error->clear(); } return true; } }; /// 脚本化 mock:每帧取脚本里的下一组框,用完后停在最后一组(模拟"车停稳了")。 class MockDetector : public Detector { public: explicit MockDetector(std::vector> script) : script_(std::move(script)) {} std::string name() const override { return "mock"; } std::vector infer(const Frame& frame) override { (void)frame; if (script_.empty()) { return {}; } const std::size_t index = index_ < script_.size() ? index_ : script_.size() - 1; const std::vector out = script_[index]; if (index_ + 1 < script_.size()) { ++index_; } return out; } void reset() { index_ = 0; } private: std::vector> script_; std::size_t index_ = 0; }; /// 造框小工具(脚本/自检用)。 inline Box make_box(const char* class_name, float x1, float y1, float x2, float y2, float score, int class_id = 0) { return Box(x1, y1, x2, y2, score, class_id, class_name); } } // namespace portcv