-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.py
More file actions
398 lines (298 loc) · 11.2 KB
/
functions.py
File metadata and controls
398 lines (298 loc) · 11.2 KB
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
import cv2
import matplotlib.pyplot as plt
import math
import numpy as np
import pandas as pd
import shutil
import os
from scipy.signal import argrelextrema
from sklearn.cluster import DBSCAN
from sklearn import metrics
from sklearn.neighbors import NearestNeighbors
# Extension for the frames
IMG_EXT = '.jpg'
def plot_data(x:list, y:list, x_label, y_label, title, x_ticks=False, y_ticks=False) -> None:
plt.figure("Figure")
plt.xlabel(x_label)
plt.ylabel(y_label)
plt.title(title)
if x_ticks and y_ticks:
plt.xticks(x)
plt.yticks(y)
plt.scatter(x, y, marker='*')
else:
plt.scatter(x, y, marker='o')
plt.show()
return
def get_directory(video_path:str) -> str:
"""Path of the video
Args:
video_path (str): A string that represent the absolute path of the video
Returns:
str: The absolute path of the video file
"""
if '/' in video_path:
dir = video_path.split('/')
dir = list(filter(lambda x: x!='', dir))
dir = '/'+'/'.join(dir[:-1])
return dir
return ''
def delete_directory(path:str) -> None:
"""Remove folder from previous analizes
Args:
video_path (str): A string that represent the absolute path of the video
Returns:
None
"""
folders = tuple(filter(lambda d: d in ('Key_Frames', 'Frames'), os.listdir(path)))
for folder in folders:
p = os.path.join(path, folder)
try:
shutil.rmtree(p)
except:
pass
return None
def get_entropy(n_frames:int, video_path:str) -> list:
"""Returns a list that contain entropy values
Args:
n_frames (int): An integer that represent the number of frames
video_path (str): A string that represent the absolute path of the video
Returns:
list: A list that contain entropy values for each frame
"""
frames_dir = os.path.join(video_path, 'Frames')
entropy_list = []
if n_frames is not None:
for i in range(1, n_frames + 1):
# We get the image
img = cv2.imread(f'{frames_dir}/Frame{i}{IMG_EXT}')
# Convert the image color to gray scales
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#entropy_image = entropy(img, disk(5))
entropy_list.append(entropy(img))
else:
dir_images = sorted(os.listdir(video_path))
for i in dir_images:
# We get the image
img = cv2.imread(os.path.join(video_path, i))
# Convert the image color to gray scales
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#entropy_image = entropy(img, disk(5))
entropy_list.append(entropy(img))
return entropy_list
def extract(video_path:str) -> int:
"""Returns the total numbers of frames
Args:
video_path (str): A string that represent the absolute path of the video
Returns:
int: Integer that represent the total of frames of the video
"""
# Cacth the video
cap = cv2.VideoCapture(video_path)
# Calculate the number of frames
numbers_of_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
print("Extracting frames from video...")
frames_dir = os.path.join(get_directory(video_path), 'Frames')
os.mkdir(frames_dir)
# Iterate on each frame
for i in range(1, numbers_of_frames + 1):
ret, frame = cap.read()
# This condition prevents from infinite looping
# incase video ends.
if ret == False:
break
# Save Frame by Frame into a specific path using imwrite method
cv2.imwrite(frames_dir+'/Frame'+str(i)+IMG_EXT, frame)
return numbers_of_frames
# Return the entropy of a image
def entropy(im:cv2.cvtColor) -> float:
"""Returns the entropy of an image
Args:
im (cv2.cvtColor): An image
Returns:
float: A float that represents the image entropy
"""
# Compute normalized histogram -> p(g)
p = np.array([(im==v).sum() for v in range(256)])
p = p/p.sum()
# Compute e = -sum(p(g)*log2(p(g)))
e = -(p[p>0]*np.log2(p[p>0])).sum()
return e
def find_locals(entropy_values:list) -> tuple:
"""Compute the minimum and maximum locals points
Args:
entropy_values (list): A list that contain entropy values
Returns:
A Tuple: it returns a tuple that contain frames and entropy values lists
"""
frame_entropy = {i+1:v for i, v in enumerate(entropy_values)}
new_array = np.array(entropy_values)
local_max = new_array[argrelextrema(new_array, np.greater)[0]]
local_min = new_array[argrelextrema(new_array, np.less)[0]]
Pext = list(local_max) + list(local_min)
frames = []
entropies = []
for k, v in frame_entropy.items():
if v in Pext:
frames.append(k)
entropies.append(v)
return (frames, entropies)
def list_to_df(frames:list, entropy:list):
"""Returns a DataFrame
Args:
frames (list): A list that contain i frames
entropy (list): A list that contain entropy values
Returns:
DataFrame: A DataFrame that contain frames and entropy values in order
"""
frames_entropy = []
for i, value in enumerate(frames):
frames_entropy.append((value, entropy[i]))
df = pd.DataFrame(frames_entropy, columns=('Frame', 'Entropy'))
return df
def get_max_curvature(distances:list) -> float:
"""Returns a float
Args:
distances (list): A list that contains the average distance between each point in the data set
Returns:
float: A float that represents epsilon value for the algorithm
"""
step = 2
result = []
for index, value in enumerate(distances):
count, acc = 0.0, 0.0
for step1 in range(1,step+1):
if index-step1>0:
temp_v = distances[index-step1]
count += 1
acc += abs(distances[index] - temp_v)
for step1 in range(1,step+1):
if index+step1<len(distances):
temp_v = distances[index+step1]
count+=1
acc += abs(distances[index] - temp_v)
result.append(acc/count)
idx = result.index(max(result))
eps = distances[idx]
print(f"This is the value of eps: {eps}")
return eps
def estimate_value(df:pd.core.frame.DataFrame) -> float:
"""Returns a list
Args:
df (DataFrame): A DataFrame that represents all the points
Returns:
float: A float that represents epsilon value for the algorithm
"""
# creating an object of the NearestNeighbors class
neighb = NearestNeighbors(n_neighbors=2)
# fitting the data to the object
nbrs = neighb.fit(df)
# finding the nearest neighbours
distances,indices = nbrs.kneighbors(df)
# Sort the distances results
distances = np.sort(distances, axis = 0)
distances = distances[:, 1]
eps = get_max_curvature(distances)
# plt.rcParams['figure.figsize'] = (5,3)
# plt.plot(distances)
# plt.xlabel('Frames')
# plt.ylabel('Distancia promedio')
# plt.title('Resultado de aplicar el algoritmo NearestNeighbors')
# plt.show()
return eps
def plot_clusters(df, labels) -> None:
plt.figure("DBSCAN Algorithm")
plt.scatter(df.Frame, df.Entropy, c = labels, cmap= "plasma")
plt.xlabel("Frames")
plt.ylabel("Entropía")
plt.title('Resultado del algoritmo DBSCAN')
plt.show()
def algorithm(df:pd.core.frame.DataFrame, Eps:float, n_points:int=4) -> list:
"""Returns a list
Args:
df (DataFrame): A DataFrame that represents all the points
Eps (Float): Epsilon that represents the size of area to consider a dense region
n_points (int): The total number of points in a dense region
Returns:
list: A list that containts all the cluster find in the data
"""
print(f"This are the parameters for the algorithm: Eps->{Eps}, n_points->{n_points}")
dbscan = DBSCAN(eps = Eps, min_samples = n_points).fit(df)
labels = dbscan.labels_
#plot_clusters(df, labels)
#accu = metrics.silhouette_score(df, labels)
#print(f"This is the acurration {accu}")
return labels
def divide_clusters(clusters:list) -> dict:
"""Returns a Dict
Args:
clusters (list): A list that contain all cluster without segmentation
Returns:
Dict: A dict that contain clusters number as key and a points list as value
"""
clusters_points = {}
for i, label in enumerate(clusters):
# We take all points except outliers
if label!=-1:
if label in clusters_points:
clusters_points[label].append(i)
else:
clusters_points[label] = [i]
print(f"This are the clusters found it: {clusters_points}")
return clusters_points
def get_den_point(positions:list, df) -> int:
"""Returns a list
Args:
positions (list): A list that contain points
Returns:
Int: A integer that represents the most dense point in a cluster
"""
densities = []
for i, current_point in enumerate(positions):
a = (df.Frame[current_point], df.Entropy[current_point])
density = 0
for j, next_point in enumerate(positions):
if j!=i:
b = (df.Frame[next_point], df.Entropy[next_point])
# Compute the Euclidean distance between two points a and b.
density += math.dist(a, b)
densities.append((current_point, density))
densities = sorted(densities, key=lambda d: d[1])
return densities[0][0]
def plot_key_frames(key_frames:list, df) -> None:
y = []
for i in key_frames:
idx = np.where(df["Frame"]==i)[0][0]
y.append(df['Entropy'][idx])
plot_data(key_frames, y, 'Frames', 'Entropía', 'Representación de los Key Frames', True, True)
return None
def find_key_frames(df, clusters:list) -> list:
"""Returns a list
Args:
df (DataFrame): A DataFrame that contain all points
clusters (list): A list that contain all cluster without segmentation
Returns:
list: A list that contain all the key frames
"""
clusters_points = divide_clusters(clusters)
key_frames = []
for points in clusters_points.values():
key_frames.append(df.Frame[get_den_point(points, df)])
#plot_key_frames(key_frames, df)
return sorted(key_frames)
def move_key_frames(key_frames:list, video_path:str) -> str:
"""Returns the path of the key frames
Args:
key_frames (list): A list that contains all the key frames
video_path (str): A string that represent the absolute path of the video
Returns:
str: A string that represents the path of the key frames
"""
key_frames_dir = os.path.join(video_path, 'Key_Frames')
os.mkdir(key_frames_dir)
frames_dir = os.path.join(video_path, 'Frames')
for key_frame in key_frames:
src_path = f'{frames_dir}/Frame{key_frame}{IMG_EXT}'
dst_path = f'{key_frames_dir}/Frame{key_frame}{IMG_EXT}'
shutil.move(src_path, dst_path)
return key_frames_dir