MNGames commited on
Commit
c592663
1 Parent(s): 7eb3ece

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -0
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import tensorflow as tf
3
+ import numpy as np
4
+ import cv2
5
+ from PIL import Image
6
+ import io
7
+
8
+ # Load a pre-trained TensorFlow model (replace with your model path)
9
+ model = tf.keras.applications.MobileNetV2(weights="imagenet")
10
+
11
+ def preprocess_image(image):
12
+ img = np.array(image)
13
+ img = cv2.resize(img, (224, 224))
14
+ img = tf.keras.applications.mobilenet_v2.preprocess_input(img)
15
+ return np.expand_dims(img, axis=0)
16
+
17
+ def classify_frame(frame):
18
+ processed_frame = preprocess_image(frame)
19
+ predictions = model.predict(processed_frame)
20
+ decoded_predictions = tf.keras.applications.mobilenet_v2.decode_predictions(predictions, top=1)[0]
21
+ return decoded_predictions[0][1]
22
+
23
+ def process_video(video):
24
+ result = ""
25
+ cap = cv2.VideoCapture(video)
26
+ frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
27
+ frame_interval = frame_count // 10 # Analyze 10 frames evenly spaced throughout the video
28
+
29
+ for i in range(0, frame_count, frame_interval):
30
+ cap.set(cv2.CAP_PROP_POS_FRAMES, i)
31
+ ret, frame = cap.read()
32
+ if not ret:
33
+ break
34
+
35
+ frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
36
+ image = Image.fromarray(frame_rgb)
37
+ label = classify_frame(image)
38
+
39
+ if "baseball" in label.lower():
40
+ result = "The runner is out"
41
+ break
42
+
43
+ cap.release()
44
+ if result == "":
45
+ result = "The runner is safe"
46
+
47
+ return result
48
+
49
+ iface = gr.Interface(
50
+ fn=process_video,
51
+ inputs=gr.inputs.Video(type="mp4"),
52
+ outputs="text",
53
+ title="Baseball Runner Status",
54
+ description="Upload a baseball video to determine if the runner is out or safe."
55
+ )
56
+
57
+ if __name__ == "__main__":
58
+ iface.launch()