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, }
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
def _preprocess_for_training(self, image_filename, output_dir, nb_images): """Resize/crop then save the training & label images Parameters ---------- image_filename : str Full path towards the image on the disk output_dir : str Output path where preprocessed image must be saved Returns ------- dict Key/values with the filenames and label ids """ raster = gdal.Open(image_filename) raw_img_width = raster.RasterXSize raw_img_height = raster.RasterYSize image_data = raster.ReadAsArray() image_data = np.swapaxes(image_data, 0, 2) result_dicts = [] logger.info( "Image filename: %s, size: (%s, %s)", image_filename.split("/")[-1], raw_img_width, raw_img_height ) label_filename = image_filename.replace("images", "gt") label_raster = gdal.Open(label_filename) labels = label_raster.ReadAsArray() nb_attempts = 0 image_counter = 0 empty_image_counter = 0 while image_counter < nb_images and nb_attempts < 2 * nb_images: # randomly pick an image x = np.random.randint(0, raw_img_width - self.image_size) y = np.random.randint(0, raw_img_height - self.image_size) tile_data = image_data[ x:(x + self.image_size), y:(y + self.image_size) ] tile_image = Image.fromarray(tile_data) mask = labels[ x:(x + self.image_size), y:(y + self.image_size) ] label_dict = utils.build_labels( mask, range(self.get_nb_labels()), "aerial" ) labelled_image = utils.build_image_from_config(mask, self.labels) if np.unique(mask).shape[0] > 1: tiled_results = self._serialize( tile_image, labelled_image, label_dict, image_filename, output_dir, x, y, "nw", ) if tiled_results: result_dicts.append(tiled_results) image_counter += 1 tile_image_ne = tile_image.transpose(Image.FLIP_LEFT_RIGHT) labelled_image_ne = labelled_image.transpose( Image.FLIP_LEFT_RIGHT ) tiled_results_ne = self._serialize( tile_image_ne, labelled_image_ne, label_dict, image_filename, output_dir, x, y, "ne", ) if tiled_results_ne: result_dicts.append(tiled_results_ne) image_counter += 1 tile_image_sw = tile_image.transpose(Image.FLIP_TOP_BOTTOM) labelled_image_sw = labelled_image.transpose( Image.FLIP_TOP_BOTTOM ) tiled_results_sw = self._serialize( tile_image_sw, labelled_image_sw, label_dict, image_filename, output_dir, x, y, "sw", ) if tiled_results_sw: result_dicts.append(tiled_results_sw) image_counter += 1 tile_image_se = tile_image_sw.transpose(Image.FLIP_LEFT_RIGHT) labelled_image_se = labelled_image_sw.transpose( Image.FLIP_LEFT_RIGHT ) tiled_results_se = self._serialize( tile_image_se, labelled_image_se, label_dict, image_filename, output_dir, x, y, "se", ) if tiled_results_se: result_dicts.append(tiled_results_se) image_counter += 1 del tile_image_se, tile_image_sw, tile_image_ne del labelled_image_se, labelled_image_sw, labelled_image_ne else: if empty_image_counter < 0.1 * nb_images: tiled_results = self._serialize( tile_image, labelled_image, label_dict, image_filename, output_dir, x, y, "nw", ) if tiled_results: result_dicts.append(tiled_results) image_counter += 1 empty_image_counter += 1 nb_attempts += 1 del raster logger.info( "Generate %s images after %s attempts.", image_counter, nb_attempts ) return result_dicts
def _preprocess_for_training(self, image_filename, output_dir, nb_images): """Resize/crop then save the training & label images Parameters ---------- image_filename : str Full path towards the image on the disk output_dir : str Output path where preprocessed image must be saved Returns ------- dict Key/values with the filenames and label ids """ raster = gdal.Open(image_filename) raw_img_width = raster.RasterXSize raw_img_height = raster.RasterYSize image_data = raster.ReadAsArray() image_data = np.swapaxes(image_data, 0, 2) result_dicts = [] logger.info( "Image filename: %s, size: (%s, %s)", image_filename.split("/")[-1], raw_img_width, raw_img_height ) label_filename = image_filename.replace("images", "labels").replace( ".tif", ".geojson" ) labels = gpd.read_file(label_filename) labels = labels.loc[~labels.geometry.isna(), ["condition", "geometry"]] none_mask = [lc is None for lc in labels.condition] labels.loc[none_mask, "condition"] = "Complete" nb_attempts = 0 image_counter = 0 empty_image_counter = 0 while image_counter < nb_images and nb_attempts < 2 * nb_images: # randomly pick an image x = np.random.randint(0, raw_img_width - self.image_size) y = np.random.randint(0, raw_img_height - self.image_size) tile_data = image_data[ x:(x + self.image_size), y:(y + self.image_size) ] tile_image = Image.fromarray(tile_data) raster_features = geometries.get_image_features(raster) tile_items = geometries.extract_tile_items( raster_features, labels, x, y, self.image_size, self.image_size ) mask = self.load_mask(tile_items, raster_features, x, y) label_dict = utils.build_labels( mask, range(self.get_nb_labels()), "tanzania" ) labelled_image = utils.build_image_from_config(mask, self.labels) if len(tile_items) > 0: tiled_results = self._serialize( tile_image, labelled_image, label_dict, image_filename, output_dir, x, y, "nw", ) if tiled_results: result_dicts.append(tiled_results) image_counter += 1 tile_image_ne = tile_image.transpose(Image.FLIP_LEFT_RIGHT) labelled_image_ne = labelled_image.transpose( Image.FLIP_LEFT_RIGHT ) tiled_results_ne = self._serialize( tile_image_ne, labelled_image_ne, label_dict, image_filename, output_dir, x, y, "ne", ) if tiled_results_ne: result_dicts.append(tiled_results_ne) image_counter += 1 tile_image_sw = tile_image.transpose(Image.FLIP_TOP_BOTTOM) labelled_image_sw = labelled_image.transpose( Image.FLIP_TOP_BOTTOM ) tiled_results_sw = self._serialize( tile_image_sw, labelled_image_sw, label_dict, image_filename, output_dir, x, y, "sw", ) if tiled_results_sw: result_dicts.append(tiled_results_sw) image_counter += 1 tile_image_se = tile_image_sw.transpose(Image.FLIP_LEFT_RIGHT) labelled_image_se = labelled_image_sw.transpose( Image.FLIP_LEFT_RIGHT ) tiled_results_se = self._serialize( tile_image_se, labelled_image_se, label_dict, image_filename, output_dir, x, y, "se", ) if tiled_results_se: result_dicts.append(tiled_results_se) image_counter += 1 del tile_image_se, tile_image_sw, tile_image_ne del labelled_image_se, labelled_image_sw, labelled_image_ne else: if empty_image_counter < 0.1 * nb_images: tiled_results = self._serialize( tile_image, labelled_image, label_dict, image_filename, output_dir, x, y, "nw", ) if tiled_results: result_dicts.append(tiled_results) image_counter += 1 empty_image_counter += 1 nb_attempts += 1 del raster logger.info( "Generate %s images after %s attempts.", image_counter, nb_attempts ) return result_dicts
def _preprocess_tile(self, x, y, image_filename, output_dir, raster, labels=None): """Preprocess one single tile built from `image_filename`, with respect to pixel coordinates `(x, y)` Parameters ---------- x : int Horizontal pixel coordinate (*i.e.* west bound) y : int Vertical pixel coordinate (*i.e.* north bound) image_filename : str Full path towards the image on the disk output_dir : str Output path where preprocessed image must be saved raster : osgeo.gdal.Dataset Original georeferenced raster labels : geopandas.GeoDataFrame Raw image labels (*i.e.* georeferenced buildings) Returns ------- dict Key/values with the filenames and label ids """ basename_decomp = os.path.splitext(os.path.basename(image_filename)) img_id_str = (str(self.image_size) + '_' + str(self.image_size) + '_' + str(x) + '_' + str(y)) new_in_filename = (basename_decomp[0] + '_' + img_id_str + ".png") new_in_path = os.path.join(output_dir, 'images', new_in_filename) gdal.Translate(new_in_path, raster, format="PNG", srcWin=[x, y, self.image_size, self.image_size]) if not labels is None: raster_features = get_image_features(raster) tile_items = extract_tile_items(raster_features, labels, x, y, self.image_size, self.image_size, tile_srid=32737) out_labelname = (new_in_path.replace("images", "labels")) mask = self.load_mask(tile_items, raster_features, x, y) label_dict = utils.build_labels(mask, range(self.get_nb_labels()), "tanzania") labelled_image = utils.build_image_from_config(mask, self.labels) labelled_image.save(out_labelname) return { "raw_filename": image_filename, "image_filename": new_in_path, "label_filename": out_labelname, "labels": label_dict } else: return { "raw_filename": image_filename, "image_filename": new_in_path }