azimuth_range = dp['test_obj_azimuth_range'] elev_range = dp['test_obj_elev_range'] min_n_views = 200 clip_near = 10 # [mm] clip_far = 10000 # [mm] ambient_weight = 0.8 # Weight of ambient light [0, 1] shading = 'phong' # 'flat', 'phong' # Load model model_path = dp['model_mpath'].format(obj_id) model = inout.load_ply(model_path) # Load model texture if dp['model_texture_mpath']: model_texture_path = dp['model_texture_mpath'].format(obj_id) model_texture = inout.load_im(model_texture_path) else: model_texture = None ###################################################### # prepare renderer rather than rebuilding every time texture = model_texture surf_color = None mode = 'rgb+depth' K = dp['cam']['K'] assert ({'pts', 'faces'}.issubset(set(model.keys()))) # Set texture / color of vertices if texture is not None: if texture.max() > 1.0:
t_model = bbox_cens[obj_id - 1, :].reshape((3, 1)) R_model = transform.rotation_matrix(math.pi, [0, 1, 0])[:3, :3] # Extra rotation around Z axis by pi for some models if hinter_flip.obj_flip_z[obj_id]: R_z = transform.rotation_matrix(math.pi, [0, 0, 1])[:3, :3] R_model = R_z.dot(R_model) R_model_inv = np.linalg.inv(R_model) for im_id in im_ids: if im_id % 10 == 0: print('scene,view: ' + str(scene_id) + ',' + str(im_id)) # Load the RGB and depth image rgb = inout.load_im(rgb_in_mpath.format(scene_id, im_id)) depth = load_hinter_depth(depth_in_mpath.format(scene_id, im_id)) depth *= 10.0 # Convert depth map to [100um] # Save the RGB and depth image inout.save_im(rgb_out_mpath.format(scene_id, im_id), rgb) inout.save_depth(depth_out_mpath.format(scene_id, im_id), depth) # Load the GT pose R_m2c = load_hinter_mat(rot_mpath.format(scene_id, im_id)) t_m2c = load_hinter_mat(tra_mpath.format(scene_id, im_id)) t_m2c *= 10 # Convert to [mm] # Transfom the GT pose (to compensate transformation of the models) R_m2c = R_m2c.dot(R_model_inv)
# Load the GT pose pose = load_gt_pose_brachmann(pose_mpath.format(obj_name, im_id)) if pose['R'].size != 0 and pose['t'].size != 0: # Transfom the GT pose R_m2c = pose['R'].dot(R_conv) t_m2c = pose['t'] * 1000 # from [m] to [mm] # Get 2D bounding box of the object model at the ground truth pose obj_bb = misc.calc_pose_2d_bbox(model, par['cam']['im_size'], par['cam']['K'], R_m2c, t_m2c) # Visualisation if False: rgb = inout.load_im(rgb_mpath.format(im_id, im_id)) ren_rgb = renderer.render(model, par['cam']['im_size'], par['cam']['K'], R_m2c, t_m2c, mode='rgb') vis_rgb = 0.4 * rgb.astype(np.float32) + 0.6 * ren_rgb.astype( np.float32) vis_rgb = vis_rgb.astype(np.uint8) vis_rgb = misc.draw_rect(vis_rgb, obj_bb) plt.imshow(vis_rgb) plt.show() scene_gt.setdefault(im_id, []).append({ 'obj_id':
# Load models of objects that appear in the current scene obj_ids = set([gt['obj_id'] for gts in scene_gt.values() for gt in gts]) models = {} for obj_id in obj_ids: models[obj_id] = inout.load_ply(par['model_mpath'].format(obj_id)) # Visualize GT poses in the selected images im_ids_curr = sorted(scene_info.keys()) if im_ids: im_ids_curr = set(im_ids_curr).intersection(im_ids) for im_id in im_ids_curr: print('scene: {}, im: {}'.format(scene_id, im_id)) # Load the images rgb = inout.load_im(par['test_rgb_mpath'].format(scene_id, im_id)) depth = inout.load_depth(par['test_depth_mpath'].format( scene_id, im_id)) depth = depth.astype(np.float32) # [mm] depth *= par['cam']['depth_scale'] # to [mm] # Render the objects at the ground truth poses im_size = (depth.shape[1], depth.shape[0]) ren_rgb = np.zeros(rgb.shape, np.float32) ren_rgb_info = np.zeros(rgb.shape, np.uint8) ren_depth = np.zeros(depth.shape, np.float32) gt_ids_curr = range(len(scene_gt[im_id])) if gt_ids: gt_ids_curr = set(gt_ids_curr).intersection(gt_ids) for gt_id in gt_ids_curr:
im_size_rgb = [int(round(x * float(ssaa_fact))) for x in par['cam']['im_size']] K_rgb = par['cam']['K'] * ssaa_fact for obj_id in obj_ids: # Prepare folders misc.ensure_dir(os.path.dirname(out_rgb_mpath.format(obj_id, 0))) misc.ensure_dir(os.path.dirname(out_depth_mpath.format(obj_id, 0))) # Load model model_path = par['model_mpath'].format(obj_id) model = inout.load_ply(model_path) # Load model texture if par['model_texture_mpath']: model_texture_path = par['model_texture_mpath'].format(obj_id) model_texture = inout.load_im(model_texture_path) else: model_texture = None obj_info = {} obj_gt = {} im_id = 0 for radius in radii: # Sample views views, views_level = view_sampler.sample_views(min_n_views, radius, azimuth_range, elev_range) print('Sampled views: ' + str(len(views))) view_sampler.save_vis(out_views_vis_mpath.format(str(radius)), views, views_level) # Render the object model from all the views
# Considered subset of images for the current scene if im_ids_sets is not None: im_ids_curr = im_ids_sets[scene_id] else: im_ids_curr = sorted(scene_info.keys()) if im_ids: im_ids_curr = set(im_ids_curr).intersection(im_ids) for im_id in im_ids_curr: print('scene: {}, im: {}'.format(scene_id, im_id)) K = scene_info[im_id]['cam_K'] render_K = K # Load the images rgb = inout.load_im(dp['test_rgb_mpath'].format(scene_id, im_id)) depth = inout.load_depth(dp['test_depth_mpath'].format(scene_id, im_id)) depth = depth.astype(np.uint16) # [mm] # depth *= dp['cam']['depth_scale'] # to [mm] im_size = (depth.shape[1], depth.shape[0]) match_ids = list() match_ids.append('{:02d}_template'.format(scene_id)) start_time = time.time() # result = cxx_3d_seg.convex_cloud_seg(rgb, depth, K.astype(np.float32)) result = cxx_3d_seg_pybind.convex_cloud_seg(rgb, depth, K.astype(np.float32)) indices = result.getIndices() cloud = result.getCloud() normal = result.getNormal()
if res_id % 10 == 0: print('Processing: {}, {}, {}, {}, {}, {}'.format( method, dataset, test_type, scene_id, im_id, obj_id)) # Colors if vis_orig_color: color = (1, 1, 1) else: color = tuple(colors[(obj_id - 1) % len(colors)]) color_uint8 = tuple([int(255 * c) for c in color]) # Load the RGB-D image im_size = None if vis_rgb: rgb_path = dp['test_rgb_mpath'].format(scene_id, im_id) rgb = inout.load_im(rgb_path) ren_rgb = np.zeros(rgb.shape, np.float32) ren_rgb_info = np.zeros(rgb.shape, np.uint8) im_size = (rgb.shape[1], rgb.shape[0]) if vis_depth: depth_path = dp['test_depth_mpath'].format(scene_id, im_id) depth = inout.load_depth(depth_path) depth *= dp['cam']['depth_scale'] # to [mm] if im_size: assert(im_size == (depth.shape[1], depth.shape[0])) else: im_size = (depth.shape[1], depth.shape[0]) if vis_depth or (vis_rgb and vis_rgb_resolve_visib): ren_depth = np.zeros((im_size[1], im_size[0]), np.float32)
azimuth_range = (0, 2 * math.pi) elev_range = (0, 0.5 * math.pi) min_n_views = 100 clip_near = 10 # [mm] clip_far = 10000 # [mm] ambient_weight = 0.8 # Weight of ambient light [0, 1] shading = 'phong' # 'flat', 'phong' # Load model model_path = dp['model_mpath'].format(obj_id) model = inout.load_ply(model_path) # Load model texture if dp['model_texture_mpath']: model_texture_path = dp['model_texture_mpath'].format(obj_id) model_texture = inout.load_im(model_texture_path) else: model_texture = None for radius in radii: # Sample views views, views_level = view_sampler.sample_views(min_n_views, radius, azimuth_range, elev_range, tilt_range=(-math.pi/2, math.pi/2), tilt_step=0.2*math.pi) print('Sampled views: ' + str(len(views))) # Render the object model from all the views for view_id, view in enumerate(views): if view_id % 10 == 0: print('obj,radius,view: ' + str(obj_id) + ',' + str(radius) + ',' + str(view_id))