forked from pjreddie/darknet
-
Notifications
You must be signed in to change notification settings - Fork 11
/
crowdhuman_train_anno.py
120 lines (90 loc) · 3.76 KB
/
crowdhuman_train_anno.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import numpy
import cv2
from multiprocessing import Process, Manager
import yaml
from tqdm import tqdm
intPERSON = 0
intHEAD = 1
manager = Manager()
return_dict = manager.dict()
def generate_annotations(ii, return_dict):
line = return_dict[ii]
del return_dict[ii]
dictLine = yaml.load(line)
strID = dictLine['ID']
img = cv2.imread('crowdhuman_train/{}.jpg'.format(strID),1)
imgWidth = img.shape[1]
imgHeight = img.shape[0]
# Create .txt label file
with open('crowdhuman_train/{}.txt'.format(strID), 'w+') as txtf:
for label in dictLine['gtboxes']:
if 'extra' in label and 'ignore' in label['extra'] and label['extra']['ignore'] == 1:
continue
if 'extra' in label and 'unsure' in label['extra'] and label['extra']['unsure'] == 1:
continue
# Person BB
px = float(label['fbox'][0])
py = float(label['fbox'][1])
pw = float(label['fbox'][2])
ph = float(label['fbox'][3])
# Head BB
hx = float(label['hbox'][0])
hy = float(label['hbox'][1])
hw = float(label['hbox'][2])
hh = float(label['hbox'][3])
# Absolute person BB
cpx = px + pw/2
cpy = py + ph/2
abspx = cpx / imgWidth
abspy = cpy / imgHeight
abspw = pw / imgWidth
absph = ph / imgHeight
abspx = 1 if abspx > 1 else abspx
abspy = 1 if abspy > 1 else abspy
abspw = 1 if abspw > 1 else abspw
absph = 1 if absph > 1 else absph
abspx = 0.000001 if abspx < 0 else abspx
abspy = 0.000001 if abspy < 0 else abspy
abspw = 0.000001 if abspw < 0 else abspw
absph = 0.000001 if absph < 0 else absph
# Absolute head BB
chx = hx + hw/2
chy = hy + hh/2
abshx = chx / imgWidth
abshy = chy / imgHeight
abshw = hw / imgWidth
abshh = hh / imgHeight
abshx = 1 if abshx > 1 else abshx
abshy = 1 if abshy > 1 else abshy
abshw = 1 if abshw > 1 else abshw
abshh = 1 if abshh > 1 else abshh
abshx = 0.000001 if abshx < 0 else abshx
abshy = 0.000001 if abshy < 0 else abshy
abshw = 0.000001 if abshw < 0 else abshw
abshh = 0.000001 if abshh < 0 else abshh
# Write to file
txtf.write('{} {:.4f} {:.4f} {:.4f} {:.4f}\n'.format(intPERSON,
abspx,
abspy,
abspw,
absph))
txtf.write('{} {:.4f} {:.4f} {:.4f} {:.4f}\n'.format(intHEAD,
abshx,
abshy,
abshw,
abshh))
with open('annotation_train.odgt') as f:
processes = []
max_iter = 500
for ii, line in tqdm(enumerate(f)):
return_dict[ii] = line
pcs = Process(target = generate_annotations, args = (ii, return_dict))
processes.append(pcs)
pcs.start()
if ii % max_iter == 0:
for jj in range(len(processes)):
processes[jj].join()
processes = []
for jj in range(len(processes)):
processes[jj].join()
processes = []