Example #1
0
    def visualize(self,
                  image_name: str,
                  image: np.ndarray,
                  polygons: typing.List[typing.List[float]],
                  score_map: np.ndarray,
                  training_mask: np.ndarray,
                  rois: np.ndarray,
                  transcripts: typing.List[str] = None):

        polygon_list = []
        for polygon in polygons:
            polygon_list.append(
                ia_polys.Polygon(np.array(polygon).reshape(4, 2)))

        polygons_on_image = ia_polys.PolygonsOnImage(polygons=polygon_list,
                                                     shape=image.shape)
        new_image = polygons_on_image.draw_on_image(image)

        colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (0, 0, 0)]

        for polygon in polygons:
            for i, p in enumerate(polygon.reshape(4, 2)):
                cv2.circle(new_image, tuple(p), radius=5, color=colors[i])

        cv2.imwrite(image_name + '_bbox.jpg', new_image[:, :, ::-1])

        rois_list = []

        new_image = image.copy()
        for roi, transcript in zip(rois, transcripts):
            center = (roi[0], roi[1])
            wh = (roi[3], roi[2])
            angle = -roi[4]
            box = cv2.boxPoints((center, wh, angle))
            rois_list.append(ia_polys.Polygon(box))
            origin = box[0]
            font = cv2.FONT_HERSHEY_PLAIN
            new_image = cv2.putText(new_image, transcript,
                                    (int(origin[0]), int(origin[1] + 10)),
                                    font, 1, (0, 255, 0), 1)

        rois_on_image = ia_polys.PolygonsOnImage(polygons=rois_list,
                                                 shape=image.shape)
        new_image = rois_on_image.draw_on_image(new_image)
        cv2.imwrite(image_name + '_rois.jpg', new_image[:, :, ::-1])

        score_map = ia_segmaps.SegmentationMapsOnImage(
            score_map.astype(dtype=np.uint8), shape=image.shape)
        new_image = score_map.draw_on_image(image.astype(dtype=np.uint8))
        cv2.imwrite(image_name + '_score.jpg', new_image[0][:, :, ::-1])

        training_mask = cv2.resize(training_mask,
                                   dsize=(image.shape[1], image.shape[0]))
        new_image = image * (1 - training_mask[:, :, None])
        cv2.imwrite(image_name + '_mask.jpg', new_image[:, :, ::-1])
Example #2
0
    def test_legacy_support_for_float32_2d(self):
        arr = np.array([0.4, 0.6], dtype=np.float32).reshape((1, 2))
        with warnings.catch_warnings(record=True) as caught_warnings:
            warnings.simplefilter("always")
            segmap = segmapslib.SegmentationMapsOnImage(arr, shape=(1, 1, 3))

            arr_expected = np.array([0, 1], dtype=np.int32).reshape((1, 2, 1))
            assert np.array_equal(segmap.arr, arr_expected)
            assert segmap.shape == (1, 1, 3)

        assert len(caught_warnings) == 1
        assert ("Got a float array as the segmentation map in"
                in str(caught_warnings[0].message))
def basic_image_augmentation(image_name, image_dataframe):
    """ Basic image augmentation """

    # Basic augmentation sequence:
    seq = iaa.Sequential([
        iaa.Crop(px=(0, 16)),
        iaa.Fliplr(0.5),
        iaa.GaussianBlur(sigma=(0, 3.0)),
        iaa.Rotate((-45, 45))
    ])

    # Open image:
    image_path = '../data/train_images/' + image_name
    image = cv2.imread(image_path)

    # Get image's mask:
    image_rle = image_dataframe[(
        image_dataframe['ImageId'] == image_name)]['EncodedPixels'].values[0]
    image_mask = rle_to_mask(image_rle)

    # Create segmentation map:
    segmap = np.zeros((image.shape[0], image.shape[1]), dtype=np.int32)
    segmap = np.where(image_mask == 1, 1, segmap)
    segmap = iaas.SegmentationMapsOnImage(segmap, shape=image.shape)

    # Apply basic augmentation sequence:
    image_aug, segmap_aug = seq(image=image, segmentation_maps=segmap)

    # Extract mask:
    image_aug_mask = segmap_aug.get_arr()

    # Get rle:
    image_aug_rle = mask_to_rle(image_aug_mask)

    # Return results:
    return image_aug, image_aug_mask, image_aug_rle