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