Komal133 commited on
Commit
65b1fd4
·
verified ·
1 Parent(s): 80f4fa0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -24
app.py CHANGED
@@ -1,38 +1,38 @@
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
 
 
 
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