def prepare_data(self): for split_name, value in self.split_dict.items(): image_paths, kps_2d, kps_3d, sequences = [], [], [], [] sub_ids = value['sub_ids'] seq_ids = value['seq_ids'] cam_ids = value['cam_ids'] idx = 0 for sub_id in sub_ids: print('convert subject {} {}/{}'.format(sub_id, sub_ids.index(sub_id) + 1, len(sub_ids))) if seq_ids is None and cam_ids is None: img_dir, image_ids, kp2d, kp3d = self.convert_test(sub_id) for i, image_id in enumerate(image_ids): image_paths.append(img_dir % image_id) kps_2d.append(kp2d[i]) kps_3d.append(kp3d[i]) sequences.append('mpii_ts{}'.format(sub_id)) else: for seq_id in seq_ids: for cam_id in cam_ids: img_dir, num_images, kp2d, kp3d = self.convert_train(sub_id, seq_id, cam_id) idx += 1 for i in range(num_images): image_paths.append(img_dir % (i + 1)) kps_2d.append(kp2d[i]) kps_3d.append(kp3d[i]) image_paths = np.asarray(image_paths) kps_2d = np.asarray(kps_2d) kps_3d = np.asarray(kps_3d) sequences = np.asarray(sequences) if split_name == 'test' else None mpii_config = DataSetConfig(split_name, True, self.mpii_3d_order, lsp_only=True) self.data_set_splits.append(DataSetSplit(mpii_config, image_paths, kps_2d, kps_3d=kps_3d, seqs=sequences))
def create_dataset_split(self, annotations, img_ids, name): def convert_vis(value): if type(value) == int or (type(value) == str and value in ['1', '0']): return int(value) elif isinstance(value, np.ndarray): return int(value.size != 0) else: return 0 images, kps_2d, vis = [], [], [] img_dir = join(self.data_dir, 'images') print("prepare {} mpii annotations for conversion".format(name)) for img_id in tqdm(img_ids): try: ann_info = annotations.annolist[img_id] single_persons = annotations.single_person[img_id] if not isinstance(single_persons, np.ndarray): single_persons = np.array([single_persons]) if single_persons.size == 0: continue rects = ann_info.annorect if not isinstance(rects, np.ndarray): rects = np.array([rects]) persons = rects[single_persons - 1] for person in persons: points = person.annopoints.point if not isinstance(points, np.ndarray): # There is only one! so ignore this image continue kp2d = np.zeros((self.num_kps, 2), np.float32) v = np.zeros((self.num_kps, ), np.float32) for p in points: kp2d[p.id] = [p.x, p.y] v[p.id] = convert_vis(p.is_visible) images.append(join(img_dir, ann_info.image.name)) kps_2d.append(kp2d) vis.append(v) except (AttributeError, TypeError): print('error') continue images = np.asarray(images) kps_2d = np.asarray(kps_2d, dtype=np.float32) vis = np.asarray(vis, dtype=np.int64) mpii_config = DataSetConfig(name, False, self.mpii_order) return DataSetSplit(mpii_config, images, kps_2d, vis)
def prepare_data(self): image_paths = np.array( sorted([f for f in glob(join(self.data_dir, 'images/*.jpg'))])) joints = sio.loadmat(join(self.data_dir, 'joints.mat'), squeeze_me=True)['joints'].astype(np.float32) if self.args.dataset_name == 'lsp_ext': joints = np.transpose(joints, (2, 0, 1)) kps_2d = joints[:, :, :2] vis = joints[:, :, 2].astype(np.int64) lsp_config = DataSetConfig('train', has_3d=False, reorder=self.lsp_order) self.data_set_splits.append( DataSetSplit(lsp_config, image_paths, kps_2d, vis)) elif self.args.dataset_name == 'lsp': joints = np.transpose(joints, (2, 1, 0)) kps_2d = joints[:, :, :2] vis = (1 - joints[:, :, 2]).astype(np.int64) lsp_config_train = DataSetConfig('train', has_3d=False, reorder=self.lsp_order) self.data_set_splits.append( DataSetSplit(lsp_config_train, image_paths[:1000], kps_2d[:1000], vis[:1000])) lsp_config_test = DataSetConfig('val', has_3d=False, reorder=self.lsp_order) self.data_set_splits.append( DataSetSplit(lsp_config_test, image_paths[1000:], kps_2d[1000:], vis[1000:])) else: raise Exception('unknown LSP dataset name')
def prepare_data(self): splits = ['train', 'val'] for split in splits: image_paths, kps_2d, vis = [], [], [] data_type = split + self.args.year img_dir = join(self.data_dir, data_type) # load coco annotations self.coco = COCO( join(self.data_dir, 'annotations', 'person_keypoints_{}.json'.format(data_type))) # get id for category person cat_ids = self.coco.getCatIds(catNms=['person']) # get all image id's containing instances of person category img_ids = self.coco.getImgIds(catIds=cat_ids) # np.random.shuffle(img_ids) # img_ids = img_ids[:1000] print("prepare coco annotations for conversion") for img_id in tqdm(img_ids): img_path = join(img_dir, '%012d.jpg' % img_id) ann_ids = self.coco.getAnnIds(imgIds=img_id, catIds=cat_ids, iscrowd=False) img_annotations = self.coco.loadAnns(ann_ids) for ann in img_annotations: keypoints = np.array(ann['keypoints']) keypoints = np.reshape(keypoints, (-1, 3)) image_paths.append(img_path) kps_2d.append(keypoints[:, :2]) vis.append(keypoints[:, 2] == 2) image_paths = np.asarray(image_paths) kps_2d = np.asarray(kps_2d, dtype=np.float32) vis = np.asarray(vis, dtype=np.int64) # generate zero placeholders coco_config = DataSetConfig(split, False, self.coco_order, self.face_and_shoulder) self.data_set_splits.append( DataSetSplit(coco_config, image_paths, kps_2d, vis))
def prepare_data(self): for split_name, value in self.split_dict.items(): sub_ids = value['sub_ids'] seq_ids = value['seq_ids'] image_paths, kps_2d, kps_3d, sequences = [], [], [], [] idx = 0 for sub_id in sub_ids: print('convert subject {} {}/{}'.format(sub_id, sub_ids.index(sub_id) + 1, len(sub_ids))) ann_path = join(self.data_dir, 'S%d' % sub_id, 'Positions_3D') seqs = [str(s.rsplit('.', 1)[0]) for s in listdir(ann_path)] for seq in seqs: if seq not in seq_ids: continue with open(join(ann_path, seq + '.csv')) as file: reader = csv.reader(file, delimiter=';') content = {} cam_info = [] for row in reader: if row[0] == '#camerainfo': key = 'cam' + row[1] cam_info.append(key) else: key = row[0] content[key] = row[1:] content['#camerainfo'] = cam_info cam_infos = {cam: CameraInfo.from_line(content[cam]) for cam in content['#camerainfo']} num_frames = int(content['#number_of_frames'][0]) num_points = len(content['#point_names']) num_dimens = int(content['#dimensions'][0]) shape = (num_frames, num_points, num_dimens) data = np.empty(shape, dtype=np.float32) for f in range(num_frames): kp3d = np.asarray(content[str(f)]).reshape((-1, 3)) data[f] = kp3d[:num_points] for cam, info in cam_infos.items(): kp3d = np.add(np.tensordot(data, info.R, axes=(2, 1)).reshape(shape), info.T) kp2d = kp3d[:, :, :2] * info.f / np.expand_dims(kp3d[:, :, 2], -1) + info.o sub_dir = 'S%d' % sub_id scene_dir = 'TC_{}_{}_{}'.format(sub_dir, seq, cam) img_dir = join(self.data_dir, sub_dir, 'imageFrames', scene_dir, 'frame_%06d.jpg') idx += 1 for i in range(kp2d.shape[0]): image_paths.append(img_dir % (i + 1)) kps_2d.append(kp2d[i]) kps_3d.append(kp3d[i]) if split_name == 'test': sequences.append(scene_dir) image_paths = np.asarray(image_paths) kps_2d = np.asarray(kps_2d) kps_3d = np.asarray(kps_3d) sequences = np.asarray(sequences) if split_name == 'test' else None if value['skip_frames'] > 1: skip_frames = value['skip_frames'] image_paths = image_paths[::skip_frames] kps_2d = kps_2d[::skip_frames] kps_3d = kps_3d[::skip_frames] sequences = sequences[::skip_frames] if split_name == 'test' else None tc_config = DataSetConfig(split_name, has_3d=True, reorder=self.tc_order, lsp_only=True) self.data_set_splits.append(DataSetSplit(tc_config, image_paths, kps_2d, kps_3d=kps_3d, seqs=sequences))