一种简单的数据集处理方法,实现的功能是:
- xml文件转txt文件
- 按比例划分为 训练集 和 验证集
用的数据集是人脸口罩检测数据集: Face Mask Detection
处理效果
原始数据集存放:
face_devkit
└─ face-mask-detection
├─ annotations
├─ images
调整后:
face_devkit
├─ face-mask-detection
│ ├─ annotations
│ ├─ images
│
├─ iamges
│ ├─ train
│ ├─ val
│
└─ labels
├─ train # 下面放训练集标签
├─ val # 下面放验证集标签
代码
代码简单易读,所以记录一下:
"""
Face Mask Detection 数据集_2023.2.8
"""
import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join
import random
from shutil import copyfile
# classes = ["hat", "person"]
classes = ["with_mask", "without_mask", "mask_weared_incorrect"]
TRAIN_RATIO = 80 # 训练集验证集划分比例
def clear_hidden_files(path):
dir_list = os.listdir(path)
for i in dir_list:
abspath = os.path.join(os.path.abspath(path), i)
if os.path.isfile(abspath):
if i.startswith("._"):
os.remove(abspath)