这里写自定义目录标题
在用别人的yolo3的数据包时,需要修改为自己的径路可用
源码如下:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/dc95559dfb5b1bb345d3f8ade5625e0e.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/dfe34cb7d2fef3d22dfea97a994e512a.png)
上图1为我的路径,图2 为别人的路径
import xml.etree.ElementTree as ET
import os
path = r'C:\Users\Administrator\Desktop\YOLO\VOCdevkit\VOC2007\Annotations/' # xml文件存放路径
sv_path = r'C:\Users\Administrator\Desktop\YOLO\VOCdevkit\VOC2007/Annotations1/' # 修改后的xml文件存放路径
imgpath = r"C:\Users\Administrator\Desktop\YOLO\VOCdevkit\VOC2007\JPEGImages/" # 新的path路径
files = os.listdir(path) # 读取路径下所有文件名
for xmlFile in files:
if xmlFile.endswith('.xml'):
tree = ET.ElementTree(file=path + xmlFile) # 打开xml文件,送到tree解析
root = tree.getroot() # 得到文档元素对象
root[0].text = 'ImageSets'
# root[0].text是annotation下第一个子节点中内容,直接赋值替换文本内容
# 获得后缀.前的文件名(分离文件名和扩展名)
part = os.path.splitext(xmlFile)[0]
# 文件名+后缀
part1 = part + '.jpg'
root[2].text = imgpath + part1
# 替换后的内容保存在内存中需要将其写出
tree.write(sv_path + xmlFile)