widerface数据集转yolo格式

# author: wujiahao
# Created: 2022/3/14
#       convert widerface to yolo format

import os
import cv2
import shutil

data_root = '/data1/wjh/widerface/WIDER_train/images' 
ann_txt_path = '/data1/wjh/widerface/wider_face_split/wider_face_train_bbx_gt.txt'
dst_path = '/data1/wjh/yolo/widerface'
if not os.path.exists(dst_path):
    os.mkdir(dst_path)
    os.mkdir(os.path.join(dst_path, 'images'))
    os.mkdir(os.path.join(dst_path, 'labels'))


with open(ann_txt_path, 'r') as f:
    ann_lines = f.readlines()

line_i = 0
img_i = 0
while line_i < len(ann_lines):
    # every img for every iter
    img_path = ann_lines[line_i].strip()
    print(f'{img_i}  {line_i} : {img_path}')
    img = cv2.imread(os.path.join(data_root, img_path))
    h_img, w_img, c_img = img.shape

    # convert ann
    line_i += 1
    num_bbox = int(ann_lines[line_i].strip())

    # empty bbox
    if num_bbox ==0 : 
        line_i += 2
        img_i += 1
        continue
    
    out_lines = [] # lines write to yolo txt
    for bbox_i in range(num_bbox):
        # every bbox for every iter
        line_i += 1
        bbox_line = ann_lines[line_i].strip() 
        x1, y1, w, h, blur, expression, illumination, invalid, occlusion, pose = [int(i) for i in bbox_line.split()]
        out_lines.append(f'{0} {(x1+w/2)/w_img} {(y1+h/2)/h_img} {w/w_img} {h/h_img}\n')
    # write to yolo txt    
    with open(os.path.join(dst_path, 'labels', f"{img_i}.txt"), 'w') as f:
        f.writelines(out_lines)
    # copy img
    shutil.copy(os.path.join(data_root, img_path), os.path.join(dst_path, 'images', f"{img_i}.{img_path.split('.')[-1]}"))
    # next img
    line_i +=1
    img_i += 1

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值