-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_generator_ucf.py
229 lines (211 loc) · 8.98 KB
/
data_generator_ucf.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
import os
import numpy as np
import cv2
import csv
import keras
from skimage import transform
from skimage import util
from keras.utils import np_utils
from random import shuffle
class DataGenerator(keras.utils.Sequence):
def __init__(self, batch_size, video_dir, depth, train_flag, val_flag):
self.video_id = 0
self.batch_size = batch_size
self.video_dir = video_dir
self.files = os.listdir(video_dir)
shuffle(self.files)
self.depth = depth
self.train = train_flag
self.val = val_flag
self.label_name_number_mapping()
self.batch_id = 0
def __len__(self):
if self.train:
return int(9573/self.batch_size)
elif self.val:
return int(1064/self.batch_size)
else:
return int(2719/self.batch_size)
def label_name_number_mapping(self):
self.action_mapping = {'ApplyEyeMakeup': 23,
'ApplyLipstick': 52,
'Archery': 5,
'BabyCrawling': 1,
'BalanceBeam': 44,
'BandMarching': 41,
'BaseballPitch': 46,
'Basketball': 70,
'BasketballDunk': 76,
'BenchPress': 53,
'Biking': 20,
'Billiards': 88,
'BlowDryHair': 51,
'BlowingCandles': 21,
'BodyWeightSquats': 77,
'Bowling': 58,
'BoxingPunchingBag': 86,
'BoxingSpeedBag': 85,
'BreastStroke': 68,
'BrushingTeeth': 54,
'CleanAndJerk': 59,
'CliffDiving': 69,
'CricketBowling': 3,
'CricketShot': 79,
'CuttingInKitchen': 71,
'Diving': 35,
'Drumming': 98,
'Fencing': 56,
'FieldHockeyPenalty': 99,
'FloorGymnastics': 45,
'FrisbeeCatch': 6,
'FrontCrawl': 13,
'GolfSwing': 80,
'Haircut': 92,
'HammerThrow': 74,
'Hammering': 97,
'HandStandPushups': 0,
'HandstandWalking': 19,
'HeadMassage': 4,
'HighJump': 22,
'HorseRace': 36,
'HorseRiding': 64,
'HulaHoop': 40,
'IceDancing': 78,
'JavelinThrow': 38,
'JugglingBalls': 27,
'JumpRope': 37,
'JumpingJack': 24,
'Kayaking': 34,
'Knitting': 10,
'LongJump': 87,
'Lunges': 31,
'MilitaryParade': 28,
'Mixing': 15,
'MoppingFloor': 29,
'Nunchucks': 82,
'ParallelBars': 9,
'PizzaTossing': 55,
'PlayingCello': 18,
'PlayingDaf': 62,
'PlayingDhol': 93,
'PlayingFlute': 49,
'PlayingGuitar': 75,
'PlayingPiano': 81,
'PlayingSitar': 94,
'PlayingTabla': 8,
'PlayingViolin': 83,
'PoleVault': 16,
'PommelHorse': 33,
'PullUps': 47,
'Punch': 90,
'PushUps': 17,
'Rafting': 95,
'RockClimbingIndoor': 39,
'RopeClimbing': 72,
'Rowing': 11,
'SalsaSpin': 91,
'ShavingBeard': 100,
'Shotput': 50,
'SkateBoarding': 61,
'Skiing': 25,
'Skijet': 89,
'SkyDiving': 43,
'SoccerJuggling': 12,
'SoccerPenalty': 57,
'StillRings': 48,
'SumoWrestling': 60,
'Surfing': 30,
'Swing': 2,
'TableTennisShot': 66,
'TaiChi': 96,
'TennisSwing': 67,
'ThrowDiscus': 26,
'TrampolineJumping': 7,
'Typing': 63,
'UnevenBars': 42,
'VolleyballSpiking': 84,
'WalkingWithDog': 32,
'WallPushups': 65,
'WritingOnBoard': 73,
'YoYo': 14}
def on_epoch_end(self):
#print ("\n epoch ended", self.video_loaded_count)
self.batch_id = 0
def __getitem__(self, index):
X,y = self.extract_video_data()
X = X.transpose((0, 2, 3, 1, 4))
return X,y
def extract_video_data(self):
clips_array = []
label_array = []
self.batch_id += 1
self.clips = 0
#print("***********************************", self.batch_id)
while self.clips < self.batch_size:
try:
filename = self.files[self.video_id]
except IndexError as error:
self.video_id = 0
filename = self.files[self.video_id]
#print(filename)
filepath = os.path.join(self.video_dir, filename)
cap = cv2.VideoCapture(filepath)
label = self.get_label_data(filename)
#print(self.video_id, filename, label)
label_array.append(np_utils.to_categorical(label, num_classes=101))
task_clip = self.extract_task_clip(cap)
clips_array.append(task_clip)
self.clips +=1
self.video_id += 1
cap.release()
return np.array(clips_array), np.array(label_array)
def extract_task_clip(self, cap):
nframes = cap.get(cv2.CAP_PROP_FRAME_COUNT)
#print("no of frames", nframes)
frame_bound_ids = np.linspace(0, nframes-1, self.depth+1)
frame_bound_ids = frame_bound_ids.astype(int)
required_frame_ids = []
for i in range(1, self.depth+1):
required_frame_ids.append(np.random.randint(frame_bound_ids[i-1], frame_bound_ids[i]))
frames_array = []
#print(required_frame_ids)
for i in range(self.depth):
cap.set(cv2.CAP_PROP_POS_FRAMES, required_frame_ids[i])
ret, frame = cap.read()
frame_id = required_frame_ids[i]
while not ret:
frame_id = frame_id - 1
cap.set(cv2.CAP_PROP_POS_FRAMES, frame_id)
ret, frame = cap.read()
frame = cv2.resize(frame, (160, 120))
frames_array.append(frame)
#print(np.shape(frames_array))
return frames_array
def get_label_data(self, filename):
return self.action_mapping[filename.split('_')[1]]
def video_preprocessing(self, video):
rand_int = np.random.randint(4)
if rand_int == 0:
return video
if rand_int == 1:
return self.random_rotation(video)
if rand_int == 2:
return self.random_noise(video)
if rand_int == 3:
return self.horizontal_flip(video)
def random_rotation(self, video):
random_degree = np.random.uniform(-25,25)
rotated_clip = []
for frame in video:
rotated_clip.append(transform.rotate(frame, random_degree))
return rotated_clip
def random_noise(self, video):
noisy_frames = []
for frame in video:
noisy_frames.append(util.random_noise(frame))
return noisy_frames
def horizontal_flip(self, video):
flipped_images = []
for frame in video:
flipped_images.append(frame[:,::-1])
return flipped_images