帮我解读代码 import numpy as np


def transform_pixel_to_camera_coordinates(pixel_bbox, K):
    """
    将像素坐标系中的边界框转换为相机坐标系中的三维坐标。
    :param pixel_bbox: 边界框，格式为 [x_min, y_min, x_max, y_max]，在像素坐标系中。
    :param K: 相机内参矩阵。
    :return: 相机坐标系中的边界框，格式为 [X_min, Y_min, Z_min, X_max, Y_max, Z_max]。
    """
    # 假设深度Z为1，简化为二维像素到相机坐标的转换
    bbox_camera = []
    for (x, y) in [(pixel_bbox[0], pixel_bbox[1]), (pixel_bbox[2], pixel_bbox[3])]:
        pixel_coord = np.array([x, y, 1])
        camera_coord = np.linalg.inv(K) @ pixel_coord
        # 假设Z=1，简化处理
        bbox_camera.append(camera_coord)
    return bbox_camera


def transform_bbox_to_world_coordinates(bbox_camera, R, T):
    """
    将相机坐标系中的边界框转换到世界坐标系中。
    :param bbox_camera: 边界框，格式为 [X_min, Y_min, Z_min, X_max, Y_max, Z_max]，在相机坐标系中。
    :param R: 旋转矩阵。
    :param T: 平移向量。
    :return: 转换后的世界坐标系中的边界框，格式为 [X_min, Y_min, X_max, Y_max]。
    """
    corners_camera = [
        np.array([bbox_camera[0][0], bbox_camera[0][1], 1]),  # 左下角
        np.array([bbox_camera[1][0], bbox_camera[0][1], 1]),  # 右下角
        np.array([bbox_camera[0][0], bbox_camera[1][1], 1]),  # 左上角
        np.array([bbox_camera[1][0], bbox_camera[1][1], 1])  # 右上角
    ]

    corners_world = [R @ corner + T for corner in corners_camera]

    # 获取转换后的最小/最大坐标
    x_coords = [corner[0] for corner in corners_world]
    y_coords = [corner[1] for corner in corners_world]

    x_min, x_max = min(x_coords), max(x_coords)
    y_min, y_max = min(y_coords), max(y_coords)

    return [x_min, y_min, x_max, y_max]


def calculate_iou_in_world_coordinates(bbox1, bbox2):
    """
    计算两个边界框在世界坐标系下的IoU（交并比）。
    :param bbox1: 第一个边界框，格式为 [x_min, y_min, x_max, y_max]。
    :param bbox2: 第二个边界框，格式为 [x_min, y_min, x_max, y_max]。
    :return: IoU值。
    """
    x1_inter = max(bbox1[0], bbox2[0])
    y1_inter = max(bbox1[1], bbox2[1])
    x2_inter = min(bbox1[2], bbox2[2])
    y2_inter = min(bbox1[3], bbox2[3])

    inter_area = max(0, x2_inter - x1_inter) * max(0, y2_inter - y1_inter)
    box1_area = (bbox1[2] - bbox1[0]) * (bbox1[3] - bbox1[1])
    box2_area = (bbox2[2] - bbox2[0]) * (bbox2[3] - bbox2[1])

    union_area = box1_area + box2_area - inter_area
    return inter_area / union_area if union_area != 0 else 0


def fuse_detections_with_spatial_info(detections, camera_params_list, K_list, iou_threshold=0.5):
    """
    基于空间位置关系对多个检测结果进行融合。
    :param detections: 各个摄像头的检测结果列表，每个摄像头的检测结果是一个字典，包含 'class', 'bbox', 'score'。
    :param camera_params_list: 每个摄像头的参数，包括旋转矩阵和平移向量。
    :param K_list: 每个摄像头的内参矩阵列表。
    :param iou_threshold: IoU阈值，用于决定是否匹配检测结果。
    :return: 融合后的检测结果列表。
    """
    fused_results = []

    # 为每个摄像头的检测结果转换坐标到世界坐标系
    transformed_detections = []
    for camera_params, K, camera_detections in zip(camera_params_list, K_list, detections):
        R = camera_params['R']
        T = camera_params['T']

        transformed_camera_detections = []
        for det in camera_detections:
            bbox_camera = transform_pixel_to_camera_coordinates(det['bbox'], K)
            world_bbox = transform_bbox_to_world_coordinates(bbox_camera, R, T)
            transformed_camera_detections.append({
                'class': det['class'],
                'bbox': world_bbox,
                'score': det['score']
            })
        transformed_detections.append(transformed_camera_detections)

    # 进行后融合：基于空间位置和IoU进行匹配
    for i, det1 in enumerate(transformed_detections[0]):  # 以第一个摄像头的检测结果为基准
        for j, det2 in enumerate(transformed_detections[1:]):  # 和后续摄像头的检测结果进行匹配
            for det in det2:  # 处理每个额外摄像头的检测结果
                iou = calculate_iou_in_world_coordinates(det1['bbox'], det['bbox'])
                if iou >= iou_threshold:
                    # 融合结果，简单地取位置和得分的平均值
                    fused_det = {
                        'class': det1['class'],  # 假设类别一致
                        'bbox': [(det1['bbox'][0] + det['bbox'][0]) / 2,
                                 (det1['bbox'][1] + det['bbox'][1]) / 2,
                                 (det1['bbox'][2] + det['bbox'][2]) / 2,
                                 (det1['bbox'][3] + det['bbox'][3]) / 2],
                        'score': (det1['score'] + det['score']) / 2
                    }
                    fused_results.append(fused_det)

    return fused_results


# 示例：假设我们有6个摄像头的检测结果及其对应的相机参数和内参矩阵
# RR 右后
camera_params_6 = {'R': np.array([[
    -0.7946453781783581,
    -0.0064885033806708885,
    -0.6070386385273595
],
    [
        0.6070365645452168,
        0.0025041867704249285,
        -0.7946695785781475
    ],
    [
        0.006676359609963185,
        -0.9999756277365266,
        0.0019488194361580154
    ]]), 'T': np.array([2.1453273891159825, -0.9530625162813355, 0.7020432611980693])}
# R 后
camera_params_9 = {'R': np.array([[
    -0.0006676047669953516,
    -0.0015234904055508956,
    -0.9999982332795548
],
    [
        0.9999980962343396,
        -0.001860605636019565,
        -0.0006647697797258865
    ],
    [
        -0.0018595849474219847,
        -0.999997217126407,
        0.0015247287532574867
    ], ]), 'T': np.array([-0.5723372425611466, 0.0751189296846082, 1.499456682155145])}
# RF 右前
camera_params_4 = {'R': np.array([[
    -0.81848876484322,
    -0.010328956758134184,
    0.574430444173612
],
    [
        -0.574491345819562,
        0.02525131294836496,
        -0.8181213582955591
    ],
    [
        -0.006054735230133498,
        -0.9996275451737968,
        -0.02660181578807105
    ], ]), 'T': np.array([2.0557450395718173, -0.9822761814035698, 1.0398692903227866])}
# F 前
camera_params_7 = {'R': np.array([[
    0.011361059083101385,
    0.008435124074239518,
    0.9998997645643264
],
    [
        -0.9999258834600966,
        -0.00438054328820253,
        0.011398348197699455
    ],
    [
        0.004476258657123747,
        -0.9999546574869047,
        0.008384720780309522
    ]]), 'T': np.array([1.909720557250928, 0.0028366604946625535, 1.41])}
# LR 左后
camera_params_8 = {'R': np.array([[
    0.7887082577967174,
    -0.01749919498841899,
    -0.6145181699941538
],
    [
        0.614147480580518,
        -0.022434107280436665,
        0.7888722937184941
    ],
    [
        -0.0275907838063693,
        -0.9995953251876196,
        -0.006946877049353361
    ], ]), 'T': np.array([2.17043543967502, 0.9706947489330744, 0.6753049515923943])}
# LR 左前
camera_params_5 = {'R': np.array([[
    0.8202889576766483,
    -0.012189147595506333,
    0.5718197808021571
],
    [
        -0.5717044185679133,
        0.01176073223144555,
        0.8203753280670469
    ],
    [
        -0.01672475912936122,
        -0.9998560878430629,
        0.002678627529914049
    ], ]), 'T': np.array([2.0661760035125787, 0.97088510959516, 1.045852910021044])}

K_6 = np.array([[
    1053.8655514378872,
    0.0,
    966.7036235224228
],
    [
        0.0,
        1054.2819787861624,
        543.3632555833593
    ],
    [
        0.0,
        0.0,
        1.0
    ]])  # 摄像头6的内参矩阵
K_9 = np.array([[
    1053.8655514378872,
    0.0,
    966.7036235224228
],
    [
        0.0,
        1054.2819787861624,
        543.3632555833593
    ],
    [
        0.0,
        0.0,
        1.0
    ]])  # 摄像头9的内参矩阵
K_4 = np.array([[
    1053.8655514378872,
    0.0,
    966.7036235224228
],
    [
        0.0,
        1054.2819787861624,
        543.3632555833593
    ],
    [
        0.0,
        0.0,
        1.0
    ]])  # 摄像头4的内参矩阵
K_7 = np.array([[
    2432.962646,
    0.0,
    1912.170654
],
    [
        0.0,
        2432.962646,
        1131.568726
    ],
    [
        0.0,
        0.0,
        1.0
    ]])  # 摄像头7的内参矩阵
K_8 = np.array([[
    1053.8655514378872,
    0.0,
    966.7036235224228
],
    [
        0.0,
        1054.2819787861624,
        543.3632555833593
    ],
    [
        0.0,
        0.0,
        1.0
    ]])  # 摄像头8的内参矩阵
K_5 = np.array([[
    1053.8655514378872,
    0.0,
    966.7036235224228
],
    [
        0.0,
        1054.2819787861624,
        543.3632555833593
    ],
    [
        0.0,
        0.0,
        1.0
    ]])  # 摄像头5的内参矩阵

detections_4 = [{'class': 'car', 'bbox': [37, 408, 528, 807], 'score': 0.9},
                {'class': 'car', 'bbox': [1167, 503, 1254, 567], 'score': 0.9},
                {'class': 'car', 'bbox': [1425, 483, 1514, 560], 'score': 0.9},
                {'class': 'car', 'bbox': [1590, 478, 1683, 548], 'score': 0.9}]
detections_5 = [{'class': 'truck', 'bbox': [1014, 360, 1489, 644], 'score': 0.85},
                {'class': 'truck', 'bbox': [1542, 484, 1682, 577], 'score': 0.85}]
detections_6 = [{'class': 'car', 'bbox': [96, 487, 178, 567], 'score': 0.88},
                {'class': 'car', 'bbox': [257, 488, 352, 557], 'score': 0.88},
                {'class': 'car', 'bbox': [632, 483, 954, 598], 'score': 0.88}]
detections_7 = [{'class': 'car', 'bbox': [1189, 536, 1683, 913], 'score': 0.87},
                {'class': 'truck', 'bbox': [52, 394, 404, 689], 'score': 0.85},
                {'class': 'truck', 'bbox': [470, 525, 616, 627], 'score': 0.85}]
detections_8 = []
detections_9 = []

# 将6个检测器的结果进行融合
fused_results = fuse_detections_with_spatial_info(
    [detections_4, detections_5, detections_6, detections_7, detections_8, detections_9],
    [camera_params_4, camera_params_5, camera_params_6, camera_params_7, camera_params_8, camera_params_9],
    [K_4, K_5, K_6, K_7, K_8, K_9],
    iou_threshold=0.5
)

print(fused_results)