voc_utils.py 2.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os
import os.path as osp
import re
import random

K
Kaipeng Deng 已提交
24
__all__ = ['create_list']
25 26


27
def create_list(year_dirs, output_dir):
28
    """
K
Kaipeng Deng 已提交
29 30 31
    create following list:
        1. trainval.txt
        2. test.txt
32 33 34
    """
    trainval_list = []
    test_list = []
35 36
    for year_dir in year_dirs:
        trainval, test = _walk_voc_dir(year_dir, output_dir)
37 38 39 40
        trainval_list.extend(trainval)
        test_list.extend(test)

    random.shuffle(trainval_list)
K
Kaipeng Deng 已提交
41
    with open(osp.join(output_dir, 'trainval.txt'), 'w') as ftrainval:
42
        for item in trainval_list:
K
Kaipeng Deng 已提交
43
            ftrainval.write(item[0] + ' ' + item[1] + '\n')
44

K
Kaipeng Deng 已提交
45 46 47 48 49
    with open(osp.join(output_dir, 'test.txt'), 'w') as fval:
        ct = 0
        for item in test_list:
            ct += 1
            fval.write(item[0] + ' ' + item[1] + '\n')
50 51


52 53 54 55
def _walk_voc_dir(year_dir, output_dir):
    filelist_dir = osp.join(year_dir, 'ImageSets/Main')
    annotation_dir = osp.join(year_dir, 'Annotations')
    img_dir = osp.join(year_dir, 'JPEGImages')
56 57 58 59
    trainval_list = []
    test_list = []
    added = set()

60 61 62 63
    img_dict = {}
    for img_file in os.listdir(img_dir):
        img_dict[img_file.split('.')[0]] = img_file

64 65 66
    for _, _, files in os.walk(filelist_dir):
        for fname in files:
            img_ann_list = []
67
            if re.match('trainval\.txt', fname):
68
                img_ann_list = trainval_list
69
            elif re.match('test\.txt', fname):
70 71 72 73 74 75 76 77 78
                img_ann_list = test_list
            else:
                continue
            fpath = osp.join(filelist_dir, fname)
            for line in open(fpath):
                name_prefix = line.strip().split()[0]
                if name_prefix in added:
                    continue
                added.add(name_prefix)
W
wangguanzhong 已提交
79 80 81 82
                ann_path = osp.join(
                    osp.relpath(annotation_dir, output_dir),
                    name_prefix + '.xml')
                img_path = osp.join(
83
                    osp.relpath(img_dir, output_dir), img_dict[name_prefix])
K
Kaipeng Deng 已提交
84
                img_ann_list.append((img_path, ann_path))
85 86

    return trainval_list, test_list