It’s not easy to be original, so please pay attention and support.
Table of contents
1. Introduction to VOC format and YOLO format?
2. Set file address and label information
3. Traverse and parse XML file information
Preface
This article describes how to convert VOC format to YOLO format. See another article for converting YOLO format to VOC format.
1. Introduction to VOC format and YOLO format?
1.VOC format
The format of the VOC data set is XML format. The following is an example:
<annotation> <folder>img</folder> <filename>pikaqiu.jpg</filename> <path>E:\cv_code\image_processing\test\img\pikaqiu.jpg</path> <source> <database>Unknown</database> </source> <size> <width>1062</width> <height>974</height> <depth>3</depth> </size> <segmented>0</segmented> <object> <name>pikaqiu</name> <pose>Unspecified</pose> <truncated>0</truncated> <difficult>0</difficult> <bndbox> <xmin>83</xmin> <ymin>74</ymin> <xmax>987</xmax> <ymax>920</ymax> </bndbox> </object> </annotation>
The information we need to use includes the picture name: <filename>pikaqiu.jpg</filename>, the width, height, and channel number information of the picture: <size> <width>1062</width> <height>974</height > <depth>3</depth> </size>, category name: <name>pikaqiu</name>, box information: <xmin>83</xmin> <ymin>74</ymin> <xmax>987< /xmax> <ymax>920</ymax>.
2.YOLO format
(class,xCenter,yCenter,w,h), which respectively represent the internal classification, the center coordinates of the label box, and the relative width and length of the label box.
2. Usage steps
1. Import the library
import xml.etree.ElementTree as ET
import os
2. Set file address and label information
# 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. Traverse and parse XML file information
# 遍历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.Write TXT information
# 获取图像尺寸
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.Total code
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 to VOC
(16 messages) Convert YOLO format to VOC format_Bo Cai Lai's Blog-CSDN Blog