首页 > 解决方案 > 使用 FDDB 折叠 txt 文件创建 JSON

问题描述

TL;DR 即使我使用了 while 循环,该函数也没有从所有行返回值

FDDB fold txt 文件的数据格式如下。首先是图像文件路径,然后下一行是面数,下面几行是每个面的椭圆的参数。

2002/08/11/big/img_591 
1
123.583300 85.549500 1.265839 269.693400 161.781200  1 
2002/08/26/big/img_265 
3
67.363819 44.511485 -1.476417 105.249970 87.209036  1
41.936870 27.064477 1.471906 184.070915 129.345601  1
70.993052 43.355200 1.370217 340.894300 117.498951  1 
2002/07/19/big/img_423 
1
87.080955 59.379319 1.550861 255.383099 133.767857  1 
2002/08/24/big/img_490 
1
54.692105 35.056825 -1.384924 145.665694 78.101005  1

我编写了以下代码以可以返回特定参数(例如 center_x 或 angle)的方式提取数据

def fddb_dataset(annotations):
    for d in os.listdir(annotations):
        if fnmatch(d, 'FDDB-fold-*-ellipseList.txt'):
            with open(os.path.join(annotations, d), 'rt') as f:
                lines = [line.rstrip('\n') for line in f]
                lineId = 0
                while lineId < len(lines):
                    # Image
                    imgPath = lines[lineId]
                    lineId += 1
                    # Faces
                    numFaces = int(lines[lineId])
                    lineId += 1
                    for i in range(numFaces):
                        params = [float(v) for v in lines[lineId].split()]
                        lineId += 1
                        rad_x, rad_y, angle, center_x, center_y, detection_score = params
                        return imgPath, rad_x, rad_y, angle, center_x, center_y, detection_score

问题是即使我尝试使用 .append() 也只会返回一行

最终,我想将数据集转换为字典到 JSON。

更多信息:我知道 FDDB 评估有许多问题和教程。但是,我不打算根据注释评估我的算法。我计划将这些注释用作基础事实来训练 Mask RCNN。

标签: pythonjsondictionarydatasettxt

解决方案


我不是一次提取所有 txt 文件,而是一次从一个 txt 文件中提取数据

def fddb_dataset(annotations):
    with open(annotations) as f:
        imgPath = [] 
        rad_x = []
        rad_y = []
        angle = []
        center_x = []
        center_y = []
        detection_score = []
        lineId = 0
        lines = f.readlines()
        while lineId < len(lines):
            Path = lines[lineId]
            lineId += 1
            # Faces
            numFaces = int(lines[lineId])
            for i in range(numFaces):
                lineId += 1
                params = [float(v) for v in lines[lineId].split()]
                radx, rady, ang, centerx, centery, score = params
                imgPath.append(Path) 
                rad_x.append(radx)
                rad_y.append(rady)
                angle.append(ang)
                center_x.append(centerx)
                center_y.append(centery)
                detection_score.append(score)
                
            lineId += 1
        data = pd.DataFrame(
                {'imgPath': imgPath,
                 'rad_x': rad_x,
                 'rad_y': rad_y,
                 'angle': angle,
                 'center_x': center_x,
                 'center_y': center_y,
                 'detection_score': detection_score
                })
            
        return data  

推荐阅读