heliogrid / app.py
Harsh-7300's picture
Update app.py
deccab1 verified
import cv2
import numpy as np
import pandas as pd
import streamlit as st
from keras.models import load_model
from sklearn.preprocessing import OneHotEncoder, StandardScaler
# Load the pre-trained model
loaded_model = load_model('solar_irradiance_model.keras')
# Load the dataset for encoder and scaler setup
data = pd.read_csv('Solar_Irradiance.csv')
data['Latitude'] = data['Latitude'].str.rstrip('°').astype(float)
data['Longitude'] = data['Longitude'].str.rstrip('°').astype(float)
# Features and encoder/scaler setup
features = data[['Month', 'Hour', 'Latitude', 'Longitude', 'Panel_Capacity(W)', 'Panel_Efficiency', 'Wind_Speed(km/h)', 'Cloud_Cover(%)', 'temperature (°f)']]
encoder = OneHotEncoder(sparse_output=False, categories='auto')
categorical_features = features[['Month', 'Hour']]
encoder.fit(categorical_features)
scaler = StandardScaler()
numerical_features = features[['Latitude', 'Longitude', 'Panel_Capacity(W)', 'Panel_Efficiency', 'Wind_Speed(km/h)', 'Cloud_Cover(%)', 'temperature (°f)']]
scaler.fit(numerical_features)
# Shadow Removal Function
def remove_shadows(image):
"""Removes shadows using illumination normalization."""
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (21, 21), 0)
normalized = cv2.divide(gray, blurred, scale=255)
result = cv2.cvtColor(normalized, cv2.COLOR_GRAY2BGR)
return result
# Preprocess Image with Canny Edge Detection
def apply_canny_edge(image):
# Step 1: Remove shadows
shadow_free_image = remove_shadows(image)
# Step 2: Convert to grayscale
gray = cv2.cvtColor(shadow_free_image, cv2.COLOR_BGR2GRAY)
# Step 3: Apply Canny edge detection
edges = cv2.Canny(gray, 100, 200)
# Step 4: Dilate the edges to make them more prominent
kernel = np.ones((5, 5), np.uint8)
dilated_edges = cv2.dilate(edges, kernel, iterations=2)
return dilated_edges
# Predict Irradiance Function
def predict_irradiance(month, hour, latitude, longitude, panel_capacity, panel_efficiency, wind_speed, cloud_cover, temperature):
# Ensure the month is passed as a string (e.g., "January", "February")
# Encode the month and hour using the same encoder fitted during training
encoded_month_hour = encoder.transform([[month, hour]])
# Scale the numerical features
scaled_features = scaler.transform([[latitude, longitude, panel_capacity, panel_efficiency, wind_speed, cloud_cover, temperature]])
# Combine the encoded categorical features with the scaled numerical features
processed_features = np.concatenate((encoded_month_hour, scaled_features), axis=1)
# Reshape features to match LSTM input
reshaped_features = np.reshape(processed_features, (1, 1, processed_features.shape[1]))
# Predict the irradiance using the trained model
predicted_irradiance = loaded_model.predict(reshaped_features)
# Ensure the result is not negative
return max(predicted_irradiance[0][0], 0.0)
# Streamlit Interface Setup
st.title("Solar Irradiance Predictor")
# Upload image
uploaded_image = st.file_uploader("Upload Rooftop Image", type=["jpg", "jpeg", "png"])
if uploaded_image is not None:
image = np.array(cv2.imdecode(np.frombuffer(uploaded_image.read(), np.uint8), 1))
# User inputs
month = st.selectbox("Month", ['January', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december'])
hour = st.slider("Hour", 0, 23, step=1)
latitude = st.number_input("Latitude", value=28.57)
longitude = st.number_input("Longitude", value=77.33)
panel_capacity = st.number_input("Panel Capacity (W)", value=500.0)
panel_efficiency = st.number_input("Panel Efficiency (0-1)", value=0.15)
wind_speed = st.number_input("Wind Speed (km/h)", value=6.44)
cloud_cover = st.number_input("Cloud Cover (%)", value=17.7)
temperature = st.number_input("Temperature (°F)", value=55.0)
# Predict Button
if st.button("Predict Irradiance"):
irradiance = predict_irradiance(month, hour, latitude, longitude, panel_capacity, panel_efficiency, wind_speed, cloud_cover, temperature)
# Display results
st.subheader("Results")
st.text(f"Predicted Irradiance: {irradiance:.2f} W/m²")