import os
import csv
import xml.etree.ElementTree as ET
def csv_to_xml(csv_file_path, xml_file_path,categories):
# 创建XML根元素
root = ET.Element("annotation")
# 打开CSV文件并读取数据
with open(csv_file_path, 'r') as csv_file:
csv_reader = csv.DictReader(csv_file)
count=0
# 遍历CSV行
for row in csv_reader:
filename = row['filename']
image_width = int(row['width'])
image_height = int(row['height'])
class_name = row['class']
x_min, y_min, x_max, y_max = map(float, [row['xmin'], row['ymin'], row['xmax'], row['ymax']])
# Create the 'folder' element
if count==0:
folder=ET.SubElement(root,"folder")
folder.text="images"
# Create the 'filename' element
filename_element = ET.SubElement(root, "filename")
filename_element.text = filename
# Add 'path' element
image_path = os.path.join("D:/ultralytics-main/images", filename + ".jpg") # Assuming images are in JPG format
path = ET.SubElement(root, "path")
path.text = image_path
# Create the 'size' element
size = ET.SubElement(root , "size")
wid= ET.SubElement(size, "width")
wid.text=str(image_width)
he = ET.SubElement(size , "height")
he.text=str(image_height)
count=count+1
# Create the 'object' element
obj = ET.SubElement(root, "object")
# Create the 'name' element (class name)
name = ET.SubElement(obj, "name")
name.text = class_name
# Create the 'bndbox' element
bbox = ET.SubElement(obj, "bndbox")
# Add the 'xmin', 'ymin', 'xmax', 'ymax' elements
xmin = ET.SubElement(bbox, "xmin")
xmin.text = str(x_min)
ymin = ET.SubElement(bbox, "ymin")
ymin.text = str(y_min)
xmax = ET.SubElement(bbox, "xmax")
xmax.text = str(x_max)
ymax = ET.SubElement(bbox, "ymax")
ymax.text = str(y_max)
for key, value in row.items():
if key == 'class' and value not in categories:
categories.append(value)
# 创建XML树
tree = ET.ElementTree(root)
# 将XML保存到文件
tree.write(xml_file_path)
def batch_csv_to_xml(input_folder, output_folder):
# 确保输出文件夹存在
if not os.path.exists(output_folder):
os.makedirs(output_folder)
all_categories = [] # 用于统计所有类别
# 遍历输入文件夹中的所有文件
for filename in os.listdir(input_folder):
if filename.endswith(".csv"):
# 构建完整的文件路径
csv_file_path = os.path.join(input_folder, filename)
# 构建XML文件路径(将csv扩展名替换为xml)
xml_file_path = os.path.join(output_folder, os.path.splitext(filename)[0] + ".xml")
# 调用csv_to_xml函数进行转换,并传递类别列表
csv_to_xml(csv_file_path, xml_file_path,all_categories)
# 输出统计的类别
print("All categories found in the CSV files:")
for category in all_categories:
print(category)
# 示例用法
input_folder = "csv"
output_folder = "xml"
batch_csv_to_xml(input_folder, output_folder)
其中xls(csv)文件中有class,width,height,xmin,xmax,ymin,ymax,filename