-
Notifications
You must be signed in to change notification settings - Fork 1
/
script_dataset.py
275 lines (245 loc) · 11.8 KB
/
script_dataset.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
from utils.load_dataset import *
from utils.custom_utils import *
DELTA = 1
def build_virtual(filename):
if os.path.exists(filename):
print("File already created")
else:
files = glob.glob('data/**/*.txt', recursive = True) #find all *.txt files
#FILES NOT ORIG (normalized)
fw = open(os.path.join("datasets",filename),'w')
i = 0
print("Creating virtual dataset...")
for fi in tqdm(files):
if "orig" not in fi: #discard all *_orig.txt
f = open(fi, 'r')
file_text = f.read()
file_text = file_text.strip('\n') #remove possible newline character at the end of file
lines = file_text.split('\n')
file_name = os.path.join(fi.split("/")[-2],fi.split("/")[-1]) #last / and first part before the . with the name of the folder
if len(lines) == 0:
continue
for l in lines:
if l == '':
continue
if float(l.split(" ")[6]) < 0.81: #discard images with occlusion too high
label = l.split(" ")[0]
if int(label) == 6 or int(label) == 7:
name = file_name.split(".")[0] + ".png"
image = cv2.imread(os.path.join('data',name))
height,width, _ = image.shape #values to denormalize data
xc = float(l.split(" ")[1]) * width
yc = float(l.split(" ")[2]) * height
w = float(l.split(" ")[3]) * width
h = float(l.split(" ")[4]) * height
#take values as (x0,y0) and (x1,y1) for top-left and bottom-right points of BB
x0 = float(xc - w / 2)
y0 = float(yc - h / 2)
x1 = float(xc + w / 2)
y1 = float(yc + h / 2)
if int(label) == 6: #label 1 -> NO FALL
fw.write(name + ',' + str(x0) + ',' + str(y0) + "," + str(x1) + "," + str(y1) + "," + str(1) + "\n")
elif int(label) == 7: #label 2 -> FALL
fw.write(name + ',' + str(x0) + ',' + str(y0) + "," + str(x1) + "," + str(y1) + "," + str(2) + "\n")
fw.close()
print("Completed!")
def build_real(filename):
if os.path.exists(filename):
print("File already created")
else:
filename = filename.split(".")
filenamev = filename[0]+"_valid.txt"
filenamet = filename[0]+"_train.txt"
files = glob.glob('real_dataset/valid/**/*.png', recursive = True) #find all *.png files
fw = open(os.path.join("datasets",filenamev),'w')
i = 0
print("Creating validation dataset...")
for fi in tqdm(files):
fi = fi.split(".")[0] + ".txt"
f = open(fi, 'r')
file_text = f.read()
if file_text == "": #discard empty files
continue
file_text = file_text.strip('\n') #remove possible newline character at the end of file
lines = file_text.split('\n')
file_name = os.path.join(fi.split("/")[-2],fi.split("/")[-1])
for l in lines:
name = file_name.split(".")[0] + ".png"
image = cv2.imread(f'real_dataset/valid/{name}')
y,x,_ = image.shape
label = l.split(" ")[0]
x0 = l.split(" ")[1]
x1 = l.split(" ")[2]
y0 = l.split(" ")[3]
y1 = l.split(" ")[4]
if int(x0) < 0 or int(x1) > x:
continue
if int(y0) < 0 or int(y1) > y:
continue
if int(label) == -1: #label 1 -> NO FALL
fw.write(name + ',' + str(x0) + ',' + str(y0) + "," + str(x1) + "," + str(y1) + "," + str(1) + "\n")
elif int(label) == 1: #label 2 -> FALL
fw.write(name + ',' + str(x0) + ',' + str(y0) + "," + str(x1) + "," + str(y1) + "," + str(2) + "\n")
fw.close()
print("Validation dataset completed!")
files = glob.glob('real_dataset/train/**/*.png', recursive = True) #find all *.txt files
fw = open(os.path.join("datasets",filenamet),'w')
i = 0
print("Creating training dataset...")
for fi in tqdm(files):
fi = fi.split(".")[0] + ".txt"
f = open(fi, 'r')
file_text = f.read()
if file_text == "": #discard empty files
continue
file_text = file_text.strip('\n') #remove possible newline character at the end of file
lines = file_text.split('\n')
file_name = os.path.join(fi.split("/")[-2],fi.split("/")[-1])
for l in lines:
name = file_name.split(".")[0] + ".png"
image = cv2.imread(f'real_dataset/train/{name}')
y,x,_ = image.shape
label = l.split(" ")[0]
x0 = l.split(" ")[1]
x1 = l.split(" ")[2]
y0 = l.split(" ")[3]
y1 = l.split(" ")[4]
if int(x0) < 0 or int(x1) > x:
continue
if int(y0) < 0 or int(y1) > y:
continue
if int(label) == -1: #label 1 -> NO FALL
fw.write(name + ',' + str(x0) + ',' + str(y0) + "," + str(x1) + "," + str(y1) + "," + str(1) + "\n")
elif int(label) == 1: #label 2 -> FALL
fw.write(name + ',' + str(x0) + ',' + str(y0) + "," + str(x1) + "," + str(y1) + "," + str(2) + "\n")
fw.close()
print("Train dataset completed!")
def build_test(filename):
if os.path.exists(filename):
print("File already created")
else:
files = glob.glob('test_imgs/*.txt', recursive = True) #find all *.txt files
fw = open(os.path.join("datasets",filename),'w')
i = 0
print("Creating test dataset...")
for fi in files:
f = open(fi, 'r')
file_text = f.read()
if file_text == "": #discard empty files
continue
file_text = file_text.strip('\n') #remove possible newline character at the end of file
lines = file_text.split('\n')
file_name = fi.split("/")[-1] #the part before the . with the name of the folder
for l in lines:
name = file_name.split(".")[0] + ".png"
#image = cv2.imread(os.path.join('test_imgs',name))
label = l.split(" ")[0]
x0 = l.split(" ")[1]
x1 = l.split(" ")[2]
y0 = l.split(" ")[3]
y1 = l.split(" ")[4]
if int(label) == -1: #label 1 -> NO FALL
fw.write(name + ',' + str(x0) + ',' + str(y0) + "," + str(x1) + "," + str(y1) + "," + str(1) + "\n")
elif int(label) == 1: #label 2 -> FALL
fw.write(name + ',' + str(x0) + ',' + str(y0) + "," + str(x1) + "," + str(y1) + "," + str(2) + "\n")
fw.close()
print("Test dataset created!")
def build_test_udf(filename):
if os.path.exists(filename):
print("File already created")
else:
files = glob.glob('test_ufd/*.txt', recursive = True) #find all *.txt files
fw = open(os.path.join("datasets",filename),'w')
i = 0
print("Creating test dataset...")
for fi in files:
f = open(fi, 'r')
file_text = f.read()
file_name = fi.split("/")[-1] #the part before the . with the name of the folder
if file_text == "": #discard empty files
i += 1
name = file_name.split(".")[0] + ".png"
#image = cv2.imread(os.path.join('test_ufd',name))
fw.write(name + ',' + str(DELTA) + ',' + str(DELTA) + "," + str(0) + "," + str(0) + "," + str(1) + "\n")
continue
file_text = file_text.strip('\n') #remove possible newline character at the end of file
lines = file_text.split('\n')
for l in lines:
name = file_name.split(".")[0] + ".png"
#image = cv2.imread(os.path.join('test_ufd',name))
label = l.split(" ")[0]
x0 = l.split(" ")[1]
x1 = l.split(" ")[2]
y0 = l.split(" ")[3]
y1 = l.split(" ")[4]
if int(label) == -1: #label 1 -> NO FALL
fw.write(name + ',' + str(x0) + ',' + str(y0) + "," + str(x1) + "," + str(y1) + "," + str(1) + "\n")
elif int(label) == 1: #label 2 -> FALL
fw.write(name + ',' + str(x0) + ',' + str(y0) + "," + str(x1) + "," + str(y1) + "," + str(2) + "\n")
fw.close()
print(f"Empty annotations are: {i}")
print("Test dataset created!")
def build_test_elderly(filename):
if os.path.exists(filename):
print("File already created")
else:
files = glob.glob('test_elderly/*.txt', recursive = True) #find all *.txt files
fw = open(os.path.join("datasets",filename),'w')
e = 0
nv = 0
print("Creating test dataset...")
for fi in files:
f = open(fi, 'r')
file_text = f.read()
file_name = fi.split("/")[-1] #the part before the . with the name of the folder
if file_text == "": #discard empty files
e += 1
continue
file_text = file_text.strip('\n') #remove possible newline character at the end of file
lines = file_text.split('\n')
for l in lines:
name = file_name.split(".")[0] + ".png"
image = cv2.imread(os.path.join('test_elderly',name))
y,x,_ = image.shape
label = l.split(" ")[0]
x0 = l.split(" ")[1]
x1 = l.split(" ")[2]
y0 = l.split(" ")[3]
y1 = l.split(" ")[4]
if float(x0) < 0 or float(x1) > x:
nv += 1
continue
if float(y0) < 0 or float(y1) > y:
nv += 1
continue
if int(label) == -1: #label 1 -> NO FALL
fw.write(name + ',' + str(x0) + ',' + str(y0) + "," + str(x1) + "," + str(y1) + "," + str(1) + "\n")
elif int(label) == 1: #label 2 -> FALL
fw.write(name + ',' + str(x0) + ',' + str(y0) + "," + str(x1) + "," + str(y1) + "," + str(2) + "\n")
fw.close()
print(f"Empty annotations are: {e}")
print(f"Not valid and discard annotations are: {nv}")
print("Test dataset created!")
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--dt_type', required=True, help="Choose between real or virtual")
parser.add_argument('--filename', required=True, help="Insert filename")
args = parser.parse_args()
dt = args.dt_type
if ".txt" not in args.filename:
print("Insert the name with the extension")
sys.exit(1)
if dt != "train_real" and dt != "train_virtual" and dt != "test" and dt != "test_ufd" and dt != "test_elderly":
print("Insert \"train_real\",\"train_virtual\" or \"test\"")
sys.exit(1)
elif dt == "train_real":
build_real(args.filename)
elif dt == "train_virtual":
build_virtual(args.filename)
elif dt == "test":
build_test(args.filename)
elif dt == "test_ufd":
build_test_udf(args.filename)
elif dt == "test_elderly":
build_test_elderly(args.filename)