Update app.py
Browse files
app.py
CHANGED
|
@@ -1,80 +1,65 @@
|
|
| 1 |
-
import os
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
from mmaction.apis import init_recognizer, inference_recognizer
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
demo = gr.Interface(
|
| 67 |
-
fn=analyze_video,
|
| 68 |
-
inputs=gr.Video(label='Upload Video', height=300),
|
| 69 |
-
outputs=gr.Textbox(label='Analysis Results', lines=12),
|
| 70 |
-
title='π¬ GenVidBench - TSN (MMAction2)',
|
| 71 |
-
description='Upload a video. Inference uses TSN R50 on Kinetics-400.',
|
| 72 |
-
cache_examples=False,
|
| 73 |
-
flagging_mode='never'
|
| 74 |
-
)
|
| 75 |
-
|
| 76 |
-
if __name__ == '__main__':
|
| 77 |
-
demo.launch()
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from operator import itemgetter
|
| 4 |
+
from mmaction.apis import init_recognizer, inference_recognizer
|
| 5 |
+
|
| 6 |
+
# --- Config & Checkpoint ---
|
| 7 |
+
config_file = "demo/demo_configs/tsn_r50_1x1x8_video_infer.py"
|
| 8 |
+
checkpoint_file = "checkpoints/tsn_r50_8xb32-1x1x8-100e_kinetics400-rgb_20220818-2692d16c.pth"
|
| 9 |
+
label_file = "tools/data/kinetics/label_map_k400.txt"
|
| 10 |
+
|
| 11 |
+
# --- Initialize model ---
|
| 12 |
+
device = "cuda" if os.path.exists("/dev/nvidia0") else "cpu"
|
| 13 |
+
print(f"π Initializing TSN Model on {device}...")
|
| 14 |
+
model = init_recognizer(config_file, checkpoint_file, device=device)
|
| 15 |
+
|
| 16 |
+
# Load labels
|
| 17 |
+
with open(label_file) as f:
|
| 18 |
+
labels = [x.strip() for x in f.readlines()]
|
| 19 |
+
|
| 20 |
+
def analyze_video(video_path):
|
| 21 |
+
"""Run action recognition on uploaded video"""
|
| 22 |
+
try:
|
| 23 |
+
if video_path is None:
|
| 24 |
+
return "β Please upload a video file."
|
| 25 |
+
|
| 26 |
+
# Inference
|
| 27 |
+
results = inference_recognizer(model, video_path)
|
| 28 |
+
|
| 29 |
+
# Extract top-5 results
|
| 30 |
+
pred_scores = results.pred_score.tolist()
|
| 31 |
+
score_tuples = tuple(zip(range(len(pred_scores)), pred_scores))
|
| 32 |
+
score_sorted = sorted(score_tuples, key=itemgetter(1), reverse=True)
|
| 33 |
+
top5 = score_sorted[:5]
|
| 34 |
+
|
| 35 |
+
# Format results
|
| 36 |
+
lines = []
|
| 37 |
+
for idx, score in top5:
|
| 38 |
+
lines.append(f"{labels[idx]}: {score:.4f}")
|
| 39 |
+
|
| 40 |
+
return "\n".join(lines)
|
| 41 |
+
|
| 42 |
+
except Exception as e:
|
| 43 |
+
return f"β Error: {str(e)}"
|
| 44 |
+
|
| 45 |
+
# --- Gradio UI ---
|
| 46 |
+
demo = gr.Interface(
|
| 47 |
+
fn=analyze_video,
|
| 48 |
+
inputs=gr.Video(label="Upload Video", height=300, type="filepath"),
|
| 49 |
+
outputs=gr.Textbox(label="Top-5 Predictions", lines=10),
|
| 50 |
+
title="π¬ GenVidBench - TSN Action Recognition",
|
| 51 |
+
description="""
|
| 52 |
+
Upload a video and run **TSN (Temporal Segment Networks, ResNet-50 backbone)**
|
| 53 |
+
trained on **Kinetics-400**.
|
| 54 |
+
Model: `tsn_r50_8xb32-1x1x8-100e_kinetics400-rgb`
|
| 55 |
+
Benchmark accuracy ~80% (GenVidBench).
|
| 56 |
+
""",
|
| 57 |
+
examples=[["demo/demo.mp4"]] if os.path.exists("demo/demo.mp4") else None,
|
| 58 |
+
cache_examples=False,
|
| 59 |
+
theme=gr.themes.Soft(),
|
| 60 |
+
allow_flagging="never"
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
if __name__ == "__main__":
|
| 64 |
+
print("π Starting GenVidBench TSN Demo...")
|
| 65 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|