sakthi54321 commited on
Commit
8f9be20
·
verified ·
1 Parent(s): 8d838e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -7
app.py CHANGED
@@ -8,24 +8,35 @@ import gradio as gr
8
  model_path = "https://huggingface.co/Sakthi3214/pcb_detection/resolve/main/best.pt"
9
  model = YOLO(model_path)
10
 
 
 
 
 
 
 
11
  def detect_pcb_faults(image):
12
- """Runs YOLOv8 on the input image and returns detected objects."""
13
- results = model(image) # Run inference
 
14
  boxes = results[0].boxes.xyxy.cpu().numpy() # Extract bounding boxes
15
  confs = results[0].boxes.conf.cpu().numpy() # Extract confidence scores
 
16
 
17
- # Draw bounding boxes
18
- for (x1, y1, x2, y2), conf in zip(boxes, confs):
19
  cv2.rectangle(image, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 2)
20
- cv2.putText(image, f"{conf:.2f}", (int(x1), int(y1) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
 
 
 
21
 
22
  return image
23
 
24
- # ✅ Gradio UI
25
  gr.Interface(
26
  fn=detect_pcb_faults,
27
  inputs=gr.Image(type="numpy"),
28
  outputs=gr.Image(type="numpy"),
29
  title="PCB Fault Detection",
30
- description="Upload a PCB image to detect defects using YOLOv8."
31
  ).launch()
 
8
  model_path = "https://huggingface.co/Sakthi3214/pcb_detection/resolve/main/best.pt"
9
  model = YOLO(model_path)
10
 
11
+ # ✅ Check if model has class names
12
+ if model.names:
13
+ class_names = model.names
14
+ else:
15
+ class_names = {0: "Defect"} # Default label if no names are found
16
+
17
  def detect_pcb_faults(image):
18
+ """Runs YOLOv8 on the input image and returns detected defects."""
19
+ results = model(image, conf=0.15) # 🔥 Lower confidence threshold to detect more defects
20
+
21
  boxes = results[0].boxes.xyxy.cpu().numpy() # Extract bounding boxes
22
  confs = results[0].boxes.conf.cpu().numpy() # Extract confidence scores
23
+ class_ids = results[0].boxes.cls.cpu().numpy() # Extract class IDs
24
 
25
+ # Draw bounding boxes and labels
26
+ for (x1, y1, x2, y2), conf, class_id in zip(boxes, confs, class_ids):
27
  cv2.rectangle(image, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 2)
28
+
29
+ # Get class label from model
30
+ label = f"{class_names.get(int(class_id), 'Unknown')} ({conf:.2f})"
31
+ cv2.putText(image, label, (int(x1), int(y1) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
32
 
33
  return image
34
 
35
+ # ✅ Gradio UI for PCB Fault Detection
36
  gr.Interface(
37
  fn=detect_pcb_faults,
38
  inputs=gr.Image(type="numpy"),
39
  outputs=gr.Image(type="numpy"),
40
  title="PCB Fault Detection",
41
+ description="Upload a PCB image to detect defects using YOLOv8. The model will highlight and label detected faults."
42
  ).launch()