Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
# β
|
| 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 =
|
| 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 |
-
|
| 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(
|
| 50 |
|
| 51 |
-
#
|
| 52 |
label = f"{class_names.get(int(class_id), 'Unknown')} ({conf:.2f})"
|
| 53 |
|
| 54 |
-
#
|
| 55 |
-
cv2.putText(
|
| 56 |
|
| 57 |
-
return
|
| 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(
|