scene.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // 场景规则判定 —— 与 Python 侧 portcv/logic/scene.py 一致(composed / direct 两种模式)。
  2. //
  3. // composed(推荐):模型检测 container / bed(敞车) / flatbed(平车) 等目标,本模块用几何关系
  4. // 判断"集装箱是否落在某种车厢上";参数与配置段 scene.* 同名同默认值,现场可直接调。
  5. // direct:模型直接输出 3 类场景(not_on_train / bed / flatbed),这里只做 ROI 过滤与 argmax。
  6. //
  7. // 输出编码:0=不在车上 1=车斗(敞车) 2=平板车(平车) 255=UNKNOWN(交给时序滤波决定)。
  8. #pragma once
  9. #include <algorithm>
  10. #include <map>
  11. #include <string>
  12. #include <utility>
  13. #include <vector>
  14. #include "portcv/schema.hpp"
  15. namespace portcv {
  16. using Point = std::pair<double, double>;
  17. /// 场景规则参数(对应 Python 配置段 scene.*)。
  18. struct SceneParams {
  19. std::string mode = "composed";
  20. std::vector<std::string> container_names = {"container", "container_full", "container_empty",
  21. "container_20ft", "container_40ft"};
  22. std::vector<std::string> bed_names = {"bed", "wagon_bed", "hopper_wagon", "gondola"};
  23. std::vector<std::string> flatbed_names = {"flatbed", "flat_wagon", "platform_wagon"};
  24. std::vector<std::string> ignore_names = {"person", "people", "crane",
  25. "spreader", "truck", "wheel", "rail"};
  26. std::map<std::string, int> direct_map = {{"not_on_train", 0}, {"bed", 1}, {"flatbed", 2}};
  27. std::vector<Point> roi = {{0.03, 0.10}, {0.97, 0.10}, {0.97, 0.99}, {0.03, 0.99}};
  28. bool require_container = true;
  29. double min_class_conf = 0.30;
  30. double roi_min_ratio = 0.30;
  31. double support_overlap = 0.30;
  32. double support_dy_ratio = 0.45;
  33. double support_score_ratio = 0.60;
  34. bool unknown_on_no_detection = true;
  35. double no_container_conf = 0.60;
  36. };
  37. /// IoU(与 Python Box.iou 一致)。
  38. inline double box_iou(const Box& a, const Box& b) {
  39. const double ix1 = a.x1 > b.x1 ? a.x1 : b.x1;
  40. const double iy1 = a.y1 > b.y1 ? a.y1 : b.y1;
  41. const double ix2 = a.x2 < b.x2 ? a.x2 : b.x2;
  42. const double iy2 = a.y2 < b.y2 ? a.y2 : b.y2;
  43. const double iw = ix2 - ix1;
  44. const double ih = iy2 - iy1;
  45. if (iw <= 0.0 || ih <= 0.0) { return 0.0; }
  46. const double inter = iw * ih;
  47. const double uni = static_cast<double>(a.area()) + static_cast<double>(b.area()) - inter;
  48. return uni > 0.0 ? inter / uni : 0.0;
  49. }
  50. /// 射线法判断点是否在多边形内。
  51. inline bool point_in_polygon(double x, double y, const std::vector<Point>& polygon) {
  52. if (polygon.size() < 3) { return false; }
  53. bool inside = false;
  54. const std::size_t n = polygon.size();
  55. for (std::size_t i = 0; i < n; ++i) {
  56. const double x1 = polygon[i].first;
  57. const double y1 = polygon[i].second;
  58. const double x2 = polygon[(i + 1) % n].first;
  59. const double y2 = polygon[(i + 1) % n].second;
  60. if ((y1 > y) != (y2 > y)) {
  61. const double x_cross = x1 + (y - y1) * (x2 - x1) / (y2 - y1);
  62. if (x < x_cross) { inside = !inside; }
  63. }
  64. }
  65. return inside;
  66. }
  67. /// 归一化多边形 → 像素多边形。
  68. inline std::vector<Point> normalized_to_pixel_polygon(const std::vector<Point>& polygon, int width,
  69. int height) {
  70. std::vector<Point> out;
  71. if (polygon.empty()) {
  72. return {{0.0, 0.0},
  73. {static_cast<double>(width), 0.0},
  74. {static_cast<double>(width), static_cast<double>(height)},
  75. {0.0, static_cast<double>(height)}};
  76. }
  77. out.reserve(polygon.size());
  78. for (const Point& p : polygon) { out.push_back({p.first * width, p.second * height}); }
  79. return out;
  80. }
  81. /// 网格采样估计"框落在 ROI 内的面积比例"(0..1,与 Python box_in_polygon_ratio 一致)。
  82. inline double box_in_polygon_ratio(const Box& box, const std::vector<Point>& polygon,
  83. int samples_x = 7, int samples_y = 7) {
  84. if (polygon.size() < 3 || box.area() <= 0.0f) { return 0.0; }
  85. const int sx = samples_x > 1 ? samples_x : 1;
  86. const int sy = samples_y > 1 ? samples_y : 1;
  87. int hits = 0;
  88. int total = 0;
  89. for (int ix = 0; ix < sx; ++ix) {
  90. const double fx = (ix + 0.5) / sx;
  91. const double x = box.x1 + fx * box.width();
  92. for (int iy = 0; iy < sy; ++iy) {
  93. const double fy = (iy + 0.5) / sy;
  94. const double y = box.y1 + fy * box.height();
  95. total += 1;
  96. if (point_in_polygon(x, y, polygon)) { hits += 1; }
  97. }
  98. }
  99. return total == 0 ? 0.0 : static_cast<double>(hits) / static_cast<double>(total);
  100. }
  101. /// 非极大值抑制(per_class=true 时按类别分别抑制,YOLO 常见做法)。
  102. inline std::vector<Box> nms(std::vector<Box> boxes, double iou_threshold = 0.5,
  103. bool per_class = true) {
  104. std::stable_sort(boxes.begin(), boxes.end(),
  105. [](const Box& a, const Box& b) { return a.score > b.score; });
  106. std::vector<Box> keep;
  107. for (const Box& box : boxes) {
  108. bool suppressed = false;
  109. for (const Box& kept : keep) {
  110. if (per_class && kept.class_id != box.class_id) { continue; }
  111. if (box_iou(box, kept) > iou_threshold) { suppressed = true; break; }
  112. }
  113. if (!suppressed) { keep.push_back(box); }
  114. }
  115. return keep;
  116. }
  117. /// 检测框 → 场景编码。线程内使用;classify() 本身是 const,可多线程各自持有实例。
  118. class SceneClassifier {
  119. public:
  120. explicit SceneClassifier(const SceneParams& params = SceneParams());
  121. /// 类别名归一化(小写 + 空格→下划线 + '-'→下划线)。
  122. static std::string normalize(const std::string& name);
  123. /// 类别名 → 语义分组:container / bed / flatbed / ignore / unknown。
  124. std::string group_of(const std::string& class_name) const;
  125. std::vector<Point> roi_polygon(int width, int height) const;
  126. /// 主入口:frame_size 由调用方给出(像素)。
  127. Observation classify(const std::vector<Box>& detections, int width, int height, double ts,
  128. long frame_id = 0) const;
  129. private:
  130. Observation classify_direct(std::vector<Box>& boxes, int width, int height, double ts,
  131. long frame_id) const;
  132. Observation classify_composed(std::vector<Box>& boxes, int width, int height, double ts,
  133. long frame_id) const;
  134. static double overlap_x1d(double a1, double a2, double b1, double b2);
  135. bool support_quality(const Box& container, const Box& wagon, double* ov_x,
  136. double* quality) const;
  137. bool best_support(const Box& container, const std::vector<std::pair<Box, double>>& wagons,
  138. Box* wagon_out, double* score_out, double* ovx_out) const;
  139. SceneParams p_;
  140. std::vector<std::string> container_;
  141. std::vector<std::string> bed_;
  142. std::vector<std::string> flatbed_;
  143. std::vector<std::string> ignore_;
  144. };
  145. } // namespace portcv