对yolov3的测试代码进行了一点修改,将要检测的图片批量输入,再将检测结果分别保存到对应的txt文件中。但如果如果没有检测到框,就不会生成结果txt。
import os
path_new = './input/detection-results/'
path ='./input/ground-truth/'
list = os.listdir (path)
list2 = os.listdir (path_new)
for j in range(len(list)):
name_d = [str (i) for i in list[j]]
name = ''.join(name_d)
# name2 = name.replace('png','txt')
if name in list2:
continue
else:
# os.remove(path+name)
print(path+name)
目的就是检测出哪个图片没有被检测出目标,可以考虑把它删除掉或者移动到其他文件夹,便于后续分析。
import os
path = './input/ground-truth/'
path_new ='./input/ground-truth/'
list = os.listdir (path)
for j in range(len(list)):
name_d = [str (i) for i in list[j]]
name = ''.join (name_d)
new_name = path_new+name
f = open (path + name)
fns = [line.strip () for line in f]
for i in range(len(fns)):
line = fns[i]
lines = line.split (' ')
x,y,w,h,cls = float(lines[0]),float(lines[1]),float(lines[2]),float(lines[3]),lines[4]
x1=x-w/2
y1=y-h/2
x2=x+w/2
y2=y+h/2
with open (new_name, 'a+') as g:
i = cls +' '+ str(x1) +' '+str(y1) + ' ' + str(x2) +' '+str(y2)+'\n'
g.write(str(i))
print(str(i))
g.close()
因为我的数据集格式和这个算法默认的格式不太匹配,所以把它规范一下~