File size: 9,873 Bytes
09f2579
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e4aaa7
 
c49e4ce
0e4aaa7
c67348d
 
09f2579
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import gradio as gr
import torch
import numpy as np
from PIL import Image
from ultralytics import YOLO
import cv2

# โ”€โ”€โ”€ Load Model โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
MODEL_PATH  = "yolov8s_best.pt"
CLASS_NAMES = [
    "D00 โ€” Longitudinal Crack",
    "D10 โ€” Transverse Crack",
    "D20 โ€” Alligator Crack",
    "D40 โ€” Pothole",
]
CLASS_COLORS = {
    "D00 โ€” Longitudinal Crack": "#3498db",
    "D10 โ€” Transverse Crack":   "#2ecc71",
    "D20 โ€” Alligator Crack":    "#e67e22",
    "D40 โ€” Pothole":            "#e74c3c",
}
COUNTRY_POTHOLE = {
    "๐Ÿ‡ฎ๐Ÿ‡ณ India":          "โฌ›โฌ›โฌ›โฌ›โฌ› Very High",
    "๐Ÿ‡ณ๐Ÿ‡ด Norway":         "โฌ›โฌ›โฌ›โฌ›  High",
    "๐Ÿ‡บ๐Ÿ‡ธ United States":  "โฌ›โฌ›โฌ›   Moderate",
    "๐Ÿ‡จ๐Ÿ‡ฟ Czech Republic": "โฌ›โฌ›โฌ›   Moderate",
    "๐Ÿ‡จ๐Ÿ‡ณ China":          "โฌ›โฌ›    Low-Moderate",
    "๐Ÿ‡ฏ๐Ÿ‡ต Japan":          "โฌ›     Low",
}

model = YOLO(MODEL_PATH)

# โ”€โ”€โ”€ Inference Function โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
def detect(image, conf_thresh, iou_thresh, country):
    img_np = np.array(image)

    results = model.predict(
        source    = img_np,
        conf      = conf_thresh,
        iou       = iou_thresh,
        imgsz     = 640,
        device    = 0 if torch.cuda.is_available() else "cpu",
        verbose   = False,
    )[0]

    # โ”€โ”€ Annotated image โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    annotated = results.plot()
    annotated_rgb = cv2.cvtColor(annotated, cv2.COLOR_BGR2RGB)

    # โ”€โ”€ Count detections โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    class_counts = {name: 0 for name in CLASS_NAMES}
    total        = len(results.boxes)
    pothole_count = 0

    for box in results.boxes:
        cls_id = int(box.cls.item())
        class_counts[CLASS_NAMES[cls_id]] += 1
        if cls_id == 3:
            pothole_count += 1

    # โ”€โ”€ Detection table โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    det_table = "### ๐Ÿ“Š Detection Summary\n\n"
    det_table += "| Class | Count | % of Total |\n|---|---|---|\n"
    for name, count in class_counts.items():
        pct = count / max(total, 1) * 100
        color_dot = "๐Ÿ”ต" if "D00" in name else "๐ŸŸข" if "D10" in name else "๐ŸŸ " if "D20" in name else "๐Ÿ”ด"
        det_table += f"| {color_dot} {name} | {count} | {pct:.1f}% |\n"
    det_table += f"\n**Total Detections: {total}**"

    # โ”€โ”€ Pothole report box โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    pct_pothole = pothole_count / max(total, 1) * 100
    if pct_pothole > 30:
        severity = "๐Ÿ”ด CRITICAL โ€” Immediate repair needed"
    elif pct_pothole > 10:
        severity = "๐ŸŸ  MODERATE โ€” Schedule maintenance"
    elif pothole_count > 0:
        severity = "๐ŸŸก LOW โ€” Monitor road surface"
    else:
        severity = "๐ŸŸข NONE DETECTED โ€” Road surface OK"

    pothole_box = f"""### ๐Ÿ•ณ๏ธ Pothole (D40) Report

| Field | Value |
|---|---|
| Pothole Count | **{pothole_count}** |
| % of Detections | **{pct_pothole:.1f}%** |
| Severity | {severity} |
| Avg Confidence | {"N/A" if pothole_count == 0 else f"{np.mean([b.conf.item() for b in results.boxes if int(b.cls.item())==3]):.2f}"} |
"""

    # โ”€โ”€ Country pothole context โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    country_info = COUNTRY_POTHOLE.get(country, "โ€”")
    country_box = f"""### ๐ŸŒ Country Context โ€” {country}

| Field | Value |
|---|---|
| Selected Country | {country} |
| Pothole Density (RDD2022) | {country_info} |
| Dominant Damage Types | {"D00 / D10" if "Japan" in country else "D40 / D20" if "India" in country else "D10 / D40"} |
| Collection Method | {"Smartphone" if "India" in country or "Japan" in country or "Czech" in country else "Street View" if "United" in country else "Camera / Drone"} |
"""

    return Image.fromarray(annotated_rgb), det_table, pothole_box, country_box


# โ”€โ”€โ”€ Gradio UI โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
with gr.Blocks(
    title="๐Ÿ›ฃ๏ธ Road Damage Detector โ€” RDD2022",
    theme=gr.themes.Soft(primary_hue="blue"),
    css="""
    .title-box { text-align: center; padding: 20px; }
    .result-box { border-radius: 10px; padding: 10px; }
    footer { display: none !important; }
    """
) as demo:

    # Header
    gr.HTML("""
    <div class="title-box">
        <h1>๐Ÿ›ฃ๏ธ Road Damage Detection</h1>
        <p style="color:gray;">YOLOv8s ยท Trained on RDD2022 ยท 4 Damage Classes ยท 6 Countries</p>
        <p>
            <span style="background:#3498db;color:white;padding:3px 8px;border-radius:4px;margin:2px;">๐Ÿ”ต D00 Longitudinal</span>
            <span style="background:#2ecc71;color:white;padding:3px 8px;border-radius:4px;margin:2px;">๐ŸŸข D10 Transverse</span>
            <span style="background:#e67e22;color:white;padding:3px 8px;border-radius:4px;margin:2px;">๐ŸŸ  D20 Alligator</span>
            <span style="background:#e74c3c;color:white;padding:3px 8px;border-radius:4px;margin:2px;">๐Ÿ”ด D40 Pothole</span>
        </p>
    </div>
    """)

    with gr.Row():
        # โ”€โ”€ Left column: inputs โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
        with gr.Column(scale=1):
            input_image = gr.Image(
                type="pil",
                label="๐Ÿ“ท Upload Road Image",
                height=350,
            )
            with gr.Row():
                conf_slider = gr.Slider(
                    minimum=0.1, maximum=0.9, value=0.25, step=0.05,
                    label="Confidence Threshold"
                )
                iou_slider = gr.Slider(
                    minimum=0.1, maximum=0.9, value=0.45, step=0.05,
                    label="IoU Threshold"
                )
            country_dropdown = gr.Dropdown(
                choices=list(COUNTRY_POTHOLE.keys()),
                value="๐Ÿ‡ฎ๐Ÿ‡ณ India",
                label="๐ŸŒ Country (photo origin โ€” for pothole context)",
            )
            run_btn = gr.Button("๐Ÿ” Detect Damage", variant="primary", size="lg")

        # โ”€โ”€ Right column: outputs โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
        with gr.Column(scale=1):
            output_image = gr.Image(
                label="๐Ÿ“ Detection Result",
                height=350,
            )

    with gr.Row():
        with gr.Column():
            det_output     = gr.Markdown(label="Detection Summary")
        with gr.Column():
            pothole_output = gr.Markdown(label="Pothole Report")
        with gr.Column():
            country_output = gr.Markdown(label="Country Context")

    # โ”€โ”€ Examples โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    gr.Examples(
        examples=[
            ["examples/india_test_image.jpg",  0.25, 0.45, "๐Ÿ‡ฎ๐Ÿ‡ณ India"],
            ["examples/China_Drone_000253.jpg",  0.25, 0.45, "๐Ÿ‡ฏ๐Ÿ‡ต Japan"],
            ["examples/United_States_004798.jpg", 0.25, 0.45, "๐Ÿ‡บ๐Ÿ‡ธ United States"],
            ["examples/Czech_test_image.jpg", 0.25, 0.45, "๐Ÿ‡จ๐Ÿ‡ฟ Czech Republic"],
            ["examples/China_Drone_000295.jpg", 0.25, 0.45, "๐Ÿ‡จ๐Ÿ‡ณ China"],
            ["examples/norway_road_test.jpg", 0.25, 0.45, "๐Ÿ‡ณ๐Ÿ‡ด Norway"]
        ],
        inputs=[input_image, conf_slider, iou_slider, country_dropdown],
        label="๐Ÿ“‚ Example Images",
    )

    # โ”€โ”€ Footer โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    gr.HTML("""
    <div style="text-align:center; margin-top:20px; color:gray; font-size:13px;">
        Model: YOLOv8s ยท Dataset: RDD2022 (47,420 images ยท 6 countries) ยท
        Classes: D00 / D10 / D20 / D40 ยท
        <a href="https://arxiv.org/abs/2209.08538" target="_blank">Paper</a>
    </div>
    """)

    # โ”€โ”€ Wire up โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    run_btn.click(
        fn      = detect,
        inputs  = [input_image, conf_slider, iou_slider, country_dropdown],
        outputs = [output_image, det_output, pothole_output, country_output],
    )
    input_image.change(
        fn      = detect,
        inputs  = [input_image, conf_slider, iou_slider, country_dropdown],
        outputs = [output_image, det_output, pothole_output, country_output],
    )

if __name__ == "__main__":
    demo.launch(
        server_name = "0.0.0.0",
        server_port = 7860,
        share       = True,      # generates public gradio.live link
    )