| 44 | |
| 45 | |
| 46 | class SensorData: |
| 47 | |
| 48 | def __init__(self, filename): |
| 49 | self.version = 4 |
| 50 | self.load(filename) |
| 51 | |
| 52 | |
| 53 | def load(self, filename): |
| 54 | with open(filename, 'rb') as f: |
| 55 | version = struct.unpack('I', f.read(4))[0] |
| 56 | assert self.version == version |
| 57 | strlen = struct.unpack('Q', f.read(8))[0] |
| 58 | self.sensor_name = ''.join(struct.unpack('c'*strlen, f.read(strlen))) |
| 59 | self.intrinsic_color = np.asarray(struct.unpack('f'*16, f.read(16*4)), dtype=np.float32).reshape(4, 4) |
| 60 | self.extrinsic_color = np.asarray(struct.unpack('f'*16, f.read(16*4)), dtype=np.float32).reshape(4, 4) |
| 61 | self.intrinsic_depth = np.asarray(struct.unpack('f'*16, f.read(16*4)), dtype=np.float32).reshape(4, 4) |
| 62 | self.extrinsic_depth = np.asarray(struct.unpack('f'*16, f.read(16*4)), dtype=np.float32).reshape(4, 4) |
| 63 | self.color_compression_type = COMPRESSION_TYPE_COLOR[struct.unpack('i', f.read(4))[0]] |
| 64 | self.depth_compression_type = COMPRESSION_TYPE_DEPTH[struct.unpack('i', f.read(4))[0]] |
| 65 | self.color_width = struct.unpack('I', f.read(4))[0] |
| 66 | self.color_height = struct.unpack('I', f.read(4))[0] |
| 67 | self.depth_width = struct.unpack('I', f.read(4))[0] |
| 68 | self.depth_height = struct.unpack('I', f.read(4))[0] |
| 69 | self.depth_shift = struct.unpack('f', f.read(4))[0] |
| 70 | num_frames = struct.unpack('Q', f.read(8))[0] |
| 71 | self.frames = [] |
| 72 | for i in range(num_frames): |
| 73 | frame = RGBDFrame() |
| 74 | frame.load(f) |
| 75 | self.frames.append(frame) |
| 76 | |
| 77 | |
| 78 | def export_depth_images(self, output_path, image_size=None, frame_skip=1): |
| 79 | if not os.path.exists(output_path): |
| 80 | os.makedirs(output_path) |
| 81 | print 'exporting', len(self.frames)//frame_skip, ' depth frames to', output_path |
| 82 | for f in range(0, len(self.frames), frame_skip): |
| 83 | depth_data = self.frames[f].decompress_depth(self.depth_compression_type) |
| 84 | depth = np.fromstring(depth_data, dtype=np.uint16).reshape(self.depth_height, self.depth_width) |
| 85 | if image_size is not None: |
| 86 | depth = cv2.resize(depth, (image_size[1], image_size[0]), interpolation=cv2.INTER_NEAREST) |
| 87 | #imageio.imwrite(os.path.join(output_path, str(f) + '.png'), depth) |
| 88 | with open(os.path.join(output_path, str(f) + '.png'), 'wb') as f: # write 16-bit |
| 89 | writer = png.Writer(width=depth.shape[1], height=depth.shape[0], bitdepth=16) |
| 90 | depth = depth.reshape(-1, depth.shape[1]).tolist() |
| 91 | writer.write(f, depth) |
| 92 | |
| 93 | def export_color_images(self, output_path, image_size=None, frame_skip=1): |
| 94 | if not os.path.exists(output_path): |
| 95 | os.makedirs(output_path) |
| 96 | print 'exporting', len(self.frames)//frame_skip, 'color frames to', output_path |
| 97 | for f in range(0, len(self.frames), frame_skip): |
| 98 | color = self.frames[f].decompress_color(self.color_compression_type) |
| 99 | if image_size is not None: |
| 100 | color = cv2.resize(color, (image_size[1], image_size[0]), interpolation=cv2.INTER_NEAREST) |
| 101 | imageio.imwrite(os.path.join(output_path, str(f) + '.jpg'), color) |
| 102 | |
| 103 | |