Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,185 +1,141 @@
|
|
| 1 |
-
import os
|
| 2 |
import gradio as gr
|
|
|
|
|
|
|
| 3 |
from ultralytics import YOLO
|
| 4 |
-
import
|
| 5 |
import cv2
|
| 6 |
import numpy as np
|
| 7 |
-
import
|
| 8 |
-
from datetime import datetime
|
| 9 |
import time
|
| 10 |
-
import
|
| 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 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 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 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 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)
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import mlflow
|
| 3 |
+
import dagshub
|
| 4 |
from ultralytics import YOLO
|
| 5 |
+
from PIL import Image
|
| 6 |
import cv2
|
| 7 |
import numpy as np
|
| 8 |
+
import os
|
|
|
|
| 9 |
import time
|
| 10 |
+
import tempfile
|
| 11 |
+
|
| 12 |
+
# ==============================
|
| 13 |
+
# MLflow / DagsHub Configuration
|
| 14 |
+
# ==============================
|
| 15 |
+
os.environ["MLFLOW_TRACKING_URI"] = os.getenv("MLFLOW_TRACKING_URI")
|
| 16 |
+
os.environ["MLFLOW_TRACKING_USERNAME"] = os.getenv("MLFLOW_TRACKING_USERNAME")
|
| 17 |
+
os.environ["MLFLOW_TRACKING_PASSWORD"] = os.getenv("MLFLOW_TRACKING_PASSWORD")
|
| 18 |
+
|
| 19 |
+
dagshub.init(
|
| 20 |
+
repo_owner="Mosensei7",
|
| 21 |
+
repo_name="AutonomousVehiclesDetectionDEPI",
|
| 22 |
+
mlflow=True
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
mlflow.set_experiment("YOLOv12_Inference")
|
| 26 |
+
|
| 27 |
+
# ==============================
|
| 28 |
+
# Load YOLOv12 Model
|
| 29 |
+
# ==============================
|
| 30 |
+
model = YOLO("best.pt") # YOLOv12s weights
|
| 31 |
+
|
| 32 |
+
# ==============================
|
| 33 |
+
# Inference Logic
|
| 34 |
+
# ==============================
|
| 35 |
+
def run_inference(media_file, media_type):
|
| 36 |
+
media_path = media_file.name
|
| 37 |
+
|
| 38 |
+
with mlflow.start_run(run_name=f"Inference_{int(time.time())}") as run:
|
| 39 |
+
mlflow.log_param("media_type", media_type)
|
| 40 |
+
mlflow.log_param("model", "YOLOv12s")
|
| 41 |
+
|
| 42 |
+
if media_type == "Image":
|
| 43 |
+
img = Image.open(media_path).convert("RGB")
|
| 44 |
+
|
| 45 |
+
results = model(np.array(img))[0]
|
| 46 |
+
annotated = results.plot()
|
| 47 |
+
output_img = Image.fromarray(annotated)
|
| 48 |
+
|
| 49 |
+
# Save temp artifacts
|
| 50 |
+
with tempfile.TemporaryDirectory() as tmp:
|
| 51 |
+
in_path = os.path.join(tmp, "input.jpg")
|
| 52 |
+
out_path = os.path.join(tmp, "output.jpg")
|
| 53 |
+
|
| 54 |
+
img.save(in_path)
|
| 55 |
+
output_img.save(out_path)
|
| 56 |
+
|
| 57 |
+
mlflow.log_artifact(in_path, "inputs")
|
| 58 |
+
mlflow.log_artifact(out_path, "outputs")
|
| 59 |
+
|
| 60 |
+
mlflow.log_metric("detections", len(results.boxes))
|
| 61 |
+
|
| 62 |
+
return output_img, None, run.info.run_id
|
| 63 |
+
|
| 64 |
+
else:
|
| 65 |
+
cap = cv2.VideoCapture(media_path)
|
| 66 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 67 |
+
w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 68 |
+
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 69 |
+
|
| 70 |
+
out_path = "annotated_output.mp4"
|
| 71 |
+
writer = cv2.VideoWriter(
|
| 72 |
+
out_path,
|
| 73 |
+
cv2.VideoWriter_fourcc(*"mp4v"),
|
| 74 |
+
fps,
|
| 75 |
+
(w, h)
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
frame_count = 0
|
| 79 |
+
total_detections = 0
|
| 80 |
+
|
| 81 |
+
while cap.isOpened():
|
| 82 |
+
ret, frame = cap.read()
|
| 83 |
+
if not ret:
|
| 84 |
+
break
|
| 85 |
+
|
| 86 |
+
results = model(frame)[0]
|
| 87 |
+
annotated = results.plot()
|
| 88 |
+
|
| 89 |
+
writer.write(annotated)
|
| 90 |
+
frame_count += 1
|
| 91 |
+
total_detections += len(results.boxes)
|
| 92 |
+
|
| 93 |
+
cap.release()
|
| 94 |
+
writer.release()
|
| 95 |
+
|
| 96 |
+
mlflow.log_artifact(media_path, "inputs")
|
| 97 |
+
mlflow.log_artifact(out_path, "outputs")
|
| 98 |
+
mlflow.log_metric("frames", frame_count)
|
| 99 |
+
mlflow.log_metric("total_detections", total_detections)
|
| 100 |
+
|
| 101 |
+
return None, out_path, run.info.run_id
|
| 102 |
+
|
| 103 |
+
# ==============================
|
| 104 |
+
# Futuristic UI
|
| 105 |
+
# ==============================
|
| 106 |
+
css = """
|
| 107 |
+
body {
|
| 108 |
+
background: linear-gradient(135deg, #0f0c29, #302b63, #24243e);
|
| 109 |
+
color: white;
|
| 110 |
+
font-family: 'Orbitron', sans-serif;
|
| 111 |
+
}
|
| 112 |
+
.gradio-container {
|
| 113 |
+
border: 2px solid cyan;
|
| 114 |
+
border-radius: 20px;
|
| 115 |
+
box-shadow: 0 0 20px cyan;
|
| 116 |
+
}
|
| 117 |
+
"""
|
| 118 |
+
|
| 119 |
+
with gr.Blocks(css=css) as demo:
|
| 120 |
+
gr.Markdown("""
|
| 121 |
+
<h1 style='text-align:center;color:cyan;'>YOLOv12 Autonomous Vehicle Detection</h1>
|
| 122 |
+
<p style='text-align:center;'>All inferences are logged to DagsHub MLflow</p>
|
| 123 |
+
""")
|
| 124 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
with gr.Row():
|
| 126 |
+
media = gr.File(label="Upload Image / Video")
|
| 127 |
+
media_type = gr.Radio(["Image", "Video"], value="Image")
|
| 128 |
+
|
| 129 |
+
detect = gr.Button("Run Detection")
|
| 130 |
+
|
| 131 |
+
img_out = gr.Image(label="Image Result")
|
| 132 |
+
vid_out = gr.Video(label="Video Result")
|
| 133 |
+
run_id = gr.Textbox(label="MLflow Run ID")
|
| 134 |
+
|
| 135 |
+
detect.click(
|
| 136 |
+
run_inference,
|
| 137 |
+
inputs=[media, media_type],
|
| 138 |
+
outputs=[img_out, vid_out, run_id]
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
demo.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|