文本检测训练结果评测

1. 评测链接地址参照:http://rrc.cvc.uab.es/?ch=4&com=mymethods&task=1

2. 将测试的输出结果压缩为zip包,代码如下:

import os
import xml.etree.ElementTree as ET
import numpy as np
import zipfile
from functools import reduce

#src_dir = '/home/hhh/data/labelData_6w/ImageSets/lit.txt'
src_dir = '/home/hhh/data/labelData_6w/ImageSets/test_complex.txt'
eval_dir = "/home/hhh/code/EAST/output_complex"
dst_dir = "/home/hhh/data/labelData_6w/Annotations_det/complex"
# generate zip file
eval_output='/home/hhh/complex_submit.zip'
gt_output = '/home/hhh/complex_gt.zip'

def polygon_area(poly):
    '''
    compute area of a polygon
    :param poly:
    :return:
    '''
    edge = [
        (poly[1][0] - poly[0][0]) * (poly[1][1] + poly[0][1]),
        (poly[2][0] - poly[1][0]) * (poly[2][1] + poly[1][1]),
        (poly[3][0] - poly[2][0]) * (poly[3][1] + poly[2][1]),
        (poly[0][0] - poly[3][0]) * (poly[0][1] + poly[3][1])
    ]
    return np.sum(edge)/2.


if __name__ == "__main__":
    if not os.path.exists(dst_dir):
        os.makedirs(dst_dir)
    with open(src_dir, 'r') as f:
        xml_list = [os.path.join('/home/data/vocData/labelData_6w/Annotations', ff.strip() + '.xml') for ff in f.readlines()]
    for xmlfile in xml_list:
        pre, back = os.path.splitext(os.path.basename(xmlfile))
        tree = ET.parse(xmlfile)
        root = tree.getroot()

        with open(os.path.join(dst_dir, pre+".txt"), "w") as f:
            for obj in root.iter('object'):
                box = obj.find('bndbox')
                x1 = int(box.find('x1').text)
                y1 = int(box.find('y1').text)
                x4 = int(box.find('x2').text)
                y4 = int(box.find('y2').text)
                x3 = int(box.find('x3').text)
                y3 = int(box.find('y3').text)
                x2 = int(box.find('x4').text)
                y2 = int(box.find('y4').text)
                label = obj.find('class').text
                if label is None:
                    label = '###'
                if '\n' in label:
                    label = reduce(lambda x,y:x+y, label.split('\n'))
                text_poly = np.array([[x1, y1], [x2, y2], [x3, y3], [x4, y4]])
                area = polygon_area(text_poly)
                if area > 0:
                    print('gt poly in wrong direction')
                    poly = text_poly[(0, 3, 2, 1), :]
                    (x1, y1), (x2, y2), (x3, y3), (x4, y4) = poly.tolist() 
                line = str(x1)+','+str(y1)+','+str(x2)+','+str(y2)+','+str(x3)+','+str(y3)+','+str(x4)+','+str(y4)+','+ label +'\n'

                f.write(line)
    
    # zip submit file
    arch = zipfile.ZipFile(eval_output,'w')
    files = [os.path.join(eval_dir,f) for f in os.listdir(eval_dir) if f.endswith('.txt')]
    for txtfile in files:
        with open(txtfile, 'r') as f:
            s=''
            for line in f.readlines():
                x1, y1, x2, y2, x3, y3, x4, y4 = list(map(int,line.strip().split(',')))
                text_poly = np.array([[x1, y1], [x2, y2], [x3, y3], [x4, y4]])
                area = polygon_area(text_poly)                                 
                if area > 0:                                                   
                    print('submit poly in wrong direction')                           
                    poly = text_poly[(0, 3, 2, 1), :]                          
                    (x1, y1), (x2, y2), (x3, y3), (x4, y4) = poly.tolist()     
                newline = str(x1)+','+str(y1)+','+str(x2)+','+str(y2)+','+str(x3)+','+str(y3)+','+str(x4)+','+str(y4)+'\n'
                s += newline                                                                                  
        arch.writestr(os.path.basename(txtfile), s)                                                  
    
    # zip gt file
    arch2 = zipfile.ZipFile(gt_output,'w')
    names = [f.split('/')[-1] for f in files]
    for text in [os.path.join(dst_dir,f) for f in os.listdir(dst_dir) if f.endswith('.txt')]:
        if text.split('/')[-1] in names:
            with open(text, 'r') as f:
                s=''
                for line in f.readlines():
                    x1, y1, x2, y2, x3, y3, x4, y4 = list(map(int,line.strip().split(',')[0:8]))
                    if len(line.split(',')) == 9:
                        label = line.strip().split(',')[-1]
                    else:
                        lable = reduce(lambda x,y:x+y,line.strip().split(',')[8:])
                    newline = str(x1)+','+str(y1)+','+str(x2)+','+str(y2)+','+str(x3)+','+str(y3)+','+str(x4)+','+str(y4)+',' + label +'\n'
                    s += newline
            arch2.writestr(os.path.basename(text),s)

3. 将压缩包转换测试所需要的格式,代码如下

import zipfile
import os
import numpy as np

src_gt_path = '/home/hhh/complex_gt.zip'
src_submit_path = '/home/hhh/complex_submit.zip'
dst_gt_path = '/home/hhh/tmp/complex_gt_result.zip'
dst_submit_path = '/home/hhh/tmp/complex_submit_result.zip'

try:
    archive=zipfile.ZipFile(src_gt_path, mode='r', allowZip64=True)
    arc=zipfile.ZipFile(src_submit_path, mode='r', allowZip64=True)
except :
    raise Exception('Error loading the ZIP archive.')

if len(archive.namelist()) != len(arc.namelist()):
    raise Exception('number of  file in zipfile is equal.')

print('convert gt data ...')
name_dict = {}
idx=1
zip_gt_result = zipfile.ZipFile(dst_gt_path, mode='w')
for name in archive.namelist():
    f = archive.open(name, 'r')
    name_dict[name] = idx
    s = ''
    for data in f.readlines():
    #print(str(data, encoding='utf-8'))
        s += str(data, encoding='utf-8')
    zip_gt_result.writestr('gt_img_' + str(idx) + '.txt', s)
    idx = idx + 1

print('convert submit data ...')
zip_submit_result = zipfile.ZipFile(dst_submit_path, mode='w')
for name in arc.namelist():
    #print(name)
    ff = arc.open(name, 'r')
    s2 = ''
    for data in ff.readlines():
        #print(str(data, encoding='utf-8'))
        s2 += str(data,encoding='utf-8')
    zip_submit_result.writestr('res_img_' + str(name_dict[name]) + '.txt', s2)

4. 最后,生成完需要的压缩包格式后进行测试,执行下面代码得到结果

python script.py -g=../tmp/complex_gt_result.zip -s=../tmp/complex_submit_result.zip -o=../tmp 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值