youl commited on
Commit
78fb42c
1 Parent(s): 90850d7

Upload 2 files

Browse files
Files changed (2) hide show
  1. api.py +84 -0
  2. app.py +145 -0
api.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sentinelsat import SentinelAPI, read_geojson, geojson_to_wkt
2
+ from datetime import datetime, timedelta,date
3
+ import zipfile
4
+ import rasterio
5
+ from rasterio.plot import show
6
+ from PIL import Image
7
+ import matplotlib.pyplot as plt
8
+ import numpy as np
9
+ import pandas as pd
10
+ import os
11
+ from glob import glob
12
+ from tqdm import tqdm
13
+ #from haversine import haversine, Unit
14
+ #from xml.etree import ElementTree as et
15
+ import xmltodict
16
+ import json
17
+ import warnings
18
+ import shutil
19
+ from shapely.geometry import Point
20
+ from shapely.geometry.polygon import Polygon
21
+ warnings.filterwarnings('ignore')
22
+
23
+ ##
24
+ def unzip():
25
+ files = glob('*.zip')
26
+ for file in files:
27
+ with zipfile.ZipFile(file, 'r') as zip_ref:
28
+ zip_ref.extractall()
29
+
30
+
31
+ ##
32
+ def select_best_cloud_coverage_tile():
33
+ tile_names = {}
34
+ cld_prob = []
35
+ folders = glob('*.SAFE')
36
+ for fold in folders:
37
+ metadata_path = fold+"/MTD_MSIL2A.xml"
38
+ xml_file=open(metadata_path,"r")
39
+ xml_string=xml_file.read()
40
+ python_dict=xmltodict.parse(xml_string)
41
+ cld = float(python_dict["n1:Level-2A_User_Product"]["n1:Quality_Indicators_Info"]["Cloud_Coverage_Assessment"])
42
+ tile_names[cld] = fold
43
+ cld_prob.append(cld)
44
+ name = tile_names[min(cld_prob)]
45
+ dates = name.split('_')[2][:8]
46
+ acquisition_date = datetime.strptime(dates, "%Y%m%d")
47
+ today = datetime.now()
48
+ delta = (today - acquisition_date)
49
+ days_ago = delta.days
50
+ return name,min(cld_prob),days_ago
51
+
52
+ ##
53
+ def find_good_tile(df, point):
54
+ for row in tqdm(df.itertuples(),total=len(df)):
55
+ tile_name = row.tiles
56
+ coordinate = row.coords
57
+ x = coordinate.replace(']','')
58
+ x = x.replace('[','')
59
+ x = x.replace("'",'')
60
+ tab = [[float(x.split(",")[i]),float(x.split(",")[i+1])] for i in range(0,len(x.split(",")),2)]
61
+ polygon = Polygon(tab)
62
+ result = polygon.contains(point)
63
+ if result:
64
+ print(tile_name)
65
+ return "S2B_MSIL2A_20230104T103329_N0509_R108_T30NUL_20230104T132032.SAFE.zip" #tile_name
66
+
67
+ return "S2B_MSIL2A_20230104T103329_N0509_R108_T30NUL_20230104T132032.SAFE.zip" #404
68
+
69
+ ##
70
+ def delete_tiles():
71
+ files = glob('*.zip')
72
+ folders1 = glob('*.SAFE')
73
+ folders2 = glob("*.tmp")
74
+ for f in files:
75
+ os.remove(f)
76
+ for fold in folders1:
77
+ shutil.rmtree(fold, ignore_errors=True)
78
+
79
+ for fold in folders2:
80
+ shutil.rmtree(fold, ignore_errors=True)
81
+
82
+
83
+
84
+
app.py ADDED
@@ -0,0 +1,145 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from api import *
3
+ from processing import *
4
+ import pandas as pd
5
+ from indices import indices
6
+ import xgboost as xgb
7
+ #from lightgbm import LGBMRegressor
8
+ from sklearn.preprocessing import StandardScaler
9
+ from sklearn.decomposition import PCA
10
+ import pickle as pk
11
+ import json
12
+ import boto3
13
+ from shapely.geometry import MultiPolygon,shape
14
+ from shapely.geometry import Point
15
+ from shapely.geometry.polygon import Polygon
16
+ from glob import glob
17
+ import wget
18
+
19
+
20
+ def predict(location_name,lat, lon):
21
+ cord = [lon,lat]
22
+ lon = round(lon,4)
23
+ lat = round(lat,4)
24
+ x1 = [lon,lat]
25
+ x2 = [lat,lon]
26
+ with open("data/CIV_0.json","r") as file:
27
+ data = json.load(file)
28
+ # extract ivory coast polygone
29
+ features = [data['features'][0]['geometry']['coordinates'][0]+data['features'][0]['geometry']['coordinates'][1]+data['features'][0]['geometry']['coordinates'][2]]
30
+ data['features'][0]['geometry']['coordinates'] = features
31
+ ci_polygone = data['features'][0]['geometry']['coordinates'][0][0]
32
+ point1 = Point(x1)
33
+ point2 = Point(x2)
34
+ polygon = Polygon(ci_polygone)
35
+ result = polygon.contains(point1)
36
+
37
+ if not result:
38
+ return "Choisissez une zone de la CI","","","",""
39
+
40
+ else:
41
+ df = pd.read_csv("data/frame.csv")
42
+ name = find_good_tile(df,point2)
43
+ if name ==404:
44
+ reponse = "Sentinel-2 ne dispose pas de données ce sur ce lieu à ce jour"
45
+ return reponse,"","","",""
46
+ else:
47
+ path = "https://data354-public-assets.s3.eu-west-3.amazonaws.com/cisentineldata/"
48
+ url = path+name
49
+ #wget.download(url)
50
+ unzip()
51
+ name,cld_prob,days_ago = select_best_cloud_coverage_tile()
52
+ bandes_path_10,bandes_path_20,bandes_path_60,tile_path,path_cld_20,path_cld_60 =paths(name)
53
+ # create image dataset
54
+ images_10 = extract_sub_image(bandes_path_10,tile_path,cord)
55
+
56
+ # bandes with 20m resolution
57
+ #path_cld_20
58
+ images_20 = extract_sub_image(bandes_path_20,tile_path,cord,20,1)
59
+
60
+ # bandes with 60m resolution
61
+ #path_cld_60
62
+ images_60 = extract_sub_image(bandes_path_60,tile_path,cord,60)
63
+ #
64
+ feature = images_10.tolist()+images_20.tolist()+images_60.tolist()
65
+ bands = ['B02', 'B03', 'B04', 'B05', 'B06', 'B07', 'B08', 'B8A', 'B11', 'B12','B01','B09']
66
+ print("feature : ",feature)
67
+ print("BANDES : ",bands)
68
+
69
+ X = pd.DataFrame([feature],columns = bands)
70
+ print("==================== X SHAPE", X.shape)
71
+ ## Coordinate
72
+ cord_df = pd.DataFrame({"Latitude":[lat],
73
+ "Longitude":[lon]})
74
+ print("==================== cord_df SHAPE", cord_df.shape)
75
+ ## PCA dimension reduction
76
+ # later reload the pickle file
77
+ sdc_reload = pk.load(open("data/sdc.pkl",'rb'))
78
+ pca_reload = pk.load(open("data/pca.pkl",'rb'))
79
+ # standardization
80
+ X_pca = sdc_reload.transform(X)
81
+ # make pca
82
+ principalComponents = pca_reload .transform(X_pca)
83
+ principalDf = pd.DataFrame(data =principalComponents[:,:4],
84
+ columns = ["PC1","PC2","PC3","PC4"])
85
+ print("==================== principalDf SHAPE", principalDf.shape)
86
+
87
+ # vegetation index calculation
88
+ X = indices(X)
89
+ # Drop all 12 bands of S2
90
+ tab = list(range(12))
91
+ X_index = X.drop(X.iloc[:,tab],axis=1)
92
+
93
+ print("=============SHAPE1",X_index.shape)
94
+
95
+ # Create predictive features
96
+ X_final =pd.concat([cord_df,principalDf,X_index],axis=1)
97
+ print("=============SHAPE2",X_final.shape)
98
+
99
+ # load the model from disk
100
+ filename = "data/finalized_model3.sav"
101
+ loaded_model = pk.load(open(filename, 'rb'))
102
+ # make prediction
103
+ biomass = loaded_model.predict(X_final)[0]
104
+ if biomass<0:
105
+ biomass =0.0
106
+
107
+ carbon = 0.55*biomass
108
+
109
+ # NDVI
110
+ ndvi_index = ndvi(cord,name)
111
+
112
+ # deleted download files
113
+ #delete_tiles()
114
+
115
+ return str(cld_prob)+ " % cloud coverage", str(days_ago)+" days ago",str(biomass)+" t/ha", str(carbon)+" tC/ha","NDVI: "+ str(ndvi_index)
116
+
117
+ # Create title, description and article strings
118
+ title = "🌴BEEPAS : Biomass estimation to Evaluate the Environmental Performance of Agroforestry Systems🌴"
119
+ description = "This application estimates the biomass of certain areas using AI and satellite images (S2)."
120
+ article = "Created by data354."
121
+
122
+ # Create examples list from "examples/" directory
123
+ #example_list = [["examples/" + example] for example in os.listdir("examples")]
124
+ example_list = [["Foret du banco :",5.379913, -4.050445],["Pharmacie Y4 :",5.363292, -3.9481601],["Treichville Bernabé :",5.293168, -3.999796],["Adjamé :",5.346938, -4.027849],["ile boulay :",5.280498,-4.089883]]
125
+
126
+
127
+ outputs = [
128
+ gr.Textbox(label="Cloud coverage"),
129
+ gr.Textbox(label="Number of days since sensing"),
130
+ gr.Textbox(label="Above ground biomass density(AGBD) t/ha"),
131
+ gr.Textbox(label="Carbon stock density tC/ha "),
132
+ gr.Textbox(label="Mean NDVI"),]
133
+
134
+
135
+ demo = gr.Interface(
136
+ fn=predict,
137
+ inputs=["text","number", "number"],
138
+ outputs=outputs, #[ "text", "text","text","text","text"],
139
+ examples=example_list,
140
+ title=title,
141
+ description=description,
142
+ article=article,
143
+ )
144
+
145
+ demo.launch(share=True)