Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,142 +1,185 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import mlflow
|
| 3 |
from ultralytics import YOLO
|
| 4 |
-
|
| 5 |
import cv2
|
| 6 |
import numpy as np
|
| 7 |
-
import
|
|
|
|
| 8 |
import time
|
| 9 |
-
import
|
| 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 |
-
mlflow.
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
with gr.Row():
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|