yolov4在WIDERFACE中训练进行人脸检测、修改WIDERFACE标注格式

1、简介

WIDERFACE是香港中文大学资讯工程学系 多媒体实验室 发布的人脸数据集
下载连接

2、标注格式

其有两种标注格式.mat与.txt。由于我只能打开.txt文件。其内容如下

0--Parade/0_Parade_marchingband_1_849.jpg
1
449 330 122 149 0 0 0 0 0 0 
0--Parade/0_Parade_Parade_0_904.jpg
1
361 98 263 339 0 0 0 0 0 0 
0--Parade/0_Parade_marchingband_1_799.jpg
21
78 221 7 8 2 0 0 0 0 0 
78 238 14 17 2 0 0 0 0 0 
113 212 11 15 2 0 0 0 0 0 
134 260 15 15 2 0 0 0 0 0 
163 250 14 17 2 0 0 0 0 0 
201 218 10 12 2 0 0 0 0 0 
182 266 15 17 2 0 0 0 0 0 
245 279 18 15 2 0 0 0 0 0 
304 265 16 17 2 0 0 0 2 1 
328 295 16 20 2 0 0 0 0 0 
389 281 17 19 2 0 0 0 2 0 
406 293 21 21 2 0 1 0 0 0 
436 290 22 17 2 0 0 0 0 0 
522 328 21 18 2 0 1 0 0 0 
643 320 23 22 2 0 0 0 0 0 
653 224 17 25 2 0 0 0 0 0 
793 337 23 30 2 0 0 0 0 0 
535 311 16 17 2 0 0 0 1 0 
29 220 11 15 2 0 0 0 0 0 
3 232 11 15 2 0 0 0 2 0 
20 215 12 16 2 0 0 0 2 0 

分别代表

File name
Number of bounding box
x1, y1, w, h, blur, expression, illumination, invalid, occlusion, pose

3、 格式改变

在yolov4训练人头检测器 中表述了如何把xml数据格式改为yolov4使用的voc格式。因此这里我们只要把.txt文件改为单个的xml文件。然后利用yolov4训练人头检测器 中的方法进行转换就好。
.txt转xml脚本如下

3.1 生成xml脚本

def make_xml(figname, width, height, boxlist):

    node_root = Element('annotation')

    node_folder = SubElement(node_root, 'folder')
    node_folder.text = 'VOC'

    node_filename = SubElement(node_root, 'filename')
    node_filename.text = figname

    node_object_num = SubElement(node_root, 'object_num')
    node_object_num.text = str(len(boxlist))

    node_size = SubElement(node_root, 'size')
    node_width = SubElement(node_size, 'width')
    node_width.text = str(width)

    node_height = SubElement(node_size, 'height')
    node_height.text = str(height)

    node_depth = SubElement(node_size, 'depth')
    node_depth.text = '3'

    for box in boxlist:
        node_object = SubElement(node_root, 'object')
        node_name = SubElement(node_object, 'name')
        node_name.text = 'face'
        node_difficult = SubElement(node_object, 'difficult')
        node_difficult.text = '0'

        node_bndbox = SubElement(node_object, 'bndbox')
        node_xmin = SubElement(node_bndbox, 'xmin')
        node_xmin.text = str(box['xmin'])
        node_ymin = SubElement(node_bndbox, 'ymin')
        node_ymin.text = str(box['ymin'])
        node_xmax = SubElement(node_bndbox, 'xmax')
        node_xmax.text = str(box['xmax'])
        node_ymax = SubElement(node_bndbox, 'ymax')
        node_ymax.text = str(box['ymax'])

    #print xml 打印查看结果
    tree = etree.ElementTree(node_root)
    tree.write("./"+figname.split(".")[0]+'.xml', pretty_print=True, xml_declaration=False, encoding='utf-8')
    return

3.2 读取txt文件分离图片信息

def readTxt(path):
    f = open(path,"r")
    lines = f.readlines()
    dadeList=[]
    data={}
    boxlist=[]
    flg = False
    numflg = False
    num=0
    flgnum=0
    for line in lines:
        # print("line:",line)
        if '.jpg' in line:
            tem1 = line.split("/")
            # imagrnamepath = "./images/"+tem1[1]
            imagrname = line.split("/")[1]
            # print("imagrname:",imagrname)
            path = "./images/"+imagrname.split(".")[0]+".jpg"
            # print("path:",path)
            img = cv2.imread(path)
            HW = img.shape
            print("HW:",HW)
            flg=True
            flgnum=0
            # print("imagrnamepath:",imagrnamepath)
            # HW = getHW(imagrnamepath)
            data['imagename'] = imagrname.split(".")[0]+".jpg"
            data['height'] = HW[0]
            data['width'] = HW[1]
        elif flg:
            num = int(line)
            numflg=True
            flgnum=0
            flg=False
        elif numflg:
            flg = False
            labes = line.split()
            # print("labes:",labes)
            if int(labes[8])==2:
                print("abbeng")
                flgnum =flgnum+1
                if flgnum == num:
                    data['boxlist'] = boxlist
                    dadeList.append(data)
                    boxlist=[]
                    data={}
                    flg=False
                    numflg = False
                continue
            temdata={}
            temdata['xmin'] = int(labes[0]) 
            temdata['xmax'] = int(labes[0]) + (int(labes[2]))
            temdata['ymin'] = int(labes[1])
            temdata['ymax'] = int(labes[1]) + (int(labes[3]))
            boxlist.append(temdata)
            flgnum =flgnum+1
            if flgnum == num:
                data['boxlist'] = boxlist
                dadeList.append(data)
                boxlist=[]
                data={}
                flg=False
                numflg = False

    return dadeList

3.3移动图像

import os
import shutil
path='./images/'
dirs = os.listdir(path)
for dir in dirs:
    if os.path.isdir(path+dir):
        images = os.listdir(path+dir)
        for image in images:
            shutil.move(path+dir+"/"+image,path)

完整脚本

你可能感兴趣的:(Linux/Win相关学习,Python相关学习)