def scenes2ares(seq_path, seq_name): rgb_dir = os.path.join(seq_path, seq_name, 'rgb') depth_dir = os.path.join(seq_path, seq_name, 'depth') poses_dir = os.path.join(seq_path, seq_name, 'poses') frames = sorted(glob.glob(os.path.join(rgb_dir, '*.jpg'))) frame_names = [seq.split('/')[-1].split('.jpg')[0] for seq in frames] default_intrinsic = np.asarray([525, 525, 320, 240, 0, 0], dtype=np.float32) frame_seq = FrameSeqData() # Read the pose for frame_idx, frame_name in enumerate(frame_names): pose_file = os.path.join(poses_dir, frame_name + '.txt') rgb_file = os.path.join(poses_dir, frame_name + '.jpg') depth_file = os.path.join(poses_dir, frame_name + '.png') # Read the pose pose = np.loadtxt(pose_file).astype(np.float32).reshape(4, 4) Tcw = cam_opt.camera_pose_inv(pose[:3, :3], pose[:3, 3]) timestamp = float(frame_idx) frame_seq.append_frame(frame_idx=frame_idx, img_file_name=os.path.join(seq_name, 'rgb', frame_name + '.jpg'), Tcw=Tcw, camera_intrinsic=default_intrinsic, frame_dim=(480, 640), time_stamp=timestamp, depth_file_name=os.path.join(seq_name, 'depth', frame_name + '.png')) return frame_seq
def read_tum_seq(tum_rgbd_base_dir, seq_name): """ Read the SUN3D sequence to the frames collection :param sun3d_seq_dir: input sun3d sequence directory :return: uniform frames collection. """ frames = FrameSeqData() abs_seq_dir = os.path.join(tum_rgbd_base_dir, seq_name) # Read intrinsic mat fx = 525.0 # focal length x fy = 525.0 # focal length y cx = 319.5 # optical center x cy = 239.5 # optical center y K_param = np.asarray([fx, fy, cx, cy, 0.0, 0.0], dtype=np.float32) default_img_dim = (480, 640) if os.path.exists(os.path.join(abs_seq_dir, 'rdpose_associate.txt')): gt_file = 'rdpose_associate.txt' else: gt_file = 'rd_associate.txt' frame_idx = 0 with open(os.path.join(abs_seq_dir, gt_file), 'r') as f: for line in f: # Load frame data if gt_file.startswith('rdpose_associate'): timestamp, img_file_name, _, depth_file_name, _, tx, ty, tz, qx, qy, qz, qw = line.strip( ).split(' ') tx = float(tx) ty = float(ty) tz = float(tz) qx = float(qx) qy = float(qy) qz = float(qz) qw = float(qw) R_mat = trans.quaternion_matrix([qw, qx, qy, qz]).astype(np.float32) t = np.array([tx, ty, tz]).astype(np.float32) Twc_mat = R_mat Twc_mat[:3, 3] = t Tcw = np.linalg.inv(Twc_mat)[:3, :] else: timestamp, img_file_name, _, depth_file_name = line.strip( ).split(' ') Tcw = np.eye(4)[:3, :] frames.append_frame( frame_idx=frame_idx, img_file_name=os.path.join(seq_name, img_file_name), Tcw=Tcw[:3, :], camera_intrinsic=K_param, frame_dim=default_img_dim, time_stamp=float(timestamp), depth_file_name=os.path.join(seq_name, depth_file_name)) frame_idx += 1 return frames
def read_scannet_seq(scannet_base_dir, seq_name): """ Read SCANNET sequences to the frames collection :param input_seq_dir: directory single SCANNET sequence. :return: uniform frames collection. """ frames = FrameSeqData() abs_seq_dir = os.path.join(scannet_base_dir, seq_name) # Read camera intrinsic info. intrinsic_txt = os.path.join(abs_seq_dir, 'info.txt') if not os.path.exists(intrinsic_txt): raise Exception("No camera intrinsic mat.") with open(intrinsic_txt, "r") as intrinsic_file: for line in intrinsic_file: tokens = line.split(' = ') if tokens[0].startswith("m_depthWidth"): frame_w = int(tokens[1].strip()) elif tokens[0].startswith("m_depthHeight"): frame_h = int(tokens[1].strip()) elif tokens[0].startswith("m_depthShift"): shift_factor = float(tokens[1].strip()) if shift_factor != 1000: raise Exception("Depth shift error") elif tokens[0].startswith("m_calibrationDepthIntrinsic"): k_tokens = tokens[1].split(' ')[:16] K = np.asarray(k_tokens, dtype=np.float32).reshape(4, 4) K_param = np.asarray( [K[0, 0], K[1, 1], K[0, 2], K[1, 2], 0.0, 0.0], dtype=np.float32) samples_txt = os.path.join(abs_seq_dir, 'samples.txt') if not os.path.exists(samples_txt): raise Exception("No seq samples info.") with open(samples_txt, "r") as sample_file: for line in sample_file: tokens = line.split(' ') tokens[-1] = tokens[-1].strip() frame_idx = int(tokens[0]) frame_name = tokens[1].split('/')[1].split('.')[0] img_name = os.path.join(seq_name, 'rgb', frame_name + '.color.jpg') depth_name = os.path.join(seq_name, 'depth', frame_name + '.depth.png') Twc = np.asarray(tokens[3:19], dtype=np.float32).reshape((4, 4)) Tcw = np.linalg.inv(Twc)[:3, :] frames.append_frame(frame_idx, img_name, Tcw, K_param, (frame_h, frame_w), depth_file_name=depth_name) return frames
def read_sun3d_seq(sun3d_base_dir, seq_name): """ Read the SUN3D sequence to the frames collection :param sun3d_seq_dir: input sun3d sequence directory :return: uniform frames collection. """ frames = FrameSeqData() abs_seq_dir = os.path.join(sun3d_base_dir, seq_name) # Read intrinsic mat intrinsic_file_path = os.path.join(abs_seq_dir, 'intrinsics.txt') if not os.path.exists(intrinsic_file_path): raise Exception("DIR: %s ----" % abs_seq_dir) K = np.loadtxt(intrinsic_file_path, dtype=np.float32).reshape((3, 3)) K_param = np.asarray([K[0, 0], K[1, 1], K[0, 2], K[1, 2], 0.0, 0.0], dtype=np.float32) default_img_dim = (480, 640) # Read extrinsic poses ext_pose_file_path = sorted(glob.glob(os.path.join(abs_seq_dir, 'extrinsics', '*.txt')))[-1] ext_poses = np.loadtxt(ext_pose_file_path) n_frames = int(ext_poses.shape[0] / 3) ext_poses = ext_poses.reshape((n_frames, 3, 4)) # Synchronize the image and depth with timestamp depth_list = sorted(glob.glob(os.path.join(abs_seq_dir, 'depth', '*.png'))) depth_timestamps = [] for depth_path in depth_list: depth_name = depth_path.split('/')[-1].strip() depth_tokens = depth_name.split('-')[1].split('.')[0] depth_timestamps.append(int(depth_tokens)) depth_timestamps = np.asarray(depth_timestamps) img_list = sorted(glob.glob(os.path.join(abs_seq_dir, 'image', '*.jpg'))) assert len(img_list) == n_frames for frame_idx, img_path in enumerate(img_list): img_name = img_path.split('/')[-1].strip() img_tokens = img_name.split('-') frame_timestamp = int(img_tokens[1].split('.')[0]) # Find the closest depth frame depth_frame_idx = np.argmin(np.abs(depth_timestamps - frame_timestamp)) depth_frame_path = depth_list[depth_frame_idx].split('/')[-1].strip() Twc = ext_poses[frame_idx] frames.append_frame(frame_idx=frame_idx, img_file_name=os.path.join(seq_name, 'image', img_name), Tcw=camera_pose_inv(Twc[:3, :3], Twc[:3, 3]), camera_intrinsic=K_param, frame_dim=default_img_dim, time_stamp=float(frame_timestamp), depth_file_name=os.path.join(seq_name, 'depth', depth_frame_path)) return frames
def scenes2ares(base_dir, seq_name): rgb_dir = os.path.join(base_dir, seq_name, 'rgb') depth_dir = os.path.join(base_dir, seq_name, 'depth') poses_dir = os.path.join(base_dir, seq_name, 'poses') frames = sorted(glob.glob(os.path.join(rgb_dir, '*.color.jpg'))) frame_names = [seq.split('/')[-1].split('.color.jpg')[0] for seq in frames] default_intrinsic = np.asarray([572, 572, 320, 240, 0, 0], dtype=np.float32) default_rgb_intrinsic = np.asarray([1158.3, 1153.53, 649, 483.5, 0, 0], dtype=np.float32) frame_seq = FrameSeqData() # Read the pose frame_idx = 0 for i, frame_name in enumerate(frame_names): pose_file = os.path.join(poses_dir, frame_name + '.pose.txt') INF_flag = False with open(pose_file, 'r') as f: lines = f.readlines() if 'INF' in lines[0]: INF_flag = True if INF_flag: continue # Read the pose pose = np.loadtxt(pose_file).astype(np.float32).reshape(4, 4) Tcw = cam_opt.camera_pose_inv(pose[:3, :3], pose[:3, 3]) timestamp = float(i) frame_seq.append_frame( frame_idx=frame_idx, img_file_name=os.path.join(seq_name, 'rgb', frame_name + '.color.jpg'), Tcw=Tcw, camera_intrinsic=default_intrinsic, frame_dim=(480, 640), time_stamp=timestamp, depth_file_name=os.path.join(seq_name, 'depth', frame_name + '.depth.png'), rgb_intrinsic=default_rgb_intrinsic) frame_idx += 1 return frame_seq