【python】Retinaface人脸检测与关键点定位识别数据集标注制作python

1.在制作人脸数据集的识别,往往需要制作标注自己的数据集,特征点可以自己定,所以写下这段代码,希望对你有所帮助。

2.用labelme做的标记,按照顺序:五个点+一个bbox

3.用labelme进行标注。

4.下面代码将xml文件转换为Retinaface训练集的label.txt。

5.也可以应用于工业特征点检测识别中。

from xml.dom.minidom import parse
import xml.dom.minidom
import os
# 使用minidom解析器打开 XML 文档
# def parse_file(file_name,result_path):
#     DOMTree = xml.dom.minidom.parse(file_name)
#     collection = DOMTree.documentElement
#     shapes = collection.getElementsByTagName("shapes")
#     image_path=collection.getElementsByTagName("imagePath")[0].childNodes[0].nodeValue
#     with open(result_path,"a+",encoding="utf-8") as f:
#         f.writelines([image_path,"\n"])
#         temp=[]
#         for shape in shapes:
#             points=shape.getElementsByTagName("points")
#             for point in points:
#                 value=point.childNodes[0].nodeValue
#                 value=eval(value)
#                 temp.append(str(value[0]))
#                 temp.append(str(value[1]))
#             temp.append("0.0")
#         temp= temp[0:len(temp)-1]
#         temp.append("1")
#         line=" ".join(temp)
#         f.write(line)
#         f.write("\n")

def parse_file(file_name,result_path):
    DOMTree = xml.dom.minidom.parse(file_name)
    collection = DOMTree.documentElement
    shapes = collection.getElementsByTagName("shapes")
    image_path=collection.getElementsByTagName("imagePath")[0].childNodes[0].nodeValue
    labels = dict()
    for shape in shapes:
        label=shape.getElementsByTagName("label")[0].childNodes[0].nodeValue

        label=eval(label)
        print(label)
        temp=labels.get(label,[])
        temp.append(shape)
        labels[label]=temp
    print(labels)
    line=""
    with open(result_path,"a+",encoding="utf-8") as f:
        f.writelines([image_path,])
        for label in labels:
            label_shapes=labels[label]
            shapes=[]
            for shape in label_shapes:
                points=shape.getElementsByTagName("points")
                temp=[]
                for point in points:
                    value=point.childNodes[0].nodeValue
                    value=eval(value)
                    temp.append(str(value[0]))
                    temp.append(str(value[1]))
                shapes.append(" ".join(temp))
            line+=(" 0.0 ".join(shapes)+' 1 ')
        f.write("\n")
        f.write(line)
        f.write("\n")

def exchange(dir_path,result_file_path):
    # dirs=os.listdir("D:\projects\pycharm\experiments")
    dirs=os.listdir(dir_path)
    for file in dirs:
        print(file)
        parse_file(dir_path+'/'+file,result_file_path)

    print(dirs)
if __name__ == '__main__':
    # 第一个参数是xml文件夹路径,第二个参数是txt文件路径,可以不存在
    exchange(r"D:\projects\pycharm\experiments\test",r"D:\projects\pycharm\experiments\result.txt")

你可能感兴趣的:(深度学习,python,numpy,matplotlib)