-
Notifications
You must be signed in to change notification settings - Fork 2
/
update_xmls.py
41 lines (31 loc) · 1.05 KB
/
update_xmls.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# -*- coding: utf-8 -*-
"""
@Author : wyl
@Email : [email protected]
"""
import os
import xml.etree.ElementTree as ET
current_path = os.path.abspath(__file__)
folder_path = os.path.split(current_path)
#图片路径
ann_dir="Annotations"
saved_path="rename_xml"
if not os.path.exists(saved_path):
os.makedirs(saved_path)
anns=os.listdir(ann_dir)
for ann in anns:
updateTree = ET.parse(os.path.join(ann_dir,ann)) # 读取待修改文件
root = updateTree.getroot()
sub = root.find("folder")
sub.text = "VOC2007"
sub = root.find("filename")
sub.text = ann.split(".")[0]+".jpeg"
try:
sub = root.find("path")
sub.text =folder_path[0]+"/JPEGImages/"+ ann.split(".")[0] + ".jpeg"
except:
newEle = ET.Element("path") # 创建新节点并添加为root的子节点
# newEle.attrib = {"name": "NewElement", "age": "20"}
newEle.text = folder_path[0]+"/JPEGImages/"+ ann.split(".")[0] + ".jpeg"
root.append(newEle)
updateTree.write(os.path.join(saved_path,ann)) # 写回原文件