Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,71 +3,99 @@ import cv2
|
|
| 3 |
import numpy as np
|
| 4 |
import tempfile
|
| 5 |
from ultralytics import YOLO
|
| 6 |
-
#from mediapipe_pipeline import process_workout # your MediaPipe logic
|
| 7 |
|
| 8 |
-
# Load
|
|
|
|
| 9 |
helmet_model = YOLO("models/helmet_detection.pt")
|
| 10 |
fire_model = YOLO("models/fire_detection.pt")
|
| 11 |
seg_model = YOLO("models/yolo11x-seg.pt")
|
| 12 |
|
| 13 |
model_map = {
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
}
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
def process_video(model_name, video_file, reference_file=None):
|
| 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 |
x1, y1, x2, y2 = map(int, box)
|
| 49 |
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
|
| 56 |
-
|
|
|
|
| 57 |
out.release()
|
| 58 |
-
return output_file
|
| 59 |
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
| 61 |
with gr.Blocks() as demo:
|
| 62 |
-
|
|
|
|
|
|
|
| 63 |
model_choice = gr.Dropdown(list(model_map.keys()), label="Select Model")
|
| 64 |
video_input = gr.File(label="Upload Video")
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
demo.launch()
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import tempfile
|
| 5 |
from ultralytics import YOLO
|
|
|
|
| 6 |
|
| 7 |
+
# --- Load YOLO Models ---
|
| 8 |
+
|
| 9 |
helmet_model = YOLO("models/helmet_detection.pt")
|
| 10 |
fire_model = YOLO("models/fire_detection.pt")
|
| 11 |
seg_model = YOLO("models/yolo11x-seg.pt")
|
| 12 |
|
| 13 |
model_map = {
|
| 14 |
+
"Helmet Detection": helmet_model,
|
| 15 |
+
"Fire Detection": fire_model,
|
| 16 |
+
"Instance Segmentation": seg_model,
|
| 17 |
+
"Workout Pose (MediaPipe)": None # MediaPipe handled separately
|
| 18 |
}
|
| 19 |
|
| 20 |
+
# --- Placeholder MediaPipe workout processing ---
|
| 21 |
+
|
| 22 |
+
def process_workout(reference_video_path, user_video_path):
|
| 23 |
+
# Here you would add your actual MediaPipe logic
|
| 24 |
+
# For demo, just return the user video as "processed"
|
| 25 |
+
return user_video_path
|
| 26 |
+
|
| 27 |
+
# --- Video processing function ---
|
| 28 |
+
|
| 29 |
def process_video(model_name, video_file, reference_file=None):
|
| 30 |
+
if not video_file:
|
| 31 |
+
return None
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
# MediaPipe workflow
|
| 35 |
+
if model_name == "Workout Pose (MediaPipe)":
|
| 36 |
+
if not reference_file:
|
| 37 |
+
return None
|
| 38 |
+
return process_workout(reference_file.name, video_file.name)
|
| 39 |
+
|
| 40 |
+
# Use the uploaded video path directly
|
| 41 |
+
video_path = video_file.name
|
| 42 |
+
model = model_map[model_name]
|
| 43 |
+
|
| 44 |
+
cap = cv2.VideoCapture(video_path)
|
| 45 |
+
if not cap.isOpened():
|
| 46 |
+
return None
|
| 47 |
+
|
| 48 |
+
output_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4").name
|
| 49 |
+
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
|
| 50 |
+
out = None
|
| 51 |
+
|
| 52 |
+
while True:
|
| 53 |
+
ret, frame = cap.read()
|
| 54 |
+
if not ret:
|
| 55 |
+
break
|
| 56 |
+
|
| 57 |
+
# Run YOLO prediction
|
| 58 |
+
results = model.predict(frame)
|
| 59 |
+
for r in results:
|
| 60 |
+
if hasattr(r, "boxes"):
|
| 61 |
+
boxes = r.boxes.xyxy.cpu().numpy()
|
| 62 |
+
for box in boxes:
|
| 63 |
x1, y1, x2, y2 = map(int, box)
|
| 64 |
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
| 65 |
|
| 66 |
+
if out is None:
|
| 67 |
+
h, w, _ = frame.shape
|
| 68 |
+
out = cv2.VideoWriter(output_file, fourcc, 20.0, (w, h))
|
| 69 |
+
out.write(frame)
|
| 70 |
|
| 71 |
+
cap.release()
|
| 72 |
+
if out:
|
| 73 |
out.release()
|
|
|
|
| 74 |
|
| 75 |
+
return output_file
|
| 76 |
+
|
| 77 |
+
# --- Gradio Interface ---
|
| 78 |
+
|
| 79 |
with gr.Blocks() as demo:
|
| 80 |
+
gr.Markdown("# Multi-Model AI Video Analyzer")
|
| 81 |
+
|
| 82 |
+
with gr.Row():
|
| 83 |
model_choice = gr.Dropdown(list(model_map.keys()), label="Select Model")
|
| 84 |
video_input = gr.File(label="Upload Video")
|
| 85 |
+
reference_input = gr.File(label="Reference Video (for workout)", visible=False)
|
| 86 |
+
|
| 87 |
+
output_video = gr.Video(label="Processed Video")
|
| 88 |
+
|
| 89 |
+
def toggle_reference_input(model_name):
|
| 90 |
+
return gr.update(visible=(model_name == "Workout Pose (MediaPipe)"))
|
| 91 |
+
|
| 92 |
+
model_choice.change(toggle_reference_input, inputs=model_choice, outputs=reference_input)
|
| 93 |
|
| 94 |
+
submit_btn = gr.Button("Process Video")
|
| 95 |
+
submit_btn.click(
|
| 96 |
+
process_video,
|
| 97 |
+
inputs=[model_choice, video_input, reference_input],
|
| 98 |
+
outputs=output_video
|
| 99 |
+
)
|
| 100 |
|
| 101 |
demo.launch()
|