コード例 #1
0
    def _preprocess(self,
                    image_filename,
                    output_dir,
                    aggregate,
                    labelling=True):
        """Resize/crop then save the training & label images

        Parameters
        ----------
        datadir : str
        image_filaname : str
        aggregate : boolean
        labelling : boolean

        Returns
        -------
        dict
            Key/values with the filenames and label ids
        """
        # open original images
        img_in = Image.open(image_filename)

        # resize images (self.image_size*larger_size or larger_size*self.image_size)
        img_in = utils.resize_image(img_in, self.image_size)

        # crop images to get self.image_size*self.image_size dimensions
        crop_pix = np.random.randint(0, 1 + max(img_in.size) - self.image_size)
        final_img_in = utils.mono_crop_image(img_in, crop_pix)

        # save final image
        new_in_filename = os.path.join(output_dir, 'images',
                                       image_filename.split('/')[-1])
        final_img_in.save(new_in_filename)

        # label_filename vs label image
        if labelling:
            label_filename = image_filename.replace("images/", "labels/")
            label_filename = label_filename.replace(".jpg", ".png")
            img_out = Image.open(label_filename)
            img_out = utils.resize_image(img_out, self.image_size)
            final_img_out = utils.mono_crop_image(img_out, crop_pix)
            # group some labels
            if aggregate:
                final_img_out = self.group_image_label(final_img_out)

            labels = utils.mapillary_label_building(final_img_out,
                                                    self.label_ids)
            new_out_filename = os.path.join(output_dir, 'labels',
                                            label_filename.split('/')[-1])
            final_img_out.save(new_out_filename)
        else:
            new_out_filename = None
            labels = {i: 0 for i in range(self.get_nb_labels())}

        return {
            "raw_filename": image_filename,
            "image_filename": new_in_filename,
            "label_filename": new_out_filename,
            "labels": labels
        }
コード例 #2
0
    def _preprocess(self, image_filename, output_dir, labelling=True):
        """Resize/crop then save the training & label images

        Parameters
        ----------
        datadir : str
        image_filaname : str
        labelling : boolean

        Returns
        -------
        dict
            Key/values with the filenames and label ids
        """
        # open original images
        img_in = Image.open(image_filename)

        # resize images
        # (self.image_size*larger_size or larger_size*self.image_size)
        img_in = utils.resize_image(img_in, self.image_size)

        # crop images to get self.image_size*self.image_size dimensions
        crop_pix = np.random.randint(0, 1 + max(img_in.size) - self.image_size)
        final_img_in = utils.mono_crop_image(img_in, crop_pix)

        # save final image
        new_in_filename = os.path.join(output_dir, "images",
                                       os.path.basename(image_filename))
        final_img_in.save(new_in_filename)

        # label_filename vs label image
        if labelling:
            label_filename = image_filename.replace("images/", "labels/")
            label_filename = label_filename.replace(".jpg", ".png")
            img_out = Image.open(label_filename)
            img_out = utils.resize_image(img_out, self.image_size)
            img_out = utils.mono_crop_image(img_out, crop_pix)
            # aggregate some labels
            img_out = self.group_image_label(img_out)

            labels = utils.build_labels(img_out,
                                        self.label_ids,
                                        dataset="mapillary")
            new_out_filename = os.path.join(output_dir, "labels",
                                            os.path.basename(label_filename))
            label_out = np.array(img_out)
            final_img_out = utils.build_image_from_config(
                label_out, self.label_info)
            final_img_out.save(new_out_filename)
        else:
            new_out_filename = None
            labels = {i: 0 for i in range(self.get_nb_labels())}

        return {
            "raw_filename": image_filename,
            "image_filename": new_in_filename,
            "label_filename": new_out_filename,
            "labels": labels,
        }
コード例 #3
0
    def _preprocess(self, image_filename, output_dir, labelling):
        """Resize/crop then save the training & label images

        Parameters
        ----------
        image_filename : str
            Full path towards the image on the disk
        datadir : str
            Output path where preprocessed image must be saved

        Returns
        -------
        dict
            Key/values with the filenames and label ids
        """
        img_in = Image.open(image_filename)
        raw_img_size = img_in.size[0]
        result_dicts = []
        # crop tile_size*tile_size tiles into 5000*5000 raw images
        buffer_tiles = []
        for x in range(0, raw_img_size, self.tile_size):
            for y in range(0, raw_img_size, self.tile_size):
                tile = img_in.crop(
                    (x, y, x + self.tile_size, y + self.tile_size))
                tile = utils.resize_image(tile, self.image_size)
                img_id = int(
                    (raw_img_size / self.tile_size * x / self.tile_size +
                     y / self.tile_size))
                basename_decomp = os.path.splitext(
                    os.path.basename(image_filename))
                new_in_filename = (basename_decomp[0] + '_' + str(img_id) +
                                   basename_decomp[1])
                new_in_path = os.path.join(output_dir, 'images',
                                           new_in_filename)
                tile.save(new_in_path.replace(".tif", ".png"))
                result_dicts.append({
                    "raw_filename": image_filename,
                    "image_filename": new_in_path
                })

        if labelling:
            label_filename = image_filename.replace("images/", "gt/")
            img_out = Image.open(label_filename)
            buffer_tiles = []
            for x in range(0, raw_img_size, self.tile_size):
                for y in range(0, raw_img_size, self.tile_size):
                    tile = img_out.crop(
                        (x, y, x + self.tile_size, y + self.tile_size))
                    tile = utils.resize_image(tile, self.image_size)
                    img_id = int(
                        (raw_img_size / self.tile_size * x / self.tile_size +
                         y / self.tile_size))
                    basename_decomp = os.path.splitext(
                        os.path.basename(image_filename))
                    new_out_filename = (basename_decomp[0] + '_' +
                                        str(img_id) + basename_decomp[1])
                    new_out_path = os.path.join(output_dir, 'labels',
                                                new_out_filename)
                    tile.save(new_out_path.replace(".tif", ".png"))
                    labels = utils.build_labels(tile,
                                                self.label_ids,
                                                dataset='aerial')
                    result_dicts[img_id]["label_filename"] = new_out_path
                    result_dicts[img_id]["labels"] = labels

        return result_dicts