generated from guanyuankai/bonus-edge-proxy
75 lines
2.3 KiB
C++
75 lines
2.3 KiB
C++
#pragma once
|
|
|
|
#include <atomic>
|
|
#include <condition_variable>
|
|
#include <filesystem> // C++17 用于创建文件夹
|
|
#include <iomanip> // 用于时间格式化
|
|
#include <map>
|
|
#include <memory>
|
|
#include <mutex>
|
|
#include <opencv2/opencv.hpp>
|
|
#include <queue>
|
|
#include <sstream>
|
|
#include <string>
|
|
#include <thread>
|
|
#include <vector>
|
|
|
|
#include "spdlog/spdlog.h"
|
|
#include "yoloDetector/yolo_detector.hpp"
|
|
|
|
class VideoPipeline {
|
|
public:
|
|
VideoPipeline();
|
|
~VideoPipeline();
|
|
|
|
void Start(const std::string& inputUrl, const std::string& outputUrl);
|
|
void StartTest(const std::string& filePath, const std::string& outputUrl);
|
|
void Stop();
|
|
void setTripwire(const TripwireConfig& config);
|
|
|
|
private:
|
|
void processLoop(std::string inputUrl, std::string outputUrl, bool isFileSource);
|
|
|
|
// [新增] 工作线程函数
|
|
void inferenceWorker();
|
|
|
|
void drawOverlay(cv::Mat& frame, const std::vector<TrackedVehicle>& trackedObjects);
|
|
void updateTracker(const FrameData& frameData);
|
|
float computeIOU(const cv::Rect& box1, const cv::Rect& box2);
|
|
|
|
bool isLineCrossed(const cv::Point& p1, const cv::Point& p2, const cv::Point& line_start,
|
|
const cv::Point& line_end);
|
|
|
|
void processCrossing(const TrackedVehicle& vehicle, const cv::Mat& frame);
|
|
|
|
private:
|
|
std::atomic<bool> running_;
|
|
std::thread processingThread_; // 主处理线程
|
|
std::unique_ptr<YoloDetector> detector_;
|
|
|
|
// 追踪相关
|
|
std::map<int, TrackedVehicle> tracks_;
|
|
int next_track_id_ = 0;
|
|
|
|
// === [新增] 多线程并行处理相关 ===
|
|
std::vector<std::thread> worker_threads_; // 3个工作线程
|
|
|
|
// 输入队列 (待检测)
|
|
std::queue<FrameData> input_queue_;
|
|
std::mutex input_mtx_;
|
|
std::condition_variable input_cv_;
|
|
const size_t MAX_INPUT_QUEUE_SIZE = 5; // 限制缓冲大小,防止内存爆炸
|
|
|
|
// 输出缓冲区 (已检测,等待排序)
|
|
// 使用 map 自动按 frame_id 排序,解决乱序问题
|
|
std::map<long, FrameData> output_buffer_;
|
|
std::mutex output_mtx_;
|
|
std::condition_variable output_cv_;
|
|
|
|
TripwireConfig tripwire_config_; // 配置数据
|
|
cv::Point tripwire_p1_pixel_; // 缓存:转换后的起点像素坐标
|
|
cv::Point tripwire_p2_pixel_; // 缓存:转换后的终点像素坐标
|
|
int current_frame_width_ = 0; // 缓存:当前分辨率宽
|
|
int current_frame_height_ = 0; // 缓存:当前分辨率高
|
|
};
|