import os import json from datetime import datetime # 全局配置字典 CAMERA_CONFIG_MAP = {} # {sn: config_dict} CUT_CONFIG_MAP = {} # {filename_without_ext: config_dict} DIRECTION_CAMERA = {} def load_camera_configs(config_dir="./config/camera"): """ 加载 camera 配置,按 sn 建立映射 """ if not os.path.exists(config_dir): print(f"[ERROR] Camera config directory does not exist: {config_dir}") return for filename in os.listdir(config_dir): if filename.endswith(".json"): file_path = os.path.join(config_dir, filename) try: with open(file_path, "r", encoding="utf-8") as f: config = json.load(f) sn = config.get("sn") if sn: CAMERA_CONFIG_MAP[sn] = config DIRECTION_CAMERA[config.get("direction")] = sn print(f"Loaded camera config: {sn}") else: print(f"[WARN] No 'sn' found in {filename}") except Exception as e: print(f"[ERROR] Failed to load {file_path}: {e}") load_camera_configs() def load_cut_configs(config_dir="./config/cut"): """ 加载 cut 配置,按文件名(不含扩展名)建立映射 """ if not os.path.exists(config_dir): print(f"[ERROR] Cut config directory does not exist: {config_dir}") return for filename in os.listdir(config_dir): if filename.endswith(".json"): file_path = os.path.join(config_dir, filename) name_without_ext = os.path.splitext(filename)[0] try: with open(file_path, "r", encoding="utf-8") as f: config = json.load(f) CUT_CONFIG_MAP[name_without_ext] = config print(f"Loaded cut config: {name_without_ext}") except Exception as e: print(f"[ERROR] Failed to load {file_path}: {e}") def load_configs(): CAMERA_CONFIG_MAP = {} # {sn: config_dict} CUT_CONFIG_MAP = {} # {filename_without_ext: config_dict} DIRECTION_CAMERA = {} load_camera_configs() load_cut_configs() load_configs() def save_path(type,end): # 创建保存目录 today = datetime.now().strftime("%Y-%m-%d") output_dir = os.path.join(".", "image",today,type) os.makedirs(output_dir, exist_ok=True) timestamp = datetime.now().strftime("%H%M%S%f")[:-3] # 精确到毫秒 file_name_prefix = f"{timestamp}_"+end # 构造完整路径 save_path = os.path.join(output_dir,file_name_prefix) return save_path