sakthi54321 commited on
Commit
a22e625
Β·
verified Β·
1 Parent(s): 713de1c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -28
app.py CHANGED
@@ -3,42 +3,24 @@ from ultralytics import YOLO
3
  import cv2
4
  import numpy as np
5
  import gradio as gr
6
- import requests
7
 
8
- # βœ… Download model from Hugging Face if not already present
9
- model_path = "best.pt"
10
- hf_model_url = "https://huggingface.co/Sakthi3214/pcb_detection/resolve/main/best.pt"
11
-
12
- if not os.path.exists(model_path):
13
- print("Downloading YOLOv8 model...")
14
- response = requests.get(hf_model_url, stream=True)
15
- with open(model_path, "wb") as f:
16
- for chunk in response.iter_content(chunk_size=1024):
17
- f.write(chunk)
18
- print("Download complete!")
19
-
20
- # βœ… Load the YOLOv8 model
21
  model = YOLO(model_path)
22
 
23
  # βœ… Define class names (Manually if model.names is empty)
24
- class_names = model.names if model.names else {
25
  0: "Missing Hole",
26
  1: "Mouse Bite",
27
  2: "Open Circuit",
28
  3: "Short",
29
  4: "Spur",
30
  5: "Copper",
31
- }
32
 
33
  def detect_pcb_faults(image):
34
  """Runs YOLOv8 on the input image and returns detected defects."""
35
- img_copy = image.copy() # βœ… Avoid modifying the original image
36
-
37
- # βœ… Run inference
38
- results = model.predict(img_copy, conf=0.01)
39
-
40
- if not results:
41
- return img_copy # No detections, return original image
42
 
43
  boxes = results[0].boxes.xyxy.cpu().numpy() # Extract bounding boxes
44
  confs = results[0].boxes.conf.cpu().numpy() # Extract confidence scores
@@ -46,15 +28,15 @@ def detect_pcb_faults(image):
46
 
47
  # βœ… Draw bounding boxes and labels
48
  for (x1, y1, x2, y2), conf, class_id in zip(boxes, confs, class_ids):
49
- cv2.rectangle(img_copy, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 3) # Green box
50
 
51
- # βœ… Get class label
52
  label = f"{class_names.get(int(class_id), 'Unknown')} ({conf:.2f})"
53
 
54
- # βœ… Add text
55
- cv2.putText(img_copy, label, (int(x1), int(y1) - 10), cv2.FONT_HERSHEY_SIMPLEX, 1.2, (0, 255, 0), 3)
56
 
57
- return img_copy
58
 
59
  # βœ… Gradio UI for PCB Fault Detection
60
  gr.Interface(
 
3
  import cv2
4
  import numpy as np
5
  import gradio as gr
 
6
 
7
+ # βœ… Load YOLOv8 model from Hugging Face
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.02) # πŸ”₯ 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
 
28
 
29
  # βœ… Draw bounding boxes and labels
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), 3) # 🟒 Thicker Box
32
 
33
+ # Get class label from dictionary
34
  label = f"{class_names.get(int(class_id), 'Unknown')} ({conf:.2f})"
35
 
36
+ # πŸ”₯ Larger Text Size & Thicker Font
37
+ cv2.putText(image, label, (int(x1), int(y1) - 10), cv2.FONT_HERSHEY_SIMPLEX, 1.2, (0, 255, 0), 3)
38
 
39
+ return image
40
 
41
  # βœ… Gradio UI for PCB Fault Detection
42
  gr.Interface(