Files
SupervisorAI/common/camera_config.py

50 lines
1.4 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 dataclasses import dataclass
import json
import base64
import yaml
from utils.logger import get_logger
logger = get_logger(__name__)
@dataclass
class CameraConfig:
id: int
name: str
index: str
params: dict = None # 额外参数字典,可选
def parse_cameras_from_json(json_str: str) -> list[CameraConfig]:
"""从 JSON 字符串解析摄像头配置(支持 base64 编码)"""
try:
# 尝试 base64 解码
try:
decoded = base64.b64decode(json_str).decode('utf-8')
cameras_data = json.loads(decoded)
except Exception:
# 如果不是 base64直接解析 JSON
cameras_data = json.loads(json_str)
return [CameraConfig(
id=c["id"],
name=c.get("name", f"cam_{c['id']}"),
index=c.get("index"),
params=c.get("params")
) for c in cameras_data]
except Exception as e:
logger.error(f"[ERROR] Failed to parse cameras JSON: {e}")
return []
def parse_cameras_from_yaml(yaml_path: str) -> list[CameraConfig]:
"""从 YAML 文件解析摄像头配置"""
with open(yaml_path, "r", encoding="utf-8") as f:
cfg = yaml.safe_load(f)
return [CameraConfig(
id=c["id"],
name=c.get("name", f"cam_{c['id']}"),
index=c.get("index"),
params=c.get("params")
) for c in cfg.get("cameras", [])]