def load_annotations_mat(self): """ Load ILSVRC2012 annotations (.mat file). """ # load annotation file filename = self.get_file_path('meta.mat') return load_matlab(filename)
def load_annotations(self): """ Load annotations from file and split them to train and test sets. """ annot_filepath, images_dir = self.fetch_annotations_images_paths() # load annotations file annotations = load_matlab(annot_filepath) data = { "train": [], "test": [] } image_filenames = os.listdir(images_dir) image_filenames.sort() for i in range(0, 2000): if i >= 1000: set_name = 'test' else: set_name = 'train' filename = os.path.join(self.data_path, 'images', image_filenames[i]) joints = [] for j in range(0, 14): joints.append([annotations['joints'][0][j][i], # x annotations['joints'][1][j][i], # y annotations['joints'][2][j][i]]) # is_visible (0 - visible, # 1 - hidden) data[set_name].append({"filename": filename, "joints": joints}) return data
def load_annotations(self): """ Load annotations from file and split them to train and test sets. """ annot_filepath = os.path.join(self.data_path, 'FLIC', 'examples.mat') # load annotations file annotations = load_matlab(annot_filepath) data = { "train": [], "test": [] } for i, annot in enumerate(annotations['examples'][0]): if annot[-1][0][0] == 0: set_name = 'train' else: set_name = 'test' width, height, _ = annot[4][0].tolist() moviename = annot[1][0] filename = os.path.join(self.data_path, 'FLIC', 'images', annot[3][0]) torso_box = annot[6][0].squeeze().tolist(), # [x1,y1,x2,y2] parts = [ # [x, y, is_visible] [annot[2][0][0], annot[2][1][0], 1], # -- 1, Left_Shoulder [annot[2][0][1], annot[2][1][1], 1], # -- 2, Left_Elbow [annot[2][0][2], annot[2][1][2], 1], # -- 3, Left_Wrist [annot[2][0][3], annot[2][1][3], 1], # -- 4, Right_Shoulder [annot[2][0][4], annot[2][1][4], 1], # -- 5, Right_Elbow [annot[2][0][5], annot[2][1][5], 1], # -- 6, Right_Wrist [annot[2][0][6], annot[2][1][6], 1], # -- 7, Left_Hip [annot[2][0][9], annot[2][1][9], 1], # -- 8, Right_Hip [annot[2][0][12], annot[2][1][12], 1], # -- 9, Left_Eye [annot[2][0][13], annot[2][1][13], 1], # -- 10, Right_Eye [annot[2][0][16], annot[2][1][16], 1] # -- 11, Nose ] data[set_name].append({ "filename": filename, "width": width, "height": height, "moviename": moviename, "torso_box": torso_box, "parts": parts }) return data
def load_annotations(self): """ Load annotations from file and split them to train and test sets. """ annot_filepath_lsp = os.path.join(self.data_path, 'lsp_dataset', 'joints.mat') annot_filepath_lspe = os.path.join(self.data_path, 'joints.mat') # load annotations file annotations_lsp = load_matlab(annot_filepath_lsp) annotations_lspe = load_matlab(annot_filepath_lspe) data = { "train": [], "test": [] } # --------------------------- # add lsp train + test data # --------------------------- image_filenames_lsp = os.listdir(os.path.join(self.data_path, 'lsp_dataset', 'images')) image_filenames_lsp.sort() for i in range(0, 2000): if i >= 1000: set_name = 'test' else: set_name = 'train' filename = os.path.join(self.data_path, 'lsp_dataset', 'images', image_filenames_lsp[i]) joints = [] for j in range(0, 14): # is_visible => (0 - visible, 1 - hidden) joints.append([annotations_lsp['joints'][0][j][i], # x annotations_lsp['joints'][1][j][i], # y annotations_lsp['joints'][2][j][i]]) # is_visible data[set_name].append({"filename": filename, "joints": joints}) # ----------------------- # add lspe train data # ----------------------- image_filenames_lspe = os.listdir(os.path.join(self.data_path, 'images')) image_filenames_lspe.sort() set_name = 'train' for i in range(0, 10000): filename = os.path.join(self.data_path, 'images', image_filenames_lspe[i]) joints = [] for j in range(0, 14): # is_visible => (0 - visible, 1 - hidden) joints.append([annotations_lspe['joints'][j][0][i], # x annotations_lspe['joints'][j][1][i], # y annotations_lspe['joints'][j][2][i]]) # is_visible data[set_name].append({"filename": filename, "joints": joints}) return data
def load_annotations(self): """ Load annotations from file and split them to train and test sets. """ annot_filepath = os.path.join(self.data_path, 'mpii_human_pose_v1_u12_2', 'mpii_human_pose_v1_u12_1.mat') if self.verbose: print('\n> Loading annotations file: {}'.format(annot_filepath)) # load annotations file annotations = load_matlab(annot_filepath) # total number of files nfiles = len(annotations["RELEASE"][0][0][3]) # progressbar if self.verbose: print('\n> Parsing data from the annotations...') prgbar = progressbar.ProgressBar(max_value=nfiles) data = {"train": [], "test": []} # cycle all files for ifile in range(nfiles): if annotations['RELEASE'][0][0][1][0][ifile] == 0: set_name = 'test' else: set_name = 'train' # single person single_person = [0] if any(annotations['RELEASE'][0][0][3][ifile][0]): for i in range(len(annotations['RELEASE'][0][0][3][ifile][0])): single_person.append( int(annotations['RELEASE'][0][0][3][ifile][0][i][0])) # activity/action id act = {"cat_name": '', "act_name": '', "act_id": -1} if any(annotations['RELEASE'][0][0][4][ifile][0][0]): act = { "cat_name": str(annotations['RELEASE'][0][0][4][ifile][0][0][0]), "act_name": str(annotations['RELEASE'][0][0][4][ifile][0][1][0]), "act_id": int(annotations['RELEASE'][0][0][4][ifile][0][2][0][0]) } # image annots image_filename = os.path.join( self.data_path, 'images', str(annotations['RELEASE'][0][0][0][0][ifile][0][0][0][0][0])) if any(annotations['RELEASE'][0][0][0][0][ifile][3][0]): frame_sec = int( annotations['RELEASE'][0][0][0][0][ifile][2][0][0]) video_idx = int( annotations['RELEASE'][0][0][0][0][ifile][3][0][0]) else: frame_sec, video_idx = -1, -1 # properties field names ('scale', 'objpos', ...) try: pnames = annotations['RELEASE'][0][0][0][0][ifile][1][ 0].dtype.names except IndexError: data[set_name].append({ "image_filename": image_filename, "frame_sec": frame_sec, "video_idx": video_idx, "poses_annotations": [], "activity": act, "single_person": single_person }) continue # skip rest # parse keypoints poses_annots = [] if any(annotations['RELEASE'][0][0][0][0][ifile][3][0]): for i in range( len(annotations['RELEASE'][0][0][0][0][ifile][1][0])): try: keypoints = [[0, 0, 0]] * 16 # [x, y, is_visible] annot = annotations['RELEASE'][0][0][0][0][ifile][1][ 0][i][4][0][0][0][0] vnames = annot.dtype.names for j in range(len(annot)): x = float(annot[j][vnames.index('x')][0][0]) y = float(annot[j][vnames.index('y')][0][0]) idx = int(annot[j][vnames.index('id')][0][0]) try: is_visible = int( annot[j][vnames.index('is_visible')][0]) except (ValueError, IndexError): is_visible = -1 try: keypoints[idx] = [x, y, is_visible] except IndexError as k: if set_name == 'test' or self.is_full: print('Error: ', str(k)) keypoints[idx] = [0, 0, 0] else: continue # skip this annotation except (AttributeError, IndexError): keypoints = [[0, 0, 0]] * 16 # [x, y, is_visible] try: x1 = float(annotations['RELEASE'][0][0][0][0][ifile][1] [0][i][pnames.index('x1')][0][0]) y1 = float(annotations['RELEASE'][0][0][0][0][ifile][1] [0][i][pnames.index('y1')][0][0]) x2 = float(annotations['RELEASE'][0][0][0][0][ifile][1] [0][i][pnames.index('x2')][0][0]) y2 = float(annotations['RELEASE'][0][0][0][0][ifile][1] [0][i][pnames.index('y2')][0][0]) except ValueError: if set_name == 'test' or self.is_full: x1, y1, x2, y2 = -1, -1, -1, -1 else: continue # skip this annotation try: annot_ptr = annotations['RELEASE'][0][0][0][0][ifile][ 1][0] objnames = annot_ptr[i][pnames.index( 'objpos')][0].dtype.names # objnames = annotations['RELEASE'][0][0][0][0][ifile][1][0][i] # [pnames.index('objpos')][0].dtype.names scale = float(annotations['RELEASE'][0][0][0][0][ifile] [1][0][i][pnames.index('scale')][0][0]) objpos = { "x": float(annotations['RELEASE'][0][0][0][0][ifile][1] [0][i][pnames.index('objpos')][0][0][ objnames.index('x')][0][0]), "y": float(annotations['RELEASE'][0][0][0][0][ifile][1] [0][i][pnames.index('objpos')][0][0][ objnames.index('y')][0][0]) } except (ValueError, IndexError): if set_name == 'test' or self.is_full: scale = -1 objpos = {"x": -1, "y": -1} else: continue # skip this annotation poses_annots.append({ "x1": x1, "y1": y1, "x2": x2, "y2": y2, "keypoints": keypoints, "scale": scale, "objpos": objpos }) else: if set_name == 'test' or self.is_full: for i in range( len(annotations['RELEASE'][0][0][0][0][ifile][1] [0])): try: annot_ptr = annotations['RELEASE'][0][0][0][0][ ifile][1][0] objnames = annot_ptr[i][pnames.index( 'objpos')][0].dtype.names # objnames = annotations['RELEASE'][0][0][0][0][ifile][1][0][i] # [pnames.index('objpos')][0].dtype.names scale = float( annotations['RELEASE'][0][0][0][0][ifile][1][0] [i][pnames.index('scale')][0][0]) objpos = { "x": float(annotations['RELEASE'][0][0][0][0][ifile] [1][0][i][pnames.index('objpos')][0][0][ objnames.index('x')][0][0]), "y": float(annotations['RELEASE'][0][0][0][0][ifile] [1][0][i][pnames.index('objpos')][0][0][ objnames.index('y')][0][0]) } except (IndexError, ValueError, AttributeError): scale = -1 objpos = {"x": -1, "y": -1} poses_annots.append({"scale": scale, "objpos": objpos}) else: continue # skip this annotation # add fields to data data[set_name].append({ "image_filename": image_filename, "frame_sec": frame_sec, "video_idx": video_idx, "poses_annotations": poses_annots, "activity": act, "single_person": single_person }) # update progressbar if self.verbose: prgbar.update(ifile) # update progressbar if self.verbose: prgbar.finish() # fetch video ids videonames = [] for ivideo in range(len(annotations['RELEASE'][0][0][5][0])): videonames.append( str(annotations['RELEASE'][0][0][5][0][ivideo][0])) return data, videonames
def load_file(self, filename): """Loads the data of the annotation file.""" return load_matlab(filename)