No es fácil ser original, así que preste atención y apoyo.
Tabla de contenido
1. ¿Introducción al formato VOC y al formato YOLO?
2. Establecer la dirección del archivo y la información de la etiqueta
3. Recorrer y analizar la información del archivo XML
Prefacio
Este artículo describe cómo convertir el formato VOC al formato YOLO. Consulte otro artículo para convertir el formato YOLO al formato VOC.
1. ¿Introducción al formato VOC y al formato YOLO?
1.formato COV
El formato del conjunto de datos VOC es formato XML. El siguiente es un ejemplo:
<anotación> <carpeta>img</carpeta> <nombre de archivo>pikaqiu.jpg</nombre de archivo> <ruta>E:\cv_code\image_processing\test\img\pikaqiu.jpg</ruta> <fuente> <base de datos>Desconocido</base de datos> </fuente> <tamaño> <ancho>1062</ancho> <alto>974</alto> <profundidad>3</profundidad> </tamaño > <segmentado>0</segmentedo> <objeto> <nombre>pikaqiu</nombre> <pose>Sin especificar</pose> <truncado>0</truncado> <difícil>0</difícil> <bndbox> <xmin> 83</xmin> <ymin>74</ymin> <xmax>987</xmax><xmin>83</xmin> <ymin>74</ymin> <xmax>987</xmax><xmin>83</xmin> <ymin>74</ymin> <xmax>987</xmax> <ymax>920</ymax> </bndbox></bndbox></bndbox> </objeto> </annotación>
La información que necesitamos usar incluye el nombre de la imagen: <filename>pikaqiu.jpg</filename>, el ancho, alto y la información del número de canal de la imagen: <size> <width>1062</width> <height>974 </altura > <profundidad>3</profundidad> </tamaño>, nombre de categoría: <nombre>pikaqiu</nombre>, información del cuadro: <xmin>83</xmin> <ymin>74</ymin> <xmax >987</xmax> <ymax>920</ymax>.
2.Formato YOLO
(clase, xCenter, yCenter, w, h), que representan respectivamente la clasificación interna, las coordenadas centrales del cuadro de etiqueta y el ancho y largo relativos del cuadro de etiqueta.
2. Pasos de uso
1. Importar la biblioteca
import xml.etree.ElementTree as ET
import os
2. Establecer la dirección del archivo y la información de la etiqueta
# xml文件所在目录
xml_dir = "E:/cv_code/image_processing/aug_datasets/Annotations/"
# Yolo格式文件保存目录
yolo_dir = "E:/cv_code/image_processing/aug_datasets/label/"
# 类别名称和数字标签的映射
class_map = {"pikaqiu": 0}
3. Recorrer y analizar la información del archivo XML
# 遍历XML文件夹中的所有文件
for xml_file in os.listdir(xml_dir):
if not xml_file.endswith(".xml"):
continue
# 解析XML文件
tree = ET.parse(os.path.join(xml_dir, xml_file))
root = tree.getroot()
4.Escribir información TXT
# 获取图像尺寸
size = root.find("size")
width = int(size.find("width").text)
height = int(size.find("height").text)
# 遍历所有目标
for obj in root.iter("object"):
# 获取类别和边界框坐标
cls_name = obj.find("name").text
if cls_name not in class_map:
continue
cls_id = class_map[cls_name]
bbox = obj.find("bndbox")
xmin = float(bbox.find("xmin").text)
ymin = float(bbox.find("ymin").text)
xmax = float(bbox.find("xmax").text)
ymax = float(bbox.find("ymax").text)
# 计算归一化坐标
x = (xmin + xmax) / (2 * width)
y = (ymin + ymax) / (2 * height)
w = (xmax - xmin) / width
h = (ymax - ymin) / height
# 将信息写入Yolo格式文件
yolo_file = os.path.splitext(xml_file)[0] + ".txt"
with open(os.path.join(yolo_dir, yolo_file), "a") as f:
f.write(f"{cls_id} {x:.6f} {y:.6f} {w:.6f} {h:.6f}\n")
5.Código total
import xml.etree.ElementTree as ET
import os
# xml文件所在目录
xml_dir = "E:/cv_code/image_processing/aug_datasets/Annotations/"
# Yolo格式文件保存目录
yolo_dir = "E:/cv_code/image_processing/aug_datasets/label/"
# 类别名称和数字标签的映射
class_map = {"pikaqiu": 0}
# 遍历XML文件夹中的所有文件
for xml_file in os.listdir(xml_dir):
if not xml_file.endswith(".xml"):
continue
# 解析XML文件
tree = ET.parse(os.path.join(xml_dir, xml_file))
root = tree.getroot()
# 获取图像尺寸
size = root.find("size")
width = int(size.find("width").text)
height = int(size.find("height").text)
# 遍历所有目标
for obj in root.iter("object"):
# 获取类别和边界框坐标
cls_name = obj.find("name").text
if cls_name not in class_map:
continue
cls_id = class_map[cls_name]
bbox = obj.find("bndbox")
xmin = float(bbox.find("xmin").text)
ymin = float(bbox.find("ymin").text)
xmax = float(bbox.find("xmax").text)
ymax = float(bbox.find("ymax").text)
# 计算归一化坐标
x = (xmin + xmax) / (2 * width)
y = (ymin + ymax) / (2 * height)
w = (xmax - xmin) / width
h = (ymax - ymin) / height
# 将信息写入Yolo格式文件
yolo_file = os.path.splitext(xml_file)[0] + ".txt"
with open(os.path.join(yolo_dir, yolo_file), "a") as f:
f.write(f"{cls_id} {x:.6f} {y:.6f} {w:.6f} {h:.6f}\n")
YOLO a VOC
(16 mensajes) Convierta el formato YOLO al formato VOC_Blog-CSDN de Bo Cai Lai