|
import gradio as gr |
|
import tensorflow as tf |
|
import numpy as np |
|
from PIL import Image |
|
import io |
|
import moviepy.editor as mp |
|
|
|
|
|
model = tf.keras.applications.MobileNetV2(weights="imagenet") |
|
|
|
def preprocess_image(image): |
|
img = np.array(image) |
|
img = tf.image.resize(img, (224, 224)) |
|
img = tf.keras.applications.mobilenet_v2.preprocess_input(img) |
|
return np.expand_dims(img, axis=0) |
|
|
|
def classify_frame(frame): |
|
processed_frame = preprocess_image(frame) |
|
predictions = model.predict(processed_frame) |
|
decoded_predictions = tf.keras.applications.mobilenet_v2.decode_predictions(predictions, top=1)[0] |
|
return decoded_predictions[0][1] |
|
|
|
def process_video(video_file): |
|
result = "" |
|
if isinstance(video_file, str): |
|
video = mp.VideoFileClip(video_file) |
|
else: |
|
video = mp.VideoFileClip(io.BytesIO(video_file.read())) |
|
duration = int(video.duration) |
|
frame_interval = duration // 10 |
|
|
|
for i in range(0, duration, frame_interval): |
|
frame = video.get_frame(i) |
|
image = Image.fromarray(frame) |
|
label = classify_frame(image) |
|
|
|
if "baseball" in label.lower(): |
|
result = "The runner is out" |
|
break |
|
|
|
if result == "": |
|
result = "The runner is safe" |
|
|
|
return result |
|
|
|
iface = gr.Interface( |
|
fn=process_video, |
|
inputs="video", |
|
outputs="text", |
|
title="Baseball Runner Status", |
|
description="Upload a baseball video to determine if the runner is out or safe." |
|
) |
|
|
|
if __name__ == "__main__": |
|
iface.launch() |
|
|