关于coco-hand转换为voc格式数据集

在以下地址下载数据集:

https://www3.cs.stonybrook.edu/~cvl/projects/hand_det_attention/

下载完,使用如下脚本处理:

	import os
	data_list = []
	
	with open("COCO-Hand-Big_annotations.txt", "r") as file:
	    # 逐行读取文件内容
	    for line in file:
	        # 去除行尾的换行符
	        line = line.strip()
	        # 将行添加到列表中
	        data_list.append(line)
	annotations = data_list
	img_dir="COCO-Hand-Big_Images"
	output_dir = "output"
	if not os.path.exists(output_dir):
	    os.makedirs(output_dir)
	for annotation in annotations:
	    filename, *coords, label = annotation.split(",")
	    filepath = os.path.join(output_dir, f"{filename.split('.')[0]}.txt")
	    x1, x2, y1, y2 =coords[:4]
	    with open(filepath, "a") as file:
	        file.write(f"0 {x1} {y1} {x2} {y2}\n")

运行程序之后得到“output”文件夹,再运行以下脚本:

import os
import xml.etree.ElementTree as ET

from PIL import Image

# 检测结果中的类别列表
classes = ["hand"]


def convert_txt_to_xml(txt_file_, xml_dir_):
    # 读取txt文件中每个检测结果,并创建XML标签
    root = ET.Element("annotation")

    # 获取图像文件路径和宽度、高度、深度
    img_path = os.path.join("COCO-Hand-Big_Image", os.path.splitext(os.path.basename(txt_file_))[0] + ".jpg")
    img = Image.open(img_path)
    width, height = img.size
    depth = 3
    path = os.path.abspath(img_path)

    # 添加各种标签信息
    ET.SubElement(root, "folder").text = "images"
    ET.SubElement(root, "filename").text = os.path.basename(img_path)
    ET.SubElement(root, "path").text = path
    source_tag = ET.SubElement(root, "source")
    ET.SubElement(source_tag, "database").text = "Unknown"
    size_tag = ET.SubElement(root, "size")
    ET.SubElement(size_tag, "width").text = str(width)
    ET.SubElement(size_tag, "height").text = str(height)
    ET.SubElement(size_tag, "depth").text = str(depth)
    ET.SubElement(root, "segmented").text = "0"

    with open(txt_file_) as f:
        for line in f:
            parts = line.strip().split()
            class_name = classes[int(parts[0])]
            x_min = int(float(parts[1]))
            y_min = int(float(parts[2]))
            x_max = int(float(parts[3]))
            y_max = int(float(parts[4]))
            object_tag = ET.SubElement(root, "object")
            ET.SubElement(object_tag, "name").text = class_name
            ET.SubElement(object_tag, "pose").text = "Unspecified"
            ET.SubElement(object_tag, "truncated").text = "1"
            ET.SubElement(object_tag, "difficult").text = "0"
            bndbox_tag = ET.SubElement(object_tag, "bndbox")
            ET.SubElement(bndbox_tag, "xmin").text = str(x_min)
            ET.SubElement(bndbox_tag, "ymin").text = str(y_min)
            ET.SubElement(bndbox_tag, "xmax").text = str(x_max)
            ET.SubElement(bndbox_tag, "ymax").text = str(y_max)

    # 创建XML文件
    if not os.path.exists(xml_dir_):
        os.makedirs(xml_dir_)
    xml_file_ = os.path.join(xml_dir_, os.path.splitext(os.path.basename(txt_file_))[0] + ".xml")
    tree = ET.ElementTree(root)
    tree.write(xml_file_)


if __name__ == "__main__":
    # 设置目录路径和文件扩展名
    dir_path = "output"
    ext = ".txt"
    xml_dir = "./datasets/annotations"
    # 遍历目录中的文件,并对扩展名为.txt的文件进行转换
    for file_name in os.listdir(dir_path):
        if file_name.endswith(ext):
            txt_file = os.path.join(dir_path, file_name)
            convert_txt_to_xml(txt_file, xml_dir)

    # 格式化
    # 遍历文件夹中的所有文件
    for filename in os.listdir(xml_dir):
        if filename.endswith('.xml'):
            # 打开XML文件并解析
            xml_file = os.path.join(xml_dir, filename)
            print(xml_file)
            dom = xml.dom.minidom.parse(xml_file)

            # 格式化XML并保存
            formatted_xml = dom.toprettyxml(indent='  ')
            with open(xml_file, 'w') as f:
                f.write(formatted_xml)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lindsayshuo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值