|
import gradio as gr |
|
from ultralytics import YOLO |
|
from PIL import Image |
|
import numpy as np |
|
|
|
|
|
model = YOLO('yolov8n.pt') |
|
|
|
|
|
labels = [ |
|
"R" |
|
"C" |
|
"IC" |
|
"L" |
|
"Q" |
|
|
|
] |
|
|
|
def predict(image): |
|
"""Performs object detection and returns results.""" |
|
try: |
|
|
|
image = np.array(image) |
|
results = model(image) |
|
|
|
if len(results.xyxy[0]) == 0: |
|
print("No detections found.") |
|
return "No components detected." |
|
|
|
detections = [] |
|
for *xyxy, conf, cls in results.xyxy[0]: |
|
x1, y1, x2, y2 = map(int, xyxy) |
|
label = labels[int(cls)] |
|
detections.append(f"{label} (Confidence: {conf:.2f})") |
|
|
|
return "\n".join(detections) |
|
|
|
except Exception as e: |
|
|
|
print(f"Error during prediction: {e}") |
|
return "Error: Unable to detect components." |
|
|
|
|
|
iface = gr.Interface( |
|
fn=predict, |
|
inputs=gr.Image(type="pil"), |
|
outputs=gr.Textbox(), |
|
title="PCB Component Detection", |
|
description="Upload an image of a PCB to identify its components.", |
|
) |
|
|
|
|
|
iface.launch() |
|
|