Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,26 +2,72 @@ import gradio as gr
|
|
2 |
import torch
|
3 |
from PIL import Image
|
4 |
from ultralytics import YOLO
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
|
7 |
-
model = YOLO(r'pcb-best.pt')
|
8 |
def predict(img, conf, iou):
|
9 |
results = model.predict(img, conf=conf, iou=iou)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
for i, r in enumerate(results):
|
11 |
# Plot results image
|
12 |
im_bgr = r.plot() # BGR-order numpy array
|
13 |
im_rgb = Image.fromarray(im_bgr[..., ::-1]) # RGB-order PIL image
|
14 |
|
15 |
# Show results to screen (in supported environments)
|
16 |
-
return im_rgb
|
17 |
|
18 |
|
19 |
base_conf, base_iou = 0.25, 0.45
|
20 |
title = "基于YOLO-V8的PCB电路板缺陷检测"
|
21 |
des = "鼠标点击上传图片即可检测缺陷,可通过鼠标调整预测置信度,还可点击网页最下方示例图片进行预测"
|
22 |
-
interface = gr.Interface(
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
2 |
import torch
|
3 |
from PIL import Image
|
4 |
from ultralytics import YOLO
|
5 |
+
import matplotlib.pyplot as plt
|
6 |
+
import io
|
7 |
+
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
|
8 |
+
plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
|
9 |
+
plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
|
10 |
+
model = YOLO(r'pcb-best.pt')
|
11 |
|
12 |
|
|
|
13 |
def predict(img, conf, iou):
|
14 |
results = model.predict(img, conf=conf, iou=iou)
|
15 |
+
name = results[0].names
|
16 |
+
cls = results[0].boxes.cls
|
17 |
+
copper = 0
|
18 |
+
mousebite = 0
|
19 |
+
open_defect = 0
|
20 |
+
pin_hole = 0
|
21 |
+
short = 0
|
22 |
+
spur = 0
|
23 |
+
for i in cls:
|
24 |
+
if i == 0:
|
25 |
+
copper += 1
|
26 |
+
elif i == 1:
|
27 |
+
mousebite += 1
|
28 |
+
elif i == 2:
|
29 |
+
open_defect += 1
|
30 |
+
elif i == 3:
|
31 |
+
pin_hole += 1
|
32 |
+
elif i == 4:
|
33 |
+
short += 1
|
34 |
+
elif i == 5:
|
35 |
+
spur += 1
|
36 |
+
# 绘制柱状图
|
37 |
+
fig, ax = plt.subplots()
|
38 |
+
categories = ['Copper', 'Mousebite', 'Open Defect', 'Pin Hole', 'Short', 'Spur']
|
39 |
+
counts = [copper, mousebite, open_defect, pin_hole, short, spur]
|
40 |
+
ax.bar(categories, counts)
|
41 |
+
ax.set_title('缺陷类别计数')
|
42 |
+
plt.ylim(0,5)
|
43 |
+
ax.set_xlabel('缺陷类别')
|
44 |
+
ax.set_ylabel('数目')
|
45 |
+
# 将图表保存为字节流
|
46 |
+
buf = io.BytesIO()
|
47 |
+
canvas = FigureCanvas(fig)
|
48 |
+
canvas.print_png(buf)
|
49 |
+
plt.close(fig) # 关闭图形,释放资源
|
50 |
+
|
51 |
+
# 将字节流转换为PIL Image
|
52 |
+
image_png = Image.open(buf)
|
53 |
+
# 绘制并返回结果图片和类别计数图表
|
54 |
+
|
55 |
for i, r in enumerate(results):
|
56 |
# Plot results image
|
57 |
im_bgr = r.plot() # BGR-order numpy array
|
58 |
im_rgb = Image.fromarray(im_bgr[..., ::-1]) # RGB-order PIL image
|
59 |
|
60 |
# Show results to screen (in supported environments)
|
61 |
+
return im_rgb, image_png
|
62 |
|
63 |
|
64 |
base_conf, base_iou = 0.25, 0.45
|
65 |
title = "基于YOLO-V8的PCB电路板缺陷检测"
|
66 |
des = "鼠标点击上传图片即可检测缺陷,可通过鼠标调整预测置信度,还可点击网页最下方示例图片进行预测"
|
67 |
+
interface = gr.Interface(
|
68 |
+
inputs=['image', gr.Slider(maximum=1, minimum=0, value=base_conf), gr.Slider(maximum=1, minimum=0, value=base_iou)],
|
69 |
+
outputs=["image", 'image'], fn=predict, title=title, description=des,
|
70 |
+
examples=[["example1.jpg", base_conf, base_iou],
|
71 |
+
["example2.jpg", base_conf, base_iou],
|
72 |
+
["example3.jpg", base_conf, base_iou]])
|
73 |
+
interface.launch()
|