Spaces:
Running
Running
Esmaeilkiani
commited on
Commit
•
10ebae5
1
Parent(s):
032cf90
Update app.py
Browse files
app.py
CHANGED
@@ -5,56 +5,45 @@ import ee
|
|
5 |
import geemap
|
6 |
|
7 |
# Earth Engine Authentication (Replace with your actual authentication)
|
8 |
-
# اعتبار سنجی و اتصال به Google Earth Engine
|
9 |
service_account = 'earth-engine-service-account@ee-esmaeilkiani1387.iam.gserviceaccount.com'
|
10 |
credentials = ee.ServiceAccountCredentials(service_account, 'ee-esmaeilkiani1387-1b2c5e812a1d.json')
|
11 |
ee.Initialize(credentials)
|
12 |
|
13 |
-
|
14 |
# Load pre-trained model
|
15 |
-
model = joblib.load('updated_model.pkl')
|
16 |
|
17 |
# Load farm data
|
18 |
-
farm_data = pd.read_csv('Farm_NDRE_TimeSeries.csv')
|
19 |
farm_names = farm_data['Farm'].tolist()
|
20 |
|
21 |
# Function to calculate NDRE
|
22 |
def calculate_ndre(coordinates, start_date, end_date):
|
23 |
try:
|
24 |
-
# Define the Earth Engine region of interest (ROI)
|
25 |
roi = ee.Geometry.Point(coordinates)
|
26 |
-
|
27 |
-
# Define the image collection (replace with your actual collection ID and bands)
|
28 |
imageCollection = ee.ImageCollection('COPERNICUS/S2_SR') \
|
29 |
.filterBounds(roi) \
|
30 |
.filterDate(start_date, end_date) \
|
31 |
-
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20))
|
32 |
|
33 |
-
# Function to compute NDRE
|
34 |
def ndre(image):
|
35 |
red_edge = image.select('B8A')
|
36 |
red = image.select('B4')
|
37 |
return image.addBands(red_edge.subtract(red).divide(red_edge.add(red)).rename('NDRE'))
|
38 |
|
39 |
-
#Apply NDRE to all images in collection, and reduce to median
|
40 |
ndre_image = imageCollection.map(ndre).median().select('NDRE')
|
41 |
|
42 |
-
# Get NDRE value at the point
|
43 |
ndre_value = ndre_image.reduceRegion(
|
44 |
reducer=ee.Reducer.first(),
|
45 |
geometry=roi,
|
46 |
scale=10
|
47 |
).getInfo()
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
return ndre_value
|
52 |
|
53 |
except Exception as e:
|
54 |
st.error(f"Error calculating NDRE: {e}")
|
55 |
return None
|
56 |
|
57 |
-
|
58 |
# Streamlit UI
|
59 |
st.title("Farm Parameter Prediction App")
|
60 |
|
@@ -65,44 +54,27 @@ farm_variety = st.text_input("Farm Variety")
|
|
65 |
start_date = st.date_input("Start Date")
|
66 |
end_date = st.date_input("End Date")
|
67 |
|
68 |
-
|
69 |
-
#Find Coordinates based on Farm Name Selection
|
70 |
selected_farm_data = farm_data[farm_data['Farm'] == selected_farm]
|
71 |
coordinates = (selected_farm_data['longitude'].iloc[0], selected_farm_data['latitude'].iloc[0])
|
72 |
|
73 |
-
|
74 |
-
# Inside the 'نمایش نقشه NDRE' button's code block
|
75 |
if st.button('نمایش نقشه NDRE'):
|
76 |
-
# Call the function and store the returned value in 'NDRE'
|
77 |
NDRE = calculate_ndre(coordinates, start_date, end_date)
|
78 |
-
|
79 |
-
|
|
|
|
|
80 |
Map = geemap.Map()
|
81 |
Map.centerObject(ee.Geometry.Point(coordinates), 12)
|
82 |
|
83 |
vis_params = {'min': 0, 'max': 1, 'palette': ['blue', 'green', 'yellow', 'red']}
|
84 |
Map.addLayer(NDRE, vis_params, 'NDRE')
|
85 |
-
|
86 |
-
#Display Map
|
87 |
Map.to_streamlit(height=500)
|
|
|
|
|
88 |
|
89 |
if st.button("Predict"):
|
|
|
90 |
user_input = pd.DataFrame({
|
91 |
'Farm_Name': [selected_farm],
|
92 |
'Farm_Age': [farm_age],
|
93 |
-
'Farm_Variety': [farm_variety],
|
94 |
-
'NDRE': [ndre_value] if ndre_value is not None else [0] #Handle cases where ndre_value is None. Replace 0 with a more suitable default if needed.
|
95 |
-
})
|
96 |
-
|
97 |
-
# Feature Engineering might be needed here depending on your model's input features
|
98 |
-
|
99 |
-
prediction = model.predict(user_input)
|
100 |
-
|
101 |
-
st.write("Predictions:")
|
102 |
-
st.write(f"Brix: {prediction[0][0]}") #Assuming model outputs a list of lists
|
103 |
-
st.write(f"Pol: {prediction[0][1]}")
|
104 |
-
st.write(f"Purity: {prediction[0][2]}")
|
105 |
-
st.write(f"RS: {prediction[0][3]}")
|
106 |
-
|
107 |
-
|
108 |
-
|
|
|
5 |
import geemap
|
6 |
|
7 |
# Earth Engine Authentication (Replace with your actual authentication)
|
|
|
8 |
service_account = 'earth-engine-service-account@ee-esmaeilkiani1387.iam.gserviceaccount.com'
|
9 |
credentials = ee.ServiceAccountCredentials(service_account, 'ee-esmaeilkiani1387-1b2c5e812a1d.json')
|
10 |
ee.Initialize(credentials)
|
11 |
|
|
|
12 |
# Load pre-trained model
|
13 |
+
model = joblib.load('updated_model.pkl')
|
14 |
|
15 |
# Load farm data
|
16 |
+
farm_data = pd.read_csv('Farm_NDRE_TimeSeries.csv')
|
17 |
farm_names = farm_data['Farm'].tolist()
|
18 |
|
19 |
# Function to calculate NDRE
|
20 |
def calculate_ndre(coordinates, start_date, end_date):
|
21 |
try:
|
|
|
22 |
roi = ee.Geometry.Point(coordinates)
|
|
|
|
|
23 |
imageCollection = ee.ImageCollection('COPERNICUS/S2_SR') \
|
24 |
.filterBounds(roi) \
|
25 |
.filterDate(start_date, end_date) \
|
26 |
+
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20))
|
27 |
|
|
|
28 |
def ndre(image):
|
29 |
red_edge = image.select('B8A')
|
30 |
red = image.select('B4')
|
31 |
return image.addBands(red_edge.subtract(red).divide(red_edge.add(red)).rename('NDRE'))
|
32 |
|
|
|
33 |
ndre_image = imageCollection.map(ndre).median().select('NDRE')
|
34 |
|
|
|
35 |
ndre_value = ndre_image.reduceRegion(
|
36 |
reducer=ee.Reducer.first(),
|
37 |
geometry=roi,
|
38 |
scale=10
|
39 |
).getInfo()
|
40 |
|
41 |
+
return ndre_value.get('NDRE') if ndre_value else None
|
|
|
|
|
42 |
|
43 |
except Exception as e:
|
44 |
st.error(f"Error calculating NDRE: {e}")
|
45 |
return None
|
46 |
|
|
|
47 |
# Streamlit UI
|
48 |
st.title("Farm Parameter Prediction App")
|
49 |
|
|
|
54 |
start_date = st.date_input("Start Date")
|
55 |
end_date = st.date_input("End Date")
|
56 |
|
|
|
|
|
57 |
selected_farm_data = farm_data[farm_data['Farm'] == selected_farm]
|
58 |
coordinates = (selected_farm_data['longitude'].iloc[0], selected_farm_data['latitude'].iloc[0])
|
59 |
|
|
|
|
|
60 |
if st.button('نمایش نقشه NDRE'):
|
|
|
61 |
NDRE = calculate_ndre(coordinates, start_date, end_date)
|
62 |
+
if NDRE is not None:
|
63 |
+
st.session_state.ndre_value = NDRE
|
64 |
+
st.write(f'شاخص NDRE: {NDRE}')
|
65 |
+
|
66 |
Map = geemap.Map()
|
67 |
Map.centerObject(ee.Geometry.Point(coordinates), 12)
|
68 |
|
69 |
vis_params = {'min': 0, 'max': 1, 'palette': ['blue', 'green', 'yellow', 'red']}
|
70 |
Map.addLayer(NDRE, vis_params, 'NDRE')
|
|
|
|
|
71 |
Map.to_streamlit(height=500)
|
72 |
+
else:
|
73 |
+
st.error("Unable to calculate NDRE.")
|
74 |
|
75 |
if st.button("Predict"):
|
76 |
+
ndre_value = st.session_state.get('ndre_value', 0) # Default to 0 if not calculated
|
77 |
user_input = pd.DataFrame({
|
78 |
'Farm_Name': [selected_farm],
|
79 |
'Farm_Age': [farm_age],
|
80 |
+
'Farm_Variety': [farm_variety],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|