forked from xiamenwcy/pictorial_net
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDikablis_preprocess.py
212 lines (192 loc) · 9.45 KB
/
Dikablis_preprocess.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
import os
import cv2
import numpy as np
from PIL import Image
from tqdm import tqdm
import pandas as pd
import argparse
'''
Only process on Dikablis dataset
output directory tree will looks like this:
args.root/
├─20MioEyeDS/ (Original TEyeD dataset)
| ├─CorruptFilesInZIP/
| └─TEyeDSSingleFiles/
| ├─Dikablis/
| ├─...
| ├─...
|
└─TEyeD/ (Will create this directory automatically)
'''
parser = argparse.ArgumentParser()
parser.add_argument('--root', type=str, required=True, help='Root directory')
parser.add_argument('--fpv', type=int, default=3000, help='How many frame per video you want to store.')
args = parser.parse_args()
# ROOT
# root = "/home/brianw0924/hdd/"
root = args.root
# PATH
video_path = os.path.join(root, "20MioEyeDS/TEyeDSSingleFiles/Dikablis/VIDEOS")
label_path = os.path.join(root, "20MioEyeDS/TEyeDSSingleFiles/Dikablis/ANNOTATIONS")
video_list = os.listdir(video_path)
tqdm.write(f'Number of videos: {len(video_list)}')
save_path = os.path.join(root,"TEyeD")
# OUTPUT DIR
if not os.path.exists(save_path):
os.mkdir(save_path)
os.mkdir(os.path.join(save_path,"image"))
os.mkdir(os.path.join(save_path,"gaze"))
os.mkdir(os.path.join(save_path,"landmark"))
os.mkdir(os.path.join(save_path,"pupilsegmentation"))
os.mkdir(os.path.join(save_path,"irissegmentation"))
os.mkdir(os.path.join(save_path,"lidsegmentation"))
# BROKEN FILE
broken = []
broken.append("DikablisSS_10_1.mp4")
with open("./others/pupil_seg_broken.txt", 'r') as p:
with open("./others/iris_seg_broken.txt", 'r') as i:
with open("./others/lid_seg_broken.txt", 'r') as l:
for line in p.readlines():
broken.append(line.strip())
for line in i.readlines():
broken.append(line.strip())
for line in l.readlines():
broken.append(line.strip())
with open(os.path.join(save_path,"gaze","gaze.txt"), 'w') as gaze:
with open(os.path.join(save_path,"landmark","pupil_landmark.txt"), 'w') as p_landmark:
with open(os.path.join(save_path,"landmark","iris_landmark.txt"), 'w') as i_landmark:
with open(os.path.join(save_path,"landmark","lid_landmark.txt"), 'w') as l_landmark:
gaze.write("x,y,z\n")
p_landmark.write("x,y,x,y, ...\n")
i_landmark.write("x,y,x,y, ...\n")
l_landmark.write("x,y,x,y, ...\n")
source_image_idx = 0
pupil_seg_idx = 0
iris_seg_idx = 0
lid_seg_idx = 0
for video_name in tqdm(video_list):
if video_name in broken:
continue
tqdm.write(video_name)
'''
image shape: (288, 384, 3) in Dikablis
'''
# Source video
video = cv2.VideoCapture(os.path.join(video_path,video_name))
success = True
source_image_count = 0
while(success):
success, frame = video.read()
if not success:
break
im = Image.fromarray(frame)
im.save(os.path.join(save_path,"image",f'{str(source_image_idx).zfill(7)}.png'))
source_image_count+=1
source_image_idx+=1
if source_image_count == args.fpv: # reach desired frames per video
break
# Gaze vector: FRAME;x;y;z;\n
count = 0
with open(os.path.join(label_path,f'{video_name}gaze_vec.txt')) as f:
next(f)
for i, line in enumerate(f.readlines()):
l = ','.join(line.split(';')[1:-1]) # x,y,z
gaze.write(f'{l}\n') # x,y,z\n
count+=1
if i+1 == source_image_count:
break
if count != source_image_count:
tqdm.write(f'{video_name}gaze_vec.txt is broken.')
# # pupil landmark: FRAME;AVG INACCURACY;x;y;x;y;...;\n
# count = 0
# with open(os.path.join(label_path,f'{video_name}pupil_lm_2D.txt')) as f:
# next(f)
# for i, line in enumerate(f.readlines()):
# l = ','.join(line.split(';')[2:-1]) # x,y,x,y,x,y, ...
# p_landmark.write(f'{l}\n') # x,y,x,y,x,y, ...\n
# count+=1
# if i+1 == source_image_count:
# break
# if count != source_image_count:
# tqdm.write(f'{video_name}pupil_lm_2D.txt is broken.')
# # iris landmark: FRAME;AVG INACCURACY;x;y;x;y;...;\n
# count = 0
# with open(os.path.join(label_path,f'{video_name}iris_lm_2D.txt')) as f:
# next(f)
# for i, line in enumerate(f.readlines()):
# l = ','.join(line.split(';')[2:-1]) # x,y,x,y,x,y, ...
# i_landmark.write(f'{l}\n') # x,y,x,y,x,y, ...\n
# count+=1
# if i+1 == source_image_count:
# break
# if count != source_image_count:
# tqdm.write(f'{video_name}iris_lm_2D.txt is broken.')
# # lid landmark: FRAME;AVG INACCURACY;x;y;x;y;...;\n
# count = 0
# with open(os.path.join(label_path,f'{video_name}lid_lm_2D.txt')) as f:
# next(f)
# for i, line in enumerate(f.readlines()):
# l = ','.join(line.split(';')[2:-1]) # x,y,x,y,x,y, ...
# l_landmark.write(f'{l}\n') # x,y,x,y,x,y, ...\n
# count+=1
# if i+1 == source_image_count:
# break
# if count != source_image_count:
# tqdm.write(f'{video_name}lid_lm_2D.txt is broken.')
# # pupil 2D seg
# video = cv2.VideoCapture(os.path.join(label_path,f'{video_name}pupil_seg_2D.mp4'))
# success = True
# count = 0
# while(success):
# success, frame = video.read()
# if not success:
# break
# frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# frame[frame<=128] = 0
# frame[frame>128] = 255
# im = Image.fromarray(frame)
# im.save(os.path.join(save_path,"pupilsegmentation",f'{str(pupil_seg_idx).zfill(7)}.png'))
# count+=1
# pupil_seg_idx+=1
# if count == source_image_count:
# break
# if count != source_image_count:
# tqdm.write(f'{video_name}pupil_seg_2D.mp4 is broken.')
# # iris 2D seg
# video = cv2.VideoCapture(os.path.join(label_path,f'{video_name}iris_seg_2D.mp4'))
# success = True
# count = 0
# while(success):
# success, frame = video.read()
# if not success:
# break
# frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# frame[frame<=128] = 0
# frame[frame>128] = 255
# im = Image.fromarray(frame)
# im.save(os.path.join(save_path,"irissegmentation",f'{str(iris_seg_idx).zfill(7)}.png'))
# count+=1
# iris_seg_idx+=1
# if count == source_image_count:
# break
# if count != source_image_count:
# tqdm.write(f'{video_name}iris_seg_2D.mp4 is broken.')
# # lid 2D seg
# video = cv2.VideoCapture(os.path.join(label_path,f'{video_name}lid_seg_2D.mp4'))
# success = True
# count = 0
# while(success):
# success, frame = video.read()
# if not success:
# break
# frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# frame[frame<=128] = 0
# frame[frame>128] = 255
# im = Image.fromarray(frame)
# im.save(os.path.join(save_path,"lidsegmentation",f'{str(lid_seg_idx).zfill(7)}.png'))
# count+=1
# lid_seg_idx+=1
# if count == source_image_count:
# break
# if count != source_image_count:
# tqdm.write(f'{video_name}lid_seg_2D.mp4 is broken.')