Spaces:
Runtime error
Runtime error
更新 app.py
Browse files
app.py
CHANGED
@@ -1,77 +1,76 @@
|
|
1 |
-
import gradio as gr
|
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 |
-
model = YOLO('Fracture_best.pt')
|
9 |
-
|
10 |
-
def predict(img, conf, iou):
|
11 |
-
results = model.predict(img, conf=conf, iou=iou)
|
12 |
-
name = results[0].names
|
13 |
-
cls = results[0].boxes.cls
|
14 |
-
boneanomaly = 0
|
15 |
-
bonelesion = 0
|
16 |
-
fracture = 0
|
17 |
-
metal = 0
|
18 |
-
periostealreaction = 0
|
19 |
-
pronatorsign = 0
|
20 |
-
softtissue = 0
|
21 |
-
text = 0
|
22 |
-
for i in cls:
|
23 |
-
if i == 0:
|
24 |
-
boneanomaly += 1
|
25 |
-
elif i == 1:
|
26 |
-
bonelesion += 1
|
27 |
-
elif i == 2:
|
28 |
-
fracture += 1
|
29 |
-
elif i == 3:
|
30 |
-
metal += 1
|
31 |
-
elif i == 4:
|
32 |
-
periostealreaction += 1
|
33 |
-
elif i == 5:
|
34 |
-
pronatorsign += 1
|
35 |
-
elif i==6:
|
36 |
-
softtissue += 1
|
37 |
-
elif i==
|
38 |
-
text += 1
|
39 |
-
# 绘制柱状图
|
40 |
-
fig, ax = plt.subplots()
|
41 |
-
categories = ['
|
42 |
-
|
43 |
-
|
44 |
-
ax.
|
45 |
-
|
46 |
-
|
47 |
-
ax.
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
canvas
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
#
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
["
|
76 |
-
|
77 |
-
interface.launch()
|
|
|
1 |
+
import gradio as gr
|
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 |
+
model = YOLO('Fracture_best.pt')
|
9 |
+
|
10 |
+
def predict(img, conf, iou):
|
11 |
+
results = model.predict(img, conf=conf, iou=iou)
|
12 |
+
name = results[0].names
|
13 |
+
cls = results[0].boxes.cls
|
14 |
+
boneanomaly = 0
|
15 |
+
bonelesion = 0
|
16 |
+
fracture = 0
|
17 |
+
metal = 0
|
18 |
+
periostealreaction = 0
|
19 |
+
pronatorsign = 0
|
20 |
+
softtissue = 0
|
21 |
+
text = 0
|
22 |
+
for i in cls:
|
23 |
+
if i == 0:
|
24 |
+
boneanomaly += 1
|
25 |
+
elif i == 1:
|
26 |
+
bonelesion += 1
|
27 |
+
elif i == 2:
|
28 |
+
fracture += 1
|
29 |
+
elif i == 3:
|
30 |
+
metal += 1
|
31 |
+
elif i == 4:
|
32 |
+
periostealreaction += 1
|
33 |
+
elif i == 5:
|
34 |
+
pronatorsign += 1
|
35 |
+
elif i==6:
|
36 |
+
softtissue += 1
|
37 |
+
elif i==8:
|
38 |
+
text += 1
|
39 |
+
# 绘制柱状图
|
40 |
+
fig, ax = plt.subplots()
|
41 |
+
categories = ['BMLY', 'bonelesion', 'fracture', 'metal', 'PN', 'PTS', 'SFTI', 'text']
|
42 |
+
counts = [boneanomaly, bonelesion, fracture, metal, periostealreaction, pronatorsign, softtissue, text]
|
43 |
+
ax.bar(categories, counts)
|
44 |
+
ax.set_title('Category-Count')
|
45 |
+
plt.ylim(0,5)
|
46 |
+
ax.set_xlabel('Category')
|
47 |
+
ax.set_ylabel('Count')
|
48 |
+
# 将图表保存为字节流
|
49 |
+
buf = io.BytesIO()
|
50 |
+
canvas = FigureCanvas(fig)
|
51 |
+
canvas.print_png(buf)
|
52 |
+
plt.close(fig) # 关闭图形,释放资源
|
53 |
+
|
54 |
+
# 将字节流转换为PIL Image
|
55 |
+
image_png = Image.open(buf)
|
56 |
+
# 绘制并返回结果图片和类别计数图表
|
57 |
+
|
58 |
+
for i, r in enumerate(results):
|
59 |
+
# Plot results image
|
60 |
+
im_bgr = r.plot() # BGR-order numpy array
|
61 |
+
im_rgb = Image.fromarray(im_bgr[..., ::-1]) # RGB-order PIL image
|
62 |
+
|
63 |
+
# Show results to screen (in supported environments)
|
64 |
+
return im_rgb, image_png
|
65 |
+
|
66 |
+
|
67 |
+
base_conf, base_iou = 0.25, 0.45
|
68 |
+
title = "基于改进YOLOv8算法的手腕骨折辅助诊断系统"
|
69 |
+
des = "鼠标点击上传图片即可检测缺陷,可通过鼠标调整预测置信度,还可点击网页最下方示例图片进行预测"
|
70 |
+
interface = gr.Interface(
|
71 |
+
inputs=['image', gr.Slider(maximum=1, minimum=0, value=base_conf), gr.Slider(maximum=1, minimum=0, value=base_iou)],
|
72 |
+
outputs=["image", 'image'], fn=predict, title=title, description=des,
|
73 |
+
examples=[["example1.jpg", base_conf, base_iou],
|
74 |
+
["example2.jpg", base_conf, base_iou],
|
75 |
+
["example3.jpg", base_conf, base_iou]])
|
76 |
+
interface.launch()
|
|