def generate_annotation(annotation,argoverse_data,log_id,distance_range=20): for idx in range(len(argoverse_data.image_list_sync['ring_front_center'])):#just to find length # print(idx) dic={} dic['context_name']=str(log_id) dic['frame_name']=str(argoverse_data.get_image_sync(idx=0,camera=camera_name[0],load=False)).split('/')[-1] dic["direction"]=[{"name":"ring_front_center","object":[]}, {"name":"ring_front_left","object":[]}, {"name":"ring_front_right","object":[]}, {"name":"ring_side_left","object":[]}, {"name":"ring_side_right","object":[]}, {"name":"ring_rear_left","object":[]}, {"name":"ring_rear_right","object":[]} ] dic['lidar_index']=idx for camera in camera_name: img = argoverse_data.get_image_sync(idx, camera=camera) objects = argoverse_data.get_label_object(idx) calib = argoverse_data.get_calibration(camera=camera) for obj in objects: box3d_pts_3d = obj.as_3d_bbox() cx=box3d_pts_3d.mean(axis=0)[0] cy=box3d_pts_3d.mean(axis=0)[1] dist=np.sqrt(cx**2+cy**2) if dist>distance_range: continue calib_fpath = os.path.join(args.root_dir,log_id,'vehicle_calibration_info.json') calib_data = read_json_file(calib_fpath) uv_cam = calib.project_ego_to_cam(box3d_pts_3d) uv, uv_cam_T, valid_pts_bool, camera_config = proj_cam_to_uv(uv_cam, copy.deepcopy(calib.camera_config)) if any(valid_pts_bool): #all for object completely lie inside the frame objdic={} objdic['type']=obj.label_class objdic['3d coordinates']=box3d_pts_3d.tolist() dic['direction'][camera_name.index(camera)]["object"].append(objdic) annotation.append(dic)
def render_clip_frustum_cv2( self, img: np.array, corners: np.array, planes: List[Tuple[np.array, np.array, np.array, np.array, np.array]], camera_config: CameraConfig, colors: Tuple[Tuple[int, int, int], Tuple[int, int, int], Tuple[int, int, int]] = ( BLUE_RGB, RED_RGB, GREEN_RGB, ), linewidth: int = 2, ) -> np.ndarray: r"""We bring the 3D points into each camera, and do the clipping there. Renders box using OpenCV2. Roughly based on https://github.com/nutonomy/nuscenes-devkit/blob/master/python-sdk/nuscenes_utils/data_classes.py :: 5------4 |\\ |\\ | \\ | \\ 6--\\--7 \\ \\ \\ \\ \\ l \\ 1-------0 h e \\ || \\ || e n \\|| \\|| i g \\2------3 g t width. h h. t. Args: img: Numpy array of shape (M,N,3) corners: Numpy array of shape (8,3) in camera coordinate frame. planes: Iterable of 5 clipping planes. Each plane is defined by 4 points. camera_config: CameraConfig object colors: tuple of RGB 3-tuples, Colors for front, side & rear. defaults are 0. blue (0,0,255) in RGB and (255,0,0) in OpenCV's BGR 1. red (255,0,0) in RGB and (0,0,255) in OpenCV's BGR 2. green (0,255,0) in RGB and BGR alike. linewidth: integer, linewidth for plot Returns: img: Numpy array of shape (M,N,3), representing updated image """ def draw_rect(selected_corners: np.array, color: Tuple[int, int, int]) -> None: prev = selected_corners[-1] for corner in selected_corners: draw_clipped_line_segment( img, prev.copy(), corner.copy(), camera_config, linewidth, planes, color, ) prev = corner # Draw the sides in green for i in range(4): # between front and back corners draw_clipped_line_segment( img, corners[i], corners[i + 4], camera_config, linewidth, planes, colors[2][::-1], ) # Draw front (first 4 corners) in blue draw_rect(corners[:4], colors[0][::-1]) # Draw rear (last 4 corners) in red draw_rect(corners[4:], colors[1][::-1]) # grab the top vertices center_top = np.mean(corners[TOP_VERT_INDICES], axis=0) uv_ct, _, _, _ = proj_cam_to_uv(center_top.reshape(1, 3), camera_config) uv_ct = uv_ct.squeeze().astype(np.int32) # cast to integer if label_is_closeby(center_top) and uv_coord_is_valid(uv_ct, img): top_left = (uv_ct[0] - BKGRND_RECT_OFFS_LEFT, uv_ct[1] - BKGRND_RECT_OFFS_UP) bottom_right = (uv_ct[0] + BKGRND_RECT_OFFS_LEFT, uv_ct[1] + BKGRND_RECT_OFFS_DOWN) img = draw_alpha_rectangle(img, top_left, bottom_right, EMERALD_RGB, alpha=BKGRND_RECT_ALPHA) add_text_cv2(img, text=str(self.label_class), x=uv_ct[0] - TEXT_OFFS_LEFT, y=uv_ct[1], color=WHITE_BGR) # Draw blue line indicating the front half center_bottom_forward = np.mean(corners[2:4], axis=0) center_bottom = np.mean(corners[[2, 3, 7, 6]], axis=0) draw_clipped_line_segment( img, center_bottom, center_bottom_forward, camera_config, linewidth, planes, colors[0][::-1], ) return img
def render_clip_frustum_cv2(self, img: np.array, corners: np.array, planes: List[Tuple[np.array, np.array, np.array, np.array, np.array]], camera_config: CameraConfig, colors: Tuple[Tuple[int, int, int], Tuple[int, int, int], Tuple[int, int, int]] = ( (0, 0, 255), (255, 0, 0), (0, 255, 0), ), linewidth: int = 2, plot_text: str = '') -> np.ndarray: r"""We bring the 3D points into each camera, and do the clipping there. Renders box using OpenCV2. Roughly based on https://github.com/nutonomy/nuscenes-devkit/blob/master/python-sdk/nuscenes_utils/data_classes.py :: 5------4 |\\ |\\ | \\ | \\ 6--\\--7 \\ \\ \\ \\ \\ l \\ 1-------0 h e \\ || \\ || e n \\|| \\|| i g \\2------3 g t width. h h. t. Args: img: Numpy array of shape (M,N,3) corners: Numpy array of shape (8,3) in camera coordinate frame. planes: Iterable of 5 clipping planes. Each plane is defined by 4 points. camera_config: CameraConfig object colors: tuple of RGB 3-tuples, Colors for front, side & rear. defaults are 0. blue (0,0,255) in RGB and (255,0,0) in OpenCV's BGR 1. red (255,0,0) in RGB and (0,0,255) in OpenCV's BGR 2. green (0,255,0) in RGB and BGR alike. linewidth: integer, linewidth for plot Returns: img: Numpy array of shape (M,N,3), representing updated image """ def draw_rect(selected_corners: np.array, color: Tuple[int, int, int]) -> None: prev = selected_corners[-1] for corner in selected_corners: draw_clipped_line_segment(img, prev.copy(), corner.copy(), camera_config, linewidth, planes, color) prev = corner # Draw the sides in green for i in range(4): # between front and back corners draw_clipped_line_segment(img, corners[i], corners[i + 4], camera_config, linewidth, planes, colors[2][::-1]) # Draw front (first 4 corners) in blue draw_rect(corners[:4], colors[0][::-1]) # Draw rear (last 4 corners) in red draw_rect(corners[4:], colors[1][::-1]) # Draw blue line indicating the front half center_bottom_forward = np.mean(corners[2:4], axis=0) center_bottom = np.mean(corners[[2, 3, 7, 6]], axis=0) draw_clipped_line_segment(img, center_bottom, center_bottom_forward, camera_config, linewidth, planes, colors[0][::-1]) text_coords, _, _, _ = proj_cam_to_uv(center_bottom.reshape(1, 3), camera_config) text_coords = tuple(list(text_coords.squeeze().astype(np.int64))) img = add_text_cv2(img, plot_text, coords_to_plot_at=text_coords, font_color=(0, 0, 255), font_scale=2) return img