2025-12-31 16:30:10 +08:00
|
|
|
|
#pragma once
|
2025-12-24 16:14:25 +08:00
|
|
|
|
|
|
|
|
|
|
#include <atomic>
|
2026-01-04 16:32:46 +08:00
|
|
|
|
#include <map> // [新增]
|
|
|
|
|
|
#include <memory>
|
2025-12-31 16:30:10 +08:00
|
|
|
|
#include <opencv2/opencv.hpp>
|
2025-12-24 16:14:25 +08:00
|
|
|
|
#include <string>
|
|
|
|
|
|
#include <thread>
|
2025-12-31 16:30:10 +08:00
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
|
|
#include "spdlog/spdlog.h"
|
2026-01-04 16:32:46 +08:00
|
|
|
|
#include "yoloDetector/yolo_detector.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
// [新增] 定义追踪车辆的结构体
|
|
|
|
|
|
struct TrackedVehicle {
|
|
|
|
|
|
int id; // 唯一ID
|
|
|
|
|
|
cv::Rect box; // 当前位置
|
|
|
|
|
|
float ev_score; // 新能源分数 (0.0 - 1.0), 越接近1.0越可能是新能源
|
|
|
|
|
|
int missing_frames; // 连续丢失帧数 (用于删除消失的车辆)
|
|
|
|
|
|
int last_class_id; // 上一次显示的类别ID (避免标签闪烁)
|
|
|
|
|
|
std::string label; // 当前显示的标签
|
2025-12-31 16:30:10 +08:00
|
|
|
|
};
|
2025-12-24 16:14:25 +08:00
|
|
|
|
|
|
|
|
|
|
class VideoPipeline {
|
|
|
|
|
|
public:
|
|
|
|
|
|
VideoPipeline();
|
|
|
|
|
|
~VideoPipeline();
|
|
|
|
|
|
|
|
|
|
|
|
void Start(const std::string& inputUrl, const std::string& outputUrl);
|
2026-01-04 14:52:14 +08:00
|
|
|
|
void StartTest(const std::string& filePath, const std::string& outputUrl);
|
2025-12-24 16:14:25 +08:00
|
|
|
|
void Stop();
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
2026-01-04 14:52:14 +08:00
|
|
|
|
void processLoop(std::string inputUrl, std::string outputUrl, bool isFileSource);
|
2025-12-24 16:14:25 +08:00
|
|
|
|
|
2026-01-04 16:32:46 +08:00
|
|
|
|
// [修改] 绘图函数现在接收追踪列表,而不是原始检测结果
|
|
|
|
|
|
void drawOverlay(cv::Mat& frame, const std::vector<TrackedVehicle>& trackedObjects);
|
|
|
|
|
|
|
|
|
|
|
|
// [新增] 核心:更新追踪和分数逻辑
|
|
|
|
|
|
void updateTracker(const std::vector<DetectionResult>& detections);
|
2025-12-24 16:14:25 +08:00
|
|
|
|
|
2026-01-04 16:32:46 +08:00
|
|
|
|
// [新增] 辅助函数:计算 IoU (交并比)
|
|
|
|
|
|
float computeIOU(const cv::Rect& box1, const cv::Rect& box2);
|
2025-12-24 16:14:25 +08:00
|
|
|
|
|
2026-01-04 15:47:02 +08:00
|
|
|
|
private:
|
2025-12-31 16:30:10 +08:00
|
|
|
|
std::atomic<bool> running_;
|
|
|
|
|
|
std::thread processingThread_;
|
2026-01-04 15:47:02 +08:00
|
|
|
|
std::unique_ptr<YoloDetector> detector_;
|
2026-01-04 16:32:46 +08:00
|
|
|
|
|
|
|
|
|
|
// [新增] 追踪列表,Key是ID
|
|
|
|
|
|
std::map<int, TrackedVehicle> tracks_;
|
|
|
|
|
|
int next_track_id_ = 0;
|
2025-12-24 16:14:25 +08:00
|
|
|
|
};
|