voc_loader.py 10.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
# 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.

import os
import numpy as np

import xml.etree.ElementTree as ET


21 22 23 24
def get_roidb(anno_path,
              sample_num=-1,
              cname2cid=None,
              with_background=True):
25 26 27 28 29 30 31 32 33 34 35
    """
    Load VOC records with annotations in xml directory 'anno_path'

    Notes:
    ${anno_path}/ImageSets/Main/train.txt must contains xml file names for annotations
    ${anno_path}/Annotations/xxx.xml must contain annotation info for one record

    Args:
        anno_path (str): root directory for voc annotation data
        sample_num (int): number of samples to load, -1 means all
        cname2cid (dict): the label name to id dictionary
36 37 38
        with_background (bool): whether load background as a class.
                                if True, total class number will
                                be 81. default True
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

    Returns:
        (records, catname2clsid)
        'records' is list of dict whose structure is:
        {
            'im_file': im_fname, # image file name
            'im_id': im_id, # image id
            'h': im_h, # height of image
            'w': im_w, # width
            'is_crowd': is_crowd,
            'gt_class': gt_class,
            'gt_bbox': gt_bbox,
            'gt_poly': gt_poly,
        }
        'cname2id' is a dict to map category name to class id
    """

    txt_file = anno_path
    part = txt_file.split('ImageSets')
    xml_path = os.path.join(part[0], 'Annotations')
    assert os.path.isfile(txt_file) and \
        os.path.isdir(xml_path), 'invalid xml path'

    records = []
    ct = 0
    existence = False if cname2cid is None else True
    if cname2cid is None:
        cname2cid = {}

    # mapping category name to class id
    # background:0, first_class:1, second_class:2, ...
    with open(txt_file, 'r') as fr:
        while True:
            line = fr.readline()
            if not line:
                break
            fname = line.strip() + '.xml'
            xml_file = os.path.join(xml_path, fname)
            if not os.path.isfile(xml_file):
                continue
            tree = ET.parse(xml_file)
            im_fname = tree.find('filename').text
            if tree.find('id') is None:
                im_id = np.array([ct])
            else:
                im_id = np.array([int(tree.find('id').text)])

            objs = tree.findall('object')
            im_w = float(tree.find('size').find('width').text)
            im_h = float(tree.find('size').find('height').text)
            gt_bbox = np.zeros((len(objs), 4), dtype=np.float32)
            gt_class = np.zeros((len(objs), 1), dtype=np.int32)
            gt_score = np.ones((len(objs), 1), dtype=np.float32)
            is_crowd = np.zeros((len(objs), 1), dtype=np.int32)
            difficult = np.zeros((len(objs), 1), dtype=np.int32)
            for i, obj in enumerate(objs):
                cname = obj.find('name').text
                if not existence and cname not in cname2cid:
                    # the background's id is 0, so need to add 1.
98
                    cname2cid[cname] = len(cname2cid) + int(with_background)
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
                elif existence and cname not in cname2cid:
                    raise KeyError(
                        'Not found cname[%s] in cname2cid when map it to cid.' %
                        (cname))
                gt_class[i][0] = cname2cid[cname]
                _difficult = int(obj.find('difficult').text)
                x1 = float(obj.find('bndbox').find('xmin').text)
                y1 = float(obj.find('bndbox').find('ymin').text)
                x2 = float(obj.find('bndbox').find('xmax').text)
                y2 = float(obj.find('bndbox').find('ymax').text)
                x1 = max(0, x1)
                y1 = max(0, y1)
                x2 = min(im_w - 1, x2)
                y2 = min(im_h - 1, y2)
                gt_bbox[i] = [x1, y1, x2, y2]
                is_crowd[i][0] = 0
                difficult[i][0] = _difficult
            voc_rec = {
                'im_file': im_fname,
                'im_id': im_id,
                'h': im_h,
                'w': im_w,
                'is_crowd': is_crowd,
                'gt_class': gt_class,
                'gt_score': gt_score,
                'gt_bbox': gt_bbox,
                'gt_poly': [],
                'difficult': difficult
            }
            if len(objs) != 0:
                records.append(voc_rec)

            ct += 1
            if sample_num > 0 and ct >= sample_num:
                break
    assert len(records) > 0, 'not found any voc record in %s' % (anno_path)
    return [records, cname2cid]


138 139 140 141
def load(anno_path,
         sample_num=-1,
         use_default_label=True,
         with_background=True):
142 143 144 145 146 147 148 149 150 151 152 153
    """
    Load VOC records with annotations in
    xml directory 'anno_path'

    Notes:
    ${anno_path}/ImageSets/Main/train.txt must contains xml file names for annotations
    ${anno_path}/Annotations/xxx.xml must contain annotation info for one record

    Args:
        @anno_path (str): root directory for voc annotation data
        @sample_num (int): number of samples to load, -1 means all
        @use_default_label (bool): whether use the default mapping of label to id
154 155 156
        @with_background (bool): whether load background as a class.
                                 if True, total class number will
                                 be 81. default True
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179

    Returns:
        (records, catname2clsid)
        'records' is list of dict whose structure is:
        {
            'im_file': im_fname, # image file name
            'im_id': im_id, # image id
            'h': im_h, # height of image
            'w': im_w, # width
            'is_crowd': is_crowd,
            'gt_class': gt_class,
            'gt_bbox': gt_bbox,
            'gt_poly': gt_poly,
        }
        'cname2id' is a dict to map category name to class id
    """

    txt_file = anno_path
    part = txt_file.split('ImageSets')
    xml_path = os.path.join(part[0], 'Annotations')
    assert os.path.isfile(txt_file) and \
        os.path.isdir(xml_path), 'invalid xml path'

180 181 182 183 184
    # mapping category name to class id
    # if with_background is True:
    #   background:0, first_class:1, second_class:2, ...
    # if with_background is False:
    #   first_class:0, second_class:1, ...
185 186 187 188 189 190
    records = []
    ct = 0
    cname2cid = {}
    if not use_default_label:
        label_path = os.path.join(part[0], 'ImageSets/Main/label_list.txt')
        with open(label_path, 'r') as fr:
191
            label_id = int(with_background)
192 193 194 195
            for line in fr.readlines():
                cname2cid[line.strip()] = label_id
                label_id += 1
    else:
196
        cname2cid = pascalvoc_label(with_background)
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258

    with open(txt_file, 'r') as fr:
        while True:
            line = fr.readline()
            if not line:
                break
            fname = line.strip() + '.xml'
            xml_file = os.path.join(xml_path, fname)
            if not os.path.isfile(xml_file):
                continue
            tree = ET.parse(xml_file)
            im_fname = tree.find('filename').text
            if tree.find('id') is None:
                im_id = np.array([ct])
            else:
                im_id = np.array([int(tree.find('id').text)])

            objs = tree.findall('object')
            im_w = float(tree.find('size').find('width').text)
            im_h = float(tree.find('size').find('height').text)
            gt_bbox = np.zeros((len(objs), 4), dtype=np.float32)
            gt_class = np.zeros((len(objs), 1), dtype=np.int32)
            gt_score = np.ones((len(objs), 1), dtype=np.float32)
            is_crowd = np.zeros((len(objs), 1), dtype=np.int32)
            difficult = np.zeros((len(objs), 1), dtype=np.int32)
            for i, obj in enumerate(objs):
                cname = obj.find('name').text
                gt_class[i][0] = cname2cid[cname]
                _difficult = int(obj.find('difficult').text)
                x1 = float(obj.find('bndbox').find('xmin').text)
                y1 = float(obj.find('bndbox').find('ymin').text)
                x2 = float(obj.find('bndbox').find('xmax').text)
                y2 = float(obj.find('bndbox').find('ymax').text)
                x1 = max(0, x1)
                y1 = max(0, y1)
                x2 = min(im_w - 1, x2)
                y2 = min(im_h - 1, y2)
                gt_bbox[i] = [x1, y1, x2, y2]
                is_crowd[i][0] = 0
                difficult[i][0] = _difficult
            voc_rec = {
                'im_file': im_fname,
                'im_id': im_id,
                'h': im_h,
                'w': im_w,
                'is_crowd': is_crowd,
                'gt_class': gt_class,
                'gt_score': gt_score,
                'gt_bbox': gt_bbox,
                'gt_poly': [],
                'difficult': difficult
            }
            if len(objs) != 0:
                records.append(voc_rec)

            ct += 1
            if sample_num > 0 and ct >= sample_num:
                break
    assert len(records) > 0, 'not found any voc record in %s' % (anno_path)
    return [records, cname2cid]


259
def pascalvoc_label(with_background=True):
260
    labels_map = {
K
Kaipeng Deng 已提交
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
	'aeroplane': 1,
	'bicycle': 2,
	'bird': 3,
	'boat': 4,
	'bottle': 5,
	'bus': 6,
	'car': 7,
	'cat': 8,
	'chair': 9,
	'cow': 10,
	'diningtable': 11,
	'dog': 12,
	'horse': 13,
	'motorbike': 14,
	'person': 15,
	'pottedplant': 16,
	'sheep': 17,
	'sofa': 18,
	'train': 19,
	'tvmonitor': 20
281
    }
282 283
    if not with_background:
        labels_map = {k: v - 1 for k, v in labels_map.items()}
284
    return labels_map