-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_key_frames.py
304 lines (277 loc) · 11.7 KB
/
extract_key_frames.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
import numpy as np
from pathlib import Path
import math
import cv2
import numpy as np
from skimage.util import img_as_ubyte
from sklearn.cluster import MiniBatchKMeans
from tqdm import tqdm
from auxfun_videos import VideoWriter
from skimage import io
videos = [
# 'opencv\\20240313105042_NM_1203552_0708319856_1920_1080_25_06291456_0000000000041226_01_0305260036_04_312_0000000_V02_001_E00_0.avi'
]
def UniformFramescv2(cap, numframes2pick, start, stop, Index=None):
"""Temporally uniformly sampling frames in interval (start,stop).
Visual information of video is irrelevant for this method. This code is fast and sufficient (to extract distinct frames),
when behavioral videos naturally covers many states.
The variable Index allows to pass on a subindex for the frames.
"""
nframes = len(cap)
print(
"Uniformly extracting of frames from",
round(start * nframes * 1.0 / cap.fps, 2),
" seconds to",
round(stop * nframes * 1.0 / cap.fps, 2),
" seconds.",
)
if Index is None:
if start == 0:
frames2pick = np.random.choice(
math.ceil(nframes * stop), size=numframes2pick, replace=False
)
else:
frames2pick = np.random.choice(
range(math.floor(nframes * start), math.ceil(nframes * stop)),
size=numframes2pick,
replace=False,
)
return frames2pick
else:
startindex = int(np.floor(nframes * start))
stopindex = int(np.ceil(nframes * stop))
Index = np.array(Index, dtype=int)
Index = Index[(Index > startindex) * (Index < stopindex)] # crop to range!
if len(Index) >= numframes2pick:
return list(np.random.permutation(Index)[:numframes2pick])
else:
return list(Index)
def KmeansbasedFrameselectioncv2(
cap,
numframes2pick,
start,
stop,
Index=None,
step=1,
resizewidth=30,
batchsize=100,
max_iter=50,
color=False,
):
"""This code downsamples the video to a width of resizewidth.
The video is extracted as a numpy array, which is then clustered with kmeans, whereby each frames is treated as a vector.
Frames from different clusters are then selected for labeling. This procedure makes sure that the frames "look different",
i.e. different postures etc. On large videos this code is slow.
Consider not extracting the frames from the whole video but rather set start and stop to a period around interesting behavior.
Note: this method can return fewer images than numframes2pick.
Attention: the flow of commands was not optimized for readability, but rather speed. This is why it might appear tedious and repetitive.
"""
nframes = len(cap)
nx, ny = cap.dimensions
ratio = resizewidth * 1.0 / nx
if ratio > 1:
raise Exception("Choice of resizewidth actually upsamples!")
print(
"Kmeans-quantization based extracting of frames from",
round(start * nframes * 1.0 / cap.fps, 2),
" seconds to",
round(stop * nframes * 1.0 / cap.fps, 2),
" seconds.",
)
startindex = int(np.floor(nframes * start))
stopindex = int(np.ceil(nframes * stop))
if Index is None:
Index = np.arange(startindex, stopindex, step)
else:
Index = np.array(Index)
Index = Index[(Index > startindex) * (Index < stopindex)] # crop to range!
nframes = len(Index)
if batchsize > nframes:
batchsize = nframes // 2
allocated = False
if len(Index) >= numframes2pick:
if (
np.mean(np.diff(Index)) > 1
): # then non-consecutive indices are present, thus cap.set is required (which slows everything down!)
print("Extracting and downsampling...", nframes, " frames from the video.")
if color:
for counter, index in tqdm(enumerate(Index)):
cap.set_to_frame(index) # extract a particular frame
frame = cap.read_frame(crop=True)
if frame is not None:
image = img_as_ubyte(
cv2.resize(
frame,
None,
fx=ratio,
fy=ratio,
interpolation=cv2.INTER_NEAREST,
)
) # color trafo not necessary; lack thereof improves speed.
if (
not allocated
): #'DATA' not in locals(): #allocate memory in first pass
DATA = np.empty(
(nframes, np.shape(image)[0], np.shape(image)[1] * 3)
)
allocated = True
DATA[counter, :, :] = np.hstack(
[image[:, :, 0], image[:, :, 1], image[:, :, 2]]
)
else:
for counter, index in tqdm(enumerate(Index)):
cap.set_to_frame(index) # extract a particular frame
frame = cap.read_frame(crop=True)
if frame is not None:
image = img_as_ubyte(
cv2.resize(
frame,
None,
fx=ratio,
fy=ratio,
interpolation=cv2.INTER_NEAREST,
)
) # color trafo not necessary; lack thereof improves speed.
if (
not allocated
): #'DATA' not in locals(): #allocate memory in first pass
DATA = np.empty(
(nframes, np.shape(image)[0], np.shape(image)[1])
)
allocated = True
DATA[counter, :, :] = np.mean(image, 2)
else:
print("Extracting and downsampling...", nframes, " frames from the video.")
if color:
for counter, index in tqdm(enumerate(Index)):
frame = cap.read_frame(crop=True)
if frame is not None:
image = img_as_ubyte(
cv2.resize(
frame,
None,
fx=ratio,
fy=ratio,
interpolation=cv2.INTER_NEAREST,
)
) # color trafo not necessary; lack thereof improves speed.
if (
not allocated
): #'DATA' not in locals(): #allocate memory in first pass
DATA = np.empty(
(nframes, np.shape(image)[0], np.shape(image)[1] * 3)
)
allocated = True
DATA[counter, :, :] = np.hstack(
[image[:, :, 0], image[:, :, 1], image[:, :, 2]]
)
else:
for counter, index in tqdm(enumerate(Index)):
frame = cap.read_frame(crop=True)
if frame is not None:
image = img_as_ubyte(
cv2.resize(
frame,
None,
fx=ratio,
fy=ratio,
interpolation=cv2.INTER_NEAREST,
)
) # color trafo not necessary; lack thereof improves speed.
if (
not allocated
): #'DATA' not in locals(): #allocate memory in first pass
DATA = np.empty(
(nframes, np.shape(image)[0], np.shape(image)[1])
)
allocated = True
DATA[counter, :, :] = np.mean(image, 2)
print("Kmeans clustering ... (this might take a while)")
data = DATA - DATA.mean(axis=0)
data = data.reshape(nframes, -1) # stacking
kmeans = MiniBatchKMeans(
n_clusters=numframes2pick, tol=1e-3, batch_size=batchsize, max_iter=max_iter
)
kmeans.fit(data)
frames2pick = []
for clusterid in range(numframes2pick): # pick one frame per cluster
clusterids = np.where(clusterid == kmeans.labels_)[0]
numimagesofcluster = len(clusterids)
if numimagesofcluster > 0:
frames2pick.append(
Index[clusterids[np.random.randint(numimagesofcluster)]]
)
# cap.release() >> still used in frame_extraction!
return list(np.array(frames2pick))
else:
return list(Index)
def extract_frames(
algo="kmeans",
numframes2pick = 5,
start=0,
stop=1,
cluster_step=1,
cluster_resizewidth=30,
cluster_color=False,
opencv=False,
videos_list=None):
has_failed = []
for video in videos_list:
cap = VideoWriter(video)
nframes = len(cap)
if not nframes:
print("Video could not be opened. Skipping...")
continue
indexlength = int(np.ceil(np.log10(nframes)))
fname = Path(video)
output_path = Path("opencv") / fname.stem
if not output_path.exists():
output_path.mkdir()
print("Extracting frames based on %s ..." % algo)
if algo == "uniform":
frames2pick = UniformFramescv2(cap, numframes2pick, start, stop)
elif algo == "kmeans":
frames2pick = KmeansbasedFrameselectioncv2(cap,numframes2pick,start,stop,step=cluster_step,resizewidth=cluster_resizewidth,color=cluster_color)
else:
print("Invalid algorithm. Skipping...")
continue
if not len(frames2pick):
print("Frame selection failed...")
return
is_valid = []
for index in frames2pick:
cap.set_to_frame(index) # extract a particular frame
frame = cap.read_frame(crop=True)
if frame is not None:
image = img_as_ubyte(frame)
img_name = (
str(output_path)
+ "/img"
+ str(index).zfill(indexlength)
+ ".png"
)
io.imsave(img_name, image)
is_valid.append(True)
else:
print("Frame", index, " not found!")
is_valid.append(False)
cap.close()
if not any(is_valid):
has_failed.append(True)
else:
has_failed.append(False)
if all(has_failed):
print("Frame extraction failed. Video files must be corrupted.")
return
elif any(has_failed):
print("Although most frames were extracted, some were invalid.")
else:
print(
"Frames were successfully extracted, for the videos listed in the config.yaml file."
)
print(
"\nYou can now label the frames using the function 'label_frames' "
"(Note, you should label frames extracted from diverse videos (and many videos; we do not recommend training on single videos!))."
)
if __name__ == "__main__":
extract_frames(algo='kmeans',numframes2pick=500, videos_list=videos)