WIDER FACE数据集转化为yolo标注数据

WIDER FACE是一个人脸数据集合,其中包含了人脸的标注,可以用它来转化为yolo的标注格式,做训练,极大节约成本

转化代码:

import os

def convert_wider_to_yolo(annotation_file, image_folder, output_folder):
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)

    with open(annotation_file, 'r') as f:
        lines = f.readlines()

    i = 0
    while i < len(lines):
        image_path = lines[i].strip()
        num_faces = int(lines[i + 1].strip())
        i += 2

        if num_faces == 0:
            i += 1
            continue

        image_full_path = os.path.join(image_folder, image_path)
        output_name = os.path.splitext(os.path.basename(image_path))[0] + '.txt'

        rel_dir = os.path.dirname(image_path)
        output_dir = os.path.join(output_folder, rel_dir)
        if not os.path.exists(output_dir):
            os.makedirs(output_dir)
        output_path = os.path.join(output_dir, output_name)

        with open(output_path, 'w') as f_out:
            for _ in range(num_faces):
                parts = lines[i].strip().split()
                x1, y1, w, h = map(int, parts[:4])
                i += 1

                # Assume you have a function `get_image_size()` that returns the (width, height) of the image.
                image_w, image_h = get_image_size(image_full_path)
                
                # Convert to YOLO format
                center_x = (x1 + w / 2) / image_w
                center_y = (y1 + h / 2) / image_h
                width = w / image_w
                height = h / image_h

                f_out.write(f"0 {center_x} {center_y} {width} {height}\n")


def get_image_size(image_path):
    from PIL import Image
    with Image.open(image_path) as img:
        return img.size


# 替换为你实际的文件路径
annotation_file = '/path/WIDER_train_yolo/wider_face_train_bbx_gt.txt'
image_folder = '/path/WIDER_train_yolo/WIDER_train/images/'
output_folder = '/path/WIDER_train_yolo/WIDER_train/labels'

convert_wider_to_yolo(annotation_file, image_folder, output_folder)

运行后目录结构

── images
│   ├── 0--Parade
│   ├── 10--People_Marching
。。。。。。。。
│   ├── 8--Election_Campain
│   └── 9--Press_Conference
└── labels
    ├── 0--Parade
    ├── 10--People_Marching
。。。。。。。。
    ├── 8--Election_Campain
    └── 9--Press_Conferencez

转化后的数据格式,可以用labelImg来打开,做测试

Leave a Reply

Your email address will not be published. Required fields are marked *