Spaces:
Paused
Paused
Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -39,17 +39,38 @@ def _np(x):
|
|
| 39 |
|
| 40 |
|
| 41 |
def _read_frames(path, max_frames):
|
|
|
|
|
|
|
|
|
|
| 42 |
import imageio
|
|
|
|
| 43 |
reader = imageio.get_reader(path)
|
| 44 |
-
frames = []
|
| 45 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
for i, fr in enumerate(reader):
|
| 47 |
if i >= max_frames:
|
| 48 |
break
|
| 49 |
frames.append(np.asarray(fr))
|
|
|
|
| 50 |
finally:
|
| 51 |
reader.close()
|
| 52 |
-
return frames
|
| 53 |
|
| 54 |
|
| 55 |
@spaces.GPU(duration=300)
|
|
|
|
| 39 |
|
| 40 |
|
| 41 |
def _read_frames(path, max_frames):
|
| 42 |
+
"""Sample up to `max_frames` frames EVENLY across the whole video (proper intervals), not just the
|
| 43 |
+
first N. A clip with <= max_frames frames is taken in full; a longer clip is sub-sampled at a
|
| 44 |
+
constant stride so the selection spans start→end."""
|
| 45 |
import imageio
|
| 46 |
+
max_frames = max(1, int(max_frames))
|
| 47 |
reader = imageio.get_reader(path)
|
|
|
|
| 48 |
try:
|
| 49 |
+
try:
|
| 50 |
+
n = int(reader.count_frames())
|
| 51 |
+
except Exception: # some streams can't report a count -> fall back to sequential
|
| 52 |
+
n = 0
|
| 53 |
+
if n > max_frames:
|
| 54 |
+
# evenly-spaced indices spanning 0 .. n-1 (e.g. 96 frames, max 24 -> every 4th frame)
|
| 55 |
+
idxs = sorted({round(i * (n - 1) / (max_frames - 1)) for i in range(max_frames)}) \
|
| 56 |
+
if max_frames > 1 else [0]
|
| 57 |
+
frames = []
|
| 58 |
+
for i in idxs:
|
| 59 |
+
try:
|
| 60 |
+
frames.append(np.asarray(reader.get_data(i)))
|
| 61 |
+
except Exception:
|
| 62 |
+
break
|
| 63 |
+
if frames:
|
| 64 |
+
return frames
|
| 65 |
+
# short clip (or no count / seek unsupported): read sequentially up to max_frames
|
| 66 |
+
frames = []
|
| 67 |
for i, fr in enumerate(reader):
|
| 68 |
if i >= max_frames:
|
| 69 |
break
|
| 70 |
frames.append(np.asarray(fr))
|
| 71 |
+
return frames
|
| 72 |
finally:
|
| 73 |
reader.close()
|
|
|
|
| 74 |
|
| 75 |
|
| 76 |
@spaces.GPU(duration=300)
|