Spaces:
Running
Running
Esmaeilkiani
commited on
Commit
•
d5b2220
1
Parent(s):
2d4b08f
Update app.py
Browse files
app.py
CHANGED
@@ -1,125 +1,110 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import
|
3 |
-
import
|
4 |
-
import
|
5 |
-
import
|
6 |
-
import matplotlib.pyplot as plt
|
7 |
-
import geopandas as gpd
|
8 |
-
import zipfile
|
9 |
-
import os
|
10 |
-
import requests
|
11 |
|
|
|
12 |
# اعتبار سنجی و اتصال به Google Earth Engine
|
13 |
service_account = 'earth-engine-service-account@ee-esmaeilkiani1387.iam.gserviceaccount.com'
|
14 |
credentials = ee.ServiceAccountCredentials(service_account, 'ee-esmaeilkiani1387-1b2c5e812a1d.json')
|
15 |
-
ee.Initialize(credentials)
|
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 |
-
fig, ax = plt.subplots()
|
116 |
-
ax.bar(['NDRE', 'مناسب برای برداشت'], [mean_ndre, 0.5], color=['green', 'orange'])
|
117 |
-
ax.set_ylim(0, 1)
|
118 |
-
ax.set_title(f"شاخص NDRE برای مزرعه {farm_name}")
|
119 |
-
st.pyplot(fig)
|
120 |
-
|
121 |
-
# نمایش وضعیت برداشت
|
122 |
-
if mean_ndre > 0.5:
|
123 |
-
st.write(f"مزرعه {farm_name} به حد قابل قبول برای برداشت رسیده است (NDRE = {mean_ndre:.2f}).")
|
124 |
-
else:
|
125 |
-
st.write(f"مزرعه {farm_name} هنوز آماده برداشت نیست (NDRE = {mean_ndre:.2f}).")
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import joblib
|
4 |
+
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') # Replace 'updated_model.pkl' with your actual model file
|
16 |
+
|
17 |
+
# Load farm data
|
18 |
+
farm_data = pd.read_csv('Farm_NDRE_TimeSeries.csv') # Replace 'Farm_NDRE_TimeSeries.csv' with your actual data file
|
19 |
+
farm_names = farm_data['Farm_Name'].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)) #Cloud filtering
|
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 |
+
ndre_value = ndre_value.get('NDRE') #Extract from dictionary
|
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 |
+
|
61 |
+
# User input
|
62 |
+
selected_farm = st.selectbox("Select Farm", farm_names)
|
63 |
+
farm_age = st.number_input("Farm Age (years)", min_value=0)
|
64 |
+
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 |
+
if st.button("Calculate NDRE and Show Map"):
|
75 |
+
ndre_value = calculate_ndre(coordinates, start_date.strftime("%Y-%m-%d"), end_date.strftime("%Y-%m-%d"))
|
76 |
+
if ndre_value is not None:
|
77 |
+
st.write(f"NDRE Value: {ndre_value}")
|
78 |
+
# Map Display (Replace with your actual map display logic)
|
79 |
+
Map = geemap.Map()
|
80 |
+
Map.centerObject(ee.Geometry.Point(coordinates), 12)
|
81 |
+
|
82 |
+
# Add NDRE layer
|
83 |
+
#This part requires more detailed specification of the image and display parameters.
|
84 |
+
#Example (Adapt according to your NDRE image and color scheme)
|
85 |
+
#vis_params = {'min': 0, 'max': 1, 'palette': ['blue', 'green', 'yellow', 'red']}
|
86 |
+
#Map.addLayer(ndre_image, vis_params, 'NDRE')
|
87 |
+
|
88 |
+
#Display Map
|
89 |
+
Map.to_streamlit(height=500)
|
90 |
+
|
91 |
+
if st.button("Predict"):
|
92 |
+
user_input = pd.DataFrame({
|
93 |
+
'Farm_Name': [selected_farm],
|
94 |
+
'Farm_Age': [farm_age],
|
95 |
+
'Farm_Variety': [farm_variety],
|
96 |
+
'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.
|
97 |
+
})
|
98 |
+
|
99 |
+
# Feature Engineering might be needed here depending on your model's input features
|
100 |
+
|
101 |
+
prediction = model.predict(user_input)
|
102 |
+
|
103 |
+
st.write("Predictions:")
|
104 |
+
st.write(f"Brix: {prediction[0][0]}") #Assuming model outputs a list of lists
|
105 |
+
st.write(f"Pol: {prediction[0][1]}")
|
106 |
+
st.write(f"Purity: {prediction[0][2]}")
|
107 |
+
st.write(f"RS: {prediction[0][3]}")
|
108 |
+
|
109 |
+
|
110 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|