""" Visualize 3D boxes in Image space. Align the setting in mmdetection3d: * Convert 3D box in nuplan coordinates to camera coordinates. * draw 3D box in camera. """ import cv2 import numpy as np from navsim.common.extraction.helpers import transformation def rotation_3d_in_axis(points, angles, axis=0): """Rotate points by angles according to axis. Args: points (torch.Tensor): Points of shape (N, M, 3). angles (torch.Tensor): Vector of angles in shape (N,) axis (int, optional): The axis to be rotated. Defaults to 0. Raises: ValueError: when the axis is not in range [0, 1, 2], it will \ raise value error. Returns: torch.Tensor: Rotated points in shape (N, M, 3) """ rot_sin = np.sin(angles) rot_cos = np.cos(angles) ones = np.ones_like(rot_cos) zeros = np.zeros_like(rot_cos) if axis == 1: rot_mat_T = np.stack( [ np.stack([rot_cos, zeros, -rot_sin]), np.stack([zeros, ones, zeros]), np.stack([rot_sin, zeros, rot_cos]), ] ) elif axis == 2 or axis == -1: rot_mat_T = np.stack( [ np.stack([rot_cos, -rot_sin, zeros]), np.stack([rot_sin, rot_cos, zeros]), np.stack([zeros, zeros, ones]), ] ) elif axis == 0: rot_mat_T = np.stack( [ np.stack([zeros, rot_cos, -rot_sin]), np.stack([zeros, rot_sin, rot_cos]), np.stack([ones, zeros, zeros]), ] ) else: raise ValueError(f"axis should in range [0, 1, 2], got {axis}") return np.einsum("aij,jka->aik", points, rot_mat_T) def plot_rect3d_on_img(img, num_rects, rect_corners, color=(0, 255, 0), thickness=1): """Plot the boundary lines of 3D rectangular on 2D images. Args: img (numpy.array): The numpy array of image. num_rects (int): Number of 3D rectangulars. rect_corners (numpy.array): Coordinates of the corners of 3D rectangulars. Should be in the shape of [num_rect, 8, 2]. color (tuple[int]): The color to draw bboxes. Default: (0, 255, 0). thickness (int, optional): The thickness of bboxes. Default: 1. """ line_indices = ( (0, 1), (0, 3), (0, 4), (1, 2), (1, 5), (3, 2), (3, 7), (4, 5), (4, 7), (2, 6), (5, 6), (6, 7), ) for i in range(num_rects): corners = rect_corners[i].astype(np.int) for start, end in line_indices: cv2.line( img, (corners[start, 0], corners[start, 1]), (corners[end, 0], corners[end, 1]), color, thickness, cv2.LINE_AA, ) return img.astype(np.uint8) def draw_boxes_nuplan_on_img(gt_boxes_nuplan, cam_infos, eps=1e-3): for cam_type, cam_info in cam_infos.items(): cur_img_path = cam_info["data_path"] cur_img = cv2.imread(cur_img_path) cur_img_h, cur_img_w = cur_img.shape[:2] gt_boxes_cams = transformation.transform_nuplan_boxes_to_cam( gt_boxes_nuplan, cam_info["sensor2lidar_rotation"], cam_info["sensor2lidar_translation"], ) # Then convert gt_boxes_cams to corners. cur_locs, cur_dims, cur_rots = ( gt_boxes_cams[:, :3], gt_boxes_cams[:, 3:6], gt_boxes_cams[:, 6:], ) corners_norm = np.stack(np.unravel_index(np.arange(8), [2] * 3), axis=1) corners_norm = corners_norm[[0, 1, 3, 2, 4, 5, 7, 6]] corners_norm = corners_norm - np.array([0.5, 0.5, 0.5]) corners = cur_dims.reshape([-1, 1, 3]) * corners_norm.reshape([1, 8, 3]) corners = rotation_3d_in_axis(corners, cur_rots.squeeze(-1), axis=1) corners += cur_locs.reshape(-1, 1, 3) # Then draw project corners to image. corners_img, corners_pc_in_fov = transformation.transform_cam_to_img( corners.reshape(-1, 3), cam_info["cam_intrinsic"], img_shape=(cur_img_h, cur_img_w) ) corners_img = corners_img.reshape(-1, 8, 2) corners_pc_in_fov = corners_pc_in_fov.reshape(-1, 8) valid_corners = corners_pc_in_fov.all(-1) corners_img = corners_img[valid_corners] cur_img = plot_rect3d_on_img(cur_img, len(corners_img), corners_img) cv2.imwrite(f"dbg/{cam_type}.png", cur_img) return None