ornithoscope / csv2json.py
gdamms's picture
first commit
31ea808
import csv
import json
import os
def recursive_file_function(path, function):
for file in os.listdir(path):
if os.path.isdir(os.path.join(path, file)):
recursive_file_function(os.path.join(path, file), function)
else:
function(os.path.join(path, file))
def csv2json(file):
if file[-4:] != '.csv':
return
csv_file = open(file, 'r')
csv_reader = csv.reader(csv_file)
json_file = {}
for row in csv_reader:
if row[0] not in json_file:
json_file[row[0]] = {
"boxes": [],
"size": {
"width": int(float(row[6])),
"height": int(float(row[7])),
},
}
box = {
"xmin": float(row[1]),
"ymin": float(row[2]),
"xmax": float(row[3]),
"ymax": float(row[4]),
"label": row[5],
}
json_file[row[0]]["boxes"].append(box)
csv_file.close()
json_path = file[:-4] + '.json'
with open(json_path, 'w') as f:
json.dump(json_file, f, indent=4)
recursive_file_function('sets', csv2json)