convert模块
json标注文件转yolo格式¶
主要提供一些函数,将xlabeling生成的json标注文件转换为yolo格式的txt标注文件。
通过json文件把 目标 生成yolo格式的txt文件, 进行目标检测数据分类,按指定的key进行分类, 比如:按照json文件中的 label字段进行分类
也可以直接用xlabeling 这些工具直接导出txt文件, 但需要手动划分数据集
json_to_yolo_txt
¶
json_to_yolo_txt(
json_dir: Union[Path, str],
label_key: str,
class_mapping: dict[int, str],
output_dir: Union[Path, str],
image_dir: None | Path | str = None,
image_suffix: str = ".png",
force_overwrite: bool = False,
ischeck: bool = True,
shape_type: str = "rectangle",
) -> None
将 XLabeling 标注的 JSON 文件转换为 YOLO 格式的 TXT 文件。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
json_dir
|
Union[Path, str]
|
存放 JSON 标注文件的目录 |
required |
label_key
|
str
|
用于分类的键,如 "label" |
required |
class_mapping
|
dict[int, str]
|
类别映射字典, 键为整数 ID,从0开始,值为字符串类名,类别名称映射字典,对应json中的label_key |
required |
output_dir
|
Union[Path, str]
|
输出的 TXT 文件的目录 |
required |
image_dir
|
None | Path | str
|
原始图像文件所在目录. json文件和图像文件同名 |
None
|
image_suffix
|
str
|
图像文件后缀名, 默认 ".png" |
'.png'
|
force_overwrite
|
bool
|
是否强制覆盖输出目录, 默认 False |
False
|
ischeck
|
bool
|
是否检查图像文件是否存在, 默认 True, 如果为True, 则会根据json文件的名称去检查图像文件是否存在, 如果不存在则报错 |
True
|
shape_type
|
str
|
形状类型. 暂时只能是rectangle(默认) 或 rotation. rectangle表示矩形,rotation表示旋转矩形. |
'rectangle'
|
Returns:
| Type | Description |
|---|---|
None
|
None |
Example
Source code in src/cfun/yolo/convert.py
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 | |
crop_images
¶
crop_images(
json_dir: Union[Path, str],
image_dir: Union[Path, str],
image_suffix: str = ".png",
category_key: str = "label",
remove_chinese: Literal[0, 1, 2, 3] = 3,
output_dir: Union[Path, str] = "cropped",
force_overwrite: bool = False,
ischeck: bool = True,
shape_type: str = "rectangle",
min_length: int = 10,
issplit: bool = False,
train_ratio: float = 0.8,
) -> None
根据 JSON 标注文件裁剪图像,并根据指定字段(如 label )分类保存。 (只支持矩形框的裁剪,旋转矩阵未知)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
json_dir
|
Union[Path, str]
|
存放 JSON 标注文件的目录 |
required |
image_dir
|
Union[Path, str]
|
原始图像文件所在目录. json文件和图像文件同名, |
required |
image_suffix
|
str
|
图像文件后缀名, 默认 ".png" |
'.png'
|
category_key
|
str
|
用于分类图像的字段(例如 "label") |
'label'
|
remove_chinese
|
Literal[0, 1, 2, 3]
|
移除中文的方式
|
3
|
output_dir
|
Union[Path, str]
|
裁剪后图像的输出目录, 默认 "cropped" |
'cropped'
|
force_overwrite
|
bool
|
是否强制覆盖输出目录, 默认 False, 如果为True, 则会删除原有的输出目录 |
False
|
ischeck
|
bool
|
是否检查图像文件是否存在, 默认 True, 如果为True, 检查json文件数量和图像文件数量是否一致, 如果不一致则报错 |
True
|
shape_type
|
str
|
形状类型, 默认为 "rectangle", 目前只支持 "rectangle" 或 "rotation", 如果是 "rotation" 则表示旋转矩形 |
'rectangle'
|
min_length
|
int
|
用于检查每个子分类的数量, 如果数量小于 min_length 则打印警告信息, 默认 10 |
10
|
issplit
|
bool
|
是否将裁剪后的图像进行划分, 划分为YOLO 的训练集和验证集, 默认 False, 如果为True, 则会生成一个 "{output_dir}_split" 的目录 |
False
|
train_ratio
|
float
|
训练集比例, 默认 0.8, 如果 issplit 为 True, 则会按照这个比例划分数据集 |
0.8
|
Returns:
| Type | Description |
|---|---|
None
|
None |
Example
Source code in src/cfun/yolo/convert.py
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 | |
box_to_polygon
¶
box_to_polygon(
box: Union[dict, list],
key: str = "box",
replace: str = "points",
) -> Union[dict, list]
将 box 格式 转换为 polygon 格式。
Note
-
box 表示为 [x1, y1, x2, y2], 其中 x1, y1 为左上角坐标, x2, y2 为右下角坐标。
-
polygon 表示为 [[x1, y1], [x2, y1], [x2, y2], [x1, y2]], 其中四个点分别表示左上、右上、右下、左下。
polygon对应四个点来表示一个矩形框(左上,右上,右下,左下), box对应用两个点来表示一个矩形框(左上和右下)。 二者进行互换, 只是替换key以及对应的值,其他的保持不变。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
box
|
Union[dict, list]
|
box 表示, |
required |
key
|
str
|
box 的键名,默认为 "box" |
'box'
|
replace
|
str
|
polygon 的键名,默认为 "points", 可以和key相同, |
'points'
|
Returns:
| Type | Description |
|---|---|
Union[dict, list]
|
Union[dict, list]: 转换后的 polygon 格式 |
Example
Source code in src/cfun/yolo/convert.py
polygon_to_box
¶
polygon_to_box(
polygon: Union[dict, list],
key: str = "points",
replace: str = "box",
) -> Union[dict, list]
将 polygon 格式 转换为 box 格式。
Note
-
box 表示为 [x1, y1, x2, y2], 其中 x1, y1 为左上角坐标, x2, y2 为右下角坐标。
-
polygon 表示为 [[x1, y1], [x2, y1], [x2, y2], [x1, y2]], 其中四个点分别表示左上、右上、右下、左下。
polygon对应四个点来表示一个矩形框(左上,右上,右下,左下), box对应用两个点来表示一个矩形框(左上和右下)。 二者进行互换, 只是替换key以及对应的值,其他的保持不变。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
polygon
|
Union[dict, list]
|
polygon 表示 |
required |
key
|
str
|
polygon 的键名,默认为 "points" |
'points'
|
replace
|
str
|
box 的键名,默认为 "box", 可以和key相同. |
'box'
|
Returns:
| Type | Description |
|---|---|
Union[dict, list]
|
Union[dict, list]: 转换后的 box 格式 |