Wider Face数据集转yolo格式

Wider Face数据集下载地址

WIDER FACE: A Face Detection Benchmark

2yolo.py

from PIL import Image
import os

def getLabel(file_path, label_folder, image_folder):
    label_path = ''
    width = 0
    height = 0
    with open(file_path, 'r') as file:
        lines = file.readlines()
        total_lines = len(lines)
        for i, line in enumerate(lines):
            line = line.replace('\n', '')
            if (line[-3:] == 'jpg'):
                # 获取图片长宽信息
                img = Image.open(image_folder + '//' + line)
                width, height = img.size
                # 创建文件夹
                folder_path = label_folder + '//' + line.split('/')[0]
                if not os.path.exists(folder_path):
                    os.mkdir(folder_path)
                # 创建txt文件
                label_path = folder_path + '//' + line.split('/')[1].replace('jpg', 'txt')
                with open(label_path, 'w') as file:
                    pass
            else:
                content = line.split(' ')
                if (len(content) > 1):
                    x1 = float(content[0])
                    y1 = float(content[1])
                    w = float(content[2])
                    h = float(content[3])
                    x = x1 + w/2
                    y = y1 + h/2
                    x /= width
                    w /= width
                    y /= height
                    h /= height
                    with open(label_path, 'a') as file:
                        file.write('0 ' + str(x) + ' ' + str(y) + ' ' + str(w) + ' ' + str(h) + '\n')
            # 进度条
            if (i + 1) % 1000 == 0:
                progress = (i + 1) / total_lines * 100
                print(f"Progress: {progress:.2f}%")

if __name__ == '__main__':
    file_path = 'wider_face_val_bbx_gt.txt'
    label_folder = 'labels'
    image_folder = 'images'
    getLabel(file_path, label_folder, image_folder)

使用说明

在主函数修改文件路径,可分别转换训练集、验证集、测试集的标签。

以WIDER_val为例,将WIDER_val下的images文件夹复制到目录,新建labels空文件夹,导入对应的txt文件,运行2yolo.py。

设置了进度条,可以自行估计时间在旁边摸会儿鱼。

完成之后可以在labels文件夹下看到对应的文件。

验证结果

随机拉某个类别的图片和对应标签,手动创建一个classes.txt,使用labelimg工具打开。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值