def main(): parser = argparse.ArgumentParser() parser.add_argument('--buffer_meters', default=2, type=float, help='Buffer distance (meters) around graph') parser.add_argument('--burnValue', default=150, type=int, help='Value of road pixels (for plotting)') parser.add_argument( '--test_data_loc', default='AOI_2_Vegas_Train', type=str, help='Folder within sample_data directory of test data') args = parser.parse_args() # set paths path_apls = os.path.dirname(path_apls_src) path_data = os.path.join(path_apls, 'sample_data/' + args.test_data_loc) path_outputs = os.path.join(path_apls, 'example_output_ims/' + args.test_data_loc) path_images_raw = os.path.join(path_data, 'RGB-PanSharpen') path_images_8bit = os.path.join(path_data, 'RGB-PanSharpen_8bit') path_labels = os.path.join(path_data, 'truthGeoJson/spacenetroads') # output directories path_masks = os.path.join(path_outputs, 'masks_' + str(args.buffer_meters) + 'm') path_masks_plot = os.path.join( path_outputs, 'masks_' + str(args.buffer_meters) + 'm_plots') # create directories for d in [path_outputs, path_images_8bit, path_masks, path_masks_plot]: if not os.path.exists(d): os.mkdir(d) # iterate through images, convert to 8-bit, and create masks im_files = os.listdir(path_images_raw) for im_file in im_files: if not im_file.endswith('.tif'): continue name_root = im_file.split('_')[-1].split('.')[0] # create 8-bit image im_file_raw = os.path.join(path_images_raw, im_file) im_file_out = os.path.join(path_images_8bit, im_file) # convert to 8bit apls_tools.convert_to_8Bit(im_file_raw, im_file_out, outputPixType='Byte', outputFormat='GTiff', rescale_type='rescale', percentiles=[2, 98]) # determine output files label_file = os.path.join(path_labels, 'spacenetroads_AOI_2_Vegas_' \ + name_root + '.truthGeoJson') label_file_tot = os.path.join(path_labels, label_file) output_raster = os.path.join(path_masks, 'mask_' + name_root + '.png') plot_file = os.path.join(path_masks_plot, 'mask_' + name_root + '.png') print("\nname_root:", name_root) print(" output_raster:", output_raster) print(" output_plot_file:", plot_file) # create masks mask, gdf_buffer = apls_tools.get_road_buffer( label_file_tot, im_file_out, output_raster, buffer_meters=args.buffer_meters, burnValue=args.burnValue, bufferRoundness=6, plot_file=plot_file, figsize=(6, 6), #(13,4), fontsize=8, dpi=200, show_plot=False, verbose=False) return
def create_masks(path_data, buffer_meters=2, n_bands=3, burnValue=150, make_plots=True, overwrite_ims=False, output_df_file='', header=[ 'name', 'im_file', 'im_vis_file', 'mask_file', 'mask_vis_file' ]): ''' Create masks from files in path_data. Write 8bit images and masks to file. Return a dataframe of file locations with the following columns: ['name', 'im_file', 'im_vis_file', 'mask_file', 'mask_vis_file'] We record locations of im_vis_file and mask_vis_file in case im_file or mask_file is not 8-bit or has n_channels != [1,3] if using 8band data, the RGB-PanSharpen_8bit should already exist, so 3band should be run prior to 8band ''' t0 = time.time() # set paths #path_apls = os.path.dirname(path_apls_src) #path_data = os.path.join(path_apls, args.test_data_loc) path_labels = os.path.join(path_data, 'geojson/spacenetroads') # output directories path_masks = os.path.join(path_data, 'masks_' + str(buffer_meters) + 'm') path_masks_plot = os.path.join(path_data, 'masks_' \ + str(buffer_meters) + 'm_plots') # image directories path_images_vis = os.path.join(path_data, 'RGB-PanSharpen_8bit') if n_bands == 3: path_images_raw = os.path.join(path_data, 'RGB-PanSharpen') path_images_8bit = os.path.join(path_data, 'RGB-PanSharpen_8bit') else: path_images_raw = os.path.join(path_data, 'MUL-PanSharpen') path_images_8bit = os.path.join(path_data, 'MUL-PanSharpen_8bit') if not os.path.exists(path_images_vis): print("Need to run 3band prior to 8band!") return # create directories for d in [path_images_8bit, path_masks, path_masks_plot]: if not os.path.exists(d): os.mkdir(d) # iterate through images, convert to 8-bit, and create masks outfile_list = [] im_files = os.listdir(path_images_raw) nfiles = len(im_files) for i, im_name in enumerate(im_files): if not im_name.endswith('.tif'): continue # define files name_root = 'AOI' + im_name.split('AOI')[1].split('.')[0] im_file_raw = os.path.join(path_images_raw, im_name) im_file_out = os.path.join(path_images_8bit, im_name) im_file_out_vis = im_file_out.replace('MUL', 'RGB') ## get visible file (if using 8band imagery we want the 3band file ## for plotting purposes) #if n_bands == 3: # im_file_out_vis = im_file_out #else: # name_vis = im_name.replace('MUL', 'RGB') # im_file_out_vis = os.path.join(path_images_vis, name_vis) # convert to 8bit, if desired if not os.path.exists(im_file_out) or overwrite_ims: apls_tools.convert_to_8Bit(im_file_raw, im_file_out, outputPixType='Byte', outputFormat='GTiff', rescale_type='rescale', percentiles=[2, 98]) # determine output files #label_file = os.path.join(path_labels, 'spacenetroads_AOI_2_Vegas_' \ # + name_root + '.geojson') label_file = os.path.join(path_labels, 'spacenetroads_' + name_root \ + '.geojson') label_file_tot = os.path.join(path_labels, label_file) mask_file = os.path.join(path_masks, name_root + '.png') if make_plots: plot_file = os.path.join(path_masks_plot, name_root + '.png') else: plot_file = '' print("\n", i + 1, "/", nfiles) print(" im_name:", im_name) print(" name_root:", name_root) print(" im_file_out:", im_file_out) print(" mask_file:", mask_file) print(" output_plot_file:", plot_file) # create masks if not os.path.exists(mask_file) or overwrite_ims: mask, gdf_buffer = apls_tools.get_road_buffer( label_file_tot, im_file_out_vis, mask_file, buffer_meters=buffer_meters, burnValue=burnValue, bufferRoundness=6, plot_file=plot_file, figsize=(6, 6), #(13,4), fontsize=8, dpi=500, show_plot=False, verbose=False) # resize in ingest so we don't have to save the very large arrays outfile_list.append( [im_name, im_file_out, im_file_out_vis, mask_file, mask_file]) # make dataframe and save df = pd.DataFrame(outfile_list, columns=header) if len(output_df_file) > 0: df.to_csv(output_df_file, index=False) print("\ndf.ix[0]:", df.ix[0]) print("\nTotal data length:", len(df)) t4 = time.time() print("Time to run create_masks():", t4 - t0, "seconds") return df
def main(): parser = argparse.ArgumentParser() parser.add_argument('--buffer_meters', default=2, type=float, help='Buffer distance (meters) around graph') parser.add_argument('--burnValue', default=150, type=int, help='Value of road pixels (for plotting)') parser.add_argument('--filepath', type=str, default='/home/ananya/Documents/data/spacenet/SpaceNet_Buildings_Competition_Round2_Sample/AOI_3_Paris_Roads_Train/') parser.add_argument('--imgSizePix', type=int, default=416) args = parser.parse_args() # set paths path_apls_src = os.path.dirname(os.path.realpath(__file__)) path_apls = os.path.dirname(path_apls_src) path_data = args.filepath path_outputs = os.path.join(args.filepath, 'annotations') path_images_raw = os.path.join(path_data, 'RGB-PanSharpen') path_images_8bit = os.path.join(path_data, 'RGB-PanSharpen_clip_8bit') path_labels = os.path.join(path_data, 'geojson/spacenetroads') # output directories path_masks = os.path.join( path_outputs, 'clip_masks_' + str(args.buffer_meters) + 'm') path_masks_plot = os.path.join( path_outputs, 'clip_masks_' + str(args.buffer_meters) + 'm_plots') # create directories for d in [path_outputs, path_images_8bit, path_masks, path_masks_plot]: if not os.path.exists(d): os.mkdir(d) # iterate through images, chip, convert to 8-bit, and create masks im_files = os.listdir(path_images_raw) for im_file in im_files: if not im_file.endswith('.tif'): continue # chip images name_root = im_file.split('_')[-1].split('.')[0] label_file = os.path.join(path_labels, 'spacenetroads_AOI_3_Paris_' + name_root + '.geojson') chipSummaryList = processRasterChip(os.path.join(path_images_raw,im_file), path_images_raw, label_file, os.path.dirname(label_file), outputDirectory=path_outputs, imagePixSize=args.imgSizePix, clipOverlap=0.0, randomClip=False, minpartialPerc=0.0, outputPrefix='') for chipSummary in chipSummaryList: # create 8-bit image # im_file_raw = os.path.join(path_images_raw, im_file) # im_file_out = os.path.join(path_images_8bit, im_file) # annotationName = os.path.basename(chipSummary['rasterSource']) # annotationName = os.path.join(path_outputs, annotationName) im_file_raw = chipSummary['rasterSource'] im_file_out = os.path.join( path_images_8bit, chipSummary['chipName']) # convert to 8bit apls_tools.convert_to_8Bit(im_file_raw, im_file_out, outputPixType='Byte', outputFormat='GTiff', rescale_type='rescale', percentiles=[2, 98]) # determine output files # label_file = os.path.join(path_labels, 'spacenetroads_AOI_3_Paris_' # + name_root + '.geojson') # label_file_tot = os.path.join(path_labels, label_file) label_file_tot = chipSummary['geoVectorName'] output_raster = os.path.join( path_masks, 'mask_' + name_root + '.tif') plot_file = os.path.join( path_masks_plot, 'mask_' + name_root + '.png') print("\nname_root:", name_root) print(" output_raster:", output_raster) print(" output_plot_file:", plot_file) # create masks mask, gdf_buffer = apls_tools.get_road_buffer(label_file_tot, im_file_out, output_raster, buffer_meters=args.buffer_meters, burnValue=args.burnValue, bufferRoundness=6, plot_file=plot_file, # (13,4), figsize=(6, 6), fontsize=8, dpi=200, show_plot=False, verbose=False) return
def main(): parser = argparse.ArgumentParser() parser.add_argument('--im_dir', default='', type=str, help='Image directory') parser.add_argument('--labels_dir', default='', type=str, help='Edges directory (geojsons)') parser.add_argument('--out_dir_mask', default='', type=str, help='Output folder for masks') parser.add_argument('--out_dir_comb', default='', type=str, help='Output folder for masks appended to image') parser.add_argument('--im_prefix', default='', type=str, help='prefix to match geojson with tif') parser.add_argument('--geojson_prefix', default='osmroads_', type=str, help='prefix to match geojson with tif') parser.add_argument('--buffer_meters', default=2, type=float, help='Road buffer') parser.add_argument('--burnValue', default=255, type=int, help='mask burn value') args = parser.parse_args() print("args:", args) verbose = False # path_images = args.im_dir path_labels = args.labels_dir path_masks = args.out_dir_mask path_comb = args.out_dir_comb buffer_meters = args.buffer_meters geojson_prefix = args.geojson_prefix burnValue = args.burnValue buffer_meters_str = str(np.round(buffer_meters, 1)).replace('.', 'p') # make dirs for d in [path_masks]: # print("Cleaning and remaking:", d) # shutil.rmtree(d, ignore_errors=True) print("making:", d) os.makedirs(d, exist_ok=True) if len(path_comb) > 0: os.makedirs(path_comb, exist_ok=True) # iterate through images and create masks im_files = sorted(os.listdir(path_images)) # m = defaultdict(list) for i, im_file in enumerate(im_files): if not im_file.endswith('.tif'): continue name_root_full = im_file.split('.')[0] # name_root_full = im_file.split(imfile_prefix)[-1].split('.')[0] im_path = os.path.join(path_images, im_file) # determine mask output files label_name = geojson_prefix + name_root_full + '.geojson' label_file_tot = os.path.join(path_labels, label_name) output_raster = os.path.join(path_masks, im_file) # print("\n") print(i, "/", len(im_files), "name_root:", name_root_full) print(" im_file:", im_path) print(" label_file:", label_file_tot) print(" output_mask_raster:", output_raster) # create masks mask, gdf_buffer = apls_tools.get_road_buffer( label_file_tot, im_path, output_raster, buffer_meters=buffer_meters, burnValue=burnValue, bufferRoundness=6, plot_file=None, figsize=(6, 6), # (13,4), fontsize=8, dpi=200, show_plot=False, verbose=False) # append mask to image? if len(path_comb) > 0: output_raster_comb = os.path.join(path_comb, im_file) im = skimage.io.imread(im_path) im_comb = np.dstack((im, mask)).astype(np.uint8) if verbose: print(" im.shape:", im.shape) print(" im", im) print(" mask.shape:", mask.shape) print(" mask", mask) print(" im_comb.shape:", im_comb.shape) print(" im_comb", im_comb) # move channels to front #print("im_comb.shape (init):", im_comb.shape) im_comb = np.moveaxis(im_comb, -1, 0) if verbose: print(" im_comb.shape (final):", im_comb.shape) # save save_array_gdal.CreateMultiBandGeoTiff(output_raster_comb, im_comb) # skimage.io.imsave(output_raster_comb, im_comb) # add geo referencing AddGeoReferencing.geo_that_raster(path_masks, path_images) if len(path_comb) > 0: AddGeoReferencing.geo_that_raster(path_comb, path_images)