| 163 | |
| 164 | |
| 165 | def coco(settings, coco_api, file_list, mode, batch_size, shuffle, data_dir): |
| 166 | from pycocotools.coco import COCO |
| 167 | |
| 168 | def reader(): |
| 169 | if mode == 'train' and shuffle: |
| 170 | np.random.shuffle(file_list) |
| 171 | batch_out = [] |
| 172 | for image in file_list: |
| 173 | image_name = image['file_name'] |
| 174 | image_path = os.path.join(data_dir, image_name) |
| 175 | if not os.path.exists(image_path): |
| 176 | raise ValueError("%s is not exist, you should specify " |
| 177 | "data path correctly." % image_path) |
| 178 | im = Image.open(image_path) |
| 179 | if im.mode == 'L': |
| 180 | im = im.convert('RGB') |
| 181 | im_width, im_height = im.size |
| 182 | im_id = image['id'] |
| 183 | |
| 184 | # layout: category_id | xmin | ymin | xmax | ymax | iscrowd |
| 185 | bbox_labels = [] |
| 186 | annIds = coco_api.getAnnIds(imgIds=image['id']) |
| 187 | anns = coco_api.loadAnns(annIds) |
| 188 | for ann in anns: |
| 189 | bbox_sample = [] |
| 190 | # start from 1, leave 0 to background |
| 191 | bbox_sample.append(float(ann['category_id'])) |
| 192 | bbox = ann['bbox'] |
| 193 | xmin, ymin, w, h = bbox |
| 194 | xmax = xmin + w |
| 195 | ymax = ymin + h |
| 196 | bbox_sample.append(float(xmin) / im_width) |
| 197 | bbox_sample.append(float(ymin) / im_height) |
| 198 | bbox_sample.append(float(xmax) / im_width) |
| 199 | bbox_sample.append(float(ymax) / im_height) |
| 200 | bbox_sample.append(float(ann['iscrowd'])) |
| 201 | bbox_labels.append(bbox_sample) |
| 202 | im, sample_labels = preprocess(im, bbox_labels, mode, settings) |
| 203 | sample_labels = np.array(sample_labels) |
| 204 | if len(sample_labels) == 0: continue |
| 205 | im = im.astype('float32') |
| 206 | boxes = sample_labels[:, 1:5] |
| 207 | lbls = sample_labels[:, 0].astype('int32') |
| 208 | iscrowd = sample_labels[:, -1].astype('int32') |
| 209 | if 'cocoMAP' in settings.ap_version: |
| 210 | batch_out.append((im, boxes, lbls, iscrowd, |
| 211 | [im_id, im_width, im_height])) |
| 212 | else: |
| 213 | batch_out.append((im, boxes, lbls, iscrowd)) |
| 214 | |
| 215 | if len(batch_out) == batch_size: |
| 216 | yield batch_out |
| 217 | batch_out = [] |
| 218 | |
| 219 | if mode == 'test' and len(batch_out) > 1: |
| 220 | yield batch_out |
| 221 | batch_out = [] |
| 222 | |