Hanf Chase commited on
Commit
71e7eab
·
1 Parent(s): e3adb87
app.py ADDED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ import numpy as np
4
+ import os
5
+ import tempfile
6
+ from ultralytics import YOLO
7
+
8
+ # 加载YOLOv8模型
9
+ model_path = "docgenome_object_detection_yolov8.pt"
10
+ model = YOLO(model_path)
11
+
12
+ def detect_and_visualize(image):
13
+ """
14
+ 对上传的图像进行目标检测并可视化结果
15
+
16
+ Args:
17
+ image: 上传的图像
18
+
19
+ Returns:
20
+ annotated_image: 带有检测框的图像
21
+ yolo_annotations: YOLO格式的标注内容
22
+ """
23
+ # 运行检测
24
+ results = model(image)
25
+
26
+ # 获取第一帧的结果
27
+ result = results[0]
28
+
29
+ # 创建图像副本用于可视化
30
+ annotated_image = image.copy()
31
+
32
+ # 准备YOLO格式的标注内容
33
+ yolo_annotations = []
34
+
35
+ # 获取图像尺寸
36
+ img_height, img_width = image.shape[:2]
37
+
38
+ # 在原图上绘制检测结果
39
+ for box in result.boxes:
40
+ # 获取边界框坐标
41
+ x1, y1, x2, y2 = box.xyxy[0].cpu().numpy()
42
+ x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
43
+
44
+ # 获取置信度
45
+ conf = float(box.conf[0])
46
+
47
+ # 获取类别ID和名称
48
+ cls_id = int(box.cls[0])
49
+ cls_name = result.names[cls_id]
50
+
51
+ # 为每个类别生成不同的颜色
52
+ color = tuple(np.random.randint(0, 255, 3).tolist())
53
+
54
+ # 绘制边界框
55
+ cv2.rectangle(annotated_image, (x1, y1), (x2, y2), color, 2)
56
+
57
+ # 准备标签文本
58
+ label = f'{cls_name} {conf:.2f}'
59
+
60
+ # 计算标签大小
61
+ (label_width, label_height), _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)
62
+
63
+ # 绘制标签背景
64
+ cv2.rectangle(annotated_image, (x1, y1-label_height-5), (x1+label_width, y1), color, -1)
65
+
66
+ # 绘制标签文本
67
+ cv2.putText(annotated_image, label, (x1, y1-5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
68
+
69
+ # 转换为YOLO格式 (x_center, y_center, width, height) 归一化到0-1
70
+ x_center = (x1 + x2) / (2 * img_width)
71
+ y_center = (y1 + y2) / (2 * img_height)
72
+ width = (x2 - x1) / img_width
73
+ height = (y2 - y1) / img_height
74
+
75
+ # 添加到YOLO标注列表
76
+ yolo_annotations.append(f"{cls_id} {x_center:.6f} {y_center:.6f} {width:.6f} {height:.6f}")
77
+
78
+ # 将YOLO标注转换为字符串
79
+ yolo_annotations_str = "\n".join(yolo_annotations)
80
+
81
+ return annotated_image, yolo_annotations_str
82
+
83
+ def save_yolo_annotations(yolo_annotations_str):
84
+ """
85
+ 保存YOLO标注到临时文件并返回文件路径
86
+
87
+ Args:
88
+ yolo_annotations_str: YOLO格式的标注字符串
89
+
90
+ Returns:
91
+ file_path: 保存的标注文件路径
92
+ """
93
+ # 创建临时文件
94
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".txt")
95
+ temp_file_path = temp_file.name
96
+
97
+ # 写入标注内容
98
+ with open(temp_file_path, "w") as f:
99
+ f.write(yolo_annotations_str)
100
+
101
+ return temp_file_path
102
+
103
+ # 创建Gradio界面
104
+ with gr.Blocks(title="YOLOv8目标检测可视化") as demo:
105
+ gr.Markdown("# YOLOv8目标检测可视化")
106
+ gr.Markdown("上传图像,使用YOLOv8模型进行目标检测,并下载YOLO格式的标注。")
107
+
108
+ with gr.Row():
109
+ with gr.Column():
110
+ input_image = gr.Image(label="上传图像", type="numpy")
111
+ detect_btn = gr.Button("开始检测")
112
+
113
+ with gr.Column():
114
+ output_image = gr.Image(label="检测结果")
115
+ yolo_annotations = gr.Textbox(label="YOLO标注", lines=10)
116
+ download_btn = gr.Button("下载YOLO标注")
117
+ download_file = gr.File(label="下载文件")
118
+
119
+ # 设置点击事件
120
+ detect_btn.click(
121
+ fn=detect_and_visualize,
122
+ inputs=[input_image],
123
+ outputs=[output_image, yolo_annotations]
124
+ )
125
+
126
+ download_btn.click(
127
+ fn=save_yolo_annotations,
128
+ inputs=[yolo_annotations],
129
+ outputs=[download_file]
130
+ )
131
+
132
+ # 启动应用
133
+ if __name__ == "__main__":
134
+ demo.launch()
latex2layout_object_detection_yolov8.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:94d75fc3df499e59b03857a7c2e6c22c88498cc83892c1a2674965160c976fa8
3
+ size 273162929
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ ultralytics>=8.0.0
2
+ opencv-python>=4.5.0
3
+ numpy>=1.20.0
4
+ gradio>=3.0.0
test_yolo.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from ultralytics import YOLO
2
+ import cv2
3
+ import numpy as np
4
+
5
+ def detect_and_visualize(image_path, model_path):
6
+ # 加载YOLOv8模型
7
+ model = YOLO(model_path) # 例如 'yolov8n.pt', 'yolov8s.pt' 等
8
+
9
+ # 读取图片
10
+ image = cv2.imread(image_path)
11
+
12
+ # 运行检测
13
+ results = model(image)
14
+
15
+ # 获取第一帧的结果
16
+ result = results[0]
17
+
18
+ # 在原图上绘制检测结果
19
+ for box in result.boxes:
20
+ # 获取边界框坐标
21
+ x1, y1, x2, y2 = box.xyxy[0].cpu().numpy()
22
+ x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
23
+
24
+ # 获取置信度
25
+ conf = float(box.conf[0])
26
+
27
+ # 获取类别ID和名称
28
+ cls_id = int(box.cls[0])
29
+ cls_name = result.names[cls_id]
30
+
31
+ # 为每个类别生成不同的颜色
32
+ color = tuple(np.random.randint(0, 255, 3).tolist())
33
+
34
+ # 绘制边界框
35
+ cv2.rectangle(image, (x1, y1), (x2, y2), color, 2)
36
+
37
+ # 准备标签文本
38
+ label = f'{cls_name} {conf:.2f}'
39
+
40
+ # 计算标签大小
41
+ (label_width, label_height), _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)
42
+
43
+ # 绘制标签背景
44
+ cv2.rectangle(image, (x1, y1-label_height-5), (x1+label_width, y1), color, -1)
45
+
46
+ # 绘制标签文本
47
+ cv2.putText(image, label, (x1, y1-5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
48
+
49
+ # 保存结果图片
50
+ output_path = 'output_detected.jpg'
51
+ cv2.imwrite(output_path, image)
52
+ print(f"检测结果已保存至: {output_path}")
53
+
54
+ # 使用示例
55
+ if __name__ == "__main__":
56
+ image_path = "./test_math.png" # 替换为你的图片路径
57
+ model_path = "docgenome_object_detection_yolov8.pt" # 替换为你的模型权重路径
58
+ detect_and_visualize(image_path, model_path)