-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_to_groundtruth.py
executable file
·45 lines (31 loc) · 1.39 KB
/
json_to_groundtruth.py
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
import os
import json
import numpy as np
import csv
ROOT_DIR = "../data"
def create_folders():
os.makedirs(os.path.join(ROOT_DIR, "groundtruth"))
os.makedirs(os.path.join(ROOT_DIR, "groundtruth", "train"))
os.makedirs(os.path.join(ROOT_DIR, "groundtruth", "val"))
os.makedirs(os.path.join(ROOT_DIR, "groundtruth", "test"))
def create_annotations():
object_class = 1
for dir_name in os.listdir(os.path.join(ROOT_DIR, "json")):
with open(os.path.join(ROOT_DIR, "json", dir_name, 'annotations.json')) as json_file:
data = json.load(json_file)
for key, value in data.items():
bboxes = []
for segmentation in value["objects"]:
seg_np = np.array(segmentation)
min_x = min(seg_np[:,0])
max_x = max(seg_np[:,0])
min_y = min(seg_np[:,1])
max_y = max(seg_np[:,1])
box_width = (max_x-min_x)
box_height = (max_y-min_y)
bboxes.append([object_class, min_x, min_y, box_width, box_height])
with open(os.path.join(ROOT_DIR, "groundtruth", dir_name, key[:-4]+".txt"),"w+") as my_csv:
csvWriter = csv.writer(my_csv,delimiter=' ')
csvWriter.writerows(bboxes)
create_folders()
create_annotations()