File size: 1,722 Bytes
c592663
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import tensorflow as tf
import numpy as np
import cv2
from PIL import Image
import io

# Load a pre-trained TensorFlow model (replace with your model path)
model = tf.keras.applications.MobileNetV2(weights="imagenet")

def preprocess_image(image):
    img = np.array(image)
    img = cv2.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):
    result = ""
    cap = cv2.VideoCapture(video)
    frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    frame_interval = frame_count // 10  # Analyze 10 frames evenly spaced throughout the video

    for i in range(0, frame_count, frame_interval):
        cap.set(cv2.CAP_PROP_POS_FRAMES, i)
        ret, frame = cap.read()
        if not ret:
            break

        frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        image = Image.fromarray(frame_rgb)
        label = classify_frame(image)
        
        if "baseball" in label.lower():
            result = "The runner is out"
            break

    cap.release()
    if result == "":
        result = "The runner is safe"
    
    return result

iface = gr.Interface(
    fn=process_video,
    inputs=gr.inputs.Video(type="mp4"),
    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()