当前位置:   article > 正文

目标检测中将xml数据集转换为txt,超简便_目标检测xml转txt

目标检测xml转txt

1.原始jpg和xml分布

在这里插入图片描述

2.转换txt代码

修改 root_pathname_list即可
原始 root_path = r"G:\dataset\VOCdevkit\VOC2012_sub"
确保jpg和xml按照上面的方式排布

"""
1.将voc数据集标注信息(.xml)转为yolo标注格式(.txt),并将图像文件复制到相应文件夹

"""
import os
from tqdm import tqdm
from lxml import etree


def parse_xml_to_dict(xml):
    """
    将xml文件解析成字典形式,参考tensorflow的recursive_parse_xml_to_dict
    Args:
        xml: xml tree obtained by parsing XML file contents using lxml.etree

    Returns:
        Python dictionary holding XML contents.
    """

    if len(xml) == 0:  # 遍历到底层,直接返回tag对应的信息
        return {xml.tag: xml.text}

    result = {}
    for child in xml:
        child_result = parse_xml_to_dict(child)  # 递归遍历标签信息
        if child.tag != 'object':
            result[child.tag] = child_result[child.tag]
        else:
            if child.tag not in result:  # 因为object可能有多个,所以需要放入列表里
                result[child.tag] = []
            result[child.tag].append(child_result[child.tag])
    return {xml.tag: result}


def translate_info(file_names: list, save_root: str, class_dict: dict, flag: str):
    """
    将对应xml文件信息转为yolo中使用的txt文件信息
    :param flag:
    :param file_names:
    :param save_root:
    :param class_dict:
    :return:
    """
    save_txt_path = os.path.join(save_root, "labels")
    if os.path.exists(save_txt_path) is False:
        os.makedirs(save_txt_path)
    voc_images_path = os.path.join(save_root, "images")
    voc_xml_path = os.path.join(save_root, "xmls")
    for file in tqdm(file_names, desc="translate {} file...".format(flag)):
        # 检查下图像文件是否存在
        img_path = os.path.join(voc_images_path, file + ".jpg")
        assert os.path.exists(img_path), "file:{} not exist...".format(img_path)

        # 检查xml文件是否存在
        xml_path = os.path.join(voc_xml_path, file + ".xml")
        assert os.path.exists(xml_path), "file:{} not exist...".format(xml_path)

        # read xml
        with open(xml_path) as fid:
            xml_str = fid.read()
        xml = etree.fromstring(xml_str)
        data = parse_xml_to_dict(xml)["annotation"]
        img_height = int(data["size"]["height"])
        img_width = int(data["size"]["width"])

        # write object info into txt
        assert "object" in data.keys(), "file: '{}' lack of object key.".format(xml_path)
        if len(data["object"]) == 0:
            # 如果xml文件中没有目标就直接忽略该样本
            print("Warning: in '{}' xml, there are no objects.".format(xml_path))
            continue

        with open(os.path.join(save_txt_path, file + ".txt"), "w") as f:
            for index, obj in enumerate(data["object"]):
                # 获取每个object的box信息
                xmin = float(obj["bndbox"]["xmin"])
                xmax = float(obj["bndbox"]["xmax"])
                ymin = float(obj["bndbox"]["ymin"])
                ymax = float(obj["bndbox"]["ymax"])
                class_name = obj["name"]
                class_index = class_dict[class_name]  # 目标id从0开始

                # 进一步检查数据,有的标注信息中可能有w或h为0的情况,这样的数据会导致计算回归loss为nan
                if xmax <= xmin or ymax <= ymin:
                    print("Warning: in '{}' xml, there are some bbox w/h <=0".format(xml_path))
                    continue

                # 将box信息转换到yolo格式
                xcenter = xmin + (xmax - xmin) / 2
                ycenter = ymin + (ymax - ymin) / 2
                w = xmax - xmin
                h = ymax - ymin

                # 绝对坐标转相对坐标,保存6位小数
                xcenter = round(xcenter / img_width, 6)
                ycenter = round(ycenter / img_height, 6)
                w = round(w / img_width, 6)
                h = round(h / img_height, 6)

                info = [str(i) for i in [class_index, xcenter, ycenter, w, h]]

                if index == 0:
                    f.write(" ".join(info))
                else:
                    f.write("\n" + " ".join(info))


def main():
    """
    修改root_path和name_list即可
    """
    root_path = r"G:\dataset\VOCdevkit\VOC2012_sub"
    flags = ['train', 'val']
    name_list = ['aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car',
                 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike',
                 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor']

    for flag in flags:
        voc_root = os.path.join(root_path, flag)
        class_dict = dict(zip(name_list, range(0, len(name_list))))
        xml_path = os.path.join(voc_root, "xmls")
        file_names = [line.split('.')[0] for line in os.listdir(xml_path)]

        translate_info(file_names, voc_root, class_dict, flag)


if __name__ == "__main__":
    main()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129

3. 转换完成后的分布

如下:多了labels目录
在这里插入图片描述

4. 将转换完成的数据变成yolov5训练的目录

yolov5训练,需要如下格式
在这里插入图片描述

"""

转换成yolo特有的格式
"""

import os
from shutil import copy

ends = ['images', 'labels']
flags = ['train', 'val']

src_root = "/home/wyh/datasets/VOC2012_det"
dst_root = "/home/wyh/datasets/VOC_yolo"
os.makedirs(dst_root, exist_ok=True)
for end in ends:
    dst_root_end = os.path.join(dst_root, end)
    os.makedirs(dst_root_end, exist_ok=True)
    for flag in flags:
        src_root_flag = os.path.join(src_root, flag, end)
        dst_root_flag = os.path.join(dst_root_end, flag)
        os.makedirs(dst_root_flag, exist_ok=True)

        for file in os.listdir(src_root_flag):
            src_path = os.path.join(src_root_flag, file)
            dst_path = os.path.join(dst_root_flag, file)
            copy(src_path, dst_path)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

5.VOC按照train和val取出jpg和xml

VOC格式如下
在这里插入图片描述
转换后的如第一小节截图所示

"""

读取txt中的文件,复制jpg和xml到指定目录
"""
from shutil import copy
import os


txt_path = r"G:\dataset\VOCdevkit\VOC2012\ImageSets\Main\val.txt"
src_img_folder = r"G:\dataset\VOCdevkit\VOC2012\JPEGImages/"
src_xml_folder = r"G:\dataset\VOCdevkit\VOC2012\Annotations/"

dst_img_folder = r"G:\dataset\VOCdevkit\VOC2012_sub\val\images/"
dst_xml_folder = r"G:\dataset\VOCdevkit\VOC2012_sub\val\xmls/"
with open(txt_path, 'r') as fp:
    lines = fp.readlines()

files = [line.strip() for line in lines if line.strip()]
for file in files:
    src_img = src_img_folder + file + ".jpg"
    src_xml = src_xml_folder + file + ".xml"

    dst_img = dst_img_folder + file + ".jpg"
    dst_xml = dst_xml_folder + file + ".xml"

    copy(src_img, dst_img)
    copy(src_xml, dst_xml)
print(len(files))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/433340
推荐阅读