cansik/yolo-hand-detection

Annotation conversion from CMU to YOLO

rshigemura opened this issue · 3 comments

Hi
How did you convert CMU Hand annotations data to YOLO format?

I used all the keypoints from the hands in the CMU dataset and calculated the bounding box out of it.

Are you permitted to share the script you used? I'm trying to retrain YOLO from scratch and it would be extremely valuable!!

Have a look here, I guess this was script (already some time ago). Please adjust the dataset_name variable to point to the correct dataset.

import glob
import os
import json

from scipy.io import loadmat
from PIL import Image

dataset_name = "test"
dataset = "data\\%s" % dataset_name

data = []
annotations = glob.glob("%s\\*.json" % dataset)

# create annotation dict
ann_map = {}
for ann in annotations:
	key = os.path.splitext(os.path.basename(ann))[0][:-2]
	
	if not key in ann_map:
		ann_map[key] = []

	ann_map[key].append(ann)

def load(file):
    with open(file, 'r') as f:
        return json.load(f)

def write(file, content):
	with open(file, 'w') as outfile:
		outfile.write(content)

def bounding_box(xs, ys):
	x_min = min(xs)
	x_max = max(xs)
	y_min = min(ys)
	y_max = max(ys)

	w = x_max - x_min
	h = y_max - y_min

	return (x_min + (w/2), y_min + (h/2), w, h)

for key in ann_map:
	anns = ann_map[key]
	default_file = os.path.basename(ann_map[key][0])
	default_file_name = os.path.splitext(default_file)[0]

	im = Image.open("%s\\%s.jpg" % (dataset, default_file_name))
	width = im.size[0]
	height = im.size[1]

	ann_txt = []

	for annFile in anns:
		filename = os.path.basename(annFile)
		name = os.path.splitext(filename)[0]
		detections = []

		print(annFile)
		desc = load(annFile)
		keypoints = desc["hand_pts"]

		xs = [l[0] for l in keypoints]
		ys = [l[1] for l in keypoints]
		
		x, y, w, h = bounding_box(xs, ys)

		ann_txt.append("%s %s %s %s %s" % (0, x / width, y / height, w / width, h / height))

	write("%s\\%s.txt" % (dataset, default_file_name), '\n'.join(ann_txt))
	data.append("%s\\%s.jpg" % (dataset, default_file_name))


write("%s.txt" % dataset_name, '\n'.join(data))
print("done!")