网上大多数都是下面方法:
https://blog.csdn.net/weixin_39881922/article/details/85070808
但是遇到某些不规则图片名时会报错:
使用如下代码便不会有任何问题:
import xml.etree.ElementTree as ET
import os
import shutil
def copy(xml_path,jpg_path,aim_path,aimxml_path):
filelist = os.listdir(xml_path)
# 打开xml文档
for xmlfile in filelist:
jpgname = xmlfile.replace("xml","jpg")
doc = ET.parse(xml_path + xmlfile)
root = doc.getroot()
names = [element.find('name').text for element in root.findall('object')]
if "person" in names:
#if "person" in names or "dog" in names:
shutil.copy(xml_path+xmlfile,aimxml_path)
shutil.copy(jpg_path+jpgname,aim_path)
if __name__ == '__main__':
xml_path = "./Annotations/"
jpg_path = "./JPEGImages/"
aim_path = "./JPEGImages1/"
aimxml_path = "./Annotations1/"
copy(xml_path,jpg_path,aim_path,aimxml_path)