This repository has been archived by the owner on Nov 13, 2024. It is now read-only.
forked from brianw0924/pictorial_net
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Neurobit_data.py
319 lines (248 loc) · 12.6 KB
/
Neurobit_data.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import os
import glob
import cv2
import math
from tqdm import tqdm
from PIL import Image
import argparse
import random
'''
Please put the directory containing videos under righteye/ or lefteye/
args.root/
|
├─processed/ (will be automatically created)
| |
| ├─image/
| | ├─0000000.png
| | ├─0000001.png
| | ...
| └─gaze/
| └─gaze.txt
|
└─args.data_dir/ (please create by yourself)
├─righteye
| |
| ├─YYYYMMDD_H14_NSSxxxxx
| | ├─0.mp4
| | ├─1.mp4
| | ├─2.mp4
| | ...
| ├─YYYYMMDD_H14_NSSxxxxx
| ...
|
└─lefteye
|
├─YYYYMMDD_H14_NSSxxxxx
| ├─0.mp4
| ├─1.mp4
| ├─2.mp4
| ...
├─YYYYMMDD_H14_NSSxxxxx
...
Have to determine center (left eye & right eye are different) & crop size by yourself
video frame size (H, W): (400, 1280)
'''
def arg_parser():
parser = argparse.ArgumentParser()
''' Paths '''
parser.add_argument('--root', type=str, default="../Neurobit")
parser.add_argument('--data_dir', type=str, default="20220121_raw")
parser.add_argument('--out_dir', type=str, default="dataset_nocrop")
''' Parameters'''
parser.add_argument('--h', type=float, default=6.8)
parser.add_argument('--w', type=float, default=7.7)
parser.add_argument('--c', type=tuple, default=(0,0)) # (x,y)
parser.add_argument('--d', type=float, default=78) # offset
parser.add_argument('--Lefteye_ROI', type=tuple, default=(0, 400, 640, 1280)) # (left, right, top, bot)
parser.add_argument('--Righteye_ROI', type=tuple, default=(0, 400, 0, 640)) # (left, right, top, bot)
''' Resume the last index'''
parser.add_argument('--resume', action="store_true")
args = parser.parse_args()
if not os.path.exists(os.path.join(args.root, args.out_dir)):
os.mkdir(os.path.join(args.root, args.out_dir))
if not os.path.exists(os.path.join(args.root, args.out_dir, "image")):
os.mkdir(os.path.join(args.root, args.out_dir, "image"))
if not os.path.exists(os.path.join(args.root, args.out_dir, "gaze")):
os.mkdir(os.path.join(args.root, args.out_dir, "gaze"))
return args
def get_yaw_pitch(i, h, w, c, d):
top_left_y = 4 * h + c[1]
top_left_x = -6 * w + c[0]
pitch = math.atan( (top_left_y - (i//13) * h) / d) * 180 / math.pi
yaw = math.atan( (top_left_x + (i%13) * w) / d) * 180 / math.pi
return yaw, pitch
def DataPreprocessing_nocrop():
''' 直接影片轉圖片,不做 cropping '''
args = arg_parser()
''' parametrers '''
L_top, L_bot, L_left, L_right = args.Lefteye_ROI[0], args.Lefteye_ROI[1], args.Lefteye_ROI[2], args.Lefteye_ROI[3]
R_top, R_bot, R_left, R_right = args.Righteye_ROI[0], args.Righteye_ROI[1], args.Righteye_ROI[2], args.Righteye_ROI[3]
Left_dirs = glob.glob(os.path.join(args.root, args.data_dir, "lefteye", "*"))
Right_dirs = glob.glob(os.path.join(args.root, args.data_dir, "righteye", "*"))
if args.resume:
image_idx = len(os.listdir(os.path.join(args.root, args.out_dir, "image")))
else:
image_idx = 0
print(f"Index start from: {str(image_idx).zfill(7)}")
s = 'w' if image_idx == 0 else 'a'
with open(os.path.join(args.root, args.out_dir, "gaze", "gaze.txt"), s) as f:
if image_idx == 0:
f.write("yaw,pitch\n")
# left eyes
for d in Left_dirs: # iterate thru left_eye video directories
video_files = sorted(glob.glob(os.path.join(d,"*")))
assert(len(video_files) == 9*13)
for i, v in enumerate(tqdm(video_files)): # each dir represents
yaw,pitch = get_yaw_pitch(i=float(i),h=args.h,w=args.w,c=args.c,d=args.d) # length-based label
# yaw, pitch = -30 + (i%13) * 5, 20 - (i//13) * 5 # angle-based label
video = cv2.VideoCapture(v)
success = True
while(success):
success, frame = video.read() # only take the first frame (or the dataset will be too big)
if(not success): break
im = frame[L_top:L_bot, L_left:L_right, :]
im = Image.fromarray(im)
im.save(os.path.join(args.root, args.out_dir, "image", f'{str(image_idx).zfill(7)}.png'))
image_idx+=1
f.write(f'{yaw},{pitch}\n')
# right eyes
for d in Right_dirs: # iterate thru right_eye video directories
video_files = sorted(glob.glob(os.path.join(d,"*")))
assert(len(video_files) == 9*13)
for i, v in enumerate(tqdm(video_files)):
yaw,pitch = get_yaw_pitch(i=float(i),h=args.h,w=args.w,c=args.c,d=args.d) # length-based label
# yaw, pitch = -30 + (i%13) * 5, 20 - (i//13) * 5 # angle-based label
video = cv2.VideoCapture(v)
success = True
while(success):
success, frame = video.read() # only take the first frame (or the dataset will be too big)
if(not success): break
im = frame[R_top:R_bot, R_left:R_right, :]
im = Image.fromarray(im)
im.save(os.path.join(args.root, args.out_dir, "image", f'{str(image_idx).zfill(7)}.png'))
image_idx+=1
f.write(f'{yaw},{pitch}\n')
def DataPreprocessing_v1():
''' 每個影片只取部分 frame,同一 frame 做上下左右平移 '''
args = arg_parser()
''' parametrers '''
step = 10
L_top, L_bot, L_left, L_right = args.Lefteye_ROI[0], args.Lefteye_ROI[1], args.Lefteye_ROI[2], args.Lefteye_ROI[3]
R_top, R_bot, R_left, R_right = args.Righteye_ROI[0], args.Righteye_ROI[1], args.Righteye_ROI[2], args.Righteye_ROI[3]
Left_dirs = glob.glob(os.path.join(args.root, args.data_dir, "lefteye", "*"))
Right_dirs = glob.glob(os.path.join(args.root, args.data_dir, "righteye", "*"))
if args.resume:
image_idx = len(os.listdir(os.path.join(args.root, args.out_dir, "image")))
else:
image_idx = 0
print(f"Start from: {str(image_idx).zfill(7)}")
s = 'w' if image_idx == 0 else 'a'
with open(os.path.join(args.root,args.out_dir, "gaze", "gaze.txt"), s) as f:
if image_idx == 0:
f.write("yaw,pitch\n")
# left eyes
for d in Left_dirs: # iterate thru left_eye video directories
video_files = sorted(glob.glob(os.path.join(d,"*")))
assert(len(video_files) == 9*13)
for i, v in enumerate(tqdm(video_files)): # each dir represents
yaw,pitch = get_yaw_pitch(i=float(i),h=args.h,w=args.w,c=args.c,d=args.d) # length-based label
# yaw, pitch = -30 + (i%13) * 5, 20 - (i//13) * 5 # angle-based label
video = cv2.VideoCapture(v)
success = True
count = 0
while(success and count < 251):
count+=1
success, frame = video.read() # only take the first frame (or the dataset will be too big)
if(not success): break
if count % 50 == 0:
for vertical in range(-50,51,step):
for horizontal in range(-70,71,step):
im = frame[L_top+vertical:L_bot+vertical, L_left+horizontal:L_right+horizontal, :]
im = Image.fromarray(im)
im.save(os.path.join(args.root,args.out_dir, "image", f'{str(image_idx).zfill(7)}.png'))
image_idx+=1
f.write(f'{yaw},{pitch}\n')
# right eyes
for d in Right_dirs: # iterate thru right_eye video directories
video_files = sorted(glob.glob(os.path.join(d,"*")))
assert(len(video_files) == 9*13)
for i, v in enumerate(tqdm(video_files)):
yaw,pitch = get_yaw_pitch(i=float(i),h=args.h,w=args.w,c=args.c,d=args.d) # length-based label
# yaw, pitch = -30 + (i%13) * 5, 20 - (i//13) * 5 # angle-based label
video = cv2.VideoCapture(v)
success = True
count = 0
while(success and count < 251):
count+=1
success, frame = video.read() # only take the first frame (or the dataset will be too big)
if(not success): break
if count % 50 == 0:
for vertical in range(-50,51,step):
for horizontal in range(-70,71,step):
im = frame[R_top+vertical:R_bot+vertical, R_left+horizontal:R_right+horizontal, :]
im = Image.fromarray(im)
im.save(os.path.join(args.root,args.out_dir, "image", f'{str(image_idx).zfill(7)}.png'))
image_idx+=1
f.write(f'{yaw},{pitch}\n')
def DataPreprocessing_v2():
''' 每個 frame 隨機上下左右平移'''
args = arg_parser()
''' parametrers '''
L_top, L_bot, L_left, L_right = args.Lefteye_ROI[0], args.Lefteye_ROI[1], args.Lefteye_ROI[2], args.Lefteye_ROI[3]
R_top, R_bot, R_left, R_right = args.Righteye_ROI[0], args.Righteye_ROI[1], args.Righteye_ROI[2], args.Righteye_ROI[3]
Left_dirs = glob.glob(os.path.join(args.root, args.data_dir, "lefteye", "*"))
Right_dirs = glob.glob(os.path.join(args.root, args.data_dir, "righteye", "*"))
if args.resume:
image_idx = len(os.listdir(os.path.join(args.root, args.out_dir, "image")))
else:
image_idx = 0
print(f"Start from: {str(image_idx).zfill(7)}")
s = 'w' if image_idx == 0 else 'a'
with open(os.path.join(args.root,args.out_dir, "gaze", "gaze.txt"), s) as f:
if image_idx == 0:
f.write("yaw,pitch\n")
# left eyes
for d in Left_dirs: # iterate thru left_eye video directories
video_files = sorted(glob.glob(os.path.join(d,"*")))
assert(len(video_files) == 9*13)
for i, v in enumerate(tqdm(video_files)): # each dir represents
yaw,pitch = get_yaw_pitch(i=float(i),h=args.h,w=args.w,c=args.c,d=args.d) # length-based label
# yaw, pitch = -30 + (i%13) * 5, 20 - (i//13) * 5 # angle-based label
video = cv2.VideoCapture(v)
success = True
while(success):
success, frame = video.read() # only take the first frame (or the dataset will be too big)
if(not success): break
for i in range(2):
vertical = random.randint(-50,50)
horizontal = random.randint(-70,70)
im = frame[L_top+vertical:L_bot+vertical, L_left+horizontal:L_right+horizontal, :]
im = Image.fromarray(im)
im.save(os.path.join(args.root,args.out_dir, "image", f'{str(image_idx).zfill(7)}.png'))
image_idx+=1
f.write(f'{yaw},{pitch}\n')
# right eyes
for d in Right_dirs: # iterate thru right_eye video directories
video_files = sorted(glob.glob(os.path.join(d,"*")))
assert(len(video_files) == 9*13)
for i, v in enumerate(tqdm(video_files)):
yaw,pitch = get_yaw_pitch(i=float(i),h=args.h,w=args.w,c=args.c,d=args.d) # length-based label
# yaw, pitch = -30 + (i%13) * 5, 20 - (i//13) * 5 # angle-based label
video = cv2.VideoCapture(v)
success = True
while(success):
success, frame = video.read() # only take the first frame (or the dataset will be too big)
if(not success): break
for i in range(2):
vertical = random.randint(-50,50)
horizontal = random.randint(-70,70)
im = frame[R_top+vertical:R_bot+vertical, R_left+horizontal:R_right+horizontal, :]
im = Image.fromarray(im)
im.save(os.path.join(args.root,args.out_dir, "image", f'{str(image_idx).zfill(7)}.png'))
image_idx+=1
f.write(f'{yaw},{pitch}\n')
if __name__ == "__main__":
random.seed(2022)
DataPreprocessing_nocrop()
# DataPreprocessing_v1()
# DataPreprocessing_v2()