| import gradio as gr |
| from ultralytics import YOLO |
| import cv2 |
| from PIL import Image |
|
|
| |
| try: |
| model = YOLO("Vision Classification.pt") |
| except Exception as e: |
| print(f"Error loading model: {e}") |
| model = None |
|
|
| def predict(image, conf_threshold): |
| if image is None or model is None: |
| return None, "Model not loaded or invalid image." |
| |
| |
| results = model(image, imgsz=768, conf=conf_threshold) |
| |
| |
| result = results[0] |
| |
| |
| |
| annotated_image = result.plot() |
| |
| |
| annotated_image_rgb = cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB) |
| |
| |
| boxes = result.boxes |
| class_names = result.names |
| |
| if len(boxes) == 0: |
| detection_summary = "No civic issues detected in this image." |
| else: |
| |
| detection_counts = {} |
| for box in boxes: |
| cls_id = int(box.cls) |
| cls_name = class_names[cls_id] |
| detection_counts[cls_name] = detection_counts.get(cls_name, 0) + 1 |
| |
| summary_lines = ["**Detections:**"] |
| for cls_name, count in detection_counts.items(): |
| summary_lines.append(f"- {count} {cls_name}(s)") |
| |
| detection_summary = "\n".join(summary_lines) |
| |
| return Image.fromarray(annotated_image_rgb), detection_summary |
|
|
| |
| with gr.Blocks(title="PotholeNet-YOLO11m-v1 π") as interface: |
| gr.Markdown("# π PotholeNet-YOLO11m-v1") |
| gr.Markdown("**Aamchi City AI Civic System** β Real-time pothole, road damage, and garbage detection for Indian urban roads.") |
| gr.Markdown("Upload an image of a road to detect infrastructure issues. The model was trained on 23,000+ street-level images.") |
| |
| with gr.Row(): |
| with gr.Column(): |
| input_image = gr.Image(type="pil", label="Upload Street Image") |
| conf_slider = gr.Slider(minimum=0.01, maximum=1.0, value=0.25, step=0.01, label="Confidence Threshold") |
| submit_btn = gr.Button("Detect Civic Issues", variant="primary") |
| |
| with gr.Column(): |
| output_image = gr.Image(type="pil", label="Detection Results") |
| detection_text = gr.Markdown(label="Detection Summary") |
| |
| submit_btn.click( |
| fn=predict, |
| inputs=[input_image, conf_slider], |
| outputs=[output_image, detection_text] |
| ) |
| |
| gr.Markdown("### Intended Use") |
| gr.Markdown("Real-time pothole detection, Automated civic issue reporting, Infrastructure health monitoring.") |
| gr.Markdown("**Developer:** Vansh Momaya") |
|
|
| if __name__ == "__main__": |
| interface.launch(server_name="0.0.0.0", server_port=7860) |
|
|