Update app.py
Browse files
app.py
CHANGED
|
@@ -1,38 +1,38 @@
|
|
| 1 |
-
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
from
|
| 4 |
-
|
| 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 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
|
|
|
| 27 |
demo = gr.Interface(
|
| 28 |
-
fn=
|
| 29 |
inputs=gr.Image(type="pil", label="Upload Drone Image"),
|
| 30 |
outputs=[
|
| 31 |
-
gr.Image(type="pil", label="
|
| 32 |
-
gr.Textbox(label="
|
| 33 |
],
|
| 34 |
-
title="🧱 Structural Defect Detection",
|
| 35 |
-
description="
|
| 36 |
allow_flagging="never"
|
| 37 |
)
|
| 38 |
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from PIL import Image, ImageDraw
|
| 3 |
+
import random
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# Simulated defect labels
|
| 6 |
DEFECT_LABELS = ['crack', 'spalling', 'rust', 'deformation']
|
| 7 |
|
| 8 |
+
# Dummy detection function
|
| 9 |
+
def simulate_defect_detection(image):
|
| 10 |
+
image = image.convert("RGB")
|
| 11 |
+
draw = ImageDraw.Draw(image)
|
|
|
|
| 12 |
output_lines = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
# Randomly simulate 2–4 detections
|
| 15 |
+
for _ in range(random.randint(2, 4)):
|
| 16 |
+
label = random.choice(DEFECT_LABELS)
|
| 17 |
+
conf = round(random.uniform(0.7, 0.99), 2)
|
| 18 |
+
x1, y1 = random.randint(20, 250), random.randint(20, 250)
|
| 19 |
+
x2, y2 = x1 + random.randint(50, 120), y1 + random.randint(50, 120)
|
| 20 |
+
draw.rectangle([x1, y1, x2, y2], outline="red", width=3)
|
| 21 |
+
draw.text((x1, y1 - 12), f"{label} ({conf})", fill="red")
|
| 22 |
+
output_lines.append(f"{label}: {conf}")
|
| 23 |
+
|
| 24 |
+
return image, "\n".join(output_lines)
|
| 25 |
|
| 26 |
+
# Gradio UI
|
| 27 |
demo = gr.Interface(
|
| 28 |
+
fn=simulate_defect_detection,
|
| 29 |
inputs=gr.Image(type="pil", label="Upload Drone Image"),
|
| 30 |
outputs=[
|
| 31 |
+
gr.Image(type="pil", label="Simulated Structural Defects"),
|
| 32 |
+
gr.Textbox(label="Predicted Defects with Confidence")
|
| 33 |
],
|
| 34 |
+
title="🧱 Structural Defect Detection (Demo Mode)",
|
| 35 |
+
description="Simulates detection of cracks, rust, spalling, and deformation in drone-captured images.",
|
| 36 |
allow_flagging="never"
|
| 37 |
)
|
| 38 |
|