import sys import os object_classes = dict() def convert_coordinates(x_c_n, y_c_n, width_n, height_n, img_width, img_height): ## remove normalization given the size of the image x_c = float(x_c_n) * img_width y_c = float(y_c_n) * img_height width = float(width_n) * img_width height = float(height_n) * img_height ## compute half width and half height half_width = width / 2 half_height = height / 2 left = int(x_c - half_width) + 1 top = int(y_c - half_height) + 1 right = int(x_c + half_width) + 1 bottom = int(y_c + half_height) + 1 return left, top, right, bottom def read_classes(): with open("class_list.txt") as f: for line in f: toks = line.split() id = toks[0] name = ' '.join(toks[1:]) object_classes[id] = name def convert_file(fn, width, height, outputn): with open(outputn, "w+") as output: with open(fn) as f: for line in f: class_id, x_c, y_c, w, h = tuple(line.split()) left, top, right, bottom = convert_coordinates(float(x_c), float(y_c), float(w), float(h), width, height) output.write("{} {} {} {} {}\n".format(object_classes[class_id], left, top, right, bottom)) def convert_annotations(annotation_folder): path = './ground-truth/' + os.path.split(annotation_folder)[-1] + '/' os.mkdir(path) for fn in os.listdir(annotation_folder): if fn.endswith(".txt"): file_name = os.path.basename(fn) width = 0 height = 0 tokens = file_name.split("_") for tok in tokens: if 'x' in tok and 'txt' not in tok: parts = tok.split('x') width = int(parts[0]) height = int(parts[1]) convert_file(annotation_folder+'/'+fn, width, height, path+file_name) if __name__ == "__main__": annotation_folder = sys.argv[1] #video_folder = sys.argv[2] read_classes() convert_annotations(annotation_folder)