Spaces:
Sleeping
Sleeping
Harsh-7300
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -4,12 +4,11 @@ import pandas as pd
|
|
4 |
import streamlit as st
|
5 |
from keras.models import load_model
|
6 |
from sklearn.preprocessing import OneHotEncoder, StandardScaler
|
7 |
-
from PIL import Image
|
8 |
|
9 |
# Load the pre-trained model
|
10 |
loaded_model = load_model('solar_irradiance_model.keras')
|
11 |
|
12 |
-
# Load dataset for encoder and scaler setup
|
13 |
data = pd.read_csv('Solar_Irradiance.csv')
|
14 |
data['Latitude'] = data['Latitude'].str.rstrip('°').astype(float)
|
15 |
data['Longitude'] = data['Longitude'].str.rstrip('°').astype(float)
|
@@ -17,7 +16,7 @@ data['Longitude'] = data['Longitude'].str.rstrip('°').astype(float)
|
|
17 |
# Features and encoder/scaler setup
|
18 |
features = data[['Month', 'Hour', 'Latitude', 'Longitude', 'Panel_Capacity(W)', 'Panel_Efficiency', 'Wind_Speed(km/h)', 'Cloud_Cover(%)', 'temperature (°f)']]
|
19 |
|
20 |
-
encoder = OneHotEncoder(sparse_output=False)
|
21 |
categorical_features = features[['Month', 'Hour']]
|
22 |
encoder.fit(categorical_features)
|
23 |
|
@@ -25,118 +24,77 @@ scaler = StandardScaler()
|
|
25 |
numerical_features = features[['Latitude', 'Longitude', 'Panel_Capacity(W)', 'Panel_Efficiency', 'Wind_Speed(km/h)', 'Cloud_Cover(%)', 'temperature (°f)']]
|
26 |
scaler.fit(numerical_features)
|
27 |
|
28 |
-
#
|
29 |
-
def
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
)
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
41 |
scaled_features = scaler.transform([[latitude, longitude, panel_capacity, panel_efficiency, wind_speed, cloud_cover, temperature]])
|
42 |
-
|
43 |
-
# Combine encoded
|
44 |
processed_features = np.concatenate((encoded_month_hour, scaled_features), axis=1)
|
45 |
-
|
46 |
# Reshape features to match LSTM input
|
47 |
reshaped_features = np.reshape(processed_features, (1, 1, processed_features.shape[1]))
|
48 |
-
|
49 |
-
# Predict irradiance
|
50 |
predicted_irradiance = loaded_model.predict(reshaped_features)
|
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 |
-
usable_area = pixel_area * conversion_factor
|
83 |
-
|
84 |
-
final_image = image.copy()
|
85 |
-
cv2.drawContours(final_image, contours, -1, (0, 255, 0), 2)
|
86 |
-
return final_image, usable_area
|
87 |
-
|
88 |
-
# Streamlit Interface
|
89 |
-
st.title("Solar Energy and Rooftop Panel Planner")
|
90 |
-
|
91 |
-
# File uploader for rooftop image
|
92 |
-
uploaded_file = st.file_uploader("Upload Rooftop Image", type=["png", "jpg", "jpeg"])
|
93 |
-
|
94 |
-
# Geographic bounds input with default values
|
95 |
-
lat_min = st.number_input("Latitude Min", value=90.34, step=0.0001)
|
96 |
-
lat_max = st.number_input("Latitude Max", value=90.37, step=0.0001)
|
97 |
-
lon_min = st.number_input("Longitude Min", value=89.78, step=0.0001)
|
98 |
-
lon_max = st.number_input("Longitude Max", value=89.80, step=0.0001)
|
99 |
-
|
100 |
-
# Environmental inputs for solar irradiance prediction
|
101 |
-
month = st.selectbox("Month", ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'])
|
102 |
-
hour = st.slider("Hour", 0, 23, step=1)
|
103 |
-
latitude = st.number_input("Latitude", value=90.35)
|
104 |
-
longitude = st.number_input("Longitude", value=89.79)
|
105 |
-
panel_efficiency = st.number_input("Panel Efficiency (0-1)", value=0.15)
|
106 |
-
wind_speed = st.number_input("Wind Speed (km/h)", value=6.44)
|
107 |
-
cloud_cover = st.number_input("Cloud Cover (%)", value=17.7)
|
108 |
-
temperature = st.number_input("Temperature (°F)", value=55.0)
|
109 |
-
|
110 |
-
# Panel dimensions and cost
|
111 |
-
panel_length = st.number_input("Panel Length (m)", value=5.0)
|
112 |
-
panel_breadth = st.number_input("Panel Breadth (m)", value=5.0)
|
113 |
-
|
114 |
-
# Button to process the image and calculate solar metrics
|
115 |
-
if st.button("Calculate"):
|
116 |
-
if uploaded_file is not None:
|
117 |
-
image = np.array(Image.open(uploaded_file).convert("RGB"))
|
118 |
-
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
119 |
-
|
120 |
-
geographic_bounds = {
|
121 |
-
'lat_min': lat_min,
|
122 |
-
'lat_max': lat_max,
|
123 |
-
'lon_min': lon_min,
|
124 |
-
'lon_max': lon_max,
|
125 |
-
}
|
126 |
-
|
127 |
-
final_image, usable_area = process_rooftop_image(image, geographic_bounds)
|
128 |
-
panel_area = panel_length * panel_breadth
|
129 |
-
panel_count = int(usable_area // panel_area)
|
130 |
-
|
131 |
-
st.image(cv2.cvtColor(final_image, cv2.COLOR_BGR2RGB), caption="Processed Image with Boundaries")
|
132 |
-
st.write(f"Usable Rooftop Area: **{usable_area:.2f} square meters**")
|
133 |
-
st.write(f"Number of Panels: **{panel_count}**")
|
134 |
-
|
135 |
-
irradiance = predict_irradiance(month, hour, latitude, longitude, panel_efficiency, wind_speed, cloud_cover, temperature)
|
136 |
-
total_energy = (irradiance * panel_efficiency * panel_count * panel_area) / 1000 # Convert to kWh
|
137 |
-
panel_cost = panel_count * 10000 # Fixed cost for 50 kW panels
|
138 |
-
st.write(f"Predicted Irradiance: **{irradiance:.2f} W/m²**")
|
139 |
-
st.write(f"Total Potential Energy: **{total_energy:.2f} kWh**")
|
140 |
-
st.write(f"Total Cost for Panels: **₹{panel_cost:.2f}**")
|
141 |
-
else:
|
142 |
-
st.error("Please upload a rooftop image.")
|
|
|
4 |
import streamlit as st
|
5 |
from keras.models import load_model
|
6 |
from sklearn.preprocessing import OneHotEncoder, StandardScaler
|
|
|
7 |
|
8 |
# Load the pre-trained model
|
9 |
loaded_model = load_model('solar_irradiance_model.keras')
|
10 |
|
11 |
+
# Load the dataset for encoder and scaler setup
|
12 |
data = pd.read_csv('Solar_Irradiance.csv')
|
13 |
data['Latitude'] = data['Latitude'].str.rstrip('°').astype(float)
|
14 |
data['Longitude'] = data['Longitude'].str.rstrip('°').astype(float)
|
|
|
16 |
# Features and encoder/scaler setup
|
17 |
features = data[['Month', 'Hour', 'Latitude', 'Longitude', 'Panel_Capacity(W)', 'Panel_Efficiency', 'Wind_Speed(km/h)', 'Cloud_Cover(%)', 'temperature (°f)']]
|
18 |
|
19 |
+
encoder = OneHotEncoder(sparse_output=False, categories='auto')
|
20 |
categorical_features = features[['Month', 'Hour']]
|
21 |
encoder.fit(categorical_features)
|
22 |
|
|
|
24 |
numerical_features = features[['Latitude', 'Longitude', 'Panel_Capacity(W)', 'Panel_Efficiency', 'Wind_Speed(km/h)', 'Cloud_Cover(%)', 'temperature (°f)']]
|
25 |
scaler.fit(numerical_features)
|
26 |
|
27 |
+
# Shadow Removal Function
|
28 |
+
def remove_shadows(image):
|
29 |
+
"""Removes shadows using illumination normalization."""
|
30 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
31 |
+
blurred = cv2.GaussianBlur(gray, (21, 21), 0)
|
32 |
+
normalized = cv2.divide(gray, blurred, scale=255)
|
33 |
+
result = cv2.cvtColor(normalized, cv2.COLOR_GRAY2BGR)
|
34 |
+
return result
|
35 |
+
|
36 |
+
# Preprocess Image with Canny Edge Detection
|
37 |
+
def apply_canny_edge(image):
|
38 |
+
# Step 1: Remove shadows
|
39 |
+
shadow_free_image = remove_shadows(image)
|
40 |
+
|
41 |
+
# Step 2: Convert to grayscale
|
42 |
+
gray = cv2.cvtColor(shadow_free_image, cv2.COLOR_BGR2GRAY)
|
43 |
+
|
44 |
+
# Step 3: Apply Canny edge detection
|
45 |
+
edges = cv2.Canny(gray, 100, 200)
|
46 |
+
|
47 |
+
# Step 4: Dilate the edges to make them more prominent
|
48 |
+
kernel = np.ones((5, 5), np.uint8)
|
49 |
+
dilated_edges = cv2.dilate(edges, kernel, iterations=2)
|
50 |
+
|
51 |
+
return dilated_edges
|
52 |
|
53 |
+
# Predict Irradiance Function
|
54 |
+
def predict_irradiance(month, hour, latitude, longitude, panel_capacity, panel_efficiency, wind_speed, cloud_cover, temperature):
|
55 |
+
# Ensure the month is passed as a string (e.g., "January", "February")
|
56 |
+
# Encode the month and hour using the same encoder fitted during training
|
57 |
+
encoded_month_hour = encoder.transform([[month, hour]])
|
58 |
+
|
59 |
+
# Scale the numerical features
|
60 |
scaled_features = scaler.transform([[latitude, longitude, panel_capacity, panel_efficiency, wind_speed, cloud_cover, temperature]])
|
61 |
+
|
62 |
+
# Combine the encoded categorical features with the scaled numerical features
|
63 |
processed_features = np.concatenate((encoded_month_hour, scaled_features), axis=1)
|
64 |
+
|
65 |
# Reshape features to match LSTM input
|
66 |
reshaped_features = np.reshape(processed_features, (1, 1, processed_features.shape[1]))
|
67 |
+
|
68 |
+
# Predict the irradiance using the trained model
|
69 |
predicted_irradiance = loaded_model.predict(reshaped_features)
|
70 |
+
|
71 |
+
# Ensure the result is not negative
|
72 |
+
return max(predicted_irradiance[0][0], 0.0)
|
73 |
+
|
74 |
+
# Streamlit Interface Setup
|
75 |
+
st.title("Solar Irradiance Predictor")
|
76 |
+
|
77 |
+
# Upload image
|
78 |
+
uploaded_image = st.file_uploader("Upload Rooftop Image", type=["jpg", "jpeg", "png"])
|
79 |
+
|
80 |
+
if uploaded_image is not None:
|
81 |
+
image = np.array(cv2.imdecode(np.frombuffer(uploaded_image.read(), np.uint8), 1))
|
82 |
+
|
83 |
+
# User inputs
|
84 |
+
month = st.selectbox("Month", ['January', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december'])
|
85 |
+
hour = st.slider("Hour", 0, 23, step=1)
|
86 |
+
latitude = st.number_input("Latitude", value=28.57)
|
87 |
+
longitude = st.number_input("Longitude", value=77.33)
|
88 |
+
panel_capacity = st.number_input("Panel Capacity (W)", value=500.0)
|
89 |
+
panel_efficiency = st.number_input("Panel Efficiency (0-1)", value=0.15)
|
90 |
+
wind_speed = st.number_input("Wind Speed (km/h)", value=6.44)
|
91 |
+
cloud_cover = st.number_input("Cloud Cover (%)", value=17.7)
|
92 |
+
temperature = st.number_input("Temperature (°F)", value=55.0)
|
93 |
+
|
94 |
+
# Predict Button
|
95 |
+
if st.button("Predict Irradiance"):
|
96 |
+
irradiance = predict_irradiance(month, hour, latitude, longitude, panel_capacity, panel_efficiency, wind_speed, cloud_cover, temperature)
|
97 |
+
|
98 |
+
# Display results
|
99 |
+
st.subheader("Results")
|
100 |
+
st.text(f"Predicted Irradiance: {irradiance:.2f} W/m²")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|