将照片重命名为000001的形式,适应VOC数据集的格式要求,当然仅是看着美观,实际上也可以不重命名。
from xml.etree.ElementTree import ElementTree
from os import walk, path
import cv2
import os
def read_xml(in_path):
tree = ElementTree()
tree.parse(in_path)
return tree
def write_xml(tree, out_path):
tree.write(out_path, encoding="utf-8", xml_declaration=True)
def get_path_prex(rootdir):
data_path = []
prefixs = []
for root, dirs, files in walk(rootdir, topdown=True):
for name in files:
pre, ending = path.splitext(name)
if ending != ".xml":
continue
else:
data_path.append(path.join(root, name))
prefixs.append(pre)
return data_path, prefixs
if __name__ == "__main__":
# build files which will be used in VOC2007
if not os.path.exists("Annotations_out"):
os.mkdir("Annotations_out")
if not os.path.exists("JPEGImages_out"):
os.mkdir("JPEGImages_out")
xml_paths, prefixs = get_path_prex("labels")
for i in range(len(xml_paths)):
# rename and save the corresponding xml
tree = read_xml(xml_paths[i])
# save output xml, 000001.xml
write_xml(tree, "Annotations/{}.xml".format("%06d" % (i + 1)))
# rename and save the corresponding image
img_pre = prefixs[i] + ".jpeg"
root = os.getcwd() + '/images/'
img_path = path.join(root, img_pre)
img = cv2.imread(img_path)
# save output jpg, 000001.jpg
cv2.imwrite('JPEGImages/{}.jpg'.format("%06d" % (i + 1)), img)
这段代码实现了一个功能,将指定目录下的图片和XML文件按照VOC数据集的要求进行重命名。首先遍历目录,找到所有的XML文件,读取并保存为新的XML文件名(例如000001.xml),同时获取对应的图片文件名,使用OpenCV读取并保存为重命名后的JPEG图片(例如000001.jpg)。整个过程创建了两个新的子目录'Annotations_out'和'JPEGImages_out'来存放处理后的文件。

345

被折叠的 条评论
为什么被折叠?



