在以下地址下载数据集:
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)