nam_nguyenhoai_AI
commited on
Commit
•
4808241
1
Parent(s):
e16c706
update src
Browse files- algorithm.py +2 -44
- app.py +11 -18
- utils.py +2 -2
algorithm.py
CHANGED
@@ -3,7 +3,7 @@ from sklearn.metrics import pairwise_distances_argmin_min
|
|
3 |
import random
|
4 |
from utils import *
|
5 |
|
6 |
-
def
|
7 |
# Cluster the frames using K-Means
|
8 |
|
9 |
# K-means from sklearn
|
@@ -32,49 +32,7 @@ def kmeans(number_of_clusters, features):
|
|
32 |
|
33 |
return closest_clips_frames
|
34 |
|
35 |
-
def
|
36 |
-
|
37 |
-
i = 0
|
38 |
-
clips = []
|
39 |
-
|
40 |
-
# compare the sum of squared difference between clips i and j
|
41 |
-
for j in range(1, len(features)):
|
42 |
-
if sum_of_squared_difference(features[i], features[j]) > threshold:
|
43 |
-
clip = []
|
44 |
-
|
45 |
-
# add frames from clip i to j-1 to the clip list
|
46 |
-
for b in range(i*8, j*8):
|
47 |
-
clip.append(b)
|
48 |
-
|
49 |
-
# randomly select 15% of the frames from the clip list
|
50 |
-
random_num = round(len(clip)*0.15)
|
51 |
-
|
52 |
-
# sort the frames in the clip list to ensure the order of the frames
|
53 |
-
random_Frames = sorted(random.sample(clip, random_num))
|
54 |
-
i = j
|
55 |
-
clips.extend(random_Frames)
|
56 |
-
|
57 |
-
# add the last clip to the clip list
|
58 |
-
clip = []
|
59 |
-
if i==j:
|
60 |
-
for c in range(j*8, j*8+8):
|
61 |
-
clip.append(c)
|
62 |
-
random_num = round(len(clip)*0.15)
|
63 |
-
random_Frames = sorted(random.sample(clip, random_num))
|
64 |
-
#print("i == j")
|
65 |
-
|
66 |
-
else: # (i<j)
|
67 |
-
for c in range(i*8, (j+1)*8):
|
68 |
-
clip.append(c)
|
69 |
-
random_num = round(len(clip)*0.15)
|
70 |
-
random_Frames = sorted(random.sample(clip, random_num))
|
71 |
-
#print(f"{i} with {j}")
|
72 |
-
|
73 |
-
clips.extend(random_Frames)
|
74 |
-
|
75 |
-
return clips
|
76 |
-
|
77 |
-
def tt02(features, threshold):
|
78 |
|
79 |
i = 0
|
80 |
previous = i
|
|
|
3 |
import random
|
4 |
from utils import *
|
5 |
|
6 |
+
def offline(number_of_clusters, features):
|
7 |
# Cluster the frames using K-Means
|
8 |
|
9 |
# K-means from sklearn
|
|
|
32 |
|
33 |
return closest_clips_frames
|
34 |
|
35 |
+
def online(features, threshold):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
i = 0
|
38 |
previous = i
|
app.py
CHANGED
@@ -6,15 +6,12 @@ import numpy as np
|
|
6 |
from utils import *
|
7 |
from algorithm import *
|
8 |
|
9 |
-
def make_video(video_path, outdir='./summarized_video', algorithm='
|
10 |
-
if algorithm not in ["
|
11 |
-
algorithm = "
|
12 |
-
|
13 |
-
if model_version not in ["K600", "K400", "SSv2"]:
|
14 |
-
model_version = "K600"
|
15 |
|
16 |
# nen them vao cac truong hop mo hinh khac
|
17 |
-
model, processor, device = load_model(
|
18 |
|
19 |
# total_params = sum(param.numel() for param in model.parameters())
|
20 |
# print('Total parameters: {:.2f}M'.format(total_params / 1e6))
|
@@ -101,12 +98,10 @@ def make_video(video_path, outdir='./summarized_video', algorithm='Kmeans', mode
|
|
101 |
print("Shape of each clip: ", features[0].shape)
|
102 |
|
103 |
selected_frames = []
|
104 |
-
if algorithm == "
|
105 |
-
selected_frames =
|
106 |
-
elif algorithm == "Sum of Squared Difference 01":
|
107 |
-
selected_frames = tt01(features, 400)
|
108 |
else:
|
109 |
-
selected_frames =
|
110 |
|
111 |
print("Selected frame: ", selected_frames)
|
112 |
|
@@ -145,20 +140,18 @@ with gr.Blocks(css=css) as demo:
|
|
145 |
|
146 |
with gr.Row():
|
147 |
input_video = gr.Video(label="Input Video")
|
148 |
-
algorithm_type = gr.Dropdown(["
|
149 |
-
model_type = gr.Dropdown(["K600", "K400", "SSv2"], type="value", label='Model Type')
|
150 |
|
151 |
submit = gr.Button("Submit")
|
152 |
processed_video = gr.Video(label="Summarized Video")
|
153 |
|
154 |
-
def on_submit(uploaded_video, algorithm_type
|
155 |
print("Algorithm: ", algorithm_type)
|
156 |
-
print("Model Type: ", model_type)
|
157 |
# Process the video and get the path of the output video
|
158 |
-
output_video_path = make_video(uploaded_video, algorithm=algorithm_type
|
159 |
return output_video_path
|
160 |
|
161 |
-
submit.click(on_submit, inputs=[input_video, algorithm_type
|
162 |
|
163 |
if __name__ == '__main__':
|
164 |
demo.queue().launch(share=True)
|
|
|
6 |
from utils import *
|
7 |
from algorithm import *
|
8 |
|
9 |
+
def make_video(video_path, outdir='./summarized_video', algorithm='Offline (KMeans)'):
|
10 |
+
if algorithm not in ["Offline (KMeans)", "Online (Sum of Squared Difference)"]:
|
11 |
+
algorithm = "Offline (KMeans)"
|
|
|
|
|
|
|
12 |
|
13 |
# nen them vao cac truong hop mo hinh khac
|
14 |
+
model, processor, device = load_model()
|
15 |
|
16 |
# total_params = sum(param.numel() for param in model.parameters())
|
17 |
# print('Total parameters: {:.2f}M'.format(total_params / 1e6))
|
|
|
98 |
print("Shape of each clip: ", features[0].shape)
|
99 |
|
100 |
selected_frames = []
|
101 |
+
if algorithm == "Offline (KMeans)":
|
102 |
+
selected_frames = offline(number_of_clusters, features)
|
|
|
|
|
103 |
else:
|
104 |
+
selected_frames = online(features, 400)
|
105 |
|
106 |
print("Selected frame: ", selected_frames)
|
107 |
|
|
|
140 |
|
141 |
with gr.Row():
|
142 |
input_video = gr.Video(label="Input Video")
|
143 |
+
algorithm_type = gr.Dropdown(["Offline (KMeans)", "Online (Sum of Squared Difference)"], type="value", label='Algorithm')
|
|
|
144 |
|
145 |
submit = gr.Button("Submit")
|
146 |
processed_video = gr.Video(label="Summarized Video")
|
147 |
|
148 |
+
def on_submit(uploaded_video, algorithm_type):
|
149 |
print("Algorithm: ", algorithm_type)
|
|
|
150 |
# Process the video and get the path of the output video
|
151 |
+
output_video_path = make_video(uploaded_video, algorithm=algorithm_type)
|
152 |
return output_video_path
|
153 |
|
154 |
+
submit.click(on_submit, inputs=[input_video, algorithm_type], outputs=processed_video)
|
155 |
|
156 |
if __name__ == '__main__':
|
157 |
demo.queue().launch(share=True)
|
utils.py
CHANGED
@@ -52,10 +52,10 @@ def to_video(selected_frames, frames, output_path, video_fps):
|
|
52 |
video_writer.release()
|
53 |
print("Completed summarizing the video (wait for a moment to load).")
|
54 |
|
55 |
-
def load_model(
|
56 |
try:
|
57 |
DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
|
58 |
-
model = TimesformerModel.from_pretrained(f"facebook/timesformer-base-finetuned-
|
59 |
processor=VideoMAEImageProcessor.from_pretrained("MCG-NJU/videomae-base")
|
60 |
return model, processor, DEVICE
|
61 |
|
|
|
52 |
video_writer.release()
|
53 |
print("Completed summarizing the video (wait for a moment to load).")
|
54 |
|
55 |
+
def load_model():
|
56 |
try:
|
57 |
DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
|
58 |
+
model = TimesformerModel.from_pretrained(f"facebook/timesformer-base-finetuned-k600")
|
59 |
processor=VideoMAEImageProcessor.from_pretrained("MCG-NJU/videomae-base")
|
60 |
return model, processor, DEVICE
|
61 |
|