YOLO数据集处理将xml转txt出错

报错:运行脚本处理xml文件报错

E:\ProgramData\Anaconda3\envs\py38\python.exe E:\Project\yolov5-6.0\data\voc_label.py 
E:\Project\yolov5-6.0\data
Traceback (most recent call last):
  File "E:\Project\yolov5-6.0\data\voc_label.py", line 48, in <module>
    convert_annotation(image_id)
  File "E:\Project\yolov5-6.0\data\voc_label.py", line 37, in convert_annotation
    bb = convert((w, h), b)
  File "E:\Project\yolov5-6.0\data\voc_label.py", line 9, in convert
    dw = 1. / size[0]
ZeroDivisionError: float division by zero

Process finished with exit code 1

原因分析:XML文件中存在width以及heigh使得处理过程中出现除以0的情况

处理办法:使用以下脚本对xml文件夹进行,宽、高为0内容的查找,对应xml文件夹去除相应数据集,则可以解决问题。

import os
import xml.etree.ElementTree as ET


def search_width_element(folder_path):
    for filename in os.listdir(folder_path):
        if filename.endswith('.xml'):
            xml_path = os.path.join(folder_path, filename)
            tree = ET.parse(xml_path)
            root = tree.getroot()

            for element in root.iter():
                if element.tag == 'width' and element.text == '0':
                    print(f"在文件 '{filename}' 中找到符合格式的元素:")
                    print(ET.tostring(element, encoding='utf-8').decode('utf-8'))
                    print('-' * 40)


# 指定文件夹路径
folder_path = 'D:\\desktop\\Phone_xml'

search_width_element(folder_path)

脚本运行结果:

tip:使用脚本去除文件不同部分

import os

# 定义两个文件夹路径
folder1_path = "D:\\desktop\\Phone_xml"
folder2_path = "D:\\desktop\\Phone"

# 获取folder1中的文件名(不包括扩展名)列表
folder1_files = [os.path.splitext(file)[0] for file in os.listdir(folder1_path)]

# 获取folder2中的文件名(不包括扩展名)列表
folder2_files = [os.path.splitext(file)[0] for file in os.listdir(folder2_path)]

# 遍历folder2中的文件
for file_name in folder2_files:
    if file_name not in folder1_files:
        xml_path = os.path.join(folder2_path, file_name + ".xml")
        jpg_path = os.path.join(folder2_path, file_name + ".jpg")

        if os.path.exists(xml_path):
            print(f"Removing xml file: {file_name}.xml")
            os.remove(xml_path)

        if os.path.exists(jpg_path):
            print(f"Removing jpg file: {file_name}.jpg")
            os.remove(jpg_path)

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值