Spaces:
Running
Running
added gifs support
Browse files- app/video.py +15 -1
app/video.py
CHANGED
|
@@ -1,6 +1,20 @@
|
|
|
|
|
|
|
|
| 1 |
import cv2
|
| 2 |
import numpy as np
|
| 3 |
-
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
|
| 6 |
def sample_frames(video_path: str, n_frames: int = 5) -> list[Image.Image]:
|
|
|
|
| 1 |
+
import io
|
| 2 |
+
|
| 3 |
import cv2
|
| 4 |
import numpy as np
|
| 5 |
+
from PIL import Image, ImageSequence
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def sample_gif_frames(data: bytes, n_frames: int = 5) -> list[Image.Image]:
|
| 9 |
+
"""Sample n_frames uniformly from a GIF. Returns RGB PIL images."""
|
| 10 |
+
gif = Image.open(io.BytesIO(data))
|
| 11 |
+
frames = list(ImageSequence.Iterator(gif))
|
| 12 |
+
total = len(frames)
|
| 13 |
+
if total == 0:
|
| 14 |
+
raise ValueError("GIF has no frames")
|
| 15 |
+
n = min(n_frames, total)
|
| 16 |
+
indices = np.linspace(0, total - 1, n, dtype=int)
|
| 17 |
+
return [frames[int(i)].convert("RGB") for i in indices]
|
| 18 |
|
| 19 |
|
| 20 |
def sample_frames(video_path: str, n_frames: int = 5) -> list[Image.Image]:
|