-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsave_json.py
80 lines (62 loc) · 2.22 KB
/
save_json.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# -*- encoding: utf-8 -*-
"""
@File : save_json.py
@Time : 2020/5/4 18:08
@Author : Alessia K
@Email : ------
"""
import json
import numpy as np
import os
import csv
def label_list(path):
with open(path, 'r', encoding='utf-8') as f:
class_csv = csv.reader(f, delimiter=',')
result = {}
for line, row in enumerate(class_csv):
line += 1
try:
class_name, class_id = row
except ValueError:
# raise_from(ValueError('line {}: format should be \'class_name,class_id\''.format(line)), None)
raise (ValueError('line {}: format should be \'class_name,class_id\''.format(line)), None)
if class_name in result:
raise ValueError('line {}: duplicate class name: \'{}\''.format(line, class_name))
result[int(class_id)] = class_name
return result
def create_class_dict(path):
with open(os.path.join(path, 'classify_rule.json'), 'r', encoding='utf-8') as f:
classify_rule = json.load(f)
class_name = {}
for keys, vals in classify_rule.items():
for i in range(len(vals)):
class_name[str(vals[i])]=str(keys)
return class_name
def get_classes_name(class_name, class_name_i):
return class_name[class_name_i] + '/' + class_name_i
def save_result_as_json(img_name, classes, scores, bboxes, time):
"""
{
"detection_classes": []
"detection_scores": [] (.4f)
"detection_bboxes": [] (xmin、ymin、xmax、ymax) (.1f)
"latency_time": "" (str(.1f))
}
"""
scores = np.around(scores.astype(np.float), decimals=4).tolist()
bboxes = np.around(bboxes.astype(np.float), decimals=1).tolist()
print(bboxes)
save_file = {}
save_file["detection_classes"] = classes
save_file["detection_scores"] = scores
save_file["detection_boxes"] = bboxes
save_file["latency_time"] = "{:.1f} ms".format(time)
return save_file
# save_name = os.path.join('data', os.path.basename(img_name).replace('.jpg', '.json'))
# with open(save_name, 'w+', encoding='utf-8')as f:
# json.dump(save_file, f, indent=4, ensure_ascii=False)
if __name__ == '__main__':
class_name = create_class_dict(r'D:\Work\ohter\SodicData\train_val')
print(class_name)
labels = label_list('data/class_name.csv')
print(labels)