|
@@ -0,0 +1,39 @@
|
|
|
+#include "nodes/analyze/function/cross.hpp"
|
|
|
+#include "opencv2/opencv.hpp"
|
|
|
+#include <unordered_map>
|
|
|
+data::BoxArray person_cross_line(const data::BoxArray& boxes, const cv::Point& line_start, const cv::Point& line_end)
|
|
|
+{
|
|
|
+ // 记录之前人在线的那一侧
|
|
|
+ static std::unordered_map<int, Side> person_side;
|
|
|
+ data::BoxArray result;
|
|
|
+ for (auto& box : boxes)
|
|
|
+ {
|
|
|
+ cv::Point bottom_center((box.left + box.right) / 2, box.bottom);
|
|
|
+ // > 0 代表在线段的左侧
|
|
|
+ if ((center.x - line_start.x) * (line_end.y - line_start.y) - (center.y - line_start.y) * (line_end.x - line_start.x) > 0)
|
|
|
+ {
|
|
|
+ if (person_side.find(box.class_id) == person_side.end())
|
|
|
+ {
|
|
|
+ person_side[box.class_id] = Side::LEFT;
|
|
|
+ }
|
|
|
+ else if (person_side[box.class_id] == Side::RIGHT)
|
|
|
+ {
|
|
|
+ person_side[box.class_id] = Side::LEFT;
|
|
|
+ result.push_back(box);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ if (person_side.find(box.class_id) == person_side.end())
|
|
|
+ {
|
|
|
+ person_side[box.class_id] = Side::RIGHT;
|
|
|
+ }
|
|
|
+ else if (person_side[box.class_id] == Side::LEFT)
|
|
|
+ {
|
|
|
+ person_side[box.class_id] = Side::RIGHT;
|
|
|
+ result.push_back(box);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+}
|