Пример #1
0
    def reset(self, soft=False, from_cached=None):
        if not soft:
            self.model.reset()  # can't fail, so don't worry about it
        self.current_snapshot_string = get_random_string(8)
        self.current_snapshot_idx = 0
        self.current_request_counter = AtomicCounter()
        self.request_list = []

        if self.storage_type == "table":
            self.table_service.insert_entity(
                "webtoolsessions", {
                    "PartitionKey": str(np.random.randint(0, 8)),
                    "RowKey": str(uuid.uuid4()),
                    "session_id": self.current_snapshot_string,
                    "server_hostname": os.uname()[1],
                    "server_sys_argv": ' '.join(sys.argv),
                    "base_model": from_cached
                })
Пример #2
0
    def __init__(self, session_id, model):
        LOGGER.info("Instantiating a new session object with id: %s" %
                    (session_id))

        self.storage_type = "file"  # this will be "table" or "file"
        self.storage_path = "data/"  # this will be a file path
        self.table_service = None  # this will be an instance of TableService

        self.model = model
        self.current_transform = ()

        self.current_snapshot_string = get_random_string(8)
        self.current_snapshot_idx = 0
        self.current_request_counter = AtomicCounter()
        self.request_list = []

        self.session_id = session_id
        self.creation_time = time.time()
        self.last_interaction_time = self.creation_time
Пример #3
0
def pred_tile():
    ''' Method called for POST `/predTile`'''
    bottle.response.content_type = 'application/json'
    data = bottle.request.json
    data["remote_address"] = bottle.request.client_ip

    SESSION_HANDLER.get_session(bottle.request.session.id).add_entry(
        data)  # record this interaction

    # Inputs
    geom = data["polygon"]
    class_list = data["classes"]
    name_list = [item["name"] for item in class_list]
    color_list = [item["color"] for item in class_list]
    dataset = data["dataset"]
    zone_layer_name = data["zoneLayerName"]

    if dataset not in DATASETS:
        raise ValueError(
            "Dataset doesn't seem to be valid, do the datasets in js/tile_layers.js correspond to those in TileLayers.py"
        )

    try:
        naip_data, raster_profile, raster_transform, raster_bounds, raster_crs = DATASETS[
            dataset]["data_loader"].get_data_from_shape(geom["geometry"])
        naip_data = np.rollaxis(naip_data, 0, 3)
        shape_area = get_area_from_geometry(geom["geometry"])
    except NotImplementedError as e:
        bottle.response.status = 400
        return json.dumps({
            "error":
            "Cannot currently download imagery with 'Basemap' based datasets"
        })

    output = SESSION_HANDLER.get_session(bottle.request.session.id).model.run(
        naip_data, geom, True)
    output_hard = output.argmax(axis=2)
    print("Finished, output dimensions:", output.shape)

    # apply nodata mask from naip_data
    nodata_mask = np.sum(naip_data == 0, axis=2) == naip_data.shape[2]
    output_hard[nodata_mask] = 255
    vals, counts = np.unique(output_hard[~nodata_mask], return_counts=True)

    # ------------------------------------------------------
    # Step 4
    #   Convert images to base64 and return
    # ------------------------------------------------------
    tmp_id = get_random_string(8)
    img_hard = np.round(
        class_prediction_to_img(output, True, color_list) * 255,
        0).astype(np.uint8)
    img_hard = cv2.cvtColor(img_hard, cv2.COLOR_RGB2BGRA)
    img_hard[nodata_mask] = [0, 0, 0, 0]

    img_hard, img_hard_bounds = warp_data_to_3857(img_hard,
                                                  raster_crs,
                                                  raster_transform,
                                                  raster_bounds,
                                                  resolution=10)

    cv2.imwrite(os.path.join(ROOT_DIR, "downloads/%s.png" % (tmp_id)),
                img_hard)
    data["downloadPNG"] = "downloads/%s.png" % (tmp_id)

    new_profile = raster_profile.copy()
    new_profile['driver'] = 'GTiff'
    new_profile['dtype'] = 'uint8'
    new_profile['compress'] = "lzw"
    new_profile['count'] = 1
    new_profile['transform'] = raster_transform
    new_profile['height'] = naip_data.shape[0]
    new_profile['width'] = naip_data.shape[1]
    new_profile['nodata'] = 255
    f = rasterio.open(os.path.join(ROOT_DIR, "downloads/%s.tif" % (tmp_id)),
                      'w', **new_profile)
    f.write(output_hard.astype(np.uint8), 1)
    f.close()
    data["downloadTIFF"] = "downloads/%s.tif" % (tmp_id)

    f = open(os.path.join(ROOT_DIR, "downloads/%s.txt" % (tmp_id)), "w")
    f.write("Class id\tClass name\tPercent area\tArea (km^2)\n")
    for i in range(len(vals)):
        pct_area = (counts[i] / np.sum(counts))
        if shape_area is not None:
            real_area = shape_area * pct_area
        else:
            real_area = -1
        f.write("%d\t%s\t%0.4f%%\t%0.4f\n" %
                (vals[i], name_list[vals[i]], pct_area * 100, real_area))
    f.close()
    data["downloadStatistics"] = "downloads/%s.txt" % (tmp_id)

    bottle.response.status = 200
    return json.dumps(data)