PLumber + Future + Fastsam

Hey community,
I have found some problems to run python script in future, all wrapped in plumber.

This is my Plumber endpoint

library(tidyverse)
library(plumber)

reticulate::use_python("C:/Program Files/Python310")

library(reticulate)
library(jsonlite)
library(DBI)
library(sp)
library(readxl)
library(rgeos)
library(raster)
library(RPostgreSQL)
library(sf)
library(rpostgis)
library(Rook)
library(rgdal)
library(rapidoc)
library(NbClust)
library(future)
library(planetR)
library(data.table)
library(nasapower)
library(owmr)
library(gptstudio)
library(RPostgres)
library(httr)
library(lubridate)
library(promises)
library(sen2r)
library(shinyjs)
library(emayili)
library(geojsonsf)
library(SPEI)
library(RColorBrewer)
library(mapview)
library(echarts4r)
library(base64enc)
library(reshape2)
library(soilDB)

serializer_geojson <- function(type = "application/geo+json") {
  serializer_write_file(
    fileext = ".GeoJSON",
    type = type,
    write_fn = function(val, tmpfile) {
      rgdal::writeOGR(val, dsn = tmpfile, layer = "geojson", driver = "GeoJSON", overwrite_layer = TRUE)
    }
  )
}

register_serializer("geojson", serializer_geojson)

future::plan("multisession", workers = 3)

source_python('predict_sam.py')

#* @tag FastSam
#* @param lat:double 
#* @param longi:double 
#* @post /AIBoundaries
#* @serializer geojson
function(req, res, lat, longi){
  
  future::future({
    
      predict_sam(lat, longi)

      AOI<-st_read(dsn = ".",
                 layer = "trees")

      AOI_sp<-as_Spatial(AOI)

     as_attachment(AOI_sp, "response.GeoJSON")
    
  })
}

This is the python code that i am using. It is related to SAM facebook model https://segment-anything.com/

import os
import leafmap
from samgeo import SamGeo, show_image, download_file, overlay_images, tms_to_geotiff

def predict_sam(lat, longi):
    m = leafmap.Map(center=[longi ,lat], zoom=16, height="800px", widht="800px")
    m.add_basemap("SATELLITE")
    zoom_level = m.zoom
    map_center = m.center
    lat_span = 360 / (2 ** zoom_level)
    long_span = 360 / (2 ** zoom_level)
    sw_lat = float(map_center[0]) - lat_span
    sw_long = float(map_center[1]) - long_span
    ne_lat = float(map_center[0]) + lat_span
    ne_long = float(map_center[1]) + long_span
    converted_bbox = [sw_lat, sw_long, ne_lat, ne_long]
    image = "Image.tif"
    tms_to_geotiff(output=image, bbox=converted_bbox, source="SATELLITE", overwrite=True, quiet=True, resolution=1)
    sam = SamGeo(model_type="vit_h",sam_kwargs=None)
    sam.generate(image, output="masks.tif", foreground=True, unique=True)
    sam.tiff_to_vector("masks.tif", "trees.shp")

and the follow is the rcode to perform the post request

api<-"http://127.0.0.1:3000/AIBoundaries?lat=-6.332384&longi=-35.43151"
r <- httr::POST(
  api_url
)

and this is the output of my docker container

Lazy evaluation: FALSE
Asynchronous evaluation: TRUE
Local evaluation: TRUE
Environment: 0x55fc943f0020
Capture standard output: TRUE
Capture condition classes: ‘condition’ (excluding ‘nothing’)
Globals: 18 objects totaling 1.65 MiB (environment ‘req’ of 1.11 MiB, standardGeneric ‘dbConnect’ of 10.29 KiB, character ‘vegetationindex’ of 112 bytes, python.builtin.function ‘predict_sam’ of 6.23 KiB, character ‘lat’ of 120 bytes, ...)
Packages: 9 packages (‘sf’, ‘stringr’, ‘rpostgis’, ‘reticulate’, ‘sen2r’, ‘raster’, ‘nasapower’, ‘rgeos’, ‘tidyr’)
L'Ecuyer-CMRG RNG seed: <none> (seed = FALSE)
Resolved: TRUE
Value: <not collected>
Conditions captured: <none>
Early signaling: FALSE
Owner process: 6245bd0b-a722-dcdb-650f-5ecd1a83976c
Class: ‘MultisessionFuture’, ‘ClusterFuture’, ‘MultiprocessFuture’, ‘Future’, ‘environment’

DEBUG: END TROUBLESHOOTING HELP

what can i do to solve this issue?
i have done the same thing but without the future and all works as excepted.

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.