Spaces:
Sleeping
Sleeping
update
Browse files- app.py +31 -21
- 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 =
|
| 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
|
| 63 |
try:
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
| 65 |
# Load the video
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
#
|
| 69 |
-
|
| 70 |
-
|
|
|
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 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
|
| 80 |
-
except
|
| 81 |
-
raise
|
| 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 |
-
|
| 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 |
-
|
| 186 |
-
|
| 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 |
-
|
| 2 |
-
moviepy
|
|
|
|
| 1 |
+
decord
|
|
|