def save_annotations(image_filepath, polygons): filename_no_extension = os.path.splitext(image_filepath)[0] npy_filepath = filename_no_extension + ".aligned.npy" shp_filepath = filename_no_extension + ".aligned.shp" np.save(npy_filepath, polygons) geo_utils.save_shapefile_from_polygons(polygons, image_filepath, shp_filepath)
def test_align_gt(runs_dirpath, ori_image, ori_metadata, ori_gt_polygons, batch_size, ds_fac_list, run_name_list, model_disp_max_abs_value, output_dir=None, output_name=None, output_shapefiles=True, properties_list=None): # --- Run the model --- # print("# --- Run the model --- #") aligned_gt_polygons, _ = multires_pipeline.multires_inference(runs_dirpath, ori_image, ori_metadata, ori_gt_polygons, model_disp_max_abs_value, batch_size, ds_fac_list, run_name_list) # --- Save polygons as shapefiles --- # if output_shapefiles: if output_dir is not None and output_name is not None: import geo_utils print("# --- Save polygons as shapefiles --- #") shapefile_filename_format = "{}.aligned_gt_polygons.shp" output_shapefile_filename = shapefile_filename_format.format(output_name, "ori") output_shapefile_filepath = os.path.join(output_dir, output_shapefile_filename) geo_utils.save_shapefile_from_polygons(aligned_gt_polygons, ori_metadata["filepath"], output_shapefile_filepath, properties_list=properties_list) else: print_utils.print_warning("Could not save shapefile as output_dir and/or output_name was not specified.") return aligned_gt_polygons
def main(view_info_list, pixelsize, output_base_dirpath): # --- Loading shapefiles --- # print("# --- Loading shapefiles --- #") view_list = [] for view_info in view_info_list: polygon_list, properties_list = geo_utils.get_polygons_from_shapefile( view_info["image_filepath"], view_info["shapefile_filepath"]) view = { "polygon_list": polygon_list, "properties_list": properties_list, "angle": view_info["angle"], } view_list.append(view) # Extract ground truth building heights gt_heights = [] for properties in view_list[0]["properties_list"]: gt_heights.append(properties["HEIGHT"]) gt_heights_array = np.array(gt_heights) # Iterate over all possible pairs of views: heights_list = [] view_pair_list = itertools.combinations(view_list, 2) for view_pair in view_pair_list: heights = compute_heights(view_pair[0], view_pair[1], pixelsize) heights_list.append(heights) # Average results from pairs heights_list_array = np.array(heights_list) pred_heights_array = np.mean(heights_list_array, axis=0) # Correct pred heights: pred_heights_array = pred_heights_array / 4.39 # Factor found with using the ground truth polygons for computing the height # --- Save results --- # polygon_list = view_list[0]["polygon_list"] # Take from the first view # Save shapefile output_shapefile_filepath = os.path.join( output_base_dirpath, view_info_list[0]["image_name"] + "_pred_heights.shp") pred_properties_list = view_list[0]["properties_list"].copy( ) # First copy existing properties list for i, pred_height in enumerate(pred_heights_array): # Then replace HEIGHT pred_properties_list[i]["HEIGHT"] = pred_height geo_utils.save_shapefile_from_polygons( view_list[0]["polygon_list"], view_info_list[0]["image_filepath"], output_shapefile_filepath, properties_list=pred_properties_list) # Save for modeling buildings in Blender and measuring accuracy scaled_polygon_list = [polygon * pixelsize for polygon in polygon_list] np.save(os.path.join(output_base_dirpath, "polygons.npy"), scaled_polygon_list) np.save(os.path.join(output_base_dirpath, "gt_heights.npy"), gt_heights_array) np.save(os.path.join(output_base_dirpath, "pred_heights.npy"), pred_heights_array)
def convert_npy_to_shp(raw_dirpath, polygon_dirname, city, number, shapefile_filename_format): # --- Load data --- # # Load polygon data image_filepath = read.get_image_filepath(raw_dirpath, city, number) polygons = read.load_polygons(raw_dirpath, polygon_dirname, city, number) if polygons is not None: output_shapefile_filepath = read.get_polygons_filepath(raw_dirpath, polygon_dirname, city, number, overwrite_polygons_filename_format=shapefile_filename_format) geo_utils.save_shapefile_from_polygons(polygons, image_filepath, output_shapefile_filepath)
def get_osm_annotations(filepath): filename_no_extension = os.path.splitext(filepath)[0] npy_filepath = filename_no_extension + ".npy" if os.path.exists(npy_filepath): print_utils.print_info("Loading OSM building data from disc...") gt_polygons = np.load(npy_filepath, allow_pickle=True) else: print_utils.print_info( "Fetching OSM building data from the internet...") gt_polygons = geo_utils.get_polygons_from_osm(filepath, tag="building") # Save npy to avoid re-fetching: np.save(npy_filepath, gt_polygons) # Save shapefile for visualisation: shp_filepath = filename_no_extension + ".shp" geo_utils.save_shapefile_from_polygons(gt_polygons, filepath, shp_filepath) return gt_polygons
def test(ori_image, ori_metadata, ori_gt_polygons, ori_disp_polygons, batch_size, ds_fac_list, run_name_list, model_disp_max_abs_value, thresholds, test_output_dir, output_name, output_shapefiles=True, properties_list=None): if output_shapefiles: assert properties_list is not None and len(ori_disp_polygons) == len( properties_list ), "ori_disp_polygons and properties_list should have the same length" polygons_image_plot_filename_format = "{}.polygons.png" shapefile_filename_format = "{}.{}_polygons.shp" segmentation_image_plot_filename_format = "{}.segmentation.png" accuracies_filename_format = "{}.accuracy.npy" # --- Run the model --- # print("# --- Run the model --- #") aligned_disp_polygons, segmentation_image = multires_pipeline.multires_inference( ori_image, ori_metadata, ori_disp_polygons, model_disp_max_abs_value, batch_size, ds_fac_list, run_name_list) # aligned_disp_polygons = ori_disp_polygons # segmentation_image = np.zeros((ori_image.shape[0], ori_image.shape[1], 4)) # --- Save segmentation_output --- # print("# --- Save segmentation_output --- #") plot_segmentation_image_filename = segmentation_image_plot_filename_format.format( output_name) plot_segmentation_image_filepath = os.path.join( test_output_dir, plot_segmentation_image_filename) visualization.save_plot_segmentation_image( plot_segmentation_image_filepath, segmentation_image) # --- Save polygons plot --- # plot_image_filename = polygons_image_plot_filename_format.format( output_name) plot_image_filepath = os.path.join(test_output_dir, plot_image_filename) visualization.save_plot_image_polygons(plot_image_filepath, ori_image, ori_gt_polygons, ori_disp_polygons, aligned_disp_polygons) # visualization.save_plot_image_polygons(plot_image_filepath, ori_image, [], ori_disp_polygons, # aligned_disp_polygons) # --- Save polygons as shapefiles --- # if output_shapefiles: print("# --- Save polygons as shapefiles --- #") output_shapefile_filename = shapefile_filename_format.format( output_name, "ori") output_shapefile_filepath = os.path.join(test_output_dir, output_shapefile_filename) geo_utils.save_shapefile_from_polygons(ori_gt_polygons, ori_metadata["filepath"], output_shapefile_filepath, properties_list=properties_list) output_shapefile_filename = shapefile_filename_format.format( output_name, "misaligned") output_shapefile_filepath = os.path.join(test_output_dir, output_shapefile_filename) geo_utils.save_shapefile_from_polygons(ori_disp_polygons, ori_metadata["filepath"], output_shapefile_filepath, properties_list=properties_list) output_shapefile_filename = shapefile_filename_format.format( output_name, "aligned") output_shapefile_filepath = os.path.join(test_output_dir, output_shapefile_filename) geo_utils.save_shapefile_from_polygons(aligned_disp_polygons, ori_metadata["filepath"], output_shapefile_filepath, properties_list=properties_list) # --- Measure accuracies --- # if len(ori_gt_polygons) == len(aligned_disp_polygons): print("# --- Measure accuracies --- #") accuracies_filename = accuracies_filename_format.format(output_name) accuracies_filepath = os.path.join(test_output_dir, accuracies_filename) accuracies = measure_accuracies(ori_gt_polygons, aligned_disp_polygons, thresholds, accuracies_filepath) print(accuracies)