-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsontoyolo.py
93 lines (87 loc) · 3.29 KB
/
jsontoyolo.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
81
82
83
84
85
86
87
88
89
90
91
92
93
import json
import glob
from pathlib import Path
import pandas as pd
input_path = "./annotations/*.json"
output_path = "./txt/"
files = glob.glob(input_path)
for file in files:
name = Path(file).stem
with open(file, "r") as f:
data = json.load(f)
cls = []
bbox = []
category_to_id = {'Look at the phone': 0,
'Playing with pen / Taking a pen': 1,
'Playing with fire lighter': 2,
'Playing with the watch': 3,
'Reading the book or notes': 4,
'Sleeping during the exam': 5,
'Writing': 6,
'book': 7,
'hair pulling': 8,
'hand up': 9,
'no people': 10,
'notes': 11,
'phone': 12,
'quarrel and fight': 13,
}
# category_to_id: change it according your dataset
h = data["imageHeight"]
w = data["imageWidth"]
for items in data["shapes"]:
cls.append(items['label'])
classes = [category_to_id.get(e, e) for e in cls]
bbox.append(items["points"])
xxy = []
for box in bbox:
x1 = box[0][0]
x2 = box[1][0]
y1 = box[0][1]
y2 = box[1][1]
xc = round(((x1+x2)/2)/w, 5)
yc = round(((y1+y2)/2)/h, 5)
w1 = round(abs((x1-x2))/w, 5)
h1 = round(abs((y1-y2))/h, 5)
xy = [xc, yc, w1, h1]
xxy.append(xy)
with open(output_path + name + '.txt', 'w') as t:
df = pd.DataFrame(list(zip(classes, xxy)))
df[1] = df[1].astype(str).str[1:-1]
df[1] = df[1].str.replace(',', '')
dfa = df.copy(deep=True)
items = []
items_to_change = []
d = []
d1 = []
cc1 = []
for i in dfa.loc[:, 1]:
if len(i) < 31:
items.append(i)
c = i.split(' ')
k = c.copy()
for cc in c:
if len(cc) < 7:
n = 7-len(cc)
c1 = cc + "0"*n
cc1.append(c1)
for j in cc1:
if not cc == j:
ind = c.index(cc)
k[ind] = j
dd = " ".join(k)
else:
continue
d1.append(dd)
items_to_change.append(dd)
dfb = dfa.loc[:, :].replace(items, items_to_change)
dfAsString = dfb.to_string(header=False, index=False)
# t.write(dfAsString)
tt = [list(x) for x in df.to_records(index=False)]
for i in tt:
classs = i[0]
p = i[1]
xc, yc, ww, hh = tuple([float(x) for x in p.split(' ')])
t.write("%s %.6f %.6f %.6f %.6f\n" % (classs, xc, yc, ww, hh))
t.close()
f.close()