File size: 5,838 Bytes
98e563e df4d79c 49a34d4 98e563e e6c27ee 98e563e 7f7d638 66c9d87 e6c27ee a177517 98e563e 7f7d638 5272916 98e563e 2faec43 98e563e 66c9d87 98e563e 66c9d87 98e563e 2faec43 98e563e 66c9d87 98e563e 66c9d87 7f7d638 66c9d87 98e563e 66c9d87 7f7d638 66c9d87 c79c457 98e563e c79c457 98e563e c79c457 98e563e c79c457 98e563e c79c457 98e563e c79c457 98e563e c79c457 98e563e 49a34d4 98e563e c79c457 98e563e c79c457 98e563e c79c457 66c9d87 c79c457 98e563e 63b7fbf 98e563e dc6eb16 98e563e 5272916 98e563e 63b7fbf 98e563e 5272916 98e563e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
import gradio as gr
from api import *
from processing import *
import pandas as pd
from indices import indices
#import xgboost as xgb
from lightgbm import LGBMRegressor
import pickle
import json
from datetime import datetime
from shapely.geometry import MultiPolygon,shape
from shapely.geometry import Point
from shapely.geometry.polygon import Polygon
from glob import glob
import wget
##
def timer(message,start_time=None):
if not start_time:
start_time = datetime.now()
return start_time
elif start_time:
thour, temp_sec = divmod((datetime.now() - start_time).total_seconds(), 3600)
tmin, tsec = divmod(temp_sec, 60)
print('\n'+message+' Time taken: %i hours %i minutes and %s seconds.' % (thour, tmin, round(tsec, 2)))
##
def predict(location_name,lat, lon):
cord = [lon,lat]
lon = round(lon,4)
lat = round(lat,4)
x1 = [lon,lat]
x2 = [lat,lon]
with open("data/CIV_0.json","r") as file:
data = json.load(file)
# extract ivory coast polygone
features = [data['features'][0]['geometry']['coordinates'][0]+data['features'][0]['geometry']['coordinates'][1]+data['features'][0]['geometry']['coordinates'][2]]
data['features'][0]['geometry']['coordinates'] = features
ci_polygone = data['features'][0]['geometry']['coordinates'][0][0]
point1 = Point(x1)
point2 = Point(x2)
polygon = Polygon(ci_polygone)
result = polygon.contains(point1)
if not result:
return "Choose an area of ivory coast","","","",""
else:
start_time_research = timer('research tile',None)
df = pd.read_csv("data/frame.csv")
name = find_good_tile(df,point2)
timer('research tile',start_time_research)
if name ==404:
reponse = "Sentinel-2 does not have data on this location to date"
return reponse,"","","",""
else:
start_time_download = timer('download tile',None)
path = "https://data354-public-assets.s3.eu-west-3.amazonaws.com/cisentineldata/"
url = path+name
wget.download(url)
timer('download tile',start_time_download)
start_time_unzip = timer('unzip data',None)
unzip()
timer('unzip data',start_time_unzip)
start_time_processing = timer('processing data',None)
start_time_select_best_tile = timer('select best tile',None)
name,cld_prob,days_ago = select_best_cloud_coverage_tile()
bandes_path_10,bandes_path_20,bandes_path_60,tile_path,path_cld_20,path_cld_60 =paths(name)
timer('select best tile',start_time_select_best_tile)
start_time_10m = timer('create 10m image',None)
# create image dataset
images_10 = extract_sub_image(bandes_path_10,tile_path,cord)
timer('create 10m image',start_time_10m)
start_time_20m = timer('create 20m image',None)
# bandes with 20m resolution
#path_cld_20
images_20 = extract_sub_image(bandes_path_20,tile_path,cord,20,1)
start_time_20m = timer('create 20m image',start_time_20m)
start_time_60m = timer('create 60m image',None)
# bandes with 60m resolution
#path_cld_60
images_60 = extract_sub_image(bandes_path_60,tile_path,cord,60)
start_time_60m = timer('create 60m image',start_time_60m)
#
start_time_make_prediction = timer('make prediction',None)
feature = images_10.tolist()+images_20.tolist()+images_60.tolist()
bands = ['B02', 'B03', 'B04', 'B05', 'B06', 'B07', 'B08', 'B8A', 'B11', 'B12','B01','B09']
X = pd.DataFrame([feature],columns = bands)
# vegetation index calculation
X = indices(X)
# load the model from disk
filename = "data/new_version_model.sav"
loaded_model = pickle.load(open(filename, 'rb'))
# make prediction
biomass = loaded_model.predict(X)[0]
carbon = 0.55*biomass
timer('make prediction',start_time_make_prediction)
# NDVI
start_time_make_ndvi = timer('NDVI calculation',None)
ndvi_index = ndvi(cord,name)
timer('NDVI calculation',start_time_make_ndvi)
timer('processing data',start_time_processing)
# deleted download files
delete_tiles()
return str(cld_prob)+ " % cloud coverage", str(days_ago)+" days ago",str(biomass)+" Kg/ha", str(carbon)+" KgC/ha","NDVI: "+ str(ndvi_index)
# Create title, description and article strings
title = "🌴BEEPAS : Biomass estimation to Evaluate the Environmental Performance of Agroforestry Systems🌴"
description = "This application estimates the biomass of areas in ivory coast using AI and satellite images (S2)."
article = "Created by data354."
# Create examples list from "examples/" directory
#example_list = [["examples/" + example] for example in os.listdir("examples")]
example_list = [["Foret du banco :",5.379913, -4.050445],["Pharmacie Y4 :",5.363292, -3.9481601],["Hotel ivoire : ",5.316458, -4.017172],["Adjamé :",5.346938, -4.027849]]
outputs = [
gr.Textbox(label="Cloud coverage"),
gr.Textbox(label="Number of days since sensing"),
gr.Textbox(label="Above ground biomass density(AGBD) Kg/ha"),
gr.Textbox(label="Carbon stock density KgC/ha "),
gr.Textbox(label="Mean NDVI"),]
demo = gr.Interface(
fn=predict,
inputs=["text","number", "number"],
outputs=outputs, #[ "text", "text","text","text","text"],
examples=example_list,
title=title,
description=description,
article=article,
)
demo.launch(share=True) |