imgclean 模块
图标文件清洗¶
目的¶
在进行 yolo 模型训练之前,需要标注数据集,
-
首先我们会收集很多数据集,然后去重, 裁剪, 手动对裁剪后的图片进行分类,
-
然后将分类后的图片进行清洗,
-
最终用于训练.
以图标点选的验证码为例子, 我们会收集很多验证码图片 以及验证码图片对应的 icons 文件
- 要求: 验证码图片的名字 需要包含在 icons 文件中
main1_unique_images
¶
main1_unique_images(
image_dir: Path,
output_dir: Path,
source_icons_dir: Optional[Path] = None,
output_icons_dir: Optional[Path] = None,
image_suffix: Optional[list[str]] = None,
)
根据 MD5 去重图像,并(可选)同步复制配套icon文件
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_dir
|
Path
|
输入图像目录,含子文件 |
required |
output_dir
|
Path
|
去重后的图像输出目录 |
required |
source_icons_dir
|
Optional[Path]
|
配套icon文件的源目录,可为 None, 如果存在, 则 images 文件的 stem 必须包含在 icon文件名中 |
None
|
output_icons_dir
|
Optional[Path]
|
配套icon的目标目录,可为 None |
None
|
image_suffix
|
Optional[list[str]]
|
图像文件后缀列表,默认为 [".png", ".jpg", ".jpeg", ".webp"] |
None
|
Source code in src/cfun/imgclean.py
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 | |
main2_crop_images
¶
main2_crop_images(
bg_dir: Path,
icon_dir: Path,
output_dir: Path,
box_size: int = 60,
clear_output: bool = True,
image_suffix: Optional[list[str]] = None,
iscopybg: bool = False,
)
剪切底图中的 DET 区域,并整理对应的前景图
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bg_dir
|
Path
|
底图所在目录,唯一命名 |
required |
icon_dir
|
Path
|
前景图目录,命名中包含底图 stem |
required |
output_dir
|
Path
|
输出目录 |
required |
box_size
|
int
|
裁剪框大小(正方形) |
60
|
clear_output
|
bool
|
是否清空旧的输出目录 |
True
|
image_suffix
|
Optional[list[str]]
|
图像文件后缀列表,默认为 [".png", ".jpg", ".jpeg", ".webp"] |
None
|
iscopybg
|
bool
|
是否复制底图到输出目录, 默认为 False, 如果为 True, 只是方便查看 |
False
|
Source code in src/cfun/imgclean.py
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 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 | |
main3_class_images
¶
main3_class_images(
source_dir: Path,
target_dir: Path,
isrotate: bool = True,
rotate_range: tuple[int, int] = (-30, 30),
rotate_step: int = 3,
background_color: Optional[tuple[int, int, int]] = None,
image_suffix: Optional[list[str]] = None,
subdir_min_imglen: int = 2,
subdir_max_imglen: int = 3,
unique_kw: Optional[str] = "rgba",
unique_start: Optional[str] = None,
unique_end: Optional[str] = None,
) -> None
处理分类图像:去 alpha、旋转、保存到目标目录、去重。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source_dir
|
Path
|
原始分类图片目录 |
required |
target_dir
|
Path
|
输出目录 |
required |
isrotate
|
bool
|
是否生成旋转图像,默认 True |
True
|
rotate_range
|
tuple[int, int]
|
旋转角度范围 (start, end),默认 (-30, 30) |
(-30, 30)
|
rotate_step
|
int
|
旋转角度步长,默认 3 |
3
|
background_color
|
Optional[tuple[int, int, int]]
|
旋转用于填充 alpha 的背景色, 默认为白色 (255, 255, 255) |
None
|
image_suffix
|
Optional[list[str]]
|
图像文件后缀列表,默认为 [".png", ".jpg", ".jpeg", ".webp"] |
None
|
subdir_min_imglen
|
int
|
子目录下最小图像数量,默认 2 |
2
|
subdir_max_imglen
|
int
|
子目录下最大图像数量,默认 3, 不在这个范围内的子目录将被忽略 |
3
|
unique_kw
|
Optional[str]
|
用于唯一标识的关键字,默认为 "rgba", 透明图像将被作为唯一标识 |
'rgba'
|
unique_start
|
Optional[str]
|
可选的唯一标识开始字符串 |
None
|
unique_end
|
Optional[str]
|
可选的唯一标识结束字符串 |
None
|
Source code in src/cfun/imgclean.py
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 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 | |
main4_check
¶
main4_check(
model_path: str,
img_dir: str | Path,
verbose: bool = False,
img_suffix: Optional[list[str]] = None,
) -> None
检查图片是否被正确分类。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_path
|
str
|
YOLOv8 模型路径,如 'best.pt', 必须是 YOLO 模型文件 |
required |
img_dir
|
str | Path
|
图像目录,每个子目录为类别名 |
required |
verbose
|
bool
|
是否打印分类错误的信息 |
False
|
img_suffix
|
Optional[list[str]]
|
图像文件后缀列表,默认为 [".png", ".jpg", ".jpeg", ".webp"] |
None
|
Source code in src/cfun/imgclean.py
move_images_dir
¶
move_images_dir(
src_dir: Union[str, Path],
dst_dir: Union[str, Path],
img_suffix: Optional[list[str]] = None,
) -> None
将源目录中的图像文件移动到目标目录的子目录中。主要用于整理图像文件。(请做好备份,避免数据丢失)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
src_dir
|
Union[str, Path]
|
源目录路径,包含图像文件的子目录 |
required |
dst_dir
|
Union[str, Path]
|
目标目录路径,图像文件将被移动到此目录的子目录中 |
required |
img_suffix
|
Optional[list[str]]
|
图像文件后缀列表,默认为 [".jpg", ".jpeg", ".png", ".webp"] |
None
|