Spaces:
Runtime error
Runtime error
Upload 3 files
Browse filesbuilding YOLO12s space
- best.pt +3 -0
- main.py +168 -0
- requirments.txt +9 -0
best.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:8f29aea1f80a5c713ca2d8094d751df7097620d0a29835b3729eddc58ed297c1
|
| 3 |
+
size 18914643
|
main.py
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
|
| 11 |
+
# Initialize DagsHub/MLflow
|
| 12 |
+
dagshub.init(repo_owner='Mosensei7', repo_name='AutonomousVehiclesDetectionDEPI', mlflow=True)
|
| 13 |
+
mlflow.set_experiment("Inference_Experiments")
|
| 14 |
+
|
| 15 |
+
# Load your trained model
|
| 16 |
+
model = YOLO('best.pt') # Replace with your best.pt path
|
| 17 |
+
|
| 18 |
+
def inference(media, media_type):
|
| 19 |
+
with mlflow.start_run(run_name=f"Inference_{int(time.time())}") as run:
|
| 20 |
+
# Log parameters
|
| 21 |
+
mlflow.log_param("media_type", media_type)
|
| 22 |
+
mlflow.log_param("timestamp", time.time())
|
| 23 |
+
|
| 24 |
+
if media_type == "Image":
|
| 25 |
+
# Process image
|
| 26 |
+
results = model(media)[0]
|
| 27 |
+
output = results.plot()
|
| 28 |
+
output_pil = Image.fromarray(output)
|
| 29 |
+
|
| 30 |
+
# Log input/output as artifacts
|
| 31 |
+
input_path = "input_image.jpg"
|
| 32 |
+
output_path = "output_image.jpg"
|
| 33 |
+
media.save(input_path)
|
| 34 |
+
output_pil.save(output_path)
|
| 35 |
+
mlflow.log_artifact(input_path)
|
| 36 |
+
mlflow.log_artifact(output_path)
|
| 37 |
+
|
| 38 |
+
# Log metrics if available
|
| 39 |
+
if hasattr(results, 'boxes'):
|
| 40 |
+
num_detections = len(results.boxes)
|
| 41 |
+
mlflow.log_metric("num_detections", num_detections)
|
| 42 |
+
|
| 43 |
+
os.remove(input_path)
|
| 44 |
+
os.remove(output_path)
|
| 45 |
+
|
| 46 |
+
return output_pil, run.info.run_id
|
| 47 |
+
|
| 48 |
+
elif media_type == "Video":
|
| 49 |
+
# Process video
|
| 50 |
+
cap = cv2.VideoCapture(media)
|
| 51 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 52 |
+
out = cv2.VideoWriter('output.mp4', fourcc, cap.get(cv2.CAP_PROP_FPS),
|
| 53 |
+
(int(cap.get(3)), int(cap.get(4))))
|
| 54 |
+
|
| 55 |
+
frame_count = 0
|
| 56 |
+
detections_count = 0
|
| 57 |
+
|
| 58 |
+
while cap.isOpened():
|
| 59 |
+
ret, frame = cap.read()
|
| 60 |
+
if not ret:
|
| 61 |
+
break
|
| 62 |
+
|
| 63 |
+
results = model(frame)[0]
|
| 64 |
+
annotated = results.plot()
|
| 65 |
+
out.write(annotated)
|
| 66 |
+
|
| 67 |
+
frame_count += 1
|
| 68 |
+
detections_count += len(results.boxes) if hasattr(results, 'boxes') else 0
|
| 69 |
+
|
| 70 |
+
cap.release()
|
| 71 |
+
out.release()
|
| 72 |
+
|
| 73 |
+
# Log video artifacts
|
| 74 |
+
mlflow.log_artifact(media, "input_video.mp4")
|
| 75 |
+
mlflow.log_artifact("output.mp4", "output_video.mp4")
|
| 76 |
+
|
| 77 |
+
# Log metrics
|
| 78 |
+
mlflow.log_metric("frame_count", frame_count)
|
| 79 |
+
mlflow.log_metric("total_detections", detections_count)
|
| 80 |
+
|
| 81 |
+
os.remove("output.mp4")
|
| 82 |
+
|
| 83 |
+
return "output.mp4", run.info.run_id
|
| 84 |
+
|
| 85 |
+
return None, None
|
| 86 |
+
|
| 87 |
+
# Futuristic CSS
|
| 88 |
+
css = """
|
| 89 |
+
body {
|
| 90 |
+
background: linear-gradient(135deg, #0f0c29, #302b63, #24243e);
|
| 91 |
+
color: #ffffff;
|
| 92 |
+
font-family: 'Orbitron', sans-serif;
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
.gradio-container {
|
| 96 |
+
background: rgba(0,0,0,0.3);
|
| 97 |
+
border: 2px solid #00ffff;
|
| 98 |
+
border-radius: 20px;
|
| 99 |
+
padding: 20px;
|
| 100 |
+
box-shadow: 0 0 20px #00ffff;
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
h1 {
|
| 104 |
+
text-align: center;
|
| 105 |
+
color: #00ffff;
|
| 106 |
+
text-shadow: 0 0 10px #00ffff;
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
button {
|
| 110 |
+
background: linear-gradient(45deg, #ff00ff, #00ffff);
|
| 111 |
+
border: none;
|
| 112 |
+
color: black;
|
| 113 |
+
font-weight: bold;
|
| 114 |
+
border-radius: 10px;
|
| 115 |
+
box-shadow: 0 0 10px #00ffff;
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
.input-area, .output-area {
|
| 119 |
+
border: 1px solid #00ffff;
|
| 120 |
+
border-radius: 10px;
|
| 121 |
+
padding: 10px;
|
| 122 |
+
background: rgba(255,255,255,0.05);
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
#mlflow-link {
|
| 126 |
+
color: #00ffff;
|
| 127 |
+
text-decoration: none;
|
| 128 |
+
}
|
| 129 |
+
"""
|
| 130 |
+
|
| 131 |
+
with gr.Blocks(css=css) as demo:
|
| 132 |
+
gr.Markdown("""
|
| 133 |
+
<h1>🚀 Futuristic Vehicle Detection Interface</h1>
|
| 134 |
+
<p style="text-align: center; color: #00ffff; text-shadow: 0 0 5px #00ffff;">
|
| 135 |
+
Upload image or video for detection. Every inference logged to MLflow!
|
| 136 |
+
</p>
|
| 137 |
+
""")
|
| 138 |
+
|
| 139 |
+
with gr.Row():
|
| 140 |
+
with gr.Column(scale=1, variant="panel", elem_classes="input-area"):
|
| 141 |
+
media = gr.File(label="Upload Image/Video", file_types=[".jpg", ".png", ".mp4"])
|
| 142 |
+
media_type = gr.Radio(["Image", "Video"], label="Media Type", value="Image")
|
| 143 |
+
submit = gr.Button("Detect Objects")
|
| 144 |
+
|
| 145 |
+
with gr.Column(scale=2, variant="panel", elem_classes="output-area"):
|
| 146 |
+
output = gr.Image(label="Detection Result", type="pil") # For image
|
| 147 |
+
video_output = gr.Video(label="Detection Result") # For video
|
| 148 |
+
mlflow_id = gr.Textbox(label="MLflow Run ID (click to view log)")
|
| 149 |
+
|
| 150 |
+
def process(media_path, m_type):
|
| 151 |
+
if m_type == "Image":
|
| 152 |
+
img = Image.open(media_path)
|
| 153 |
+
out_img, run_id = inference(img, m_type)
|
| 154 |
+
return out_img, None, run_id
|
| 155 |
+
else:
|
| 156 |
+
out_video, run_id = inference(media_path, m_type)
|
| 157 |
+
return None, out_video, run_id
|
| 158 |
+
|
| 159 |
+
def show_mlflow_link(run_id):
|
| 160 |
+
if run_id:
|
| 161 |
+
url = f"https://dagshub.com/Mosensei7/AutonomousVehiclesDetectionDEPI/mlflow/runs/{run_id}"
|
| 162 |
+
return f"<a id='mlflow-link' href='{url}' target='_blank'>View Inference Log on DagsHub</a>"
|
| 163 |
+
return ""
|
| 164 |
+
|
| 165 |
+
submit.click(process, [media, media_type], [output, video_output, mlflow_id])
|
| 166 |
+
mlflow_id.change(show_mlflow_link, mlflow_id, mlflow_id, show_label=False)
|
| 167 |
+
|
| 168 |
+
demo.launch(share=True)
|
requirments.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
ultralytics>=8.3.0
|
| 2 |
+
gradio>=4.0
|
| 3 |
+
mlflow>=2.0
|
| 4 |
+
dagshub>=0.3.0
|
| 5 |
+
opencv-python>=4.8.0
|
| 6 |
+
torch>=2.0.0
|
| 7 |
+
torchvision>=0.15.0
|
| 8 |
+
Pillow>=9.0.0
|
| 9 |
+
numpy>=1.21.0
|