Spaces:
Sleeping
Sleeping
File size: 3,329 Bytes
949d528 519b7fe bd43a0b cf3084d 51ea318 bd43a0b 949d528 bd43a0b 519b7fe 949d528 519b7fe 49e86c3 519b7fe ca67e52 bd43a0b d89fb10 949d528 519b7fe bd43a0b 519b7fe 949d528 519b7fe 949d528 519b7fe bd43a0b 519b7fe 949d528 519b7fe 949d528 519b7fe bd43a0b 949d528 519b7fe 949d528 bd43a0b 949d528 bd43a0b 949d528 519b7fe 949d528 bd43a0b 949d528 bd43a0b 519b7fe 949d528 bd43a0b 949d528 bd43a0b | 1 2 3 4 5 6 7 8 9 10 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 | import gradio as gr
import cv2
from ultralytics import YOLO
# 1. PRE-LOAD BOTH MODELS INTO A DICTIONARY
# This ensures they are only loaded into memory once at startup
available_models = {
"Model 1": YOLO("best_openvino_model"),
"Model 2": YOLO("best1.onnx")
}
def detect(frame, seen_ids, model_choice):
# If the frame is empty, return defaults
if frame is None:
return None, len(seen_ids), seen_ids
# Create a writable copy to avoid the read-only error
frame = frame.copy()
# 2. GRAB THE ACTIVE MODEL BASED ON THE DROPDOWN
active_model = available_models[model_choice]
# Use the active model for tracking
results = active_model.track(frame, conf=0.4, persist=True)
for r in results:
# Check if boxes exist AND if the tracker has assigned IDs yet
if r.boxes is not None and r.boxes.id is not None:
# Extract the unique tracking IDs as a standard Python list
track_ids = r.boxes.id.int().cpu().tolist()
for box, track_id in zip(r.boxes, track_ids):
# Only add to our count if it's a new ID
if track_id not in seen_ids:
seen_ids.append(track_id)
# Draw bounding box
x1, y1, x2, y2 = map(int, box.xyxy[0])
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
# Write the ID number above the pothole
cv2.putText(frame, f"ID: {track_id}", (x1, y1 - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
# The total count is simply the number of unique IDs we've collected
total_count = len(seen_ids)
# Display the total count on the video feed
cv2.putText(frame, f"Total Potholes: {total_count}",
(20, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 3)
return frame, total_count, seen_ids
# Function to reset the counter when swapping models
def reset_tracker():
return [], 0
with gr.Blocks() as demo:
gr.Markdown("# Live Pothole Counter")
gr.Markdown("Using YOLOv8 Object Tracking to count unique potholes in real-time.")
# Initialize an empty list in Gradio's memory to hold our seen IDs
seen_ids_state = gr.State(value=[])
with gr.Row():
with gr.Column():
# 3. ADD THE DROPDOWN MENU
model_dropdown = gr.Dropdown(
choices=["Model 1", "Model 2"],
value="Model 1",
label="Select Detection Model"
)
input_video = gr.Image(sources=["webcam"], streaming=True, label="Webcam Feed")
with gr.Column():
output_video = gr.Image(label="Processed Feed")
live_count = gr.Number(label="Total Unique Potholes", value=0, interactive=False)
# Pass the dropdown choice into the stream function
input_video.stream(
fn=detect,
inputs=[input_video, seen_ids_state, model_dropdown],
outputs=[output_video, live_count, seen_ids_state]
)
# When the user changes the model, reset the ID list and the counter to 0
model_dropdown.change(
fn=reset_tracker,
inputs=None,
outputs=[seen_ids_state, live_count]
)
if __name__ == "__main__":
demo.launch() |