0
点赞
收藏
分享

微信扫一扫

PennFudanPed数据集转yolo格式

pennFudanPed数据集只有不到200张图片,标注的行人为非遮挡行人,遮挡和骑车的行人都没有标注
标注框做的比较标注和准确,下面是该数据集直接转成yolo格式的代码,新建train文件夹,然后使用下面代码存为py,然后运行

#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
import os
import re
from pathlib import Path
from PIL import Image
import csv
import shutil
import numpy as np

pngpath='./PNGImages/'
oritxt='./Annotation/'
newtxt='./train/'
savepath='./train/'
 
def convert(size, box0,box1,box2,box3):
    dw = 1. / size[0]
    dh = 1. / size[1]
    x = (box0 + box2) / 2 * dw
    y = (box1 + box3)/ 2 * dh
    w = (box2-box0) * dw
    h = (box3-box1) * dh
    return (x, y, w, h)

txtfiles = os.listdir(oritxt)
matrixs  = []
for txtpath in txtfiles:
    img_path = pngpath + txtpath[:-4] + ".png";
    outpath = newtxt + txtpath[:-4] + '.txt'
    with Image.open(img_path) as Img:
            img_size = Img.size
    ans = ''
    f1 = open(oritxt + txtpath, 'r')
    for line in f1.readlines():
        if re.findall('Xmin',line):
            pt = [int(x) for x in re.findall(r"\d+",line)]
            matrixs.append(pt)
            bb = convert(img_size, pt[1], pt[2], pt[3], pt[4])
            ans = ans + '1' + ' ' + ' '.join(str(a) for a in bb) + '\n'
    f1.close()
    with open(outpath, 'w') as outfile:
            outfile.write(ans)
    shutil.copy(img_path, savepath +  txtpath[:-4] + '.png') 
举报

相关推荐

0 条评论