报错:运行脚本处理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)