This repository has been archived by the owner on Dec 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathutils.py
191 lines (144 loc) · 6.58 KB
/
utils.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
import numpy as np
from skvideo.io import FFmpegReader, ffprobe
from skvideo.utils import rgb2gray
from PIL import Image
from keras.preprocessing import image
from tqdm import tqdm
class Videos(object):
def __init__(self, target_size=None, to_gray=True, max_frames=None,
extract_frames='middle', required_fps=None,
normalize_pixels=None):
"""
Initializing the config variables
Parameters:
target_size (tuple): (New_Width, New_Height), Default 'None'
A tuple denoting the target width and height of each frame in each of the video
to_gray (boolean): Default 'True'
If True, then each frame will be converted to gray scale. Otherwise, not.
max_frames (int): Default 'None'
The maximum number of frames to return for each video.
Extra frames are removed based on the value of 'extract_frames'.
extract_frames (str): {'first', 'middle', 'last'}, Default 'middle'
'first': Extract the first 'N' frames
'last': Extract the last 'N' frames
'middle': Extract 'N' frames from the middle
Remove ((total_frames - max_frames) // 2) frames from the beginning as well as the end
required_fps (int): Default 'None'
Capture 'N' frame(s) per second from the video.
Only the first 'N' frame(s) for each second in the video are captured.
normalize_pixels (tuple/str): Default 'None'
If 'None', the pixels will not be normalized.
If a tuple - (New_min, New_max) is passed, Min-max Normalization will be used.
If the value is 'z-score', then Z-score Normalization will be used.
For each pixel p, z_score = (p - mean) / std
"""
self.target_size = target_size
self.to_gray = to_gray
self.max_frames = max_frames
self.extract_frames = extract_frames
self.required_fps = required_fps
self.normalize_pixels = normalize_pixels
self.fps = None
def read_videos(self, paths):
"""
Parameters:
paths (list): Required
A list of paths of the videos to be read
Returns:
Numpy.ndarray
A 5-d tensor with shape (<No. of Videos>, <No. of frames>, <height>, <width>, <channels>)
"""
list_of_videos = [
self._read_video(path) for path in tqdm(paths)
]
tensor = np.vstack(list_of_videos)
if self.normalize_pixels != None:
# Pixels are normalized for each video individually
if (type(self.normalize_pixels) == tuple) and (len(self.normalize_pixels) == 2):
base = self.normalize_pixels[0]
r = self.normalize_pixels[1] - base
min_ = np.min(tensor, axis=(1, 2, 3), keepdims=True)
max_ = np.max(tensor, axis=(1, 2, 3), keepdims=True)
return ((tensor.astype('float32') - min_) / (max_ - min_)) * r + base
elif self.normalize_pixels == 'z-score':
mean = np.mean(tensor, axis=(1, 2, 3), keepdims=True)
std = np.std(tensor, axis=(1, 2, 3), keepdims=True)
return (tensor.astype('float32') - mean) / std
else:
raise ValueError('Invalid value of \'normalize_pixels\'')
return tensor
def get_frame_count(self, paths):
"""
Can be used to determine the value of `max_frames`
Parameters:
paths (list): Required
A list of paths of the videos to be read
Returns:
dict (python dictionary)
For each video, the total number of frames in that video is stored in the dictionary.
"""
frame_count = {}
for path in paths:
cap = FFmpegReader(filename=path)
frame_count[path] = cap.inputframenum
cap.close()
return frame_count
def _read_video(self, path):
"""
Parameters:
path (str): Required
Path of the video to be read
Returns:
Numpy.ndarray
A 5-d tensor with shape (1, <No. of frames>, <height>, <width>, <channels>)
"""
cap = FFmpegReader(filename=path)
list_of_frames = []
self.fps = int(cap.inputfps) # Frame Rate
for index, frame in enumerate(cap.nextFrame()):
capture_frame = True
if self.required_fps != None:
is_valid = range(self.required_fps)
capture_frame = (index % self.fps) in is_valid
if capture_frame:
if self.target_size is not None:
temp_image = image.array_to_img(frame)
frame = image.img_to_array(
temp_image.resize(
self.target_size,
Image.ANTIALIAS)).astype('uint8')
# Shape of each frame -> (<height>, <width>, 3)
list_of_frames.append(frame)
temp_video = np.stack(list_of_frames)
cap.close()
if self.to_gray:
temp_video = rgb2gray(temp_video)
if self.max_frames is not None:
temp_video = self._process_video(video=temp_video)
return np.expand_dims(temp_video, axis=0)
def _process_video(self, video):
"""
Parameters:
video (Numpy.ndarray):
Shape = (<No. of frames>, <height>, <width>, <channels>)
Video whose frames are to be extracted
Returns:
Numpy.ndarray
A tensor (processed video) with shape (<`max_frames`>, <height>, <width>, <channels>)
"""
total_frames = video.shape[0]
if self.max_frames <= total_frames:
if self.extract_frames == 'first':
video = video[:self.max_frames]
elif self.extract_frames == 'last':
video = video[(total_frames - self.max_frames):]
elif self.extract_frames == 'middle':
# No. of frames to remove from the front
front = ((total_frames - self.max_frames) // 2) + 1
video = video[front:(front + self.max_frames)]
else:
raise ValueError('Invalid value of \'extract_frames\'')
else:
raise IndexError(
'Required number of frames is greater than the total number of frames in the video')
return video