xiaoming32236046 commited on
Commit
6922116
·
verified ·
1 Parent(s): a5c36e6

Upload 2 files

Browse files
Files changed (2) hide show
  1. app2.py +57 -0
  2. best.pt +3 -0
app2.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from ultralytics import YOLO
3
+ from PIL import Image
4
+ import io
5
+
6
+ # 加载 YOLOv8 模型
7
+ model = YOLO('/mnt/project/yolo_view/best.pt') # 替换为您的模型文件路径
8
+
9
+ def detect_objects(image):
10
+ # 使用 YOLOv8 模型进行预测
11
+ results = model(image)
12
+
13
+ # 处理预测结果
14
+ class_counts = {}
15
+ output_text = ''
16
+ annotated_image = Image.fromarray(results[0].plot()) # 将 NumPy 数组转换为 PIL 图像
17
+
18
+ # 将 PIL 图像转换为 bytes 以供 Gradio 显示
19
+ buffered = io.BytesIO()
20
+ annotated_image.save(buffered, format="PNG")
21
+ annotated_image_bytes = buffered.getvalue()
22
+
23
+ # 将 bytes 转换为 PIL 图像对象
24
+ annotated_image = Image.open(io.BytesIO(annotated_image_bytes))
25
+
26
+ for result in results[0].boxes.data:
27
+ class_id = int(result[5])
28
+ class_name = model.names[class_id]
29
+ confidence = result[4]
30
+ x1, y1, x2, y2 = result[:4].cpu().numpy().astype(int)
31
+
32
+ # 统计类别数量
33
+ if class_name in class_counts:
34
+ class_counts[class_name] += 1
35
+ else:
36
+ class_counts[class_name] = 1
37
+
38
+ # 构建输出文本
39
+ output_text += f'类别: {class_name}, 置信度: {confidence:.2f}, 坐标: ({x1}, {y1}), ({x2}, {y2})\n'
40
+
41
+ # 构建每个类别数量的输出
42
+ class_count_text = '\n'.join([f'{class_name}: {count}' for class_name, count in class_counts.items()])
43
+ output_text += f'\n每个类别的数量:\n{class_count_text}'
44
+
45
+ return annotated_image, output_text
46
+
47
+ # 创建 Gradio 界面
48
+ demo = gr.Interface(
49
+ fn=detect_objects,
50
+ inputs=gr.Image(type="pil"),
51
+ outputs=[gr.Image(type="pil"), "text"],
52
+ title="细胞检测",
53
+ description="上传图片,进行物体检测并显示结果"
54
+ )
55
+
56
+ # 启动应用
57
+ demo.launch()
best.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c5a801f9379af97c95d555246f1b684b49f9464fafa106096e817164fe0a2ff4
3
+ size 6250521