Spaces:
Sleeping
Sleeping
update
Browse files
app.py
CHANGED
|
@@ -27,8 +27,36 @@ from PIL import Image
|
|
| 27 |
cached_latest_posts_df = None
|
| 28 |
last_fetched = None
|
| 29 |
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
| 31 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
|
| 34 |
def extract_frames_moviepy(video_url, num_frames=10):
|
|
@@ -147,6 +175,13 @@ with gr.Blocks() as demo:
|
|
| 147 |
|
| 148 |
sampled_frames = gr.Gallery()
|
| 149 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
sample_btn.click(
|
| 151 |
extract_frames_moviepy,
|
| 152 |
inputs=[video_player, num_frames],
|
|
|
|
| 27 |
cached_latest_posts_df = None
|
| 28 |
last_fetched = None
|
| 29 |
|
| 30 |
+
import os
|
| 31 |
+
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:
|
| 44 |
+
# Save each frame as a JPEG image in the temporary directory
|
| 45 |
+
for i, frame in enumerate(frames):
|
| 46 |
+
frame_path = os.path.join(temp_dir, f"frame_{i}.jpg")
|
| 47 |
+
frame.save(
|
| 48 |
+
frame_path, format="JPEG", quality=85
|
| 49 |
+
) # Adjust quality as needed
|
| 50 |
+
|
| 51 |
+
# Create a zip file in a persistent location
|
| 52 |
+
zip_path = "frames.zip"
|
| 53 |
+
with ZipFile(zip_path, "w") as zipf:
|
| 54 |
+
for i in range(num_frames):
|
| 55 |
+
frame_path = os.path.join(temp_dir, f"frame_{i}.jpg")
|
| 56 |
+
zipf.write(frame_path, os.path.basename(frame_path))
|
| 57 |
+
|
| 58 |
+
# Return the path of the zip file
|
| 59 |
+
return zip_path
|
| 60 |
|
| 61 |
|
| 62 |
def extract_frames_moviepy(video_url, num_frames=10):
|
|
|
|
| 175 |
|
| 176 |
sampled_frames = gr.Gallery()
|
| 177 |
|
| 178 |
+
download_samples_btn = gr.Button("Download Samples")
|
| 179 |
+
output_files = gr.File()
|
| 180 |
+
|
| 181 |
+
download_samples_btn.click(
|
| 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],
|