-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexport.py
More file actions
196 lines (158 loc) · 6.93 KB
/
Copy pathexport.py
File metadata and controls
196 lines (158 loc) · 6.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import os
import json
from datetime import datetime
from typing import Literal, Any
from utils import load_annotations
import random
import shutil
import yaml
def export(project_name: str, format: Literal["YAML"] = "YAML", type: Literal["rect", "mask"] = "rect", ratio: float = 0.8) -> tuple[bool, str | None, str | None]:
"""
导出标注数据到指定格式。
Args:
project_name: 项目名称
format: 导出格式,目前支持 YAML
type: 标注类型,rect 为矩形框,mask 为分割掩码
ratio: 训练集占比
Returns:
tuple: (成功标志, 错误信息, 输出目录路径)
"""
project_dir = os.path.join(".", "data", project_name)
if not os.path.exists(project_dir):
return False, f"项目目录不存在: {project_dir}", None
time_str = datetime.now().strftime("%Y%m%d%H%M%S")
out_dir = os.path.join(".", "output", f"{project_name}_{time_str}")
try:
os.makedirs(os.path.join(out_dir, "images", "train"), exist_ok=True)
os.makedirs(os.path.join(out_dir, "images", "val"), exist_ok=True)
os.makedirs(os.path.join(out_dir, "labels", "train"), exist_ok=True)
os.makedirs(os.path.join(out_dir, "labels", "val"), exist_ok=True)
if format == "YAML":
return _export_yaml(project_name, project_dir, out_dir, type, ratio)
else:
return False, f"不支持的导出格式: {format}", None
except Exception as e:
return False, str(e), None
def _export_yaml(project_name, project_dir: str, out_dir: str, type: Literal["rect", "mask"], ratio: float) -> tuple[bool, str | None, str | None]:
images_train_dir = os.path.join(out_dir, "images", "train")
images_val_dir = os.path.join(out_dir, "images", "val")
labels_train_dir = os.path.join(out_dir, "labels", "train")
labels_val_dir = os.path.join(out_dir, "labels", "val")
# 读取配置文件
config_path = os.path.join(project_dir, "config.json")
if not os.path.exists(config_path):
return False, f"配置文件不存在: {config_path}", None
with open(config_path, "r", encoding="utf-8") as f:
config = json.load(f)
# 解析类别
classes = config.get("classes", [])
categories = []
for category, _ in classes:
if category not in categories:
categories.append(category)
# 遍历图片目录
images_dir = os.path.join(project_dir, "images")
if not os.path.exists(images_dir):
return False, f"图片目录不存在: {images_dir}", None
# 获取所有图片文件
image_extensions = {".jpg", ".jpeg", ".png", ".bmp", ".webp"}
valid_image_files = []
for filename in os.listdir(images_dir):
filenname_base, ext = os.path.splitext(filename)
if ext.lower() not in image_extensions:
continue
image_path = os.path.join(images_dir, filename)
annotation_path = os.path.join(images_dir, filenname_base + ".txt")
if not os.path.exists(annotation_path) or not os.path.exists(image_path):
continue
valid_image_files.append((filenname_base, image_path, annotation_path))
# 分配训练集和验证集
total_files_count = len(valid_image_files)
train_file_count = int(round(total_files_count * ratio))
val_file_count = total_files_count - train_file_count
train_files = []
val_files = []
for paths in valid_image_files:
if ((random.random() <= ratio) and len(train_files) < train_file_count) or len(val_files) >= val_file_count:
train_files.append(paths)
else:
val_files.append(paths)
# 复制文件和保存标注
for filenname_base, image_path, annotation_path in train_files:
shutil.copy(image_path, images_train_dir)
annotations = load_annotations(annotation_path)
if type == "rect":
annotations_str = _convert_to_rect_str(annotations, categories)
elif type == "mask":
# todo: 导出遮罩
annotations_str = _convert_to_mask_str(annotations, categories)
annotation_path = os.path.join(labels_train_dir, filenname_base + ".txt")
with open(annotation_path, "w") as annotation_file:
annotation_file.write(annotations_str)
for filenname_base, image_path, annotation_path in val_files:
shutil.copy(image_path, images_val_dir)
annotations = load_annotations(annotation_path)
if type == "rect":
annotations_str = _convert_to_rect_str(annotations, categories)
elif type == "mask":
# todo: 导出遮罩
annotations_str = _convert_to_mask_str(annotations, categories)
annotation_path = os.path.join(labels_val_dir, filenname_base + ".txt")
with open(annotation_path, "w") as annotation_file:
annotation_file.write(annotations_str)
# 导出YAML文件
data = {
"path": project_name,
"train": "images/train",
"val": "images/val",
"test": None,
"names": {i: v for i, v in enumerate(categories)},
}
yaml_path = os.path.join(out_dir, project_name + ".yaml")
with open(yaml_path, "w") as f:
yaml.safe_dump(data, f)
return True, None, out_dir
def _convert_to_rect_str(annotations, categories: list[Any]) -> str:
ret = ""
for annotation in annotations:
mode = annotation.get('mode')
name = annotation.get('class')
points = annotation.get('points')
if name in categories:
index = categories.index(name)
else:
categories.append(name)
index = len(categories) - 1
if mode == "R" or mode == "M":
ret += f"{index} {points[0][0]} {points[0][1]} {points[1][0]} {points[1][1]}\n"
elif mode == "P":
xmin, ymin = points[0]
xmax, ymax = xmin, ymin
for x, y in points[1:]:
xmin = min(xmin, x)
ymin = min(ymin, y)
xmax = max(xmax, x)
ymax = max(ymax, y)
ret += f"{index} {xmin} {ymin} {xmax} {ymax}\n"
return ret
def _convert_to_mask_str(annotations, categories: list[Any]) -> str:
ret = ""
for annotation in annotations:
mode = annotation.get('mode')
name = annotation.get('class')
points = annotation.get('points')
if name not in categories:
index = categories.index(name)
else:
categories.append(name)
index = len(categories) - 1
polygon_points = []
if mode == "R" or mode == "M":
x1, y1 = points[0]
x2, y2 = points[1]
polygon_points = [(x1, y1), (x2, y1), (x2, y2), (x1, y2)]
elif mode == "P":
polygon_points = points
polygon_points_str = " ".join([f"{x} {y}" for x, y in polygon_points])
ret += f"{index} {polygon_points_str}\n"
return ret