362 lines
9.4 KiB
Markdown
362 lines
9.4 KiB
Markdown
# API 参考文档
|
||
|
||
## 配置参数
|
||
|
||
### RTSP 配置
|
||
RTSP 相关配置位于配置文件的 `rtsp` 部分:
|
||
|
||
```yaml
|
||
rtsp:
|
||
url: "rtsp://example/stream" # RTSP 流地址
|
||
buffer_size: 30 # 缓冲区大小,影响延迟和流畅度
|
||
max_retry_count: 3 # 最大重连次数
|
||
retry_interval_ms: 1000 # 重连间隔(毫秒)
|
||
frame_timeout_ms: 5000 # 读取帧超时时间(毫秒)
|
||
target_fps: 30.0 # 目标帧率,用于控制读取速度
|
||
```
|
||
|
||
#### 参数说明
|
||
- `url`: RTSP 流地址,必须是有效的 RTSP URL
|
||
- `buffer_size`:
|
||
- 类型:整数
|
||
- 默认值:30
|
||
- 说明:设置 RTSP 缓冲区大小,较大的值会增加延迟但提高流畅度,较小的值会降低延迟但可能造成卡顿
|
||
|
||
- `max_retry_count`:
|
||
- 类型:整数
|
||
- 默认值:3
|
||
- 说明:连接断开后的最大重试次数,超过此次数将停止重连
|
||
|
||
- `retry_interval_ms`:
|
||
- 类型:整数
|
||
- 默认值:1000
|
||
- 说明:重连尝试之间的间隔时间(毫秒),避免频繁重连
|
||
|
||
- `frame_timeout_ms`:
|
||
- 类型:整数
|
||
- 默认值:5000
|
||
- 说明:读取单帧的超时时间,超过此时间将视为读取失败
|
||
|
||
- `target_fps`:
|
||
- 类型:浮点数
|
||
- 默认值:30.0
|
||
- 说明:目标帧率,用于控制读取速度,设置为 0 则不限制帧率
|
||
|
||
### YOLO 模型配置
|
||
YOLO 模型相关配置位于配置文件的 `inference.model` 部分:
|
||
|
||
```yaml
|
||
inference:
|
||
model:
|
||
onnx_path: "/app/models/yolov8n.onnx" # ONNX模型路径
|
||
engine_path: "/app/models/yolov8n.engine" # TensorRT引擎路径
|
||
input_shape: [3, 640, 640] # YOLOv8n的输入尺寸
|
||
precision: "FP16" # FP32/FP16/INT8
|
||
version: "yolov8" # 可选:YOLO版本信息
|
||
labels: ["person", "car", "truck"] # 可选:模型标签列表
|
||
threshold:
|
||
conf: 0.5 # 置信度阈值
|
||
nms: 0.45 # NMS阈值
|
||
gpu_id: 0 # GPU设备ID
|
||
```
|
||
|
||
#### 参数说明
|
||
- `onnx_path`: ONNX模型文件路径,必须指定
|
||
- `engine_path`: TensorRT引擎文件路径,必须指定
|
||
- `input_shape`:
|
||
- 类型:整数数组 [channels, height, width]
|
||
- 默认值:[3, 640, 640]
|
||
- 说明:模型输入尺寸,必须与模型定义匹配
|
||
|
||
- `precision`:
|
||
- 类型:字符串
|
||
- 默认值:"FP16"
|
||
- 可选值:["FP32", "FP16", "INT8"]
|
||
- 说明:模型精度模式,影响推理速度和精度
|
||
|
||
- `version`:
|
||
- 类型:字符串
|
||
- 默认值:空
|
||
- 可选值:任意字符串
|
||
- 说明:YOLO模型版本信息,用于记录和管理
|
||
|
||
- `labels`:
|
||
- 类型:字符串数组
|
||
- 默认值:空
|
||
- 说明:模型支持的标签列表,按class_id顺序排列
|
||
|
||
- `threshold.conf`:
|
||
- 类型:浮点数
|
||
- 默认值:0.5
|
||
- 范围:[0.0, 1.0]
|
||
- 说明:检测置信度阈值,低于此值的检测框会被过滤
|
||
|
||
- `threshold.nms`:
|
||
- 类型:浮点数
|
||
- 默认值:0.45
|
||
- 范围:[0.0, 1.0]
|
||
- 说明:非极大值抑制阈值,用于过滤重叠框
|
||
|
||
- `gpu_id`:
|
||
- 类型:整数
|
||
- 默认值:0
|
||
- 说明:使用的GPU设备ID
|
||
|
||
## 类说明
|
||
|
||
### RtspReader
|
||
RTSP 流读取器,用于从 RTSP 流中读取视频帧。
|
||
|
||
#### 主要功能
|
||
- 自动重连机制
|
||
- 帧率控制
|
||
- 超时处理
|
||
- 支持配置缓冲区大小
|
||
|
||
#### 示例用法
|
||
```cpp
|
||
// 创建配置
|
||
RtspReader::Config config;
|
||
config.buffer_size = 30;
|
||
config.max_retry_count = 3;
|
||
config.retry_interval_ms = 1000;
|
||
config.frame_timeout_ms = 5000;
|
||
config.target_fps = 30.0f;
|
||
|
||
// 创建读取器
|
||
RtspReader reader(config);
|
||
|
||
// 连接到 RTSP 流
|
||
if (reader.connect("rtsp://example/stream")) {
|
||
cv::Mat frame;
|
||
while (reader.read(frame)) {
|
||
// 处理帧
|
||
}
|
||
}
|
||
```
|
||
|
||
## 1. 输入模块 API
|
||
|
||
### 1.1 InputManager
|
||
```cpp
|
||
class InputManager {
|
||
public:
|
||
// 添加输入源
|
||
bool addSource(const std::string& name, const InputConfig& config);
|
||
|
||
// 获取下一帧
|
||
bool getNextFrames(std::vector<FramePtr>& frames);
|
||
|
||
// 获取输入源状态
|
||
SourceStatus getSourceStatus(const std::string& name);
|
||
};
|
||
```
|
||
|
||
### 1.2 RtspReader
|
||
```cpp
|
||
class RtspReader {
|
||
public:
|
||
// 连接RTSP流
|
||
bool connect(const std::string& url);
|
||
|
||
// 读取帧
|
||
bool read(FramePtr& frame);
|
||
};
|
||
```
|
||
|
||
## 2. 推理模块 API
|
||
|
||
### 2.1 TrtInference
|
||
```cpp
|
||
class TrtInference {
|
||
public:
|
||
// 加载模型
|
||
bool loadEngine(const std::string& enginePath);
|
||
|
||
// 批量推理
|
||
bool infer(const std::vector<FramePtr>& inputs, std::vector<DetectionResult>& results);
|
||
};
|
||
```
|
||
|
||
## 3. 渲染模块 API
|
||
|
||
### 3.1 FrameDrawer
|
||
```cpp
|
||
class FrameDrawer {
|
||
public:
|
||
// 绘制检测结果
|
||
void drawDetections(FramePtr& frame, const DetectionResult& result);
|
||
|
||
// 添加文本信息
|
||
void addText(FramePtr& frame, const std::string& text, const Point& pos);
|
||
};
|
||
```
|
||
|
||
## 4. 输出模块 API
|
||
|
||
### 4.1 OutputManager
|
||
|
||
输出管理器,负责管理多路输出目标和输入源映射。
|
||
|
||
### 类定义
|
||
|
||
```cpp
|
||
class OutputManager {
|
||
public:
|
||
OutputManager();
|
||
~OutputManager();
|
||
|
||
// 禁用拷贝
|
||
OutputManager(const OutputManager&) = delete;
|
||
OutputManager& operator=(const OutputManager&) = delete;
|
||
|
||
// 添加输出目标
|
||
bool addTarget(const OutputTargetConfig& config);
|
||
|
||
// 写入帧
|
||
bool writeFrames(const cv::Mat& frame);
|
||
bool writeFrames(const std::string& source_name, const cv::Mat& frame);
|
||
|
||
// 获取目标状态
|
||
bool getTargetStatus(const std::string& name, std::string& error_msg) const;
|
||
|
||
// 获取目标数量
|
||
size_t getTargetCount() const;
|
||
|
||
// 获取所有目标名称
|
||
std::vector<std::string> getTargetNames() const;
|
||
|
||
// 移除目标
|
||
bool removeTarget(const std::string& name);
|
||
|
||
// 清理所有资源
|
||
void cleanup();
|
||
|
||
// 添加输入源到输出目标的映射
|
||
bool addSourceTargetMapping(const std::string& source_name,
|
||
const std::vector<std::string>& target_names);
|
||
|
||
// 移除输入源的映射
|
||
bool removeSourceMapping(const std::string& source_name);
|
||
};
|
||
```
|
||
|
||
### 配置结构
|
||
|
||
```cpp
|
||
struct OutputTargetConfig {
|
||
std::string type; // "video" 或 "rtsp"
|
||
std::string name; // 目标名称
|
||
std::string path; // 输出路径
|
||
int fps{30}; // 帧率
|
||
std::string codec; // 编码器
|
||
int bitrate{4000000}; // 比特率
|
||
};
|
||
```
|
||
|
||
### 方法说明
|
||
|
||
#### 构造函数和析构函数
|
||
|
||
- `OutputManager()`: 创建输出管理器实例
|
||
- `~OutputManager()`: 析构时自动清理所有资源
|
||
|
||
#### 目标管理
|
||
|
||
- `bool addTarget(const OutputTargetConfig& config)`
|
||
- 添加新的输出目标
|
||
- 参数:输出目标配置
|
||
- 返回:添加是否成功
|
||
- 线程安全:是
|
||
|
||
- `bool removeTarget(const std::string& name)`
|
||
- 移除指定的输出目标
|
||
- 参数:目标名称
|
||
- 返回:移除是否成功
|
||
- 线程安全:是
|
||
|
||
#### 帧写入
|
||
|
||
- `bool writeFrames(const cv::Mat& frame)`
|
||
- 将帧写入所有输出目标
|
||
- 参数:要写入的帧
|
||
- 返回:写入是否成功
|
||
- 线程安全:是
|
||
|
||
- `bool writeFrames(const std::string& source_name, const cv::Mat& frame)`
|
||
- 将帧写入指定输入源映射的输出目标
|
||
- 参数:
|
||
- source_name: 输入源名称
|
||
- frame: 要写入的帧
|
||
- 返回:写入是否成功
|
||
- 线程安全:是
|
||
|
||
#### 状态查询
|
||
|
||
- `bool getTargetStatus(const std::string& name, std::string& error_msg) const`
|
||
- 获取指定目标的状态
|
||
- 参数:
|
||
- name: 目标名称
|
||
- error_msg: 错误信息输出
|
||
- 返回:目标是否正常
|
||
- 线程安全:是
|
||
|
||
- `size_t getTargetCount() const`
|
||
- 获取当前输出目标数量
|
||
- 返回:目标数量
|
||
- 线程安全:是
|
||
|
||
- `std::vector<std::string> getTargetNames() const`
|
||
- 获取所有输出目标的名称
|
||
- 返回:目标名称列表
|
||
- 线程安全:是
|
||
|
||
#### 映射管理
|
||
|
||
- `bool addSourceTargetMapping(const std::string& source_name, const std::vector<std::string>& target_names)`
|
||
- 添加输入源到输出目标的映射
|
||
- 参数:
|
||
- source_name: 输入源名称
|
||
- target_names: 输出目标名称列表
|
||
- 返回:添加是否成功
|
||
- 线程安全:是
|
||
|
||
- `bool removeSourceMapping(const std::string& source_name)`
|
||
- 移除输入源的映射关系
|
||
- 参数:输入源名称
|
||
- 返回:移除是否成功
|
||
- 线程安全:是
|
||
|
||
#### 资源管理
|
||
|
||
- `void cleanup()`
|
||
- 清理所有资源
|
||
- 线程安全:是
|
||
|
||
### 错误处理
|
||
|
||
所有可能失败的操作都返回bool值表示成功或失败。对于需要详细错误信息的场景,可以通过`getTargetStatus`获取具体的错误信息。
|
||
|
||
### 线程安全性
|
||
|
||
所有公共接口都是线程安全的,内部使用互斥锁保护共享资源。支持多线程并发写入和状态查询。
|
||
|
||
### 使用示例
|
||
|
||
参见README.md中的使用示例部分。
|
||
|
||
## 5. 配置模块 API
|
||
|
||
### 5.1 ConfigParser
|
||
```cpp
|
||
class ConfigParser {
|
||
public:
|
||
// 加载配置文件
|
||
bool load(const std::string& configPath);
|
||
|
||
// 获取配置项
|
||
template<typename T>
|
||
T getValue(const std::string& key, const T& defaultValue);
|
||
};
|
||
```
|