// 场景规则判定 —— 与 Python 侧 portcv/logic/scene.py 一致(composed / direct 两种模式)。 // // composed(推荐):模型检测 container / bed(敞车) / flatbed(平车) 等目标,本模块用几何关系 // 判断"集装箱是否落在某种车厢上";参数与配置段 scene.* 同名同默认值,现场可直接调。 // direct:模型直接输出 3 类场景(not_on_train / bed / flatbed),这里只做 ROI 过滤与 argmax。 // // 输出编码:0=不在车上 1=车斗(敞车) 2=平板车(平车) 255=UNKNOWN(交给时序滤波决定)。 #pragma once #include #include #include #include #include #include "portcv/schema.hpp" namespace portcv { using Point = std::pair; /// 场景规则参数(对应 Python 配置段 scene.*)。 struct SceneParams { std::string mode = "composed"; std::vector container_names = {"container", "container_full", "container_empty", "container_20ft", "container_40ft"}; std::vector bed_names = {"bed", "wagon_bed", "hopper_wagon", "gondola"}; std::vector flatbed_names = {"flatbed", "flat_wagon", "platform_wagon"}; std::vector ignore_names = {"person", "people", "crane", "spreader", "truck", "wheel", "rail"}; std::map direct_map = {{"not_on_train", 0}, {"bed", 1}, {"flatbed", 2}}; std::vector roi = {{0.03, 0.10}, {0.97, 0.10}, {0.97, 0.99}, {0.03, 0.99}}; bool require_container = true; double min_class_conf = 0.30; double roi_min_ratio = 0.30; double support_overlap = 0.30; double support_dy_ratio = 0.45; double support_score_ratio = 0.60; bool unknown_on_no_detection = true; double no_container_conf = 0.60; }; /// IoU(与 Python Box.iou 一致)。 inline double box_iou(const Box& a, const Box& b) { const double ix1 = a.x1 > b.x1 ? a.x1 : b.x1; const double iy1 = a.y1 > b.y1 ? a.y1 : b.y1; const double ix2 = a.x2 < b.x2 ? a.x2 : b.x2; const double iy2 = a.y2 < b.y2 ? a.y2 : b.y2; const double iw = ix2 - ix1; const double ih = iy2 - iy1; if (iw <= 0.0 || ih <= 0.0) { return 0.0; } const double inter = iw * ih; const double uni = static_cast(a.area()) + static_cast(b.area()) - inter; return uni > 0.0 ? inter / uni : 0.0; } /// 射线法判断点是否在多边形内。 inline bool point_in_polygon(double x, double y, const std::vector& polygon) { if (polygon.size() < 3) { return false; } bool inside = false; const std::size_t n = polygon.size(); for (std::size_t i = 0; i < n; ++i) { const double x1 = polygon[i].first; const double y1 = polygon[i].second; const double x2 = polygon[(i + 1) % n].first; const double y2 = polygon[(i + 1) % n].second; if ((y1 > y) != (y2 > y)) { const double x_cross = x1 + (y - y1) * (x2 - x1) / (y2 - y1); if (x < x_cross) { inside = !inside; } } } return inside; } /// 归一化多边形 → 像素多边形。 inline std::vector normalized_to_pixel_polygon(const std::vector& polygon, int width, int height) { std::vector out; if (polygon.empty()) { return {{0.0, 0.0}, {static_cast(width), 0.0}, {static_cast(width), static_cast(height)}, {0.0, static_cast(height)}}; } out.reserve(polygon.size()); for (const Point& p : polygon) { out.push_back({p.first * width, p.second * height}); } return out; } /// 网格采样估计"框落在 ROI 内的面积比例"(0..1,与 Python box_in_polygon_ratio 一致)。 inline double box_in_polygon_ratio(const Box& box, const std::vector& polygon, int samples_x = 7, int samples_y = 7) { if (polygon.size() < 3 || box.area() <= 0.0f) { return 0.0; } const int sx = samples_x > 1 ? samples_x : 1; const int sy = samples_y > 1 ? samples_y : 1; int hits = 0; int total = 0; for (int ix = 0; ix < sx; ++ix) { const double fx = (ix + 0.5) / sx; const double x = box.x1 + fx * box.width(); for (int iy = 0; iy < sy; ++iy) { const double fy = (iy + 0.5) / sy; const double y = box.y1 + fy * box.height(); total += 1; if (point_in_polygon(x, y, polygon)) { hits += 1; } } } return total == 0 ? 0.0 : static_cast(hits) / static_cast(total); } /// 非极大值抑制(per_class=true 时按类别分别抑制,YOLO 常见做法)。 inline std::vector nms(std::vector boxes, double iou_threshold = 0.5, bool per_class = true) { std::stable_sort(boxes.begin(), boxes.end(), [](const Box& a, const Box& b) { return a.score > b.score; }); std::vector keep; for (const Box& box : boxes) { bool suppressed = false; for (const Box& kept : keep) { if (per_class && kept.class_id != box.class_id) { continue; } if (box_iou(box, kept) > iou_threshold) { suppressed = true; break; } } if (!suppressed) { keep.push_back(box); } } return keep; } /// 检测框 → 场景编码。线程内使用;classify() 本身是 const,可多线程各自持有实例。 class SceneClassifier { public: explicit SceneClassifier(const SceneParams& params = SceneParams()); /// 类别名归一化(小写 + 空格→下划线 + '-'→下划线)。 static std::string normalize(const std::string& name); /// 类别名 → 语义分组:container / bed / flatbed / ignore / unknown。 std::string group_of(const std::string& class_name) const; std::vector roi_polygon(int width, int height) const; /// 主入口:frame_size 由调用方给出(像素)。 Observation classify(const std::vector& detections, int width, int height, double ts, long frame_id = 0) const; private: Observation classify_direct(std::vector& boxes, int width, int height, double ts, long frame_id) const; Observation classify_composed(std::vector& boxes, int width, int height, double ts, long frame_id) const; static double overlap_x1d(double a1, double a2, double b1, double b2); bool support_quality(const Box& container, const Box& wagon, double* ov_x, double* quality) const; bool best_support(const Box& container, const std::vector>& wagons, Box* wagon_out, double* score_out, double* ovx_out) const; SceneParams p_; std::vector container_; std::vector bed_; std::vector flatbed_; std::vector ignore_; }; } // namespace portcv