fixed id in dataset_to_mmpose2d

This commit is contained in:
David PAGNON 2024-11-22 00:43:37 +01:00 committed by GitHub
parent 116b8f0791
commit cde7eaa6e3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -56,7 +56,7 @@ biocvplus_markers = ['ACROM_R', 'ACROM_L', 'C7', 'T10', 'CLAV', 'XIP_PROC', 'UA_
## FUNCTIONS ## FUNCTIONS
def str_to_id(string, length=8): def str_to_id(string, length=12):
''' '''
Convert a string to an integer id Convert a string to an integer id
''' '''
@ -285,12 +285,6 @@ def dataset_to_mmpose2d(coords_df, mmpose_json_file, img_size, markerset='custom
- labels2d_json: saved json file - labels2d_json: saved json file
''' '''
# transform first name in integer, and append other numbers from persons
persons = list(set(['_'.join(item.split('_')[:5]) for item in coords_df.columns.levels[1]]))
person_ids = [int(str(str_to_id(p.split('_')[1])) + ''.join(p.split('_')[3:])) if len(p.split('_'))>=3
else str_to_id(p.split('_')[0])
for p in persons]
labels2d_json_data = {} labels2d_json_data = {}
labels2d_json_data['info'] = {'description': f'Bedlam Pose {markerset}', labels2d_json_data['info'] = {'description': f'Bedlam Pose {markerset}',
'url': 'https://github.com/davidpagnon/bedlam_pose', 'url': 'https://github.com/davidpagnon/bedlam_pose',
@ -305,12 +299,11 @@ def dataset_to_mmpose2d(coords_df, mmpose_json_file, img_size, markerset='custom
labels2d_json_data['categories'] = [{'id': 1, 'name': 'person'}] labels2d_json_data['categories'] = [{'id': 1, 'name': 'person'}]
# for each image # for each image
persons = list(set(['_'.join(item.split('_')[:5]) for item in coords_df.columns.levels[1]]))
for i in range(len(coords_df)): for i in range(len(coords_df)):
file_name = coords_df.index[i] file_name = coords_df.index[i][0]
w, h = img_size w, h = int(img_size[0]), int(img_size[1])
# id from concatenation of numbers from path file_id = str_to_id(file_name, length=12)
# file_id = int(''.join(re.findall(r'\d+', str(file_name))))
file_id = int(hashlib.md5(file_name.encode()).hexdigest(), 16) % (10**12) # Keep only 12 digits
labels2d_json_data['images'] += [{'file_name': file_name, labels2d_json_data['images'] += [{'file_name': file_name,
'height': h, 'height': h,
@ -319,7 +312,8 @@ def dataset_to_mmpose2d(coords_df, mmpose_json_file, img_size, markerset='custom
'license': 1}] 'license': 1}]
# for each person # for each person
for p, person in enumerate(persons): for person in persons:
# store 2D keypoints and respect model keypoint order # store 2D keypoints and respect model keypoint order
coords = coords_df.iloc[i, coords_df.columns.get_level_values(1)==person] coords = coords_df.iloc[i, coords_df.columns.get_level_values(1)==person]
coords_list = [] coords_list = []
@ -338,8 +332,9 @@ def dataset_to_mmpose2d(coords_df, mmpose_json_file, img_size, markerset='custom
bbox_height = np.round(max_y - min_y, decimals=1) bbox_height = np.round(max_y - min_y, decimals=1)
# bbox = [min_x, min_y, max_x, max_y] # bbox = [min_x, min_y, max_x, max_y]
bbox = [min_x, min_y, bbox_width, bbox_height] # coco format bbox = [min_x, min_y, bbox_width, bbox_height] # coco format
# bbox = [min_x-bbox_width*0.1, min_y-bbox_height*0.1, bbox_width*1.2, bbox_height]*1.2 # + 10 % margin
person_id = person_ids[p] ann_id = str_to_id(person+file_name)
category_id = 1 category_id = 1
segmentation = [[min_x, min_y, min_x, max_y, max_x, max_y, max_x, min_y]] # no segmentation segmentation = [[min_x, min_y, min_x, max_y, max_x, max_y, max_x, min_y]] # no segmentation
area = np.round(bbox_width * bbox_height, decimals=1) area = np.round(bbox_width * bbox_height, decimals=1)
@ -349,7 +344,7 @@ def dataset_to_mmpose2d(coords_df, mmpose_json_file, img_size, markerset='custom
labels2d_json_data['annotations'] += [{ 'keypoints': coords_list, labels2d_json_data['annotations'] += [{ 'keypoints': coords_list,
'num_keypoints': num_keypoints, 'num_keypoints': num_keypoints,
'bbox': bbox, 'bbox': bbox,
'id': person_id, 'id': ann_id,
'image_id': file_id, 'image_id': file_id,
'category_id': category_id, 'category_id': category_id,
'segmentation': segmentation, 'segmentation': segmentation,