rtsp_tensorrt/pipeline/render/renderer.hpp

107 lines
3.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#pragma once
#include <opencv2/opencv.hpp>
#include <string>
#include <vector>
#include <memory>
#include <map>
#include <chrono>
#include "../inference/types.hpp" // 添加类型定义头文件
namespace pipeline {
namespace renderer { // 添加renderer命名空间
// 渲染器配置结构
struct RendererConfig {
bool enable; // 控制是否启用渲染功能,从配置文件中读取
std::string window_name = "Detection Results";
int window_width = 1280;
int window_height = 720;
bool fullscreen = false;
bool test_mode = false; // 添加测试模式标志
// 类别样式配置
struct ClassStyle {
cv::Scalar box_color = cv::Scalar(0, 255, 0); // 默认绿色
cv::Scalar text_color = cv::Scalar(255, 255, 255); // 默认白色
float transparency = 0.0f; // 默认不透明
int box_thickness = 2; // 默认线宽
double font_scale = 0.5; // 默认字体大小
int font_thickness = 1; // 默认字体粗细
};
// 每个类别的样式映射
std::map<std::string, ClassStyle> class_styles;
// 默认样式(当类别未指定时使用)
ClassStyle default_style;
// 性能指标设置
struct {
bool show_fps = true;
bool show_inference_time = true;
bool show_gpu_usage = true;
int update_interval_ms = 1000;
} metrics;
};
// 检测结果结构
struct DetectionResult {
cv::Rect bbox; // 边界框
float confidence; // 置信度
int class_id; // 类别ID
std::string label; // 类别标签
};
} // namespace renderer
// 性能指标结构保持在pipeline命名空间
struct PerformanceMetrics {
float fps; // 帧率
float inference_time_ms; // 推理时间(毫秒)
float gpu_usage_percent; // GPU使用率
};
class Renderer {
public:
Renderer() = default;
~Renderer() = default;
// 禁用拷贝构造和赋值操作
Renderer(const Renderer&) = delete;
Renderer& operator=(const Renderer&) = delete;
// 初始化渲染器
bool init(const renderer::RendererConfig& config);
// 渲染单帧图像
bool render(const cv::Mat& frame,
const std::vector<renderer::DetectionResult>& results,
const PerformanceMetrics& metrics);
// 清理资源
void cleanup();
// 获取最后渲染的帧(用于测试)
cv::Mat getLastFrame() const { return last_frame_; }
private:
// 内部渲染方法
void drawDetections(cv::Mat& frame, const std::vector<renderer::DetectionResult>& results);
void drawMetrics(cv::Mat& frame, const PerformanceMetrics& metrics);
void drawText(cv::Mat& frame, const std::string& text, const cv::Point& pos,
const renderer::RendererConfig::ClassStyle& style);
// 配置和状态
renderer::RendererConfig config_;
bool initialized_ = false;
// 性能监控
std::chrono::steady_clock::time_point last_metrics_update_;
float current_fps_ = 0.0f;
// 测试相关
cv::Mat last_frame_; // 保存最后渲染的帧
};
} // namespace pipeline