AnsenH commited on
Commit
c6145cf
·
1 Parent(s): 24860f2

chore: update dependency version, UI

Browse files
Files changed (3) hide show
  1. README.md +1 -1
  2. app.py +27 -21
  3. requirements.txt +2 -1
README.md CHANGED
@@ -1,6 +1,6 @@
1
  ---
2
  title: Highlight Detection with MomentDETR
3
- emoji: ✍️
4
  colorFrom: purple
5
  colorTo: yellow
6
  sdk: gradio
 
1
  ---
2
  title: Highlight Detection with MomentDETR
3
+ emoji: 🎞️
4
  colorFrom: purple
5
  colorTo: yellow
6
  sdk: gradio
app.py CHANGED
@@ -1,10 +1,15 @@
1
  import gradio as gr
2
  from run_on_video.run import MomentDETRPredictor
3
  from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
 
 
 
 
 
4
 
5
  ckpt_path = "run_on_video/moment_detr_ckpt/model_best.ckpt"
6
  clip_model_name_or_path = "ViT-B/32"
7
- device = 'cpu'
8
 
9
  moment_detr_predictor = MomentDETRPredictor(
10
  ckpt_path=ckpt_path,
@@ -16,20 +21,24 @@ def trim_video(video_path, start, end, output_file='result.mp4'):
16
  ffmpeg_extract_subclip(video_path, start, end, targetname=output_file)
17
  return output_file
18
 
19
- with gr.Blocks() as demo:
 
 
 
20
  output_videos = gr.State([])
21
- moment_scores = gr.State([])
22
- gr.HTML("""<h2 align="center"> ✍️ Highlight Detection with MomentDETR </h2>""")
 
23
  with gr.Column():
24
  with gr.Row():
25
  with gr.Blocks():
26
  with gr.Column():
27
  gr.HTML("""<h3 align="center"> Input Video </h3>""")
28
- input_video = gr.PlayableVideo()
29
  with gr.Blocks():
30
  with gr.Column():
31
  gr.HTML("""<h3 align="center"> Highlight Videos </h3>""")
32
- playable_video = gr.PlayableVideo()
33
  with gr.Row():
34
  with gr.Column():
35
  retrieval_text = gr.Textbox(
@@ -41,15 +50,15 @@ with gr.Blocks() as demo:
41
  with gr.Column():
42
  display_score = gr.Markdown("### Moment Score: ")
43
  radio_button = gr.Radio(
44
- choices=[i for i in range(10)],
45
  label="Moments",
46
- value=0
47
  )
48
 
49
- def update_video_player(radio_value, output_videos, moment_scores):
50
  return {
51
- playable_video: output_videos[radio_value],
52
- display_score: f'### Moment Score: {moment_scores[radio_value]}'
53
  }
54
 
55
  def submit_video(input_video, retrieval_text):
@@ -61,35 +70,32 @@ with gr.Blocks() as demo:
61
  video_path=input_video,
62
  query_list=[retrieval_text]
63
  )
64
- pred_windows = [[pred[0], pred[1]]for pred in predictions[0]['pred_relevant_windows']]
65
- scores = [pred[-1] for pred in predictions[0]['pred_relevant_windows']]
66
-
67
- print(f'== predict start end time: {pred_windows}')
68
- print(f'== prediction scores: {scores}')
69
  output_files = [ trim_video(
70
  video_path=input_video,
71
  start=pred_windows[i][0],
72
  end=pred_windows[i][1],
73
  output_file=f'{i}.mp4'
74
  ) for i in range(10)]
75
- print(f'== output_files: {output_files}')
76
  return {
77
  output_videos: output_files,
78
- moment_scores: scores,
79
  playable_video: output_files[0],
80
- display_score: f'### Moment Score: {scores[0]}'
81
  }
82
 
83
  radio_button.change(
84
  fn=update_video_player,
85
- inputs=[radio_button, output_videos, moment_scores],
86
  outputs=[playable_video, display_score]
87
  )
88
 
89
  submit.click(
90
  fn=submit_video,
91
  inputs=[input_video, retrieval_text],
92
- outputs=[output_videos, moment_scores, playable_video, display_score]
93
  )
94
 
95
  demo.launch()
 
1
  import gradio as gr
2
  from run_on_video.run import MomentDETRPredictor
3
  from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
4
+ import torch
5
+
6
+ DESCRIPTION = """
7
+ _This Space demonstrates model [QVHighlights: Detecting Moments and Highlights in Videos via Natural Language Queries](https://arxiv.org/abs/2107.09609), NeurIPS 2021, by [Jie Lei](http://www.cs.unc.edu/~jielei/), [Tamara L. Berg](http://tamaraberg.com/), [Mohit Bansal](http://www.cs.unc.edu/~mbansal/)_
8
+ """
9
 
10
  ckpt_path = "run_on_video/moment_detr_ckpt/model_best.ckpt"
11
  clip_model_name_or_path = "ViT-B/32"
12
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
13
 
14
  moment_detr_predictor = MomentDETRPredictor(
15
  ckpt_path=ckpt_path,
 
21
  ffmpeg_extract_subclip(video_path, start, end, targetname=output_file)
22
  return output_file
23
 
24
+ def display_prediction(result):
25
+ return f'Moment({result[0]} ~ {result[1]}), Score: {result[2]}'
26
+
27
+ with gr.Blocks(theme=gr.themes.Default()) as demo:
28
  output_videos = gr.State([])
29
+ moment_prediction = gr.State([])
30
+ gr.HTML("""<h2 align="center"> 🎞️ Highlight Detection with MomentDETR </h2>""")
31
+ gr.Markdown(DESCRIPTION)
32
  with gr.Column():
33
  with gr.Row():
34
  with gr.Blocks():
35
  with gr.Column():
36
  gr.HTML("""<h3 align="center"> Input Video </h3>""")
37
+ input_video = gr.Video(label="Please input mp4", height=400)
38
  with gr.Blocks():
39
  with gr.Column():
40
  gr.HTML("""<h3 align="center"> Highlight Videos </h3>""")
41
+ playable_video = gr.Video(height=400)
42
  with gr.Row():
43
  with gr.Column():
44
  retrieval_text = gr.Textbox(
 
50
  with gr.Column():
51
  display_score = gr.Markdown("### Moment Score: ")
52
  radio_button = gr.Radio(
53
+ choices=[i+1 for i in range(10)],
54
  label="Moments",
55
+ value=1
56
  )
57
 
58
+ def update_video_player(radio_value, output_videos, moment_prediction):
59
  return {
60
+ playable_video: output_videos[radio_value-1],
61
+ display_score: display_prediction(moment_prediction[radio_value-1])
62
  }
63
 
64
  def submit_video(input_video, retrieval_text):
 
70
  video_path=input_video,
71
  query_list=[retrieval_text]
72
  )
73
+ predictions = predictions[0]['pred_relevant_windows']
74
+ pred_windows = [[pred[0], pred[1]]for pred in predictions]
 
 
 
75
  output_files = [ trim_video(
76
  video_path=input_video,
77
  start=pred_windows[i][0],
78
  end=pred_windows[i][1],
79
  output_file=f'{i}.mp4'
80
  ) for i in range(10)]
81
+
82
  return {
83
  output_videos: output_files,
84
+ moment_prediction: predictions,
85
  playable_video: output_files[0],
86
+ display_score: display_prediction(predictions[0])
87
  }
88
 
89
  radio_button.change(
90
  fn=update_video_player,
91
+ inputs=[radio_button, output_videos, moment_prediction],
92
  outputs=[playable_video, display_score]
93
  )
94
 
95
  submit.click(
96
  fn=submit_video,
97
  inputs=[input_video, retrieval_text],
98
+ outputs=[output_videos, moment_prediction, playable_video, display_score]
99
  )
100
 
101
  demo.launch()
requirements.txt CHANGED
@@ -12,4 +12,5 @@ ffmpeg-python
12
  ftfy
13
  regex
14
  Pillow
15
- moviepy
 
 
12
  ftfy
13
  regex
14
  Pillow
15
+ moviepy==1.0.3
16
+ gradio==3.41.0