taesiri commited on
Commit
30bb4fd
1 Parent(s): bff8782
Files changed (2) hide show
  1. app.py +31 -21
  2. requirements.txt +1 -2
app.py CHANGED
@@ -32,12 +32,13 @@ import tempfile
32
  from zipfile import ZipFile
33
 
34
  import numpy as np
35
- from moviepy.editor import VideoFileClip
36
  from PIL import Image
 
 
37
 
38
 
39
  def download_samples(video_url, num_frames):
40
- frames = extract_frames_moviepy(video_url, num_frames)
41
 
42
  # Create a temporary directory to store the images
43
  with tempfile.TemporaryDirectory() as temp_dir:
@@ -59,26 +60,35 @@ def download_samples(video_url, num_frames):
59
  return zip_path
60
 
61
 
62
- def extract_frames_moviepy(video_url, num_frames=10):
63
  try:
64
- print(f"Extracting {num_frames} frames from {video_url}")
 
 
 
65
  # Load the video
66
- clip = VideoFileClip(video_url)
 
 
 
 
 
 
 
 
 
67
 
68
- # Calculate the times to extract frames
69
- duration = clip.duration
70
- frame_times = np.linspace(0, duration, num_frames, endpoint=False)
 
71
 
72
- frameas = []
73
- # Extract and save frames
74
- for i, time in enumerate(frame_times):
75
- frame = clip.get_frame(time)
76
- frame_image = Image.fromarray(frame)
77
- frameas.append(frame_image)
78
 
79
- return frameas
80
- except e as Exception:
81
- raise gr.Error(f"Error extracting frames from video: {e}")
82
 
83
 
84
  def get_latest_pots():
@@ -107,7 +117,7 @@ def get_latest_pots():
107
  for post in posts:
108
  title = post["data"]["title"]
109
  post_id = post["data"]["id"]
110
- print(f"ID: {post_id}, Title: {title}")
111
 
112
  # create [post_id, title] list
113
  examples = [[post["data"]["id"], post["data"]["title"]] for post in posts]
@@ -171,7 +181,7 @@ with gr.Blocks() as demo:
171
  gr.Markdown("## Sampled Frames from Video")
172
  with gr.Row():
173
  num_frames = gr.Slider(minimum=1, maximum=20, step=1, value=10)
174
- sample_btn = gr.Button("Sample")
175
 
176
  sampled_frames = gr.Gallery()
177
 
@@ -182,8 +192,8 @@ with gr.Blocks() as demo:
182
  download_samples, inputs=[video_player, num_frames], outputs=[output_files]
183
  )
184
 
185
- sample_btn.click(
186
- extract_frames_moviepy,
187
  inputs=[video_player, num_frames],
188
  outputs=[sampled_frames],
189
  )
 
32
  from zipfile import ZipFile
33
 
34
  import numpy as np
 
35
  from PIL import Image
36
+ from decord import VideoReader
37
+ from decord import cpu
38
 
39
 
40
  def download_samples(video_url, num_frames):
41
+ frames = extract_frames_decord(video_url, num_frames)
42
 
43
  # Create a temporary directory to store the images
44
  with tempfile.TemporaryDirectory() as temp_dir:
 
60
  return zip_path
61
 
62
 
63
+ def extract_frames_decord(video_path, num_frames=10):
64
  try:
65
+ start_time = time.time()
66
+
67
+ print(f"Extracting {num_frames} frames from {video_path}")
68
+
69
  # Load the video
70
+ vr = VideoReader(video_path, ctx=cpu(0))
71
+
72
+ # Calculate the indices for the frames to be extracted
73
+ total_frames = len(vr)
74
+ frame_indices = np.linspace(
75
+ 0, total_frames - 1, num_frames, dtype=int, endpoint=False
76
+ )
77
+
78
+ # Extract frames
79
+ batch_frames = vr.get_batch(frame_indices).asnumpy()
80
 
81
+ # Convert frames to PIL Images
82
+ frame_images = [
83
+ Image.fromarray(batch_frames[i]) for i in range(batch_frames.shape[0])
84
+ ]
85
 
86
+ end_time = time.time()
87
+ print(f"Decord extraction took {end_time - start_time} seconds")
 
 
 
 
88
 
89
+ return frame_images
90
+ except Exception as e:
91
+ raise Exception(f"Error extracting frames from video: {e}")
92
 
93
 
94
  def get_latest_pots():
 
117
  for post in posts:
118
  title = post["data"]["title"]
119
  post_id = post["data"]["id"]
120
+ # print(f"ID: {post_id}, Title: {title}")
121
 
122
  # create [post_id, title] list
123
  examples = [[post["data"]["id"], post["data"]["title"]] for post in posts]
 
181
  gr.Markdown("## Sampled Frames from Video")
182
  with gr.Row():
183
  num_frames = gr.Slider(minimum=1, maximum=20, step=1, value=10)
184
+ sample_decord_btn = gr.Button("Sample decord")
185
 
186
  sampled_frames = gr.Gallery()
187
 
 
192
  download_samples, inputs=[video_player, num_frames], outputs=[output_files]
193
  )
194
 
195
+ sample_decord_btn.click(
196
+ extract_frames_decord,
197
  inputs=[video_player, num_frames],
198
  outputs=[sampled_frames],
199
  )
requirements.txt CHANGED
@@ -1,2 +1 @@
1
- ffmpy
2
- moviepy
 
1
+ decord