| import gradio as gr |
| from ultralytics import YOLO |
| import torch |
| from PIL import Image |
| import os |
|
|
| |
| model = YOLO("model/best.pt") |
|
|
| |
| DEFECT_LABELS = ['crack', 'spalling', 'rust', 'deformation'] |
|
|
| |
| def detect_defects(image): |
| results = model(image) |
| annotated_img = results[0].plot() |
| predictions = results[0].boxes.data.cpu().numpy() |
|
|
| |
| output = [] |
| for pred in predictions: |
| x1, y1, x2, y2, conf, cls_id = pred |
| label = DEFECT_LABELS[int(cls_id)] |
| output.append(f"{label}: {conf:.2f}") |
|
|
| return Image.fromarray(annotated_img), "\n".join(output) |
|
|
| |
| interface = gr.Interface( |
| fn=detect_defects, |
| inputs=gr.Image(type="pil"), |
| outputs=[gr.Image(type="pil", label="Detected Image"), gr.Textbox(label="Detected Faults")], |
| title="Structural Defect Detection", |
| description="Upload drone-captured image to detect cracks, rust, spalling, and deformations.", |
| allow_flagging="never" |
| ) |
|
|
| if __name__ == "__main__": |
| interface.launch() |
|
|