def show(self, max_num=-1, points=None, with_centroids=False, boxes_show_together=None, points_keep_rate=POINTS_KEEP_RATE, points_sample_rate=POINTS_SAMPLE_RATE, colors=None): import numpy as np from utils3d.bbox3d_ops import Bbox3D boxes = self.bbox3d.cpu().data.numpy() if with_centroids: centroids = boxes.copy() if self.mode == 'yx_zb': centroids[:,2] += centroids[:,5]*0.5 centroids[:,3:6] = 0.03 if max_num > 0 and max_num < boxes.shape[0]: step = 4 ids0 = np.random.choice(boxes.shape[0]//step-1, max_num, replace=False).reshape(-1,1)*step tmp = np.arange(step).reshape(1,step) ids = (ids0 + tmp).reshape(-1) print(ids) boxes = boxes[ids] if with_centroids: boxes = np.concatenate([boxes, centroids], 0) if boxes_show_together: boxes_show_together = boxes_show_together.bbox3d.cpu().data.numpy() labels = np.array( [0]*boxes.shape[0] + [1]*boxes_show_together.shape[0]) boxes = np.concatenate([boxes, boxes_show_together], 0) else: if 'labels' in self.fields(): labels = self.get_field('labels').cpu().data.numpy().astype(np.int32) else: labels = None if points is None: Bbox3D.draw_bboxes(boxes, 'Z', is_yx_zb=self.mode=='yx_zb', \ labels = labels, random_color=False) else: Bbox3D.draw_points_bboxes(points, boxes, 'Z', is_yx_zb=self.mode=='yx_zb',\ labels = labels, random_color=False, points_keep_rate=points_keep_rate, points_sample_rate=points_sample_rate, box_colors=colors)
def show_final_preds(pred_boxes, points, gt_boxes): pred_boxes = pred_boxes.data.cpu().numpy() boxes_show = np.concatenate([pred_boxes, gt_boxes], 0) pnum = pred_boxes.shape[0] gnum = gt_boxes.shape[0] labels_show = np.array([0]*pnum + [1]*gnum) Bbox3D.draw_points_bboxes(points, boxes_show, 'Z', is_yx_zb=True, labels=labels_show)
def read_room_obj(obj_fn, room_parts_dir): import pymesh #mesh0 = pymesh.load_mesh(obj_fn) mesh_parts = read_obj_parts(obj_fn) bboxs_roomparts = [part['bbox'] for part in mesh_parts] room_name = os.path.splitext(os.path.basename(obj_fn))[0] is_save_part = False if is_save_part: print(f'save room parts in:\n {room_parts_dir}') for i in range(len(mesh_parts)): part_i = mesh_parts[i] bbox_i = part_i['bbox'] part_bbox_fn = os.path.join( room_parts_dir, 'bbox_' + room_name + '_' + part_i['name'] + '.ply') Bbox3D.save_bbox_ply(part_bbox_fn, bbox_i, 'Y') for i in range(len(mesh_parts)): part_i = mesh_parts[i] mesh_i = pymesh.form_mesh(part_i['vertices'], part_i['face_vidx']) mesh_i.add_attribute('face_norm') mesh_i.set_attribute('face_norm', part_i['face_norms']) part_obj_fn = os.path.join( room_parts_dir, room_name + '_' + part_i['name'] + '.ply') pymesh.save_mesh(part_obj_fn, mesh_i, ascii=True, use_float=True) return bboxs_roomparts
def merge_2close_walls(bbox0, bbox1): ''' in: [n,7] [n,7] out: [n,7] centroid: mean x_size: max y_size: max z_size: max yaw: mean ''' bboxes = np.concatenate( [bbox0.reshape([-1, 1, 7]), bbox1.reshape([-1, 1, 7])], 1) bboxes_new = bboxes.mean(axis=1) bboxes_new[:, -1] = ave_angles(bboxes[:, 0, -1], bboxes[:, 1, -1], scope_id=1) bboxes_new[:, 3:6] = bboxes[:, :, 3:6].max(axis=1) show = False if show: show_boxes = np.concatenate([bboxes[0], bboxes_new], 0) show_boxes[-1, 2] += 0.2 Bbox3D.draw_bboxes(show_boxes, 'Z', False) import pdb pdb.set_trace() # XXX BREAKPOINT pass return bboxes_new
def show_points_in_box(points, bboxes, bbox_corners, surfaces, point_masks, is_yx_zb): if is_yx_zb: bboxes = Bbox3D.convert_from_yx_zb_boxes(bboxes) pn_in_box = np.sum(point_masks, 0) bn = bboxes.shape[0] points_in_boxes = [] for i in range(bn): points_in_boxes.append( points[point_masks[:,i]] ) pcd0 = points2pcd_open3d(points[:,:3]) pcd0_aug = points2pcd_open3d(points[:,:3]-np.array([[0,0,4]])) corner_pcds = [points2pcd_open3d(corners) for corners in bbox_corners] surface_pcds = [points2pcd_open3d(surface) for surface in surfaces] points_inb_pcds = [points2pcd_open3d(points[:,:3]) for points in points_in_boxes] bboxes_ls0 = [Bbox3D.draw_bbox_open3d(box, 'Z') for box in bboxes] frame_mesh = open3d.create_mesh_coordinate_frame(size = 2.0, origin = bboxes[0,0:3]) points_inb_pcds_valid = [pcd for i,pcd in enumerate(points_inb_pcds) if pn_in_box[i]>0] open3d.draw_geometries( bboxes_ls0 + points_inb_pcds_valid + [pcd0_aug]) show_corners = False show_surfaces = False show_points_inb = False for i in range(bn): frame_mesh = open3d.create_mesh_coordinate_frame(size = 2.0, origin = bboxes[i,0:3]) if show_corners: open3d.draw_geometries(bboxes_ls0[i:i+1]+corner_pcds[i:i+1]+[frame_mesh]) if show_surfaces: open3d.draw_geometries(bboxes_ls0[i:i+1]+surface_pcds[i:i+1]+[frame_mesh]) if show_points_inb: open3d.draw_geometries(bboxes_ls0[i:i+1]+points_inb_pcds[i:i+1]+[frame_mesh]) pass
def show_pcl_corners(self, pcl): import numpy as np from utils3d.bbox3d_ops import Bbox3D corners, _ = self.get_2top_corners_offseted() corners = corners.view([-1, 3]).cpu().data.numpy() corners_bot = corners.copy() tmp = self.bbox3d[:, 5].cpu().data.numpy() tmp = np.tile(np.expand_dims(tmp, 1), [1, 2]).reshape(-1) corners_bot[:, 2] = corners_bot[:, 2] - tmp corners = np.concatenate([corners, corners_bot], 0) n = corners.shape[0] boxes = np.zeros([n, 7]) boxes[:, 0:3] = corners boxes[:, 3:6] = 0.2 #points = pcl.cpu().data.numpy() points = pcl Bbox3D.draw_points_bboxes(points, boxes, 'Z', is_yx_zb=self.mode == 'standard', random_color=True) import pdb pdb.set_trace() # XXX BREAKPOINT pass
def show_high(windows, walls, win_high_ids, wall_high_ids): boxes = np.concatenate([windows, walls], 0) win_high_ids = np.array(win_high_ids).reshape([-1]) wall_high_ids = np.array(wall_high_ids).reshape([-1]) + windows.shape[0] high_ids = np.concatenate([win_high_ids, wall_high_ids], 0).astype(np.int) Bbox3D.draw_bboxes(boxes, 'Z', False, highlight_ids=high_ids) pass
def render_parsed_house_walls(parsed_dir, show_pcl=SHOW_PCL, show_by_class=0): print(f'parsed_dir:{parsed_dir}') house_name = os.path.basename(parsed_dir) bboxes = [] labels = [] for obj in CLASSES: bbox_fn_ = f'{parsed_dir}/object_bbox/{obj}.txt' bboxes_ = np.loadtxt(bbox_fn_).reshape([-1, 7]) bboxes.append(bboxes_) label = DSET_METAS0.class_2_label[obj] labels += [label] * bboxes_.shape[0] bboxes = np.concatenate(bboxes, 0) labels = np.array(labels).astype(np.int8) if bboxes.shape[0] > 0: scene_size = Bbox3D.boxes_size(bboxes) print(f'scene wall size:{scene_size}') #Bbox3D.draw_bboxes(bboxes, up_axis='Z', is_yx_zb=False, labels=labels) #if not show_pcl: #Bbox3D.draw_bboxes_mesh(bboxes, up_axis='Z', is_yx_zb=False) #Bbox3D.draw_bboxes_mesh(bboxes, up_axis='Z', is_yx_zb=False, labels=labels) #show_walls_offsetz(bboxes) pass if show_by_class: for c in range(1, max(labels) + 1): cs = DSET_METAS0.label_2_class[c] print(cs) if cs not in ['wall', 'window', 'door']: #if cs not in ['wall']: continue mask = labels == c bboxes_c = bboxes[mask] show_walls_offsetz(bboxes_c) if show_pcl: pcl_fn = f'{parsed_dir}/pcl_camref.ply' if not os.path.exists(pcl_fn): return pcd = open3d.io.read_point_cloud(pcl_fn) points = np.asarray(pcd.points) points = cam2world_pcl(points) colors = np.asarray(pcd.colors) pcl = np.concatenate([points, colors], 1) scene_size = pcl_size(pcl) print(f'scene pcl size:{scene_size}') print(f'point num: {pcl.shape[0]}') #pcl = cut_points_roof(pcl) Bbox3D.draw_points(pcl, points_keep_rate=POINTS_KEEP_RATE, points_sample_rate=0.05) #Bbox3D.draw_points(pcl, points_keep_rate=POINTS_KEEP_RATE, animation_fn='points.mp4', ani_size=AniSizes[house_name]) import pdb pdb.set_trace() # XXX BREAKPOINT bboxes[:, 2] += 0.1
def merge_pieces_of_same_walls_alongX(wall_bboxes): ''' 1) Find all the walls with not both corners intersected 2) Merge all the walls can be merged ''' if wall_bboxes.shape[0] == 0: return wall_bboxes intersections = Bbox3D.all_intersections_by_cenline( wall_bboxes, check_same_height=False, only_on_corners=True) num_inters = np.array([it.shape[0] for it in intersections]) mask = num_inters < 2 candidate_ids = np.where(mask)[0] show = DEBUG and False if show: show_boxes = wall_bboxes.copy() show_boxes[:, 2] -= 1 show_boxes = np.concatenate([show_boxes, wall_bboxes[candidate_ids]], 0) print(f'candidate_ids:{candidate_ids}') Bbox3D.draw_bboxes(show_boxes, 'Z', False) #Bbox3D.draw_bboxes(wall_bboxes[[5,6,12]], 'Z', False) pass n = candidate_ids.shape[0] keep_mask = np.array([True] * wall_bboxes.shape[0]) for i in range(n - 1): idx_i = candidate_ids[i] for j in range(i + 1, n): idx_next = candidate_ids[j] merged_i = merge_2pieces_of_1wall(wall_bboxes[idx_i], wall_bboxes[idx_next], 'X') if merged_i is not None: keep_mask[idx_i] = False wall_bboxes[idx_next] = merged_i[0] if show: show_boxes = wall_bboxes.copy() show_boxes[:, 2] -= 1 show_boxes = np.concatenate( [show_boxes, wall_bboxes[[idx_i, idx_next]], merged_i], 0) show_boxes[-1, 2] += 1 Bbox3D.draw_bboxes(show_boxes, 'Z', False) import pdb pdb.set_trace() # XXX BREAKPOINT pass wall_bboxes = wall_bboxes[keep_mask] rm_num = np.sum(1 - keep_mask) print(f'merge along X: rm {rm_num} walls') if show: show_walls_offsetz(wall_bboxes) return wall_bboxes
def show_walls_1by1(wall_bboxes): n = wall_bboxes.shape[0] for i in range(n): tmp = wall_bboxes.copy() tmp[:, 2] -= 1 show_box = np.concatenate([tmp, wall_bboxes[i:i + 1]], 0) print(f'wall {i}/{n}\n{wall_bboxes[i]}') Bbox3D.draw_bboxes(show_box, 'Z', False)
def render_pth_file(pth_fn, show_by_class=0): pcl, bboxes0 = torch.load(pth_fn) #points = pcl[:,0:3] #colors = pcl[:,3:6] #normals = pcl[:,6:9] bboxes = {} for c in CLASSES: bboxes[c] = bboxes0[c] scene_size = pcl_size(pcl) print(f'scene pcl size:{scene_size}') print(f'point num: {pcl.shape[0]}') #pcl = cut_points_roof(pcl) classes = [k for k in bboxes.keys()] num_classes = {k: bboxes[k].shape[0] for k in bboxes.keys()} print(f'\nclasses: {num_classes}\n\n') all_bboxes = np.concatenate([boxes for boxes in bboxes.values()], 0) nums = [boxes.shape[0] for boxes in bboxes.values()] labels = [] for i, n in enumerate(nums): labels += [i] * n labels = np.array(labels) Bbox3D.draw_points(pcl, points_keep_rate=POINTS_KEEP_RATE) #show_walls_offsetz(all_bboxes) #Bbox3D.draw_bboxes_mesh(all_bboxes, up_axis='Z', is_yx_zb=False, labels=labels) #Bbox3D.draw_bboxes_mesh(all_bboxes, up_axis='Z', is_yx_zb=False) #Bbox3D.draw_points_bboxes_mesh(pcl, all_bboxes, up_axis='Z', is_yx_zb=False, labels=labels, points_keep_rate=POINTS_KEEP_RATE) #Bbox3D.draw_points_bboxes_mesh(pcl, all_bboxes, up_axis='Z', is_yx_zb=False, points_keep_rate=POINTS_KEEP_RATE) #Bbox3D.draw_points_bboxes(pcl, all_bboxes, up_axis='Z', is_yx_zb=False,points_keep_rate=POINTS_KEEP_RATE) #Bbox3D.draw_points_bboxes(pcl, all_bboxes, up_axis='Z', is_yx_zb=False, labels=labels, points_keep_rate=POINTS_KEEP_RATE) #Bbox3D.draw_points_bboxes(pcl, all_bboxes, up_axis='Z', is_yx_zb=False, labels=labels, points_keep_rate=POINTS_KEEP_RATE, animation_fn='anima.mp4', ani_size=[280,700,550,1350]) if show_by_class: for clas in bboxes.keys(): if clas not in ['ceiling', 'floor']: #if clas not in ['wall', 'window', 'door','ceiling', 'floor']: continue print(clas) #if clas not in CLASSES: # continue boxes = bboxes[clas] #Bbox3D.draw_points_bboxes(points, boxes, up_axis='Z', is_yx_zb=False) Bbox3D.draw_points_bboxes_mesh(pcl, boxes, up_axis='Z', is_yx_zb=False, points_keep_rate=POINTS_KEEP_RATE) show_walls_offsetz(boxes) import pdb pdb.set_trace() # XXX BREAKPOINT pass
def show_centroids(self, max_num=-1, points=None): import numpy as np from utils3d.bbox3d_ops import Bbox3D boxes = self.bbox3d.cpu().data.numpy() if max_num > 0 and max_num < boxes.shape[0]: ids = np.random.choice(boxes.shape[0], max_num, replace=False) boxes = boxes[ids] if points is None: Bbox3D.draw_centroids(boxes, 'Z', is_yx_zb=self.mode=='yx_zb') else: Bbox3D.draw_points_centroids(points, boxes, 'Z', is_yx_zb=self.mode=='yx_zb')
def show_pcl_boxdic(pcl, bboxes_dic): from utils3d.bbox3d_ops import Bbox3D boxes = [] for obj in bboxes_dic: print(f'{obj}: {len(bboxes_dic[obj])}') boxes.append(bboxes_dic[obj]) boxes = np.concatenate(boxes, 0) Bbox3D.draw_points_bboxes(pcl[:,0:6], boxes, 'Z', is_yx_zb=True) Bbox3D.draw_points_bboxes(pcl[:,0:6], bboxes_dic['wall'], 'Z', is_yx_zb=True) import pdb; pdb.set_trace() # XXX BREAKPOINT pass
def show_with_corners(self, only_corner=0): import numpy as np from utils3d.bbox3d_ops import Bbox3D corners, _ = self.get_2top_corners_offseted() corners = corners.view([-1, 3]) points = corners.cpu().data.numpy() boxes = self.bbox3d.cpu().data.numpy() if only_corner: boxes = boxes[0:0, :] Bbox3D.draw_points_bboxes(points, boxes, 'Z', is_yx_zb=self.mode=='yx_zb',\ random_color=True) pass
def show_highlight(self, ids, points=None): from utils3d.bbox3d_ops import Bbox3D ids = np.array(ids) n = len(self) labels = np.zeros([n]).astype(np.int) labels[ids] = 1 boxes = self.bbox3d.cpu().data.numpy() if points is None: Bbox3D.draw_bboxes(boxes, 'Z', is_yx_zb=self.mode=='yx_zb', labels=labels, random_color=False) else: if isinstance(points, torch.Tensor): points = points.cpu().data.numpy() Bbox3D.draw_points_bboxes(points, boxes, 'Z', is_yx_zb=self.mode=='yx_zb', labels=labels, random_color=False)
def is_edge_wall_of_ceiling(wall_cenlines, ceiling, walls_org): c_corners = Bbox3D.bbox_corners(ceiling, 'Z') c_lines = np.take(c_corners, Bbox3D._lines_z0_vids, axis=0) c_lines[:, :, 2] = 0 wall_cenlines_ = np.concatenate( [wall_cenlines, wall_cenlines.mean(1, keepdims=True)], 1) wn = wall_cenlines.shape[0] in_mask0 = points_in_lines(wall_cenlines_.reshape([-1, 3]), c_lines, threshold_dis=0.1) # [wn*3,4] in_mask0 = in_mask0.reshape([wn, 3, 4]) # [wn,3,4] # find all wdge walls in_mask1 = in_mask0.any(2) # [wn,3] in any of four edge of a ceiling in_state = in_mask1.sum(1) winc_mask = in_state >= 2 # [wn] the number of points on edge: 0~3. two of three: two corners, one centroid winc_ids = np.where(winc_mask)[0] # clean repeat edge walls on same edge: only at most one edge wall on each # edge allowed if winc_ids.shape[0] == 0: winc_ids_1 = winc_ids winc_state = [] else: keep_ids = clean_extend_lines(wall_cenlines[winc_ids]) winc_ids_1 = winc_ids[keep_ids] winc_state = in_state[winc_ids_1] edge_wall_num = winc_ids_1.shape[0] if Debug and edge_wall_num < 2 and 0: #if Debug: print(f'edge_wall_num: {edge_wall_num}') boxes = np.concatenate([ceiling.reshape([1, -1]), walls_org[winc_ids]], 0) wo = walls_org.copy() wo[:, 2] -= 2 #boxes = np.concatenate([boxes, wo],0) #boxes = ceiling.reshape([1,-1]) points = np.concatenate( [wall_cenlines_.reshape([-1, 3]), c_lines.reshape([-1, 3])], 0) Bbox3D.draw_points_bboxes(points, boxes, 'Z', False) boxes = np.concatenate( [ceiling.reshape([1, -1]), walls_org[winc_ids_1]], 0) Bbox3D.draw_points_bboxes(points, boxes, 'Z', False) #show_walls_1by1(walls_org) return edge_wall_num, winc_state
def save_cam_ply(cam_fn=None, show=False, with_walls=True, with_pcl=True): if cam_fn is None: house_name = '0004d52d1aeeb8ae6de39d6bd993e992_A' parsed_dir = f'{SUNCG_V1_DIR}/parsed/{house_name}' cam_fn = os.path.join(parsed_dir, 'cam') cam_fn = '/home/z/parsed/28297783bce682aac7fb35a1f35f68fa/cam' cam_pos = read_cam_pos(cam_fn) parsed_dir = os.path.dirname(cam_fn) cam_num = cam_pos.shape[0] cam_centroid = cam_pos[:, 0:3] # cam centroid box tmp = np.array([[0.1, 0.1, 0.1, 0]]) tmp = np.tile(tmp, [cam_num, 1]) cam_cen_box = cam_pos[:, 0:3] cam_cen_box = np.concatenate([cam_centroid, tmp], 1) cam_cen_box = Bbox3D.bboxes_lineset(cam_cen_box, 'Z', False, random_color=False) # cam orientations cam_ori = cam_centroid + cam_pos[:, 3:6] cam_centroid = np.expand_dims(cam_centroid, 1) cam_ori = np.expand_dims(cam_ori, 1) cam_vec = np.concatenate([cam_centroid, cam_ori], 1) cam_lines = Bbox3D.draw_lines_open3d(cam_vec, color=[200, 0, 0], show=False) pcl_fn = os.path.dirname(cam_fn) + '/pcl_camref.ply' if show: if with_pcl and os.path.exists(pcl_fn): pcl = open3d.read_point_cloud(pcl_fn) open3d.draw_geometries(cam_cen_box + [cam_lines, pcl]) if with_walls: bbox_fn = f'{parsed_dir}/object_bbox/wall.txt' walls = np.loadtxt(bbox_fn).reshape([-1, 7]) walls = world2cam_box(walls) bboxes_lineset_ls = Bbox3D.bboxes_lineset(walls, 'Y', False) open3d.draw_geometries(cam_cen_box + [cam_lines] + bboxes_lineset_ls) # save cam pos as ply pcd = open3d.PointCloud() pcd.points = open3d.Vector3dVector(cam_pos[:, 0:3]) cam_ply_fn = cam_fn + '_pos.ply' open3d.write_point_cloud(cam_ply_fn, pcd)
def crop_walls(wall_bboxes): ''' crop walls with intersections not on the corner ''' #show_walls_1by1(wall_bboxes) if wall_bboxes.shape[0] == 0: return wall_bboxes intersections = Bbox3D.all_intersections_by_cenline( wall_bboxes, check_same_height=False, not_on_corners=True) n = wall_bboxes.shape[0] new_walls = [] keep_mask = np.array([True] * n) for i in range(n): inters_i = intersections[i] if inters_i.shape[0] == 0: continue new_walls_i = Bbox3D.split_wall_by_centroid_intersections( wall_bboxes[i], inters_i) keep_mask[i] = False new_walls.append(new_walls_i) show = False #tmp = np.concatenate(new_walls, 0) #if tmp.shape[0] >= 7: # show = True if show: #tmp = wall_bboxes.copy() tmp = new_walls_i.copy() tmp[:, 2] += 1 for ti in range(1, tmp.shape[0]): tmp[ti:, 2] += 0.2 show_box = np.concatenate([tmp, wall_bboxes[i:i + 1]], 0) Bbox3D.draw_points_bboxes(inters_i, show_box, 'Z', False) #Bbox3D.draw_bboxes(show_box, 'Z', False) import pdb pdb.set_trace() # XXX BREAKPOINT pass num_croped = np.sum(keep_mask == False) print(f'num croped:{num_croped}\nnum new:{len(new_walls)}') wall_bboxes = wall_bboxes[keep_mask] if len(new_walls) > 0: new_walls = np.concatenate(new_walls, 0) wall_bboxes = np.concatenate([wall_bboxes, new_walls], 0) #show_walls_1by1(wall_bboxes) #show_walls_offsetz(wall_bboxes) return wall_bboxes
def preprocess_windows(windows0, walls): ''' both windows0 ad walls are standard: [xc, yc, zc, x_size, y_size, z_size, yaw] ''' if DEBUG and False: print('input') show_all([windows0, walls]) #Bbox3D.draw_bboxes(walls, 'Z', False) #Bbox3D.draw_bboxes(windows0, 'Z', False) #print(f'windows0: \n{windows0}') if len(windows0) == 0: return windows0 windows1 = Bbox3D.define_walls_direction(windows0, 'Z', yx_zb=False) #print(f'windows1: \n{windows0}') win_bad_ids, wall_ids_for_bad_win = find_wall_ids_for_windows( windows1, walls) windows_bad = windows1[win_bad_ids].reshape(-1, 7) walls_bw = walls[wall_ids_for_bad_win].reshape(-1, 7) windows_corrected = correct_bad_windows(windows_bad, walls_bw) windows1[win_bad_ids] = windows_corrected if DEBUG and False: print('out') show_all([windows1, walls]) return windows1
def get_sung_info(data_path, house_names0): data_path = os.path.join(data_path, 'houses') house_names1 = os.listdir(data_path) house_names = [h for h in house_names1 if h in house_names0] infos = [] for house in house_names: house_path = os.path.join(data_path, house) object_path = os.path.join(house_path, 'objects') pcl_fns = glob.glob(os.path.join(house_path, 'pcl*.bin')) pcl_fns.sort() #pcl_fns = [pcl_fn.split('houses')[1] for pcl_fn in pcl_fns] pcl_num = len(pcl_fns) box_fns = glob.glob(os.path.join(object_path, '*.bin')) box_fns.sort() objects = set([os.path.basename(fn).split('_')[0] for fn in box_fns]) for i in range(pcl_num): info = {} info['velodyne_path'] = pcl_fns[i] info['pointcloud_num_features'] = 6 info['image_idx'] = '0' info['image_path'] = 'empty' info['calib/R0_rect'] = np.eye(4) info['calib/Tr_velo_to_cam'] = np.eye(4) # np.array([[0,-1,0,0]. [0,0,-1,0], [1,0,0,0], [0,0,0,1]], dtype=np.float32) info['calib/P2'] = np.eye(4) base_name = os.path.splitext( os.path.basename(pcl_fns[i]) )[0] idx = int(base_name.split('_')[-1]) annos = defaultdict(list) for obj in objects: box_fn = os.path.join(object_path, obj+'_'+str(idx)+'.bin') box = np.fromfile(box_fn, np.float32) box = box.reshape([-1,7]) box = Bbox3D.convert_to_yx_zb_boxes(box) box_num = box.shape[0] annos['location'].append(box[:,0:3]) annos['dimensions'].append(box[:,3:6]) annos['rotation_y'].append(box[:,6]) annos['name'].append( np.array([obj]*box_num) ) annos['difficulty'].append(np.array(['A']*box_num)) annos['bbox'].append( box3d_t_2d(box, info['calib/P2'] ) ) annos['box3d_camera'].append( get_box3d_cam(box, info['calib/R0_rect'], info['calib/Tr_velo_to_cam']) ) bn = box.shape[0] annos["truncated"].append(np.array([0.0]*bn)) annos["occluded"].append(np.array([0.0]*bn)) annos["alpha"].append( get_alpha(box) ) for key in annos: annos[key] = np.concatenate(annos[key], 0) info['annos'] = annos infos.append(info) return infos
def show_anchors(points, anchors, gt_boxes, anchors_mask=None): # show valid anchors if anchors_mask is not None: indices = np.where(anchors_mask)[0] anchors = anchors[indices,:] show_num = -1 if show_num>0 and show_num < anchors.shape[0]: indices = np.random.choice(anchors.shape[0], show_num) anchors = anchors[indices,:] n0 = anchors.shape[0] n1 = gt_boxes.shape[0] anchors = np.concatenate([anchors, gt_boxes], 0) labels = np.array([1]*n0 + [0]*n1) Bbox3D.draw_points_bboxes(points, anchors, 'Z', is_yx_zb=True, labels=labels)
def merge_two_parts(p0, p1): p1['face_vidx'] += p0['vertices'].shape[0] for item in ['vertices', 'face_vidx', 'face_norms']: p0[item] = np.concatenate([p0[item], p1[item]], 0) p0['name'] = p0['name'].replace('Inside', '') p0['bbox'] = Bbox3D.merge_two_bbox(p0['bbox'], p1['bbox'], 'Y') return p0
def forcely_crop_scene(pcl0, walls, ceilings): scene_min = pcl0[:,:3].min(0) scene_max = pcl0[:,:3].max(0) scene_size = scene_max - scene_min abandon = scene_size - MAX_SCENE_SIZE masks = [] ref_boxes = [walls, walls, ceilings] for i in range(3): if abandon[i] > 0: if ref_boxes[i].shape[0] == 0: if i<2: rate_min = 0.5 else: rate_min = 0 else: ref_min = ref_boxes[i][:,i].min() ref_max = ref_boxes[i][:,i].max() wmin = ref_min - scene_min[i] wmax = scene_max[i] - ref_max rate_min = wmin / (wmin+wmax) new_mini = scene_min[i] + abandon[i] * rate_min new_maxi = scene_max[i] - abandon[i] * (1-rate_min) masks.append( (pcl0[:,i] > new_mini) * (pcl0[:,i] < new_maxi) ) if len(masks) > 0: mask = masks[0] for m in masks: mask = mask * m pcl1 = pcl0[mask] else: pcl1 = pcl0 show = 0 if show: scene_min_new = pcl1[:,:3].min(0) scene_max_new = pcl1[:,:3].max(0) print(f'Org: {scene_min} - {scene_max}') print(f'New: {scene_min_new} - {scene_max_new}') pcl00 = pcl0.copy() pcl00[:,2] -= 15 pcl_show = np.concatenate([pcl00, pcl1], 0) Bbox3D.draw_points_bboxes(pcl_show, walls, 'Z', False, points_keep_rate=1) return pcl1
def preprocess_walls(wall_bboxes): ''' The framework to generate wall ground truth from SUNCG 1) Make y-dim as thickness, x_size > y_size, yaw in (-pi/2, pi/2] 2) If a final wall is originally splitted as pieces in SUNCG, merge them as one 3) If a wall in SUNCG actually contains several finally walls, crop it 4) Clean repeated walls ''' show_pro = False if show_pro: print('original') show_walls_offsetz(wall_bboxes) #Bbox3D.draw_bboxes_mesh(wall_bboxes, 'Z', False) #Bbox3D.draw_bboxes(wall_bboxes, 'Z', False) if wall_bboxes.shape[0] == 0: return wall_bboxes wall_bboxes = Bbox3D.define_walls_direction(wall_bboxes, 'Z', yx_zb=False, check_thickness=True) wall_bboxes = merge_pieces_of_same_walls_alongY(wall_bboxes) if show_pro: print('merge_pieces_of_same_walls_alongY') show_walls_offsetz(wall_bboxes) wall_bboxes = merge_pieces_of_same_walls_alongX(wall_bboxes) if show_pro: print('merge_pieces_of_same_walls_alongX') show_walls_offsetz(wall_bboxes) wall_bboxes = crop_walls(wall_bboxes) if show_pro: print('crop_walls') show_walls_offsetz(wall_bboxes) wall_bboxes = merge_pieces_of_same_walls_alongY(wall_bboxes) if show_pro: print( 'merge_pieces_of_same_walls_alongY again after crop_walls to solve some special case: Originally, a wall is broken by no intersection like 0058113bdc8bee5f387bb5ad316d7b28' ) show_walls_offsetz(wall_bboxes) wall_bboxes = find_close_walls(wall_bboxes) if show_pro: print('clean_close_walls') show_walls_offsetz(wall_bboxes) #intersections = Bbox3D.all_intersections_by_cenline(wall_bboxes, not_on_corners=True, show_res=True) #mask =( wall_bboxes[:,0] < 45) * (wall_bboxes[:,0] > 43) #wall_bboxes_ = wall_bboxes[mask] if DEBUG and 0: show_walls_offsetz(wall_bboxes) return wall_bboxes
def unused_read_indoor_info(): info_path = f'{SPLITED_DIR}/sung_infos_train.pkl' with open(info_path, 'rb') as f: infos = pickle.load(f) idx = 0 print(f'totally {len(infos)} blocks') for idx in range(0,len(infos)): info = infos[idx] pcl_path = info['velodyne_path'] pointcloud_num_features = info['pointcloud_num_features'] points = np.fromfile( pcl_path, dtype=np.float32).reshape([-1, pointcloud_num_features]) annos = info['annos'] loc = annos['location'] dims = annos['dimensions'] rots = annos['rotation_y'] gt_boxes = np.concatenate([loc, dims, rots[..., np.newaxis]], axis=1).astype(np.float32) Bbox3D.draw_points_bboxes(points, gt_boxes, 'Z', is_yx_zb=True)
def show_high_score_preds(score_threshold0, score_threshold1, total_scores, box_preds, anchors, gt_boxes, points): pos_ids = np.where(np.logical_and(total_scores >= score_threshold0, total_scores < score_threshold1))[0] box_preds_pos = box_preds[pos_ids] anchors = anchors[pos_ids] pnum = box_preds_pos.shape[0] gnum = gt_boxes.shape[0] #gt_boxes[:,2] -= 0.2 #points[:,2] -= 1 labels_show = np.array([0]*pnum + [1]*gnum) boxes_show = np.concatenate([box_preds_pos, gt_boxes], 0) #Bbox3D.draw_bboxes(boxes_show, 'Z', is_yx_zb=True, labels=labels_show) print(f'pred boxes with scores in [{score_threshold0}, {score_threshold1}]') Bbox3D.draw_points_bboxes(points, boxes_show, 'Z', is_yx_zb=True, labels=labels_show) #print(f'anchors with scores in [{score_threshold0}, {score_threshold1}]') #Bbox3D.draw_points_bboxes(points, anchors, 'Z', is_yx_zb=True) pass
def show__together(self, boxlist_1, max_num=-1, max_num_1=-1, points=None, offset_x=None, twolabels=False, mesh=False, points_keep_rate=POINTS_KEEP_RATE, points_sample_rate=POINTS_SAMPLE_RATE, random_color=False, colors=None): import numpy as np from utils3d.bbox3d_ops import Bbox3D boxes = self.bbox3d.cpu().data.numpy().copy() if max_num > 0 and max_num < boxes.shape[0]: ids = np.random.choice(boxes.shape[0], max_num, replace=False) boxes = boxes[ids] boxes_1 = boxlist_1.bbox3d.cpu().data.numpy().copy() if max_num_1 > 0 and max_num_1 < boxes_1.shape[0]: ids = np.random.choice(boxes_1.shape[0], max_num_1, replace=False) boxes_1 = boxes_1[ids] if offset_x is not None: boxes_1[:,0] += offset_x if not twolabels and 'labels' in self.fields(): labels = self.get_field('labels').cpu().data.numpy().astype(np.int32) labels_1 = boxlist_1.get_field('labels').cpu().data.numpy().astype(np.int32) labels = np.concatenate([labels, labels_1], 0) else: labels = np.array([0]*boxes.shape[0] + [1]*boxes_1.shape[0]) boxes = np.concatenate([boxes, boxes_1], 0) if colors is not None: colors = np.concatenate(colors, 0) assert colors.shape[0] == boxes.shape[0] if points is None: if mesh: Bbox3D.draw_bboxes_mesh(boxes, 'Z', is_yx_zb=self.mode=='yx_zb', labels=labels) else: Bbox3D.draw_bboxes(boxes, 'Z', is_yx_zb=self.mode=='yx_zb', labels=labels, random_color=False) else: if isinstance(points, torch.Tensor): points = points.cpu().data.numpy() if offset_x is not None: tp = points.copy() tp[:,0] += offset_x points = np.concatenate([points, tp], 0) # tp = tp.copy() tp[:,0] += offset_x points = np.concatenate([points, tp], 0) if mesh: Bbox3D.draw_points_bboxes_mesh(points, boxes, 'Z', is_yx_zb=self.mode=='yx_zb', labels=labels, points_keep_rate=points_keep_rate, points_sample_rate=points_sample_rate, random_color=random_color, box_colors=colors) else: Bbox3D.draw_points_bboxes(points, boxes, 'Z', is_yx_zb=self.mode=='yx_zb', labels=labels, random_color=random_color, points_keep_rate=points_keep_rate, points_sample_rate=points_sample_rate, box_colors=colors)
def pos_targets(labels, bbox_targets, anchors, is_show=False,\ gt_boxes=None, points=None): anchors0 = anchors.copy() anchors0[:,2] -= 5 if gt_boxes is None: gt_boxes = np.zeros((0,7), dtype=np.float32) else: gt_boxes =gt_boxes.copy() gt_boxes[:,2] -= 1 if points is None: points = np.zeros((0,3), dtype=np.float32) pos_ids = np.where(labels==1)[0] neg_ids = np.where(labels==0)[0] bbox_targets_pos = bbox_targets[pos_ids] anchors_pos = anchors[pos_ids] pos_num = pos_ids.shape[0] gt_num = gt_boxes.shape[0] anchor_num = anchors.shape[0] if is_show: print(f"gt_box_num:{gt_num} anchor_num: {anchor_num}") print(f"all positive anchors: {pos_num}") labels_show = np.array([0]*pos_num + [1]*gt_num) boxes_show_0 = np.concatenate([anchors_pos, gt_boxes],0) Bbox3D.draw_points_bboxes(points, boxes_show_0, 'Z', is_yx_zb=True, labels=labels_show) # decoded anchors: shoud be exactly the gt_boxes from second.core.box_np_ops import second_box_decode bboxes_decoded = second_box_decode(bbox_targets_pos, anchors_pos, smooth_dim=True) #Bbox3D.draw_bboxes(bboxes_decoded, 'Z', is_yx_zb=True) #Bbox3D.draw_bboxes(gt_boxes, 'Z', is_yx_zb=True) boxes_show = np.concatenate([bboxes_decoded, gt_boxes, anchors0], 0) labels_show = np.array([0]*pos_num + [1]*gt_num + [2]*anchors0.shape[0]) if is_show: print('gt boxes from targets') Bbox3D.draw_points_bboxes(points, boxes_show, 'Z', is_yx_zb=True, labels=labels_show) return bboxes_decoded
def is_close_2walls(wall0, wall1): ''' check if 2 walls are close whem iou < 0.85 ''' assert wall0.shape == wall1.shape == (7, ) dif = (wall0 - wall1) cen_dis = np.linalg.norm(dif[0:3]) size_dif = np.max(np.abs(dif[3:6])) yaw_dif = np.abs(Bbox3D.limit_period(dif[-1], 0.5, np.pi)) close = (cen_dis < 0.05) and (size_dif < 0.07) and (yaw_dif < 0.05) #print(f'cen_dis:{cen_dis}, size_dif:{size_dif}, yaw_dif:{yaw_dif}\nclose:{close}') return close
def gen_junction_labels(pth_fn): pcl, bboxes0 = torch.load(pth_fn) corners = {} for obj in bboxes0: corners_8 = Bbox3D.bboxes_corners(bboxes0[obj], 'Z', False) corners[obj] = (corners_8[:, Bbox3D._yneg_vs, :] + corners_8[:, Bbox3D._ypos_vs, :]) / 2.0 #Bbox3D.draw_bboxes(bboxes0[obj], 'Z', False) #Bbox3D.draw_points(corners[obj].reshape([-1,3])) #Bbox3D.draw_points_bboxes(corners[obj].reshape([-1,3]), bboxes0[obj], 'Z', False) connect_num = cal_connection_label(corners[obj]) import pdb pdb.set_trace() # XXX BREAKPOINT pass