mirror_fold.py_utils_0207curso

Source

import os

import random

import time

from typing import Dict, Optional, Tuple

import 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-1),None 表示不启用兜底

MIRROR_FOLD_CAR_BOX_RATIOS: Optional[Tuple[float, float, float, float]] = None

# 是否启用后视镜折叠增强

MIRROR_FOLD_ENABLE = True

MIRROR_FOLD_PROB = 1.0  # 1.0=每次都做,0.5=50%概率

# 粉色色块颜色

MIRROR_FOLD_PINK_COLOR_BGR = (255, 0, 255)  # OpenCV/BGR

MIRROR_FOLD_PINK_COLOR_RGB = (255, 0, 255)  # PIL/RGB

# 语义分割标签填充值(按你的数据集语义修改)

FSD_PINK_VALUE = 1  # FSD中粉色区域视为可行驶

RM_PINK_VALUE = 0   # RM中粉色区域视为背景

# Debug保存(预处理后可视化)

MIRROR_FOLD_DEBUG_SAVE = False

MIRROR_FOLD_DEBUG_DIR = "runs/mirror_fold_debug"

MIRROR_FOLD_DEBUG_MAX = 200

MIRROR_FOLD_DEBUG_EVERY = 1

MIRROR_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 None

    return x1, y1, x2, y2


 

def 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_RATIOS

        car_box = (int(x1r * width), int(y1r * height), int(x2r * width), int(y2r * height))

    if car_box is None:

        return None

    return _clamp_box(*car_box, w=width, h=height)


 

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, w=width, h=height) or (None, None, None, None)

    if x1 is None:

        return None

    mask = np.zeros((height, width), dtype=bool)

    if x1 > 0:

        mask[y1:y2, :x1] = True

    if x2 < width:

        mask[y1:y2, x2:] = True

    return mask


 

def should_apply_mirror_fold() -> bool:

    return MIRROR_FOLD_ENABLE and random.random() < MIRROR_FOLD_PROB


 

def get_debug_save_path(branch: str, img_path: str, suffix: str = "jpg") -> Optional[str]:

    if not MIRROR_FOLD_DEBUG_SAVE:

        return None

    count = _debug_counts.get(branch, 0)

    if count >= MIRROR_FOLD_DEBUG_MAX:

        return None

    if MIRROR_FOLD_DEBUG_EVERY > 1 and (count % MIRROR_FOLD_DEBUG_EVERY) != 0:

        _debug_counts[branch] = count + 1

        return None

    _debug_counts[branch] = count + 1

    base = os.path.splitext(os.path.basename(img_path))[0]

    out_dir = os.path.join(MIRROR_FOLD_DEBUG_DIR, branch)

    os.makedirs(out_dir, exist_ok=True)

    ts = int(time.time() * 1000)

    return os.path.join(out_dir, f"{base}_{count:06d}_{ts}.{suffix}")