Update app.py
Browse files
app.py
CHANGED
|
@@ -1,39 +1,40 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from ultralytics import YOLO
|
| 3 |
-
import torch
|
| 4 |
from PIL import Image
|
| 5 |
-
import os
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
# Define defect labels if custom
|
| 11 |
DEFECT_LABELS = ['crack', 'spalling', 'rust', 'deformation']
|
| 12 |
|
| 13 |
-
# Inference function
|
| 14 |
def detect_defects(image):
|
| 15 |
results = model(image)
|
| 16 |
-
|
| 17 |
-
predictions = results[0].boxes.data.cpu().numpy()
|
| 18 |
|
| 19 |
-
|
| 20 |
-
output = []
|
| 21 |
for pred in predictions:
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
| 25 |
|
| 26 |
-
return Image.fromarray(
|
| 27 |
|
| 28 |
-
|
| 29 |
-
interface = gr.Interface(
|
| 30 |
fn=detect_defects,
|
| 31 |
-
inputs=gr.Image(type="pil"),
|
| 32 |
-
outputs=[
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
| 35 |
allow_flagging="never"
|
| 36 |
)
|
| 37 |
|
| 38 |
if __name__ == "__main__":
|
| 39 |
-
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
from ultralytics import YOLO
|
|
|
|
| 4 |
from PIL import Image
|
|
|
|
| 5 |
|
| 6 |
+
# Prevent config error on HF Spaces
|
| 7 |
+
os.environ["YOLO_CONFIG_DIR"] = "/tmp/ultralytics"
|
| 8 |
+
|
| 9 |
+
model = YOLO("model/best.pt")
|
| 10 |
|
|
|
|
| 11 |
DEFECT_LABELS = ['crack', 'spalling', 'rust', 'deformation']
|
| 12 |
|
|
|
|
| 13 |
def detect_defects(image):
|
| 14 |
results = model(image)
|
| 15 |
+
rendered = results[0].plot()
|
| 16 |
+
predictions = results[0].boxes.data.cpu().numpy()
|
| 17 |
|
| 18 |
+
output_lines = []
|
|
|
|
| 19 |
for pred in predictions:
|
| 20 |
+
conf = float(pred[4])
|
| 21 |
+
class_id = int(pred[5])
|
| 22 |
+
label = DEFECT_LABELS[class_id] if class_id < len(DEFECT_LABELS) else f"Class {class_id}"
|
| 23 |
+
output_lines.append(f"{label}: {conf:.2f}")
|
| 24 |
|
| 25 |
+
return Image.fromarray(rendered), "\n".join(output_lines)
|
| 26 |
|
| 27 |
+
demo = gr.Interface(
|
|
|
|
| 28 |
fn=detect_defects,
|
| 29 |
+
inputs=gr.Image(type="pil", label="Upload Drone Image"),
|
| 30 |
+
outputs=[
|
| 31 |
+
gr.Image(type="pil", label="Detected Structural Defects"),
|
| 32 |
+
gr.Textbox(label="Detected Faults and Confidence")
|
| 33 |
+
],
|
| 34 |
+
title="🧱 Structural Defect Detection",
|
| 35 |
+
description="Upload drone-captured image to detect cracks, spalling, rust, and deformation.",
|
| 36 |
allow_flagging="never"
|
| 37 |
)
|
| 38 |
|
| 39 |
if __name__ == "__main__":
|
| 40 |
+
demo.launch()
|