[python]代码库
#!/usr/bin/python
'''
此文件用于整理网络上搜集的数据集的xml文件,整理后方便用于训练。
'''
import os # 文件操作相关
import xml.etree.ElementTree as ET # xml文件操作相关
import cv2
# PRINT_FLAG=True
PRINT_FLAG = False
# 批量修改整个文件夹所有的xml文件
def change_all_xml(xmlfilepath, string1):
total_xml = os.listdir(xmlfilepath) # 用于返回指定的文件夹包含的文件或文件夹的名字的列表。
num = len(total_xml) # xml文件个数
print(num)
print(total_xml[0])
for xmlfile in total_xml:
# print("**********************************************************************************************************")
# print(xmlfile)
in_file = open('%s/%s' % (xmlfilepath, xmlfile), encoding='UTF-8')
# print(in_file)
tree = ET.parse(in_file)
# print(tree)
root = tree.getroot()
# print(root)
obj = root.find('path') # 找到filename标签,
# print(obj)
path_text = obj.text
# print(path_text)
# end = "."
end = "."
string3 = path_text[path_text.rfind(end):] # 在strint1中查找最后一个反斜杠\后面的字符,图片名称
# print("string3:", string3)
end = "."
# string4 = string3[string3.rfind(end):] # 在strint1中查找最后一个正斜杠/后面的字符,图片名称
# print("string4:", string4)
# print("xmlfile:", xmlfile)
# print("string1:", string1)
end = "."
string2 = xmlfile[:xmlfile.rfind(end)] # 在strint1中查找最后一个正斜杠/后面的字符,图片名称
# print("string2:", string2)
path_text_1 = string1 + string2 + string3
try:
# print("path_text_1:", path_text_1)
image = cv2.imread(path_text_1, 1)
image.shape
# cv2.imshow("aa",image)
# cv2.waitKey(2000)
# print('Open image ok! ')
except:
print('1111:Open image Error! Try again!')
print("path_text_1:", path_text_1)
string3 = '.jpg'
path_text_1 = string1 + string2 + string3
print("path_text_1:", path_text_1)
try:
# print("path_text_1:", path_text_1)
image = cv2.imread(path_text_1, 1)
image.shape
# cv2.imshow("aa",image)
# cv2.waitKey(2000)
print('Open image ok! ')
print('*****************************************************************************')
except:
print('222 : Open image Error! Try again!')
string3 = ".jpg"
path_text_1 = string1 + string2 + string3
print("path_text_1:", path_text_1)
# print("path_text_1:",path_text_1)
obj.text = path_text_1 # 修改标签内容
# tree.write('%s/%s' % (xmlfilepath, xmlfile)) # 保存修改
obj_2 = root.find('filename') # 找到filename标签
#
path_text_2 = string2 + string3
#
obj_2.text = path_text_2 # 修改标签内容
if PRINT_FLAG == True:
print("string1:", string1)
print("string2:", string2)
print("string3:", string3)
print("path_text_1:", path_text_1)
print("obj_2:", obj_2)
print("path_text_2:", path_text_2)
tree.write('%s/%s' % (xmlfilepath, xmlfile)) # 保存修改
# xmlfilepath = 'temp' # xml文件保存地址
xmlfilepath = 'helmet_xml' # xml文件保存地址
# 要修改的内容
string1 = 'E:\\1_Training_picture\\6_helmet\\helmet_train\\'
change_all_xml(xmlfilepath, string1)
print("ok")