File size: 3,304 Bytes
b5a0c9c
 
 
 
 
 
 
a1a77e0
b5a0c9c
 
 
 
 
1c5dc18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5a0c9c
1c5dc18
 
 
 
b5a0c9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46b306d
b5a0c9c
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from ultralytics import YOLO
import cv2
from PIL import Image

# Load the YOLO model - YOLOv11m for pothole, road damage, and garbage detection
try:
    model = YOLO("model.pt")
except Exception as e:
    print(f"Error loading model: {e}")
    model = None

def predict(image, conf_threshold):
    try:
        if image is None or model is None:
            return None, "Model not loaded or invalid image."
        
        # Run inference
        results = model(image, imgsz=768, conf=conf_threshold)
        result = results[0]
        
        # Plotting the detections on the image returns a BGR numpy array
        annotated_image = result.plot()
        annotated_image_rgb = cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB)
        
        # Detection overview text
        boxes = result.boxes
        class_names = result.names
        
        if len(boxes) == 0:
            detection_summary = "No civic issues detected in this image."
        else:
            # Count detections safely
            detection_counts = {}
            for box in boxes:
                # box.cls is usually a tensor. Safe conversion to integer:
                cls_id = int(box.cls.item() if hasattr(box.cls, "item") else box.cls[0])
                cls_name = class_names.get(cls_id, f"Class {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
    
    except Exception as e:
        import traceback
        error_msg = f"ERROR during prediction: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
        return None, error_msg

# Gradio Interface
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.Textbox(label="Detection Summary", interactive=False, lines=4)
            
    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)