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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -7
app.py CHANGED
@@ -8,15 +8,19 @@ import gradio as gr
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
@@ -26,7 +30,7 @@ def detect_pcb_faults(image):
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
 
 
8
  model_path = "https://huggingface.co/Sakthi3214/pcb_detection/resolve/main/best.pt"
9
  model = YOLO(model_path)
10
 
11
+ # ✅ Define class names (Manually if model.names is empty)
12
+ class_names = {
13
+ 0: "Missing Hole",
14
+ 1: "Mouse Bite",
15
+ 2: "Open Circuit",
16
+ 3: "Short",
17
+ 4: "Spur",
18
+ 5: "Copper",
19
+ } if not model.names else model.names # Use model.names if available
20
 
21
  def detect_pcb_faults(image):
22
  """Runs YOLOv8 on the input image and returns detected defects."""
23
+ results = model(image, conf=0.15) # 🔥 Lower confidence to detect more defects
24
 
25
  boxes = results[0].boxes.xyxy.cpu().numpy() # Extract bounding boxes
26
  confs = results[0].boxes.conf.cpu().numpy() # Extract confidence scores
 
30
  for (x1, y1, x2, y2), conf, class_id in zip(boxes, confs, class_ids):
31
  cv2.rectangle(image, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 2)
32
 
33
+ # Get class label from dictionary
34
  label = f"{class_names.get(int(class_id), 'Unknown')} ({conf:.2f})"
35
  cv2.putText(image, label, (int(x1), int(y1) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
36