File size: 1,162 Bytes
31ea808
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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)