Last active
August 4, 2019 04:11
-
-
Save harrisonford/6e2523bcc8778e6050ad188ee161975a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# convert VIA-VGG annotations to MSCOCO JSON format | |
def load_annotations(json_path): | |
with open(json_path) as f: | |
annotations = json.load(f) | |
return annotations | |
def via2coco(via_json, output_path): | |
# load original via data | |
loaded_json = load_annotations(via_json) | |
# mscoco parts to be saved (example: not saving forehead, that's for mpii database) | |
coco_parts = dict(nose=0, right_shoulder=1, right_elbow=2, right_wrist=3, left_shoulder=4, left_elbow=5, | |
left_wrist=6, right_hip=7, right_knee=8, right_ankle=9, left_hip=10, left_knee=11, left_ankle=12, | |
right_eye=13, left_eye=14, right_ear=15, left_ear=16) | |
# create header info starting coco file | |
# first: 'categories' data | |
categories = [] | |
category = dict(supercategory='person', id=1, skeleton=[], | |
keypoints=['nose', 'right_shoulder', 'right_elbow', 'right_wrist', 'left_shoulder', | |
'left_elbow', 'left_wrist', 'right_hip', 'right_knee', 'right_ankle', 'left_hip', | |
'left_knee', 'left_ankle', 'right_eye', 'left_eye', 'right_ear', 'left_ear'], | |
name='baby') | |
categories.append(category) | |
# second: 'annotations' data | |
annotations = [] | |
filenames = [] | |
area = 0 | |
via_annotations = loaded_json['_via_img_metadata'] | |
for an_item in via_annotations.values(): | |
filename = an_item['filename'] | |
filenames.append(filename) | |
image_id = filename.split('_') | |
image_id = image_id.pop() | |
image_id = int(image_id[:-4]) | |
# generate keypoint vector | |
keypoint_vector = np.zeros(len(coco_parts.keys())*3) # x-y-visible, make it json serializable later | |
bbox = [] # bounding baby box | |
regions = an_item['regions'] | |
for a_region in regions: | |
shape = a_region['shape_attributes'] | |
region = a_region['region_attributes'] | |
if region['id'] in coco_parts.keys(): # recognize mscoco part | |
index = coco_parts[region['id']]*3 | |
keypoint_vector[index] = shape['cx'] | |
keypoint_vector[index + 1] = shape['cy'] | |
keypoint_vector[index + 2] = int(region['visible']) + 1 # visible in coco is 1 false 2 true | |
if region['id'] == 'body': | |
area = shape['width'] * shape['height'] | |
x0 = shape['x'] | |
w = shape['width'] | |
y0 = shape['y'] | |
h = shape['height'] | |
# save bbox in this weird cocotools notation | |
bbox.append(int(x0)) | |
bbox.append(int(y0)) | |
bbox.append(int(w)) | |
bbox.append(int(h)) | |
# TODO: consolidate this parameters appart from keypoints | |
annotation = dict(image_id=image_id, category_id=1, iscrowd=0, num_keypoints=len(keypoint_vector)/3, | |
id=image_id, segmentation=[], area=area, keypoints=keypoint_vector.tolist(), bbox=bbox) | |
# TODO: id=image_id OR filename? | |
annotations.append(annotation) | |
# third: 'info' data | |
info = dict(url='https://github.com/harrisonford', contributor='harrisonford', year=2019, description='alpha', | |
date_created='2019', version=1.0) | |
# fourth: 'images' data | |
images = [] | |
for a_file in filenames: | |
subname = a_file.split('_') | |
file_id = subname[1] | |
file_id = int(file_id[:-4]) # take ".png" out | |
# TODO: We add a lot of dummy numbers to image dict (check wid-hei most importantly) | |
an_image = dict(date_captured='secret_hehexd', id=file_id, coco_url='no-coco', height=0, width=0, license=0, | |
file_name=a_file, flickr_url='who_uses_flicker?') | |
images.append(an_image) | |
# fifth: 'licenses' data | |
licenses = ['private'] | |
# put data in final dictionary | |
data = dict(categories=categories, annotations=annotations, info=info, images=images, licenses=licenses) | |
with open(output_path, 'w') as outfile: | |
json.dump(data, outfile) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, thanks for your code. What is difference between json_path and via_json and _via_img_metadata?
And how do you run this code?