在用Bounding Box label code标记的过程中,会出现与ground truth不相符的情况,所以需要找到这些误标的文件,进行更正。
# choose labels with with no saliency object but label the object in labels/*.txt file
#首先读取已经手动标注的txt文件,然后分别与ground truth进行比较
from PIL import Image
import shutil
import os, glob
in_dir = '001/' # 用BBox code生成的labels文件夹
image_in_dir = '../Images/001/' # the original image filepath
con_dir = '001_plane_salmap/' # ground truth文件夹
d_dir = '001_d/' # 存放误标文件的文件夹
if not os.path.exists(d_dir): os.mkdir(d_dir)
num_wronglabel = []
def main():
for files in glob.glob(in_dir+'*.txt'):
filepath, filename = os.path.split(files)
print 'name the name of the file is "%s"\n' % filename
f = open(files, "r")
line = f.readline()
# print line
salname = filename.replace('.txt', '.png')
# print 'saliencymap is "%s"\n' % salname, 'the conresponding txt file is "%s"' % filename
salmap = con_dir+salname
# print salmap
if line == '1\n':
if os.path.exists(salmap):
message = 'congratulations, the "%s" file exists, and detect the plane correctly.'
else:
message = 'no "%s" file exists, relabel it'
num_wronglabel.append(filename)
shutil.copyfile(files, os.path.join(d_dir, filename))
elif line == '0\n':
if os.path.exists(salmap):
message = 'Sorry, the "%s" file exists, you must relabel the image.'
num_wronglabel.append(filename)
im = Image.open(salmap)
im.save(os.path.join(d_dir, salname))
shutil.copyfile(files, os.path.join(d_dir, filename))
else:
message = 'congratulations, no "%s" file exists, and you label the plane correctly'
print message % salname
print num_wronglabel
# write the wrong number in labeling into .txt file
ff = open('diff.txt', 'w')
for i in num_wronglabel:
ff.write(i)
ff.write('\n')
ff.close()
# put th raw image file into the different file diff.txt
for files in glob.glob(d_dir+'*.txt'):
filepath, filename = os.path.split(files)
print filepath, filename
Imname = filename.replace('.txt', '.jpg')
print 'raw image is "%s"\n' % Imname, 'the conresponding txt file is "%s"' % filename
Im_path = image_in_dir + Imname
print Im_path
shutil.copyfile(Im_path, os.path.join(d_dir, Imname))
main()
代码是用python写的,这是我第二次自己用python写,所以可能不是很简洁,但是功能实现了,欢迎提出简化意见。