daanidev commited on
Commit
0d0f9f8
·
verified ·
1 Parent(s): 19ac1ac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -47
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 your models
 
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
  def process_video(model_name, video_file, reference_file=None):
21
- if model_name == "Workout Pose (MediaPipe)":
22
- if not reference_file or not video_file:
23
- return None
24
- # MediaPipe workout comparison returns processed video path
25
- output_path = process_workout(reference_file.name, video_file.name)
26
- return output_path
27
-
28
- # Save uploaded video to temp file
29
- tmp_input = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
30
- tmp_input.write(video_file.read())
31
- tmp_input.close()
32
-
33
- model = model_map[model_name]
34
- cap = cv2.VideoCapture(tmp_input.name)
35
- output_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4").name
36
- fourcc = cv2.VideoWriter_fourcc(*"mp4v")
37
- out = None
38
-
39
- while True:
40
- ret, frame = cap.read()
41
- if not ret:
42
- break
43
-
44
- # Run YOLO detection
45
- results = model(frame)
46
- for r in results:
47
- for box in r.boxes.xyxy:
 
 
 
 
 
 
48
  x1, y1, x2, y2 = map(int, box)
49
  cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
50
 
51
- if out is None:
52
- h, w, _ = frame.shape
53
- out = cv2.VideoWriter(output_file, fourcc, 20.0, (w, h))
54
- out.write(frame)
55
 
56
- cap.release()
 
57
  out.release()
58
- return output_file
59
 
60
- # Gradio interface
 
 
 
61
  with gr.Blocks() as demo:
62
- gr.Markdown("# Multi-Model AI Video Analyzer")
 
 
63
  model_choice = gr.Dropdown(list(model_map.keys()), label="Select Model")
64
  video_input = gr.File(label="Upload Video")
65
- #ref_video_input = gr.File(label="Reference Video (for workout)", optional=True)
66
- output_video = gr.Video(label="Processed Video")
67
- submit_btn = gr.Button("Process Video")
 
 
 
 
 
68
 
69
- submit_btn.click(process_video,
70
- inputs=[model_choice, video_input],
71
- outputs=output_video)
 
 
 
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()