File size: 2,144 Bytes
efb1c7c |
1 2 3 4 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 67 |
import os
from operator import itemgetter
import gradio as gr
from mmaction.apis import init_recognizer, inference_recognizer
CONFIG_FILE = 'demo/demo_configs/tsn_r50_1x1x8_video_infer.py'
CHECKPOINT_FILE = 'checkpoints/tsn_r50_1x1x3_100e_kinetics400_rgb_20200614-e508be42.pth'
LABEL_FILE = 'tools/data/kinetics/label_map_k400.txt'
def load_labels(path):
if os.path.exists(path):
with open(path, 'r') as f:
return [x.strip() for x in f if x.strip()]
return None
def build_model():
if not os.path.exists(CHECKPOINT_FILE):
raise FileNotFoundError(f'Checkpoint not found at {CHECKPOINT_FILE}')
return init_recognizer(CONFIG_FILE, CHECKPOINT_FILE, device='cpu')
print('Initializing model...')
try:
model = build_model()
print('✅ Model loaded successfully!')
except Exception as e:
print(f'❌ Error loading model: {e}')
model = None
labels = load_labels(LABEL_FILE)
def analyze_video(video_path: str):
try:
if video_path is None:
return 'Please upload a video file.'
if model is None:
return '⚠️ Model not loaded. Check logs for details.'
result = inference_recognizer(model, video_path)
pred_scores = result.pred_score.tolist()
score_sorted = sorted(zip(range(len(pred_scores)), pred_scores), key=itemgetter(1), reverse=True)
top5 = score_sorted[:5]
lines = []
for idx, score in top5:
name = labels[idx] if labels and idx < len(labels) else f'class_{idx}'
lines.append(f'{name}: {score}')
return '\n'.join(lines)
except Exception as e:
return f'❌ Error processing video: {str(e)}'
demo = gr.Interface(
fn=analyze_video,
inputs=gr.Video(label='Upload Video', height=300),
outputs=gr.Textbox(label='Analysis Results', lines=12),
title='🎬 GenVidBench - TSN (MMAction2)',
description='Upload a video. Inference uses TSN R50 on Kinetics-400.',
cache_examples=False,
allow_flagging='never'
)
if __name__ == '__main__':
demo.launch()
|