【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")

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值