print('Loading object models...') models = {} for obj_id in range(1, dp['obj_count'] + 1): models[obj_id] = inout.load_ply(dp['model_mpath'].format(obj_id)) # Directories with results for individual scenes scene_dirs = sorted([ d for d in glob.glob(os.path.join(result_path, '*')) if os.path.isdir(d) ]) for scene_dir in scene_dirs: scene_id = int(os.path.basename(scene_dir)) # Load info and GT poses for the current scene scene_info = inout.load_info(dp['scene_info_mpath'].format(scene_id)) scene_gt = inout.load_gt(dp['scene_gt_mpath'].format(scene_id)) res_paths = sorted(glob.glob(os.path.join(scene_dir, '*.yml'))) errs = [] im_id = -1 depth_im = None for res_id, res_path in enumerate(res_paths): # t = time.time() # Parse image ID and object ID from the filename filename = os.path.basename(res_path).split('.')[0] im_id_prev = im_id im_id, obj_id = map(int, filename.split('_')) if res_id % 10 == 0:
for scene_id in scene_ids_curr: obj_id_in_scene_array = list() obj_id_in_scene_array.append(scene_id) if dataset =='doumanoglou' and scene_id == 3: obj_id_in_scene_array = [1, 2] if dataset == 'hinterstoisser' and scene_id == 2: obj_id_in_scene_array = [1, 2, 5, 6, 8, 9, 10, 11, 12] # for occ dataset for obj_id_in_scene in obj_id_in_scene_array: # Load scene info and gt poses print('#' * 20) print('\nreading detector template & info, obj: {}'.format(obj_id_in_scene)) misc.ensure_dir(join(result_base_path, '{:02d}'.format(scene_id))) scene_info = inout.load_info(dp['scene_info_mpath'].format(scene_id)) scene_gt = inout.load_gt(dp['scene_gt_mpath'].format(scene_id)) model = inout.load_ply(dp['model_mpath'].format(obj_id_in_scene)) ###################################################### # prepare renderer rather than rebuilding every time clip_near = 10 # [mm] clip_far = 10000 # [mm] ambient_weight = 0.8 surf_color = None mode = 'rgb+depth' K = dp['cam']['K'] shading = 'phong'
readTemplFrom = './yaml/%s_templ.yaml' readInfoFrom = './yaml/{}_info.yaml' readModelFrom = './models/{0}/{0}.fly' K_cam = None detector = linemodLevelup_pybind.Detector() poseRefine = linemodLevelup_pybind.poseRefine() detector.readClasses(objIds, readTemplFrom) infos = {} models = {} for id in objIds: model = inout.load_ply(readModelFrom.format(id)) models[id] = model templateInfo = inout.load_info(readInfoFrom.format(id)) infos[id] = templateInfo def nms_norms(ts, scores, thresh): order = scores.argsort()[::-1] keep = [] while order.size > 0: # magic: order[[]] = [] i = order[0] keep.append(i) norms = np.linalg.norm(ts[i] - ts[order[1:]], axis=1) inds = np.where(norms > thresh)[0] order = order[inds + 1] return keep
# vis_delta_mpath = vis_base + '{:' + str(dp['im_id_pad']).zfill(2) +\ # 'd}_{:02d}_diff_below_delta={}.jpg' print('Loading object models...') models = {} for obj_id in obj_ids: models[obj_id] = inout.load_ply(dp['model_mpath'].format(obj_id)) # visib_to_below_delta_fracs = [] for data_id in data_ids: if do_vis: misc.ensure_dir( os.path.dirname(vis_mpath.format(dataset, delta, data_id, 0, 0))) # Load scene info and gts info = inout.load_info(dp[info_mpath_key].format(data_id)) gts = inout.load_gt(dp[gt_mpath_key].format(data_id)) im_ids = sorted(gts.keys()) gt_stats = {} for im_id in im_ids: print('dataset: {}, scene/obj: {}, im: {}'.format( dataset, data_id, im_id)) K = info[im_id]['cam_K'] depth_path = dp[depth_mpath_key].format(data_id, im_id) depth_im = inout.load_depth(depth_path) depth_im *= dp['cam']['depth_scale'] # to [mm] im_size = (depth_im.shape[1], depth_im.shape[0]) gt_stats[im_id] = []
mode = 'test' # template_saved_to = join(dp['base_path'], 'linemod', '%s.yaml') # tempInfo_saved_to = join(dp['base_path'], 'linemod', '{:02d}_info.yaml') template_saved_to = join(dp['base_path'], 'linemod_render', '%s.yaml') tempInfo_saved_to = join(dp['base_path'], 'linemod_render', '{:02d}_info.yaml') if mode == 'train': start_time = time.time() # im_ids = list(range(1, 1000, 10)) # obj's img im_ids = [] visual = True misc.ensure_dir(os.path.dirname(template_saved_to)) for obj_id in obj_ids_curr: scene_info = inout.load_info(dp['obj_info_mpath'].format(obj_id)) scene_gt = inout.load_gt(dp['obj_gt_mpath'].format(obj_id)) im_ids_curr = sorted(scene_info.keys()) if im_ids: im_ids_curr = set(im_ids_curr).intersection(im_ids) templateInfo = dict() for im_id in im_ids_curr: print('obj: {}, im: {}'.format(obj_id, im_id)) # Load the images rgb = inout.load_im(dp['train_rgb_mpath'].format(obj_id, im_id)) depth = inout.load_depth(dp['train_depth_mpath'].format( obj_id, im_id))
# Load object models if error_type in ['vsd', 'add', 'adi', 'cou']: print('Loading object models...') models = {} for obj_id in range(1, dp['obj_count'] + 1): models[obj_id] = inout.load_ply(dp['model_mpath'].format(obj_id)) # Directories with results for individual scenes scene_dirs = sorted([d for d in glob.glob(os.path.join(result_path, '*')) if os.path.isdir(d)]) for scene_dir in scene_dirs: scene_id = int(os.path.basename(scene_dir)) # Load info and GT poses for the current scene scene_info = inout.load_info(dp['scene_info_mpath'].format(scene_id)) scene_gt = inout.load_gt(dp['scene_gt_mpath'].format(scene_id)) res_paths = sorted(glob.glob(os.path.join(scene_dir, '*.yml'))) errs = [] im_id = -1 depth_im = None for res_id, res_path in enumerate(res_paths): # t = time.time() # Parse image ID and object ID from the filename filename = os.path.basename(res_path).split('.')[0] im_id_prev = im_id im_id, obj_id = map(int, filename.split('_')) if res_id % 10 == 0:
def load_scene(scene_id): """ Loads the specified scene. """ clean() global ref_im_ind global scene_info global scene_gt # Load scene info scene_info_path = par['scene_info_mpath'].format(scene_id) print('Loading scene info: ' + scene_info_path) scene_info = inout.load_info(scene_info_path) ref_im_id = sorted(scene_info.keys())[ref_im_ind] scene_info_ref = scene_info[ref_im_id] R_w2c = scene_info_ref['cam_R_w2c'] R_w2c_inv = np.linalg.inv(R_w2c) t_w2c = scene_info_ref['cam_t_w2c'] # Load ground truth poses for the reference camera coordinate system scene_gt_path = par['scene_gt_mpath'].format(scene_id) print('Loading GT poses: ' + scene_gt_path) scene_gt = inout.load_gt(scene_gt_path) scene_gt_ref = scene_gt[ref_im_id] # Load scene model #bpy.ops.import_mesh.ply(filepath=scene_model_mpath.format(scene_id)) # Load models of objects that are present in the scene for gt in scene_gt_ref: model_path = par['model_mpath'].format(gt['obj_id']) print('Loading model {}: {}'.format(gt['obj_id'], model_path)) bpy.ops.import_mesh.ply(filepath=model_path) # Take poses from the reference camera coordinate system and transform them # to the world coordinate system (using the known camera-to-world trans.) objs = list(bpy.data.objects) objs_moved = [False for _ in objs] for gt in scene_gt_ref: for i, obj in enumerate(objs): obj_id = int(obj.name.split('obj_')[1].split('.')[0]) if gt['obj_id'] == obj_id and not objs_moved[i]: print('Setting pose of model {}'.format(gt['obj_id'])) #print(i, gt, obj_id, objs_moved[i], obj.location) R_m2w = R_w2c_inv.dot(gt['cam_R_m2c']) t_m2w = R_w2c_inv.dot(gt['cam_t_m2c']) - R_w2c_inv.dot(t_w2c) obj.rotation_euler = transform.euler_from_matrix(R_m2w, 'sxyz') obj.location = t_m2w.flatten().tolist() objs_moved[i] = True break # Align view to the loaded elements for area in bpy.context.screen.areas: if area.type == 'VIEW_3D': ctx = bpy.context.copy() ctx['area'] = area ctx['region'] = area.regions[-1] bpy.ops.view3d.view_selected(ctx) # points view # bpy.ops.view3d.camera_to_view_selected(ctx) # points camera print("Scene loaded.")
# vis_delta_mpath = vis_base + '{:' + str(dp['im_id_pad']).zfill(2) +\ # 'd}_{:02d}_diff_below_delta={}.jpg' print('Loading object models...') models = {} for obj_id in obj_ids: models[obj_id] = inout.load_ply(dp['model_mpath'].format(obj_id)) # visib_to_below_delta_fracs = [] for data_id in data_ids: if do_vis: misc.ensure_dir(os.path.dirname( vis_mpath.format(dataset, delta, data_id, 0, 0))) # Load scene info and gts info = inout.load_info(dp[info_mpath_key].format(data_id)) gts = inout.load_gt(dp[gt_mpath_key].format(data_id)) # Considered subset of images for the current scene if im_ids_sets is not None: im_ids = im_ids_sets[data_id] else: im_ids = sorted(gts.keys()) gt_stats = {} for im_id in im_ids: print('dataset: {}, scene/obj: {}, im: {}'.format(dataset, data_id, im_id)) K = info[im_id]['cam_K'] depth_path = dp[depth_mpath_key].format(data_id, im_id) depth_im = inout.load_depth(depth_path)
readTemplFrom = './yaml/%s_templ.yaml' readInfoFrom = './yaml/{}_info.yaml' readModelFrom = './models/{0}/{0}.fly' K_cam = None detector = linemodLevelup_pybind.Detector() poseRefine = linemodLevelup_pybind.poseRefine() detector.readClasses(objIds, readTemplFrom) infos = {} models = {} for id in objIds: model = inout.load_ply(readModelFrom.format(id)) models[id] = model templateInfo = inout.load_info(readInfoFrom.format(id)) infos[id] = templateInfo def nms_norms(ts, scores, thresh): order = scores.argsort()[::-1] keep = [] while order.size > 0: # magic: order[[]] = [] i = order[0] keep.append(i) norms = np.linalg.norm(ts[i]-ts[order[1:]], axis=1) inds = np.where(norms > thresh)[0] order = order[inds + 1] return keep