from samgis import app_logger from samgis.io.geo_helpers import get_vectorized_raster_as_geojson from samgis.io.tms2geotiff import download_extent from samgis.prediction_api.global_models import models_dict from samgis.utilities.constants import DEFAULT_URL_TILES from samgis_core.utilities.type_hints import llist_float, list_dict, dict_str_int from lisa_on_cuda.utils import app_helpers def lisa_predict( bbox: llist_float, prompt: str, zoom: float, inference_function_name_key: str = "lisa", source: str = DEFAULT_URL_TILES ) -> dict_str_int: """ Return predictions as a geojson from a geo-referenced image using the given input prompt. 1. if necessary instantiate a segment anything machine learning instance model 2. download a geo-referenced raster image delimited by the coordinates bounding box (bbox) 3. get a prediction image from the segment anything instance model using the input prompt 4. get a geo-referenced geojson from the prediction image Args: bbox: coordinates bounding box prompt: machine learning input prompt zoom: Level of detail inference_function_name_key: machine learning model name source: xyz Returns: Affine transform """ app_logger.info("start lisa inference...") if models_dict[inference_function_name_key]["inference"] is None: app_logger.info(f"missing inference function {inference_function_name_key}, instantiating it now!") parsed_args = app_helpers.parse_args([]) inference_fn = app_helpers.get_inference_model_by_args(parsed_args) models_dict[inference_function_name_key]["inference"] = inference_fn app_logger.debug(f"using a {inference_function_name_key} instance model...") inference_fn = models_dict[inference_function_name_key]["inference"] pt0, pt1 = bbox app_logger.info(f"tile_source: {source}: downloading geo-referenced raster with bbox {bbox}, zoom {zoom}.") img, transform = download_extent(w=pt1[1], s=pt1[0], e=pt0[1], n=pt0[0], zoom=zoom, source=source) app_logger.info( f"img type {type(img)} with shape/size:{img.size}, transform type: {type(transform)}, transform:{transform}.") _, mask, output_string = inference_fn(prompt, img) # app_logger.info(f"created {n_predictions} masks, preparing conversion to geojson...") return { "output_string": output_string, **get_vectorized_raster_as_geojson(mask, transform) }