nam_nguyenhoai_AI commited on
Commit
48858f9
·
1 Parent(s): 3d8c5e9

fix bugs and update src

Browse files
Files changed (2) hide show
  1. app.py +18 -18
  2. utils.py +2 -2
app.py CHANGED
@@ -6,13 +6,15 @@ import numpy as np
6
  from utils import *
7
  from algorithm import *
8
 
9
- def make_video(video_path, outdir='./summarized_video',encoder='Kmeans'):
10
- if encoder not in ["Kmeans", "Sum of Squared Difference 01", "Sum of Squared Difference 02"]:
11
- encoder = "Kmeans"
12
- # nen them vao cac truong hop mo hinh khac
13
- margin_width = 50
14
 
15
- model, processor, device = load_model()
 
 
 
 
16
 
17
  # total_params = sum(param.numel() for param in model.parameters())
18
  # print('Total parameters: {:.2f}M'.format(total_params / 1e6))
@@ -35,8 +37,7 @@ def make_video(video_path, outdir='./summarized_video',encoder='Kmeans'):
35
  frame_width, frame_height = int(raw_video.get(cv2.CAP_PROP_FRAME_WIDTH)), int(raw_video.get(cv2.CAP_PROP_FRAME_HEIGHT))
36
  frame_rate = int(raw_video.get(cv2.CAP_PROP_FPS))
37
  #length = int(raw_video.get(cv2.CAP_PROP_FRAME_COUNT))
38
- output_width = frame_width * 2 + margin_width
39
-
40
  filename = os.path.basename(filename)
41
 
42
  # Find the size to resize
@@ -55,13 +56,8 @@ def make_video(video_path, outdir='./summarized_video',encoder='Kmeans'):
55
  frames = []
56
  features = []
57
 
58
- # output_path = os.path.join(outdir, filename[:filename.rfind('.')] + '_video_depth.mp4')
59
  with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as tmpfile:
60
  output_path = tmpfile.name
61
- #out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*"avc1"), frame_rate, (output_width, frame_height))
62
- fourcc = cv2.VideoWriter_fourcc(*'mp4v')
63
- out = cv2.VideoWriter(output_path, fourcc, frame_rate, (output_width, frame_height))
64
- # count=0
65
 
66
  while raw_video.isOpened():
67
  ret, raw_frame = raw_video.read()
@@ -102,13 +98,15 @@ def make_video(video_path, outdir='./summarized_video',encoder='Kmeans'):
102
  print("Shape of each clip: ", features[0].shape)
103
 
104
  selected_frames = []
105
- if encoder == "Kmeans":
106
  selected_frames = kmeans(number_of_clusters, features)
107
- elif encoder == "Sum of Squared Difference 01":
108
  selected_frames = tt01(features, 400)
109
  else:
110
  selected_frames = tt02(features, 400)
111
 
 
 
112
  video_writer = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'mp4v'), frame_rate, (frames[0].shape[1], frames[0].shape[0]))
113
  for idx in selected_frames:
114
  video_writer.write(frames[idx])
@@ -144,16 +142,18 @@ with gr.Blocks(css=css) as demo:
144
  with gr.Row():
145
  input_video = gr.Video(label="Input Video")
146
  algorithm_type = gr.Dropdown(["Kmeans", "Sum of Squared Difference 01", "Sum of Squared Difference 02"], type="value", label='Algorithm')
 
 
147
  submit = gr.Button("Submit")
148
  processed_video = gr.Video(label="Summarized Video")
149
 
150
- def on_submit(uploaded_video,algorithm_type):
151
 
152
  # Process the video and get the path of the output video
153
- output_video_path = make_video(uploaded_video,encoder=algorithm_type)
154
  return output_video_path
155
 
156
  submit.click(on_submit, inputs=[input_video, algorithm_type], outputs=processed_video)
157
 
158
  if __name__ == '__main__':
159
- demo.queue().launch()
 
6
  from utils import *
7
  from algorithm import *
8
 
9
+ def make_video(video_path, outdir='./summarized_video', algorithm='Kmeans', model_version='K600'):
10
+ if algorithm not in ["Kmeans", "Sum of Squared Difference 01", "Sum of Squared Difference 02"]:
11
+ algorithm = "Kmeans"
 
 
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(model_version)
18
 
19
  # total_params = sum(param.numel() for param in model.parameters())
20
  # print('Total parameters: {:.2f}M'.format(total_params / 1e6))
 
37
  frame_width, frame_height = int(raw_video.get(cv2.CAP_PROP_FRAME_WIDTH)), int(raw_video.get(cv2.CAP_PROP_FRAME_HEIGHT))
38
  frame_rate = int(raw_video.get(cv2.CAP_PROP_FPS))
39
  #length = int(raw_video.get(cv2.CAP_PROP_FRAME_COUNT))
40
+
 
41
  filename = os.path.basename(filename)
42
 
43
  # Find the size to resize
 
56
  frames = []
57
  features = []
58
 
 
59
  with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as tmpfile:
60
  output_path = tmpfile.name
 
 
 
 
61
 
62
  while raw_video.isOpened():
63
  ret, raw_frame = raw_video.read()
 
98
  print("Shape of each clip: ", features[0].shape)
99
 
100
  selected_frames = []
101
+ if algorithm == "Kmeans":
102
  selected_frames = kmeans(number_of_clusters, features)
103
+ elif algorithm == "Sum of Squared Difference 01":
104
  selected_frames = tt01(features, 400)
105
  else:
106
  selected_frames = tt02(features, 400)
107
 
108
+ print("Selected frame: ", selected_frames)
109
+
110
  video_writer = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'mp4v'), frame_rate, (frames[0].shape[1], frames[0].shape[0]))
111
  for idx in selected_frames:
112
  video_writer.write(frames[idx])
 
142
  with gr.Row():
143
  input_video = gr.Video(label="Input Video")
144
  algorithm_type = gr.Dropdown(["Kmeans", "Sum of Squared Difference 01", "Sum of Squared Difference 02"], type="value", label='Algorithm')
145
+ model_type = gr.Dropdown(["K600", "K400", "SSv2"], type="value", label='Model Type')
146
+
147
  submit = gr.Button("Submit")
148
  processed_video = gr.Video(label="Summarized Video")
149
 
150
+ def on_submit(uploaded_video, algorithm_type, model_type):
151
 
152
  # Process the video and get the path of the output video
153
+ output_video_path = make_video(uploaded_video, encoder=algorithm_type, model_version= model_type)
154
  return output_video_path
155
 
156
  submit.click(on_submit, inputs=[input_video, algorithm_type], outputs=processed_video)
157
 
158
  if __name__ == '__main__':
159
+ 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("facebook/timesformer-base-finetuned-k600").to(DEVICE).eval()
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(model_version):
56
  try:
57
  DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
58
+ model = TimesformerModel.from_pretrained(f"facebook/timesformer-base-finetuned-{model_version}").to(DEVICE).eval()
59
  processor=VideoMAEImageProcessor.from_pretrained("MCG-NJU/videomae-base")
60
  return model, processor, DEVICE
61