Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import os
|
| 4 |
+
import tempfile
|
| 5 |
+
import numpy as np
|
| 6 |
+
from models import Model # Modify based on your actual model class
|
| 7 |
+
from dataset import extract_features # Or however you handle input
|
| 8 |
+
from eval import predict # Assume this runs inference and returns timestamps
|
| 9 |
+
|
| 10 |
+
# Load model
|
| 11 |
+
def load_model(checkpoint_path='checkpoint/ckp_best.pth.tar'):
|
| 12 |
+
checkpoint = torch.load(checkpoint_path, map_location='cpu')
|
| 13 |
+
model = Model(**checkpoint['config']) # Adjust depending on how your model is initialized
|
| 14 |
+
model.load_state_dict(checkpoint['state_dict'])
|
| 15 |
+
model.eval()
|
| 16 |
+
return model
|
| 17 |
+
|
| 18 |
+
model = load_model()
|
| 19 |
+
|
| 20 |
+
def process_video(video_file):
|
| 21 |
+
# Save uploaded file
|
| 22 |
+
temp_path = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4").name
|
| 23 |
+
with open(temp_path, "wb") as f:
|
| 24 |
+
f.write(video_file.read())
|
| 25 |
+
|
| 26 |
+
# Optional: convert to features using your function
|
| 27 |
+
features = extract_features(temp_path) # Modify if needed
|
| 28 |
+
# Save to temp .npz file if your pipeline needs it
|
| 29 |
+
npz_path = temp_path.replace(".mp4", ".npz")
|
| 30 |
+
np.savez(npz_path, features=features)
|
| 31 |
+
|
| 32 |
+
# Predict
|
| 33 |
+
predictions = predict(model, npz_path)
|
| 34 |
+
|
| 35 |
+
# Format output
|
| 36 |
+
results = "\n".join([
|
| 37 |
+
f"{label}: {start:.2f}s - {end:.2f}s"
|
| 38 |
+
for label, start, end in predictions
|
| 39 |
+
])
|
| 40 |
+
|
| 41 |
+
os.remove(temp_path)
|
| 42 |
+
os.remove(npz_path)
|
| 43 |
+
return results
|
| 44 |
+
|
| 45 |
+
demo = gr.Interface(
|
| 46 |
+
fn=process_video,
|
| 47 |
+
inputs=gr.Video(label="Upload a video"),
|
| 48 |
+
outputs=gr.Textbox(label="Detected Actions"),
|
| 49 |
+
title="Temporal Action Localization"
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
if __name__ == "__main__":
|
| 53 |
+
demo.launch()
|