Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import numpy as np
|
| 4 |
+
import cv2
|
| 5 |
+
from transformers import AutoTokenizer, AutoModel
|
| 6 |
+
|
| 7 |
+
# Load model and tokenizer
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained("facebook/vjepa2-vitl-fpc64-256")
|
| 9 |
+
model = AutoModel.from_pretrained("facebook/vjepa2-vitl-fpc64-256")
|
| 10 |
+
|
| 11 |
+
def extract_frames(video_path, num_frames=8):
|
| 12 |
+
"""Extract frames from a video file."""
|
| 13 |
+
cap = cv2.VideoCapture(video_path)
|
| 14 |
+
frames = []
|
| 15 |
+
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 16 |
+
frame_indices = np.linspace(0, total_frames - 1, num_frames, dtype=int)
|
| 17 |
+
|
| 18 |
+
for idx in frame_indices:
|
| 19 |
+
cap.set(cv2.CAP_PROP_POS_FRAMES, idx)
|
| 20 |
+
ret, frame = cap.read()
|
| 21 |
+
if ret:
|
| 22 |
+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 23 |
+
frames.append(frame)
|
| 24 |
+
cap.release()
|
| 25 |
+
return frames
|
| 26 |
+
|
| 27 |
+
def process_video(video_file):
|
| 28 |
+
"""Process video and extract embeddings."""
|
| 29 |
+
# Extract frames
|
| 30 |
+
frames = extract_frames(video_file)
|
| 31 |
+
|
| 32 |
+
# Preprocess frames (resize, normalize, etc.)
|
| 33 |
+
processed_frames = []
|
| 34 |
+
for frame in frames:
|
| 35 |
+
frame = cv2.resize(frame, (256, 256)) # Adjust to model's expected input
|
| 36 |
+
frame = frame / 255.0 # Normalize
|
| 37 |
+
processed_frames.append(frame)
|
| 38 |
+
|
| 39 |
+
# Convert to tensor (batch_size, num_frames, C, H, W)
|
| 40 |
+
video_tensor = torch.tensor(np.stack(processed_frames)).permute(0, 3, 1, 2).unsqueeze(0).float()
|
| 41 |
+
|
| 42 |
+
# Get embeddings
|
| 43 |
+
with torch.no_grad():
|
| 44 |
+
outputs = model(video_tensor)
|
| 45 |
+
|
| 46 |
+
# Return the embeddings (or process further)
|
| 47 |
+
embeddings = outputs.last_hidden_state.mean(dim=1).squeeze().numpy()
|
| 48 |
+
|
| 49 |
+
return {
|
| 50 |
+
"embeddings": embeddings,
|
| 51 |
+
"frames": frames # Display the extracted frames
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
# Gradio Interface
|
| 55 |
+
with gr.Blocks() as demo:
|
| 56 |
+
gr.Markdown("# V-JEPA Video Embedding Extractor")
|
| 57 |
+
gr.Markdown("Upload a video to extract embeddings using `facebook/vjepa2-vitl-fpc64-256`.")
|
| 58 |
+
|
| 59 |
+
with gr.Row():
|
| 60 |
+
video_input = gr.Video(label="Upload Video")
|
| 61 |
+
submit_btn = gr.Button("Process")
|
| 62 |
+
|
| 63 |
+
with gr.Row():
|
| 64 |
+
frame_gallery = gr.Gallery(label="Extracted Frames")
|
| 65 |
+
embeddings_output = gr.JSON(label="Embeddings")
|
| 66 |
+
|
| 67 |
+
submit_btn.click(
|
| 68 |
+
fn=process_video,
|
| 69 |
+
inputs=video_input,
|
| 70 |
+
outputs=[frame_gallery, embeddings_output]
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
demo.launch()
|