Komal133 commited on
Commit
80f4fa0
·
verified ·
1 Parent(s): 866adec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -21
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
- # Load model (you can switch to 'fasterrcnn' based loading if needed)
8
- model = YOLO("model/best.pt") # fine-tuned YOLOv8 model
 
 
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
- annotated_img = results[0].plot() # Draw boxes
17
- predictions = results[0].boxes.data.cpu().numpy() # [x1, y1, x2, y2, conf, class]
18
 
19
- # Create readable output
20
- output = []
21
  for pred in predictions:
22
- x1, y1, x2, y2, conf, cls_id = pred
23
- label = DEFECT_LABELS[int(cls_id)]
24
- output.append(f"{label}: {conf:.2f}")
 
25
 
26
- return Image.fromarray(annotated_img), "\n".join(output)
27
 
28
- # Gradio UI
29
- interface = gr.Interface(
30
  fn=detect_defects,
31
- inputs=gr.Image(type="pil"),
32
- outputs=[gr.Image(type="pil", label="Detected Image"), gr.Textbox(label="Detected Faults")],
33
- title="Structural Defect Detection",
34
- description="Upload drone-captured image to detect cracks, rust, spalling, and deformations.",
 
 
 
35
  allow_flagging="never"
36
  )
37
 
38
  if __name__ == "__main__":
39
- interface.launch()
 
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()