mirror_fold.py_utils_0207curso

📅 发布时间:2026/7/8 17:58:25 👁️ 浏览次数:
mirror_fold.py_utils_0207curso
import osimport randomimport timefrom typing import Dict, Optional, Tupleimport numpy as np# 后视镜折叠场景配置请按你的4种分辨率填写# key: (width, height) value: (x1, y1, x2, y2) 车辆黑色区域在原图上的像素坐标MIRROR_FOLD_CAR_BOXES: Dict[Tuple[int, int], Tuple[int, int, int, int]] {# (960, 1088): (x1, y1, x2, y2),# (1280, 720): (x1, y1, x2, y2),# (1920, 1080): (x1, y1, x2, y2),# (1440, 1080): (x1, y1, x2, y2),}# 若分辨率不在上表中可用比例兜底0-1None 表示不启用兜底MIRROR_FOLD_CAR_BOX_RATIOS: Optional[Tuple[float, float, float, float]] None# 是否启用后视镜折叠增强MIRROR_FOLD_ENABLE TrueMIRROR_FOLD_PROB 1.0 # 1.0每次都做0.550%概率# 粉色色块颜色MIRROR_FOLD_PINK_COLOR_BGR (255, 0, 255) # OpenCV/BGRMIRROR_FOLD_PINK_COLOR_RGB (255, 0, 255) # PIL/RGB# 语义分割标签填充值按你的数据集语义修改FSD_PINK_VALUE 1 # FSD中粉色区域视为可行驶RM_PINK_VALUE 0 # RM中粉色区域视为背景# Debug保存预处理后可视化MIRROR_FOLD_DEBUG_SAVE FalseMIRROR_FOLD_DEBUG_DIR runs/mirror_fold_debugMIRROR_FOLD_DEBUG_MAX 200MIRROR_FOLD_DEBUG_EVERY 1MIRROR_FOLD_DEBUG_ALPHA 0.45_debug_counts {det: 0, fsd: 0, rm: 0}def _clamp_box(x1: int, y1: int, x2: int, y2: int, w: int, h: int) - Optional[Tuple[int, int, int, int]]:x1 int(max(0, min(x1, w)))x2 int(max(0, min(x2, w)))y1 int(max(0, min(y1, h)))y2 int(max(0, min(y2, h)))if x2 x1 or y2 y1:return Nonereturn x1, y1, x2, y2def get_car_box_for_shape(width: int, height: int) - Optional[Tuple[int, int, int, int]]:car_box MIRROR_FOLD_CAR_BOXES.get((width, height))if car_box is None and MIRROR_FOLD_CAR_BOX_RATIOS is not None:x1r, y1r, x2r, y2r MIRROR_FOLD_CAR_BOX_RATIOScar_box (int(x1r * width), int(y1r * height), int(x2r * width), int(y2r * height))if car_box is None:return Nonereturn _clamp_box(*car_box, wwidth, hheight)def build_pink_mask(width: int, height: int, car_box: Tuple[int, int, int, int]) - Optional[np.ndarray]:x1, y1, x2, y2 _clamp_box(*car_box, wwidth, hheight) or (None, None, None, None)if x1 is None:return Nonemask np.zeros((height, width), dtypebool)if x1 0:mask[y1:y2, :x1] Trueif x2 width:mask[y1:y2, x2:] Truereturn maskdef should_apply_mirror_fold() - bool:return MIRROR_FOLD_ENABLE and random.random() MIRROR_FOLD_PROBdef get_debug_save_path(branch: str, img_path: str, suffix: str jpg) - Optional[str]:if not MIRROR_FOLD_DEBUG_SAVE:return Nonecount _debug_counts.get(branch, 0)if count MIRROR_FOLD_DEBUG_MAX:return Noneif MIRROR_FOLD_DEBUG_EVERY 1 and (count % MIRROR_FOLD_DEBUG_EVERY) ! 0:_debug_counts[branch] count 1return None_debug_counts[branch] count 1base os.path.splitext(os.path.basename(img_path))[0]out_dir os.path.join(MIRROR_FOLD_DEBUG_DIR, branch)os.makedirs(out_dir, exist_okTrue)ts int(time.time() * 1000)return os.path.join(out_dir, f{base}_{count:06d}_{ts}.{suffix})