加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
get_gt_txt.py 2.51 KB
一键复制 编辑 原始数据 按行查看 历史
Bubbliiiing 提交于 2020-12-30 16:44 . Add files via upload
#----------------------------------------------------#
# 获取测试集的ground-truth
# 具体视频教程可查看
# https://www.bilibili.com/video/BV1zE411u7Vw
#----------------------------------------------------#
import sys
import os
import glob
import xml.etree.ElementTree as ET
'''
!!!!!!!!!!!!!注意事项!!!!!!!!!!!!!
# 这一部分是当xml有无关的类的时候,下方有代码可以进行筛选!
'''
#---------------------------------------------------#
# 获得类
#---------------------------------------------------#
def get_classes(classes_path):
'''loads the classes'''
with open(classes_path) as f:
class_names = f.readlines()
class_names = [c.strip() for c in class_names]
return class_names
image_ids = open('VOCdevkit/VOC2007/ImageSets/Main/test.txt').read().strip().split()
if not os.path.exists("./input"):
os.makedirs("./input")
if not os.path.exists("./input/ground-truth"):
os.makedirs("./input/ground-truth")
for image_id in image_ids:
with open("./input/ground-truth/"+image_id+".txt", "w") as new_f:
root = ET.parse("VOCdevkit/VOC2007/Annotations/"+image_id+".xml").getroot()
for obj in root.findall('object'):
difficult_flag = False
if obj.find('difficult')!=None:
difficult = obj.find('difficult').text
if int(difficult)==1:
difficult_flag = True
obj_name = obj.find('name').text
'''
!!!!!!!!!!!!注意事项!!!!!!!!!!!!
# 这一部分是当xml有无关的类的时候,可以取消下面代码的注释
# 利用对应的classes.txt来进行筛选!!!!!!!!!!!!
'''
# classes_path = 'model_data/voc_classes.txt'
# class_names = get_classes(classes_path)
# if obj_name not in class_names:
# continue
bndbox = obj.find('bndbox')
left = bndbox.find('xmin').text
top = bndbox.find('ymin').text
right = bndbox.find('xmax').text
bottom = bndbox.find('ymax').text
if difficult_flag:
new_f.write("%s %s %s %s %s difficult\n" % (obj_name, left, top, right, bottom))
else:
new_f.write("%s %s %s %s %s\n" % (obj_name, left, top, right, bottom))
print("Conversion completed!")
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化