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 render_suncg_raw_house_walls(house_fn): from suncg import split_room_parts, Suncg with open(house_fn) as f: house = json.loads(f.read()) scaleToMeters = house['scaleToMeters'] assert scaleToMeters == 1 bboxes = defaultdict(list) bboxes['house'].append(Bbox3D.bbox_from_minmax(house['bbox'])) for level in house['levels']: if 'bbox' not in level: continue bboxes['level'].append(Bbox3D.bbox_from_minmax(level['bbox'])) nodes = level['nodes'] for node in nodes: node_type = node['type'] if node_type == 'Object': modelId = node['modelId'] category = Suncg.modelId_2_class[modelId] bboxes[category].append(Bbox3D.bbox_from_minmax(node['bbox'])) elif node_type == 'Room': if 'bbox' in node: bboxes['room'].append(Bbox3D.bbox_from_minmax( node['bbox'])) room_bboxes = split_room_parts(house_fn, node['modelId']) for c in room_bboxes: bboxes[c] += room_bboxes[c] else: if 'bbox' in node: bboxes[node_type].append( Bbox3D.bbox_from_minmax(node['bbox'])) centroid = (np.array(house['bbox']['min']) + np.array(house['bbox']['max'])) / 2.0 mesh_frame = open3d.create_mesh_coordinate_frame(size=0.6, origin=centroid) for obj in bboxes: bboxes[obj] = np.concatenate([b.reshape([1, 7]) for b in bboxes[obj]], 0) bboxes[obj] = cam2world_box(bboxes[obj]) walls = bboxes['wall'] print('\nThe raw SUNCG walls\n') #Bbox3D.draw_bboxes(walls, up_axis='Z', is_yx_zb=False) Bbox3D.draw_bboxes_mesh(walls, up_axis='Z', is_yx_zb=False)
def preprocess_cfr(ceilings_org, walls_org, obj): ''' Z is up, Y is thickness A ceiling is good: (1) not contains multiple rooms: cover no other ceilings (2) >= 3 edge walls A edge wall of a ceiling: In three points of the wall cenline: two corners and centroid at least two are on an edge of ceiling ''' assert ceilings_org.ndim == walls_org.ndim == 2 dis_threshold = 0.07 if ceilings_org.shape[0] == 0: return ceilings_org if walls_org.shape[0] == 0: return np.zeros(shape=[0, 7], dtype=np.float32) #Bbox3D.draw_bboxes(walls_org, 'Z', False) #Bbox3D.draw_bboxes(ceilings_org, 'Z', False) ceilings = ceilings_org.copy() ceilings, keep_ids0 = clean_repeat(ceilings) walls = walls_org.copy() cn = ceilings.shape[0] ceilings[:, 2] = 0 walls[:, 2] = 0 walls[:, 5] = 0 ceil_corners0 = Bbox3D.bboxes_corners(ceilings, 'Z') ceil_corners = np.take(ceil_corners0, Bbox3D._zpos_vs, axis=1) ceil_corners[:, :, 2] = 0 ceiling_cens = ceilings[:, 0:3] wall_cenlines = Bbox3D.bboxes_centroid_lines(walls, 'X', 'Z') good_ceiling_ids0 = [] bad_small_ids = [] for c in range(cn): # (1) #print(f'c:{c}') ceil_c = ceilings[c:c + 1].copy() ceil_c[:, 3:6] += 0.2 mask_overlap = Bbox3D.points_in_bbox(ceil_corners.reshape([-1, 3]), ceil_c).reshape([cn, 4]) mask_overlap = mask_overlap.all(1) num_overlap = mask_overlap.sum() - 1 ol_ids = np.where(mask_overlap)[0] ol_ids = [i for i in ol_ids if i != c] #if any_overlap: # area_ol = np.product(tmp[ol_ids,3:5], axis=1).sum() # area_c = np.product(ceilings[c,3:5]) # area_ol_rate = area_ol / area_c # import pdb; pdb.set_trace() # XXX BREAKPOINT # pass if Debug and 0: print(f'ceiling, contain {num_overlap} other celings') box_show = np.concatenate([walls_org, ceilings_org[c:c + 1]], 0) Bbox3D.draw_bboxes_mesh(box_show, 'Z', False) if num_overlap > 1: continue #if num_overlap >0: # bad_small_ids += ol_ids # (2) edge_wall_num, winc_state = is_edge_wall_of_ceiling( wall_cenlines, ceilings[c], walls_org) if edge_wall_num >= 3 or (edge_wall_num == 2 and (winc_state == 3).all()): good_ceiling_ids0.append(c) #good_ceiling_ids1 = [i for i in good_ceiling_ids0 if i not in bad_small_ids] good_ceiling_ids1 = keep_ids0[good_ceiling_ids0] good_ceiling_ids1 = np.array(good_ceiling_ids1).astype(np.int) rm_num = cn - good_ceiling_ids1.shape[0] bad_ceiling_ids = np.array( [i for i in range(cn) if i not in good_ceiling_ids1]).astype(np.int32) new_ceilings = ceilings_org[good_ceiling_ids1] if Debug and rm_num > 0: print(f'{cn} -> {good_ceiling_ids1.shape[0]}') box_show = np.concatenate([walls_org, ceilings_org[good_ceiling_ids1]], 0) Bbox3D.draw_bboxes_mesh(box_show, 'Z', False) if rm_num > 0: box_show = np.concatenate( [walls_org, ceilings_org[bad_ceiling_ids]], 0) Bbox3D.draw_bboxes_mesh(box_show, 'Z', False) import pdb pdb.set_trace() # XXX BREAKPOINT #show_walls_1by1(new_ceilings) pass return new_ceilings
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) 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.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) #Bbox3D.draw_points(pcl, points_keep_rate=POINTS_KEEP_RATE, animation_fn='points.mp4', ani_size=AniSizes[house_name]) bboxes[:, 2] += 0.1 Bbox3D.draw_points_bboxes(pcl, bboxes, up_axis='Z', is_yx_zb=False, points_keep_rate=POINTS_KEEP_RATE) Bbox3D.draw_points_bboxes_mesh(pcl, bboxes, up_axis='Z', is_yx_zb=False, points_keep_rate=POINTS_KEEP_RATE)
def preprocess_cfr(ceilings_org, walls_org, obj): ''' Z is up, Y is thickness ''' #Bbox3D.draw_bboxes(walls, 'Z', False) ceilings = ceilings_org.copy() walls = walls_org.copy() walls = replace_slant_walls(walls) dis_threshold = 0.07 ceiling_cens = ceilings[:, 0:3] ceiling_cens[:, 2] = 0 ceil_cenlines_x = Bbox3D.bboxes_centroid_lines(ceilings, 'X', 'Z') ceil_cenlines_x[:, :, 2] = 0 #ceil_cenlines_y = Bbox3D.bboxes_centroid_lines(ceilings, 'Y', 'Z') wall_cenlines = Bbox3D.bboxes_centroid_lines(walls, 'X', 'Z') wall_cenlines[:, :, 2] = 0 ceilings_shrink = ceilings.copy() ceilings_shrink[:, 3:5] -= 0.3 cn = ceilings.shape[0] ## Find edge wall nums good_ceiling_ids = [] for c in range(cn): # (0.1) If no any other overlap ceiling, try to keep it # Otherwise, delete it when >3 wall inside ceiling tmp = np.delete(ceiling_cens.copy(), c, axis=0) any_overlap = Bbox3D.points_in_bbox(tmp, ceilings[c:c + 1]).any() if any_overlap: wall_corner0_in_ceil = Bbox3D.points_in_bbox( wall_cenlines[:, 0, :], ceilings_shrink[c:c + 1]) wall_corner1_in_ceil = Bbox3D.points_in_bbox( wall_cenlines[:, 1, :], ceilings_shrink[c:c + 1]) wall_inside_ceil = wall_corner0_in_ceil + wall_corner1_in_ceil wall_inside_ceil_ids = np.where(wall_inside_ceil)[0] nwic = wall_inside_ceil_ids.shape[0] if nwic > 3: if Debug and 1: print(f'bad ceiling, contain {nwic} walls inside') box_show = np.concatenate( [walls_org, ceilings_org[c:c + 1]], 0) Bbox3D.draw_bboxes_mesh(box_show, 'Z', False) box_show = np.concatenate([ walls_org[wall_inside_ceil_ids], ceilings_org[c:c + 1] ], 0) Bbox3D.draw_bboxes_mesh(box_show, 'Z', False) import pdb pdb.set_trace() # XXX BREAKPOINT continue # (1) the central corners of wall are inside of ceiling wall_cenlines_auged = line_aug(wall_cenlines) cw_cen_dis = ceiling_cens[c].reshape([1, 1, -1]) - wall_cenlines_auged cw_cen_dis = np.linalg.norm(cw_cen_dis, axis=2) ceil_diag_size = np.linalg.norm(ceilings[c, 3:5]) on_inside_ceil = (cw_cen_dis - ceil_diag_size / 2 < dis_threshold).sum(1) >= 2 if Debug and 0: #Bbox3D.draw_points_bboxes(wall_cenlines_auged.reshape([-1,3]), walls, 'Z', False) inside_ids = np.where(on_inside_ceil)[0] box_show = np.concatenate( [walls_org[inside_ids], ceilings_org[c:c + 1]], 0) Bbox3D.draw_bboxes_mesh(box_show, 'Z', False) # (2) along x: wall central line is on x boundaries of ceiling dis_cw = vertical_dis_points_lines(ceil_cenlines_x[c], wall_cenlines) ceil_y_thickness = ceilings[c, 4] mask_x0 = np.abs(dis_cw[0] - dis_cw[1]) < dis_threshold mask_x1 = (np.abs(dis_cw - ceil_y_thickness / 2) < dis_threshold).all(0) mask_x = mask_x0 * mask_x1 * on_inside_ceil wall_on_ceil_boundary_parall_x = np.where(mask_x)[0] num_edgew_x = clean_edge_wall_same_side( wall_cenlines[wall_on_ceil_boundary_parall_x]) # (3) along x: wall central line is on x boundaries of ceiling ceil_x_thickness = ceilings[c, 3] mask_y0 = dis_cw < dis_threshold mask_y1 = np.abs(dis_cw - ceil_x_thickness) < dis_threshold mask_y = (mask_y0 + mask_y1).all(0) mask_y = mask_y * on_inside_ceil wall_on_ceil_boundary_parall_y = np.where(mask_y)[0] num_edgew_y = clean_edge_wall_same_side( wall_cenlines[wall_on_ceil_boundary_parall_y]) #Bbox3D.point_in_box(wall_cenlines, ceilings[]) edge_wall_num = num_edgew_x + num_edgew_y if edge_wall_num >= 3: good_ceiling_ids.append(c) if Debug and edge_wall_num < 3 and 0: print(f'edge_wall_num: {edge_wall_num}') box_show = np.concatenate([walls_org, ceilings_org[c:c + 1]], 0) Bbox3D.draw_bboxes_mesh(box_show, 'Z', False) #Bbox3D.draw_points_bboxes(ceil_cenlines_x[c], box_show, 'Z', False) #Bbox3D.draw_points_bboxes(ceil_cenlines_x[c], ceilings[c:c+1], 'Z', False) edge_walls_x = walls_org[wall_on_ceil_boundary_parall_x] box_x = np.concatenate([edge_walls_x, ceilings_org[c:c + 1]], 0) #Bbox3D.draw_bboxes_mesh(box_x, 'Z', False) edge_walls_y = walls_org[wall_on_ceil_boundary_parall_y] box_y = np.concatenate([edge_walls_y, ceilings_org[c:c + 1]], 0) #Bbox3D.draw_bboxes_mesh(box_y, 'Z', False) walls_inside = walls_org[wall_inside_ceil_ids] box_ins = np.concatenate([walls_inside, ceilings_org[c:c + 1]], 0) #Bbox3D.draw_bboxes_mesh(box_ins, 'Z', False) import pdb pdb.set_trace() # XXX BREAKPOINT pass good_ceiling_ids = np.array(good_ceiling_ids).reshape([-1]) new_cn = good_ceiling_ids.shape[0] print(f'\n\n{obj} {cn} -> {new_cn}') if new_cn == 0: new_ceilings = ceilings_org[0:0] else: new_ceilings = ceilings_org[good_ceiling_ids] if Debug and new_cn < cn: print(good_ceiling_ids) box_show = np.concatenate([walls_org, new_ceilings], 0) Bbox3D.draw_bboxes_mesh(box_show, 'Z', False) bad_ceil_ids = np.array([ i for i in range(cn) if i not in good_ceiling_ids ]).astype(np.int32) if bad_ceil_ids.shape[0] > 0: box_show = np.concatenate([walls_org, ceilings_org[bad_ceil_ids]], 0) Bbox3D.draw_bboxes_mesh(box_show, 'Z', False) import pdb pdb.set_trace() # XXX BREAKPOINT return ceilings