llzzyy233 commited on
Commit
3559adb
1 Parent(s): b108b66

更新 app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -77
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==7:
38
- text += 1
39
- # 绘制柱状图
40
- fig, ax = plt.subplots()
41
- categories = ['boneanomaly', 'bonelesion', 'fracture', 'metal',
42
- 'periostealreaction', 'pronatorsign', 'softtissue', 'text']
43
- counts = [boneanomaly, bonelesion, fracture, metal, periostealreaction, pronatorsign, softtissue, text]
44
- ax.bar(categories, counts)
45
- ax.set_title('Category-Count')
46
- plt.ylim(0,5)
47
- ax.set_xlabel('Category')
48
- ax.set_ylabel('Count')
49
- # 将图表保存为字节流
50
- buf = io.BytesIO()
51
- canvas = FigureCanvas(fig)
52
- canvas.print_png(buf)
53
- plt.close(fig) # 关闭图形,释放资源
54
-
55
- # 将字节流转换为PIL Image
56
- image_png = Image.open(buf)
57
- # 绘制并返回结果图片和类别计数图表
58
-
59
- for i, r in enumerate(results):
60
- # Plot results image
61
- im_bgr = r.plot() # BGR-order numpy array
62
- im_rgb = Image.fromarray(im_bgr[..., ::-1]) # RGB-order PIL image
63
-
64
- # Show results to screen (in supported environments)
65
- return im_rgb, image_png
66
-
67
-
68
- base_conf, base_iou = 0.25, 0.45
69
- title = "基于改进YOLOv8算法的手腕骨折辅助诊断系统"
70
- des = "鼠标点击上传图片即可检测缺陷,可通过鼠标调整预测置信度,还可点击网页最下方示例图片进行预测"
71
- interface = gr.Interface(
72
- inputs=['image', gr.Slider(maximum=1, minimum=0, value=base_conf), gr.Slider(maximum=1, minimum=0, value=base_iou)],
73
- outputs=["image", 'image'], fn=predict, title=title, description=des,
74
- examples=[["example1.jpg", base_conf, base_iou],
75
- ["example2.jpg", base_conf, base_iou],
76
- ["example3.jpg", base_conf, base_iou]])
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()