Example #1
0
def test_tub_augment(tub):
    """Tub with augmented images which only differ slightly."""
    import numpy as np
    index = tub.get_index(shuffled=False)
    img_arr_before = [tub.get_record(ix)['cam/image_array'] for ix in index]
    tub.augment_images()
    img_arr_after = [tub.get_record(ix)['cam/image_array'] for ix in index]
    total_change = 0
    for img_arr_b, img_arr_a in zip(img_arr_before, img_arr_after):
        assert img_arr_a.shape == img_arr_b.shape, 'image size broken'
        img_a = arr_to_img(img_arr_a)
        img_b = arr_to_img(img_arr_b)
        diff = ImageChops.difference(img_a, img_b)
        diff_arr = img_to_arr(diff)
        # normalise this number
        num_pixel_channel = np.prod(diff_arr.shape)
        avg_change = diff_arr.sum() / num_pixel_channel
        # check that the augmented image is different if not totally black
        if img_arr_b.max() > 0:
            assert avg_change != 0, "Augmentation didn't do anything"
        # change per chanel can be maximally 255 in 8-bit
        total_change += avg_change

    # An average change of 1 (per 255) would be already too big on the moving
    # square images. Empirically we see changes in the order of 0.1
    assert total_change / len(img_arr_before) < 1.0, \
        'The augmented pictures differ too much.'
Example #2
0
    def run(self, img, path):
        if type(img) is numpy.ndarray:
            stacked_img = numpy.stack((img, ) * 3, axis=-1)
            img = arr_to_img(stacked_img)

        draw = ImageDraw.Draw(img)
        self.plot_path(path, draw)

        return img
Example #3
0
 def run(self, img_arr):
     if img_arr is None:
         return None
     try:
         image = arr_to_img(img_arr)
         jpg = img_to_binary(image)
         return jpg
     except:
         return None
Example #4
0
    def run(self, img, waypoints):
        if type(img) is numpy.ndarray:
            stacked_img = numpy.stack((img, ) * 3, axis=-1)
            img = arr_to_img(stacked_img)

        draw = ImageDraw.Draw(img)
        for waypt in waypoints:
            self.plot_wp(waypt, draw, color=self.color)

        return img
Example #5
0
    def run(self, img, path):

        if type(img) is numpy.ndarray:
            stacked_img = numpy.stack((img, ) * 3, axis=-1)
            img = arr_to_img(stacked_img)

        draw = ImageDraw.Draw(img)
        color = (255, 0, 0)
        for iP in range(0, len(path) - 1):
            ax, ay = path[iP]
            bx, by = path[iP + 1]
            self.plot_line(ax * self.scale + self.offset[0],
                           ay * self.scale + self.offset[1],
                           bx * self.scale + self.offset[0],
                           by * self.scale + self.offset[1], draw, color)

        return img
 def augment_images(self):
     # Get all record's index
     index = self.get_index(shuffled=False)
     # Go through index
     count = 0
     for ix in index:
         data = self.get_record(ix)
         for key, val in data.items():
             typ = self.get_input_type(key)
             if typ == 'image_array':
                 # here val is an img_arr
                 img = arr_to_img(val)
                 # then augment and denormalise
                 img_aug = augment_pil_image(img)
                 name = self.make_file_name(key, ext='.jpg', ix=ix)
                 try:
                     img_aug.save(os.path.join(self.path, name))
                     count += 1
                 except IOError as err:
                     print(err)
     print('Augmenting', count, 'images in', self.path)
Example #7
0
 def run(self, map_bytes):
     np_arr = np.array(map_bytes).reshape(self.resolution)
     return arr_to_img(np_arr)