Spaces:
Sleeping
Sleeping
Download app.py from knightsam/PatholeDetection: direct link, hf CLI and curl.
- Browser
- Download file 3.33 kB
-
https://huggingface.co/spaces/knightsam/PatholeDetection/resolve/main/app.py
- Command line
-
hf download hf://spaces/knightsam/PatholeDetection/app.py
-
curl -L -o app.py https://huggingface.co/spaces/knightsam/PatholeDetection/resolve/main/app.py
3.33 kB
| 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() |