Beispiel #1
0
 def show_ref(self, ref, seg_box='seg'):
     ax = plt.gca()
     # show image
     image = self.imgs[ref['image_id']]
     img = io.imread(osp.join(self.IMAGE_DIR, image['file_name']))
     ax.imshow(img)
     # show refer expression
     for sid, sent in enumerate(ref['sentences']):
         print('%s. %s' % (sid + 1, sent['sent']))
     # show segmentations
     if seg_box == 'seg':
         ann_id = ref['ann_id']
         ann = self.anns[ann_id]
         polygons = []
         color = []
         c = 'none'
         if type(ann['segmentation'][0]) == list:
             # polygon used for refcoco*
             for seg in ann['segmentation']:
                 poly = np.array(seg).reshape(int((len(seg)) / 2, 2))
                 polygons.append(Polygon(poly, True, alpha=0.4))
                 color.append(c)
             p = PatchCollection(polygons,
                                 facecolors=color,
                                 edgecolors=(1, 1, 0, 0),
                                 linewidths=3,
                                 alpha=1)
             ax.add_collection(p)  # thick yellow polygon
             p = PatchCollection(polygons,
                                 facecolors=color,
                                 edgecolors=(1, 0, 0, 0),
                                 linewidths=1,
                                 alpha=1)
             ax.add_collection(p)  # thin red polygon
         else:
             # mask used for refclef
             rle = ann['segmentation']
             m = mask.decode(rle)
             img = np.ones((m.shape[0], m.shape[1], 3))
             color_mask = np.array([2.0, 166.0, 101.0]) / 255
             for i in range(3):
                 img[:, :, i] = color_mask[i]
             ax.imshow(np.dstack((img, m * 0.5)))
     # show bounding-box
     elif seg_box == 'box':
         ann_id = ref['ann_id']
         ann = self.anns[ann_id]
         bbox = self.get_ref_box(ref['ref_id'])
         box_plot = Rectangle((bbox[0], bbox[1]),
                              bbox[2],
                              bbox[3],
                              fill=False,
                              edgecolor='green',
                              linewidth=3)
         ax.add_patch(box_plot)
Beispiel #2
0
 def get_mask(self, ref):
     # return mask, area and mask-center
     ann = self.ref_to_ann[ref['ref_id']]
     image = self.imgs[ref['image_id']]
     if isinstance(ann['segmentation'][0], list):  # polygon
         rle = mask.frPyObjects(ann['segmentation'], image['height'],
                                image['width'])
     else:
         rle = ann['segmentation']
     m = mask.decode(rle)
     """
     Sometimes there are multiple binary map
     (corresponding to multiple segs)
     """
     m = np.sum(m, axis=2)
     m = m.astype(np.uint8)  # convert to np.uint8
     # compute area
     area = sum(mask.area(rle))  # should be close to ann['area']
     return {'mask': m, 'area': area}