1. 引出问题
本人最近在做毕设相关内容,第一阶段目标是通过目标检测来统计课堂人数,因此需要对人脸和人头进行目标检测。模型方面没什么好说的无脑用YOLO,数据集方面,人脸部分找到了来自港中文的WIDER FACE数据集。但是解压后发现标注数据并不是YOLO格式的,因此通过分析后写了一个简单的脚本进行转换。
2. 标注数据转YOLO格式
2.1 源代码
通过分析发现原标注数据的格式如下:
[图片路径]
[该图片中检测框数量]
[左上角x坐标 左上角y坐标 检测框宽 检测框高 …(一些额外信息)]
…
因此得到如下格式转换代码:
import os
from PIL import Image
parent_path = "P:/Graduation_Design/Dataset/WIDER_FACE/"
def convert_to_yolo_format(input_file, output_dir, image_dir):
with open(input_file, 'r') as f:
lines = f.readlines()
i = 0
while i < len(lines):
image_path = lines[i].strip() # Get the relative path of image
num_boxes = int(lines[i + 1].strip()) # Get the number of boxes
# Path of the label file
label_path = os.path.join(output_dir, os.path.basename(image_path).replace('.jpg', '.txt'))
os.makedirs(os.path.dirname(label_path), exist_ok=True