import os
import xml.etree.ElementTree as ET
def find_xmls_with_class(xml_dir, class_name):
xml_files_with_class = []
# 文件名(不带扩展名)
file_names = [name.split('.')[0] for name in os.listdir(xml_dir)]
for file_name in file_names:
xml_path = os.path.join(xml_dir, file_name+'.xml')
tree = ET.parse(xml_path)
root = tree.getroot()
for obj in root.findall('object'):
name = obj.find('name').text
if name == class_name:
xml_files_with_class.append(file_name+'.xml')
break
return xml_files_with_class
# 你的目录
dir_path = './'
# 指定类别名称
class_name = 'airplane'
# 获取标注文件
xml_dir = os.path.join(dir_path, 'Annotations')
xml_files_with_class = find_xmls_with_class(xml_dir, class_name)
# 输出包含该类别的xml文件
for xml_file in xml_files_with_class:
print(xml_file)
05-24
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交