Files
HighwayEventDet/pipeline/pipeline_data.py
T
zimoyin 963e858341 feat(pipeline): 添加Pipeline与Handle设计框架
- doc: 各处理器独立实现特定功能,支持解耦合和复用。同步处理保证依赖性,异步处理提升性能,异步处理作为管道终端操作后续将引入BUS机制,作为事件的发布者。统一的数据存取接口,内置类型转换和验证机制
- 创建BaseProcessor抽象基类定义统一处理接口
- 实现video_yolo_iterator和video_yolo_detect_iterator数据源
- 构建Pipeline核心类管理同步和异步处理器
- 设计PipelineData数据包承载检测结果和缓存信息
- 支持同步和异步处理器的混合执行模式
- 提供数据缓存管理和内部数据存储功能
2026-01-10 09:40:02 +08:00

78 lines
2.7 KiB
Python
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.
from typing import List, Optional, Dict, Any
import copy
import cv2
from ultralytics.engine.results import Results
class PipelineData:
"""管道传输的数据包,承载YOLO检测结果、缓存及辅助信息"""
def __init__(self):
# 当前帧的YOLO检测结果
self.current_result: Optional[Results] = None
# 检测结果缓存(list[Results]
self.result_cache: List[Results] = []
# 辅助信息:帧数据、时间戳、帧序号(便于调试/扩展)
self.frame: Optional[cv2.Mat] = None
self.timestamp: Optional[float] = None
self.frame_idx: int = 0
# 内部数据存储map
self.internal_map: Dict[str, Any] = {}
def add_to_cache(self, result: Results, max_cache_size: int = 125) -> None:
"""将结果加入缓存,控制缓存最大长度"""
self.result_cache.append(result)
if len(self.result_cache) > max_cache_size:
self.result_cache.pop(0) # 移除最旧的缓存
def set_to_cache(self, result_cache: List[Results]) -> None:
"""设置缓存"""
self.result_cache = result_cache
def clear_cache(self) -> None:
"""清空缓存"""
self.result_cache.clear()
def copy(self):
"""创建当前PipelineData的副本"""
new_data = PipelineData()
new_data.current_result = self.current_result
new_data.result_cache = self.result_cache.copy()
new_data.frame = self.frame.copy() if self.frame is not None else None
new_data.timestamp = self.timestamp
new_data.frame_idx = self.frame_idx
new_data.internal_map = self.internal_map.copy()
return new_data
def put_data(self, data_type: str, key: str, value: Any) -> None:
"""存储数据到内部map
Args:
data_type: 数据类型标识
key: 数据键名
value: 数据值
"""
map_key = f"{data_type}:{key}"
self.internal_map[map_key] = value
def get_data(self, data_label: str, key: str, target_type=None):
"""从内部map获取数据
Args:
data_label: 数据来源标识
key: 数据键名
target_type: 目标类型,用于类型转换
Returns:
获取的数据,如果不存在则返回None
"""
map_key = f"{data_label}:{key}"
value = self.internal_map.get(map_key)
if value is not None and target_type is not None:
try:
value = target_type(value)
except (ValueError, TypeError):
# 如果类型转换失败,返回原始值
pass
return value