|
import gradio as gr |
|
from transformers import pipeline |
|
import cv2 |
|
|
|
|
|
model_id = "sayakpaul/videomae-base-finetuned-ucf101-subset" |
|
|
|
def analyze_video(video): |
|
|
|
frames = extract_key_frames(video) |
|
|
|
|
|
results = [] |
|
classifier = pipeline("video-classification", model=model_id) |
|
for frame in frames: |
|
predictions = classifier(images=frame) |
|
|
|
result = analyze_predictions_ucf101(predictions) |
|
results.append(result) |
|
|
|
|
|
final_result = aggregate_results(results) |
|
|
|
return final_result |
|
|
|
def extract_key_frames(video): |
|
cap = cv2.VideoCapture(video) |
|
frames = [] |
|
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) |
|
fps = int(cap.get(cv2.CAP_PROP_FPS)) |
|
|
|
for i in range(frame_count): |
|
ret, frame = cap.read() |
|
if ret and i % (fps // 2) == 0: |
|
frames.append(frame) |
|
|
|
cap.release() |
|
return frames |
|
|
|
def analyze_predictions_ucf101(predictions): |
|
|
|
|
|
actions = [pred['label'] for pred in predictions] |
|
|
|
relevant_actions = ["running", "sliding", "jumping"] |
|
runner_actions = [action for action in actions if action in relevant_actions] |
|
|
|
|
|
if "sliding" in runner_actions: |
|
return "potentially safe" |
|
elif "running" in runner_actions: |
|
return "potentially out" |
|
else: |
|
return "inconclusive" |
|
|
|
def aggregate_results(results): |
|
|
|
safe_count = results.count("potentially safe") |
|
out_count = results.count("potentially out") |
|
|
|
if safe_count > out_count: |
|
return "Safe" |
|
elif out_count > safe_count: |
|
return "Out" |
|
else: |
|
return "Inconclusive" |
|
|
|
|
|
interface = gr.Interface( |
|
fn=analyze_video, |
|
inputs="video", |
|
outputs="text", |
|
title="Baseball Play Analysis (UCF101 Subset Exploration)", |
|
description="Upload a video of a baseball play (safe/out at a base). This app explores using a video classification model (UCF101 subset) for analysis. Note: The model might not be specifically trained for baseball plays.", |
|
) |
|
|
|
interface.launch() |
|
|