提取用Bounding box label标记后的误标文件

在用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写,所以可能不是很简洁,但是功能实现了,欢迎提出简化意见。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值