Mosensei commited on
Commit
ecc0e2b
Β·
verified Β·
1 Parent(s): bcd900b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +179 -136
app.py CHANGED
@@ -1,142 +1,185 @@
 
1
  import gradio as gr
2
- import mlflow
3
  from ultralytics import YOLO
4
- from PIL import Image
5
  import cv2
6
  import numpy as np
7
- import os
 
8
  import time
9
- import tempfile
10
- import sys
11
- import subprocess
12
-
13
- # ==============================
14
- # MLflow / DagsHub Configuration
15
- # ==============================
16
- os.environ["MLFLOW_TRACKING_URI"] = os.getenv("MLFLOW_TRACKING_URI")
17
- os.environ["MLFLOW_TRACKING_USERNAME"] = os.getenv("MLFLOW_TRACKING_USERNAME")
18
- os.environ["MLFLOW_TRACKING_PASSWORD"] = os.getenv("MLFLOW_TRACKING_PASSWORD")
19
-
20
- dagshub.init(
21
- repo_owner="Mosensei7",
22
- repo_name="AutonomousVehiclesDetectionDEPI",
23
- mlflow=True
24
- )
25
-
26
- mlflow.set_experiment("YOLOv12_Inference")
27
-
28
- # ==============================
29
- # Load YOLOv12 Model
30
- # ==============================
31
- model = YOLO("best.pt") # YOLOv12s weights
32
-
33
- # ==============================
34
- # Inference Logic
35
- # ==============================
36
- def run_inference(media_file, media_type):
37
- media_path = media_file.name
38
-
39
- with mlflow.start_run(run_name=f"Inference_{int(time.time())}") as run:
40
- mlflow.log_param("media_type", media_type)
41
- mlflow.log_param("model", "YOLOv12s")
42
-
43
- if media_type == "Image":
44
- img = Image.open(media_path).convert("RGB")
45
-
46
- results = model(np.array(img))[0]
47
- annotated = results.plot()
48
- output_img = Image.fromarray(annotated)
49
-
50
- # Save temp artifacts
51
- with tempfile.TemporaryDirectory() as tmp:
52
- in_path = os.path.join(tmp, "input.jpg")
53
- out_path = os.path.join(tmp, "output.jpg")
54
-
55
- img.save(in_path)
56
- output_img.save(out_path)
57
-
58
- mlflow.log_artifact(in_path, "inputs")
59
- mlflow.log_artifact(out_path, "outputs")
60
-
61
- mlflow.log_metric("detections", len(results.boxes))
62
-
63
- return output_img, None, run.info.run_id
64
-
65
- else:
66
- cap = cv2.VideoCapture(media_path)
67
- fps = cap.get(cv2.CAP_PROP_FPS)
68
- w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
69
- h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
70
-
71
- out_path = "annotated_output.mp4"
72
- writer = cv2.VideoWriter(
73
- out_path,
74
- cv2.VideoWriter_fourcc(*"mp4v"),
75
- fps,
76
- (w, h)
77
- )
78
-
79
- frame_count = 0
80
- total_detections = 0
81
-
82
- while cap.isOpened():
83
- ret, frame = cap.read()
84
- if not ret:
85
- break
86
-
87
- results = model(frame)[0]
88
- annotated = results.plot()
89
-
90
- writer.write(annotated)
91
- frame_count += 1
92
- total_detections += len(results.boxes)
93
-
94
- cap.release()
95
- writer.release()
96
-
97
- mlflow.log_artifact(media_path, "inputs")
98
- mlflow.log_artifact(out_path, "outputs")
99
- mlflow.log_metric("frames", frame_count)
100
- mlflow.log_metric("total_detections", total_detections)
101
-
102
- return None, out_path, run.info.run_id
103
-
104
- # ==============================
105
- # Futuristic UI
106
- # ==============================
107
- css = """
108
- body {
109
- background: linear-gradient(135deg, #0f0c29, #302b63, #24243e);
110
- color: white;
111
- font-family: 'Orbitron', sans-serif;
112
- }
113
- .gradio-container {
114
- border: 2px solid cyan;
115
- border-radius: 20px;
116
- box-shadow: 0 0 20px cyan;
117
- }
118
- """
119
-
120
- with gr.Blocks(css=css) as demo:
121
- gr.Markdown("""
122
- <h1 style='text-align:center;color:cyan;'>YOLOv12 Autonomous Vehicle Detection</h1>
123
- <p style='text-align:center;'>All inferences are logged to DagsHub MLflow</p>
124
- """)
125
-
 
 
 
 
 
 
126
  with gr.Row():
127
- media = gr.File(label="Upload Image / Video")
128
- media_type = gr.Radio(["Image", "Video"], value="Image")
129
-
130
- detect = gr.Button("Run Detection")
131
-
132
- img_out = gr.Image(label="Image Result")
133
- vid_out = gr.Video(label="Video Result")
134
- run_id = gr.Textbox(label="MLflow Run ID")
135
-
136
- detect.click(
137
- run_inference,
138
- inputs=[media, media_type],
139
- outputs=[img_out, vid_out, run_id]
140
- )
141
-
142
- demo.launch(share=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
  import gradio as gr
 
3
  from ultralytics import YOLO
4
+ import tempfile
5
  import cv2
6
  import numpy as np
7
+ import mlflow
8
+ from datetime import datetime
9
  import time
10
+ import pandas as pd
11
+ from collections import defaultdict
12
+
13
+
14
+ DAGSHUB_REPO_OWNER = os.getenv("DAGSHUB_REPO_OWNER")
15
+ DAGSHUB_REPO_NAME = os.getenv("DAGSHUB_REPO_NAME")
16
+
17
+ MLFLOW_ENABLED = False
18
+ if DAGSHUB_REPO_OWNER and DAGSHUB_REPO_NAME:
19
+ mlflow.set_tracking_uri(f"https://dagshub.com/{DAGSHUB_REPO_OWNER}/{DAGSHUB_REPO_NAME}.mlflow" )
20
+ MLFLOW_ENABLED = True
21
+ print("MLflow tracking is configured for DagsHub.")
22
+ else:
23
+ print("DagsHub secrets not found. MLflow logging will be disabled.")
24
+
25
+ os.environ.setdefault("YOLO_CONFIG_DIR", "/tmp/Ultralytics")
26
+
27
+
28
+ MODEL_PATH = "best.pt"
29
+ model = YOLO(MODEL_PATH)
30
+
31
+
32
+ def log_image_prediction(input_img_pil, output_image_path, conf, inference_time, detections_df):
33
+ if not MLFLOW_ENABLED: return
34
+ try:
35
+ with mlflow.start_run(run_name=f"Image_Prediction_{datetime.now().strftime('%Y%m%d-%H%M%S')}"):
36
+ mlflow.log_param("confidence_threshold", conf)
37
+ mlflow.log_param("prediction_type", "image")
38
+ mlflow.log_metric("inference_time_seconds", inference_time)
39
+ mlflow.log_metric("total_detections", len(detections_df))
40
+
41
+ if not detections_df.empty:
42
+ class_counts = defaultdict(int)
43
+ for _, row in detections_df.iterrows():
44
+ class_name = row['class_name']
45
+ confidence = row['confidence']
46
+ class_counts[class_name] += 1
47
+ metric_name = f"detection_{class_name}_{class_counts[class_name]}"
48
+ mlflow.log_metric(metric_name, confidence)
49
+
50
+ input_path = tempfile.NamedTemporaryFile(suffix=".png", delete=False).name
51
+ input_img_pil.save(input_path)
52
+ mlflow.log_artifact(input_path, "input_image")
53
+ mlflow.log_artifact(output_image_path, "output_image")
54
+ print(f"Successfully logged image prediction.")
55
+ except Exception as e:
56
+ print(f"Error logging to MLflow: {e}")
57
+
58
+ def log_video_prediction(input_path, output_path, conf):
59
+ if not MLFLOW_ENABLED: return
60
+ try:
61
+ with mlflow.start_run(run_name=f"Video_Prediction_{datetime.now().strftime('%Y%m%d-%H%M%S')}"):
62
+ mlflow.log_param("confidence_threshold", conf)
63
+ mlflow.log_param("prediction_type", "video")
64
+ mlflow.log_artifact(input_path, "input")
65
+ mlflow.log_artifact(output_path, "output")
66
+ print(f"Successfully logged video prediction.")
67
+ except Exception as e:
68
+ print(f"Error logging to MLflow: {e}")
69
+
70
+
71
+ def run_image_inference(img_pil, conf=0.25):
72
+ if img_pil is None: return None, 0.0, pd.DataFrame()
73
+ img_np = np.array(img_pil)
74
+ img_bgr = cv2.cvtColor(img_np, cv2.COLOR_RGB2BGR)
75
+ start_time = time.time()
76
+
77
+ results = model(img_bgr, conf=conf, iou=0.4, verbose=False, imgsz=640)
78
+ end_time = time.time()
79
+ inference_time = end_time - start_time
80
+ result = results[0]
81
+ detections = []
82
+ for box in result.boxes:
83
+ class_id = int(box.cls.cpu().item())
84
+ class_name = result.names[class_id]
85
+ confidence = float(box.conf.cpu().item())
86
+ detections.append({"class_name": class_name, "confidence": round(confidence, 4)})
87
+ detections_df = pd.DataFrame(detections)
88
+ annotated_img = result.plot()
89
+ annotated_rgb = cv2.cvtColor(annotated_img, cv2.COLOR_BGR2RGB)
90
+ out_path = tempfile.NamedTemporaryFile(suffix=".png", delete=False).name
91
+ cv2.imwrite(out_path, cv2.cvtColor(annotated_rgb, cv2.COLOR_RGB2BGR))
92
+ return out_path, inference_time, detections_df
93
+
94
+ def run_video_inference(video_path, conf=0.25, frame_skip=2):
95
+ if video_path is None: return None
96
+ temp_out = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False).name
97
+
98
+ results_generator = model(video_path, conf=conf, iou=0.4, verbose=False, stream=True, imgsz=640)
99
+
100
+ try:
101
+ first_result = next(results_generator)
102
+ except StopIteration:
103
+ return None
104
+
105
+ cap = cv2.VideoCapture(video_path)
106
+ fps = cap.get(cv2.CAP_PROP_FPS) or 25.0
107
+ h, w = first_result.orig_shape
108
+
109
+ output_fps = fps / (frame_skip + 1) if frame_skip > -1 else fps
110
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
111
+ out = cv2.VideoWriter(temp_out, fourcc, output_fps, (w, h))
112
+
113
+ out.write(first_result.plot())
114
+
115
+ frame_count = 0
116
+ for result in results_generator:
117
+ frame_count += 1
118
+ if frame_skip > -1 and frame_count % (frame_skip + 1) != 0:
119
+ continue
120
+ annotated_frame = result.plot()
121
+ out.write(annotated_frame)
122
+
123
+ cap.release()
124
+ out.release()
125
+ return temp_out
126
+
127
+ dark_css = """<style> body { background-color: #0f1724; color: #e6eef8; } .gradio-container { background-color: transparent !important; } h1 { color: #ffcc00; } .subtle { color: #9fb0c8; } .card-like { background: rgba(255,255,255,0.03); border-radius: 12px; padding: 12px; } </style>"""
128
+
129
+ with gr.Blocks() as demo:
130
+ gr.HTML(dark_css)
131
+ gr.Markdown("# 🎯 YOLO Detection Studio β€” Image & Video")
132
+ gr.Markdown("<div class='subtle'>Upload an image or video, then press Detect.</div>")
133
  with gr.Row():
134
+ with gr.Column(scale=2):
135
+ with gr.Tabs():
136
+ with gr.TabItem("Image"):
137
+ image_input = gr.Image(type="pil", label="Upload Image")
138
+ img_conf = gr.Slider(0.0, 1.0, value=0.25, step=0.01, label="Confidence Threshold")
139
+ img_detect_btn = gr.Button("πŸ” Detect Image")
140
+ image_output = gr.Image(label="Detected Image")
141
+ with gr.TabItem("Video"):
142
+ video_input = gr.Video(label="Upload Video")
143
+ vid_conf = gr.Slider(0.0, 1.0, value=0.25, step=0.01, label="Confidence Threshold")
144
+ frame_skip_slider = gr.Slider(-1, 10, value=2, step=1, label="Frame Skip", info="Process 1 frame every (N+1) frames. -1 to process all frames.")
145
+ vid_detect_btn = gr.Button("🎬 Detect Video")
146
+ video_output = gr.Video(label="Detected Video")
147
+ with gr.Column(scale=1):
148
+ gr.Markdown("### βš™οΈ Options & Status")
149
+ status = gr.Textbox(label="Status", value="Ready", interactive=False)
150
+ clear_btn = gr.Button("🧹 Clear Outputs")
151
+
152
+ def on_detect_image(img, conf):
153
+ try:
154
+ out_path, inference_time, detections_df = run_image_inference(img, conf=conf)
155
+ log_image_prediction(img, out_path, conf, inference_time, detections_df)
156
+ status_msg = f"Done. Inference: {inference_time:.2f}s. Detections: {len(detections_df)}."
157
+ if MLFLOW_ENABLED: status_msg += " Logged to DagsHub."
158
+ return out_path, status_msg
159
+ except Exception as e:
160
+ return None, f"Error: {e}"
161
+
162
+ def on_detect_video(video_path, conf, frame_skip):
163
+ try:
164
+ start_time = time.time()
165
+ out_path = run_video_inference(video_path, conf=conf, frame_skip=frame_skip)
166
+ end_time = time.time()
167
+ if out_path:
168
+ log_video_prediction(video_path, out_path, conf)
169
+ status_msg = f"Done β€” video processed in {end_time - start_time:.2f}s."
170
+ if MLFLOW_ENABLED: status_msg += " Logged to DagsHub."
171
+ return out_path, status_msg
172
+ else:
173
+ return None, "Could not process video."
174
+ except Exception as e:
175
+ import traceback
176
+ print(traceback.format_exc())
177
+ return None, f"Error: {e}"
178
+
179
+ img_detect_btn.click(fn=on_detect_image, inputs=[image_input, img_conf], outputs=[image_output, status])
180
+ vid_detect_btn.click(fn=on_detect_video, inputs=[video_input, vid_conf, frame_skip_slider], outputs=[video_output, status])
181
+
182
+ def on_clear(): return None, "Ready", None
183
+ clear_btn.click(fn=on_clear, inputs=None, outputs=[image_output, status, video_output])
184
+
185
+ demo.launch(server_name="0.0.0.0", share=False)