File size: 2,154 Bytes
58aa9b1
063d0af
 
719a3a2
 
58aa9b1
063d0af
719a3a2
063d0af
 
58aa9b1
 
 
063d0af
 
58aa9b1
063d0af
58aa9b1
 
063d0af
719a3a2
 
 
 
 
 
063d0af
58aa9b1
719a3a2
 
58aa9b1
719a3a2
 
58aa9b1
 
 
719a3a2
 
 
 
 
58aa9b1
 
063d0af
58aa9b1
 
 
063d0af
58aa9b1
 
 
 
 
 
 
 
 
 
 
063d0af
 
58aa9b1
de07b63
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
import gradio as gr
print("[STARTUP] Imports completed (gradio, ultralytics, PIL loaded)", flush=True)

from ultralytics import YOLO
from PIL import Image

print("[STARTUP] Loading YOLO model from best.pt ...", flush=True)
model = YOLO("best.pt")
print("[STARTUP] Model loaded successfully.", flush=True)
print(f"[STARTUP] Model classes: {model.names}", flush=True)


def detect_damage(input_image: Image.Image):
    print("[REQUEST] Received new image for detection", flush=True)

    if input_image is None:
        print("[REQUEST] No image provided, returning early", flush=True)
        return None, "No image provided."

    print("[REQUEST] Running model.predict() ...", flush=True)
    results = model.predict(
        source=input_image,
        imgsz=640,
        conf=0.4,
        iou=0.45,
    )
    print("[REQUEST] Inference completed", flush=True)

    result = results[0]
    result_image = Image.fromarray(result.plot()[:, :, ::-1])  # BGR -> RGB

    detections = result.boxes
    if detections is None or len(detections) == 0:
        summary = "No damage detected."
    else:
        lines = [f"Found {len(detections)} detection(s):"]
        for box in detections:
            class_id = int(box.cls[0])
            confidence = float(box.conf[0])
            class_name = model.names[class_id]
            lines.append(f"- {class_name} (confidence: {confidence:.2f})")
        summary = "\n".join(lines)

    print(f"[REQUEST] Returning result: {summary}", flush=True)
    return result_image, summary


print("[STARTUP] Building Gradio interface ...", flush=True)
demo = gr.Interface(
    fn=detect_damage,
    inputs=gr.Image(type="pil", label="Upload an image"),
    outputs=[
        gr.Image(type="pil", label="Detection result"),
        gr.Textbox(label="Summary"),
    ],
    title="Structural Damage Detection (Crack / Spalling / Pothole)",
    description="Upload an image of a road or concrete structure to detect cracks, spalling, and potholes using a YOLOv8 model.",
)

print("[STARTUP] Interface built. Calling demo.launch() now ...", flush=True)

if __name__ == "__main__":
    demo.launch(ssr_mode=False, share=True)