Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,143 +1,53 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
from
|
| 6 |
-
from sklearn.preprocessing import OneHotEncoder, StandardScaler
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
from keras.losses import MeanSquaredError
|
| 11 |
|
| 12 |
-
|
| 13 |
-
'custom_loss_name': MeanSquaredError(), # Replace 'custom_loss_name' if needed
|
| 14 |
-
}
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
try:
|
| 18 |
-
loaded_model = load_model('solar_irradiance_model.h5', custom_objects=custom_objects)
|
| 19 |
-
except Exception as e:
|
| 20 |
-
print("Error loading model:", e)
|
| 21 |
-
raise
|
| 22 |
-
|
| 23 |
-
# Load the dataset for encoder and scaler setup
|
| 24 |
-
data = pd.read_csv('Solar_Irradiance.csv')
|
| 25 |
-
data['Latitude'] = data['Latitude'].str.rstrip('°').astype(float)
|
| 26 |
-
data['Longitude'] = data['Longitude'].str.rstrip('°').astype(float)
|
| 27 |
-
|
| 28 |
-
# Features and encoder/scaler setup
|
| 29 |
-
features = data[['Month', 'Hour', 'Latitude', 'Longitude', 'Panel_Capacity(W)', 'Panel_Efficiency', 'Wind_Speed(km/h)', 'Cloud_Cover(%)', 'temperature (°f)']]
|
| 30 |
-
encoder = OneHotEncoder(sparse_output=False, categories='auto')
|
| 31 |
-
categorical_features = features[['Month', 'Hour']]
|
| 32 |
-
encoder.fit(categorical_features)
|
| 33 |
-
scaler = StandardScaler()
|
| 34 |
-
numerical_features = features[['Latitude', 'Longitude', 'Panel_Capacity(W)', 'Panel_Efficiency', 'Wind_Speed(km/h)', 'Cloud_Cover(%)', 'temperature (°f)']]
|
| 35 |
-
scaler.fit(numerical_features)
|
| 36 |
-
|
| 37 |
-
# Shadow Removal Function
|
| 38 |
-
def remove_shadows(image):
|
| 39 |
-
"""Removes shadows using illumination normalization."""
|
| 40 |
-
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 41 |
-
blurred = cv2.GaussianBlur(gray, (21, 21), 0)
|
| 42 |
-
normalized = cv2.divide(gray, blurred, scale=255)
|
| 43 |
-
result = cv2.cvtColor(normalized, cv2.COLOR_GRAY2BGR)
|
| 44 |
-
return result
|
| 45 |
-
|
| 46 |
-
# Preprocess Image with Watershed Algorithm
|
| 47 |
-
def apply_watershed(image):
|
| 48 |
-
shadow_free_image = remove_shadows(image)
|
| 49 |
-
denoised = cv2.fastNlMeansDenoisingColored(shadow_free_image, None, 10, 10, 7, 21)
|
| 50 |
-
gray = cv2.cvtColor(denoised, cv2.COLOR_BGR2GRAY)
|
| 51 |
-
_, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
|
| 52 |
-
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
|
| 53 |
-
sure_bg = cv2.dilate(binary, kernel, iterations=3)
|
| 54 |
-
sure_fg = cv2.erode(binary, kernel, iterations=3)
|
| 55 |
-
unknown = cv2.subtract(sure_bg, sure_fg)
|
| 56 |
-
_, markers = cv2.connectedComponents(sure_fg)
|
| 57 |
-
markers = markers + 1
|
| 58 |
-
markers[unknown == 255] = 0
|
| 59 |
-
markers = cv2.watershed(image, markers)
|
| 60 |
-
segmented = np.zeros_like(image)
|
| 61 |
-
segmented[markers > 1] = image[markers > 1]
|
| 62 |
-
return segmented, markers
|
| 63 |
-
|
| 64 |
-
# Calculate Usable Roof Area
|
| 65 |
-
def find_usable_area(image, min_area, panel_area):
|
| 66 |
-
segmented_image, markers = apply_watershed(image)
|
| 67 |
-
gray = cv2.cvtColor(segmented_image, cv2.COLOR_BGR2GRAY)
|
| 68 |
-
contours, _ = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 69 |
-
|
| 70 |
-
usable_area = 0
|
| 71 |
-
output_image = image.copy()
|
| 72 |
-
|
| 73 |
-
for contour in contours:
|
| 74 |
-
area = cv2.contourArea(contour)
|
| 75 |
-
if area >= min_area:
|
| 76 |
-
usable_area += area
|
| 77 |
-
cv2.drawContours(output_image, [contour], -1, (0, 255, 0), 3)
|
| 78 |
-
else:
|
| 79 |
-
cv2.drawContours(output_image, [contour], -1, (0, 0, 255), 3)
|
| 80 |
-
|
| 81 |
-
num_panels = usable_area // panel_area
|
| 82 |
-
return usable_area, int(num_panels), output_image
|
| 83 |
-
|
| 84 |
-
# Predict Irradiance
|
| 85 |
def predict_irradiance(month, hour, latitude, longitude, panel_capacity, panel_efficiency, wind_speed, cloud_cover, temperature):
|
|
|
|
| 86 |
encoded_month_hour = encoder.transform([[month, hour]])
|
|
|
|
| 87 |
scaled_features = scaler.transform([[latitude, longitude, panel_capacity, panel_efficiency, wind_speed, cloud_cover, temperature]])
|
|
|
|
| 88 |
processed_features = np.concatenate((encoded_month_hour, scaled_features), axis=1)
|
| 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 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
inputs=[
|
| 124 |
-
gr.Image(type="numpy", label="Upload Rooftop Image"),
|
| 125 |
-
gr.Dropdown(['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], label="Month"),
|
| 126 |
-
gr.Slider(0, 23, step=1, label="Hour"),
|
| 127 |
-
gr.Number(label="Latitude"),
|
| 128 |
-
gr.Number(label="Longitude"),
|
| 129 |
-
gr.Number(label="Panel Capacity (W)"),
|
| 130 |
-
gr.Number(label="Panel Efficiency (0-1)"),
|
| 131 |
-
gr.Number(label="Wind Speed (km/h)"),
|
| 132 |
-
gr.Number(label="Cloud Cover (%)"),
|
| 133 |
-
gr.Number(label="Temperature (°F)")
|
| 134 |
-
],
|
| 135 |
-
outputs=[
|
| 136 |
-
gr.Textbox(label="Results"),
|
| 137 |
-
gr.Image(type="numpy", label="Segmented Rooftop Area")
|
| 138 |
-
],
|
| 139 |
-
title="Solar Energy Potential and Cost Estimator",
|
| 140 |
-
description="Upload an image of the rooftop and enter environmental details to calculate potential solar energy, number of panels, and cost."
|
| 141 |
-
)
|
| 142 |
-
|
| 143 |
-
interface.launch()
|
|
|
|
| 1 |
+
# Load the saved model
|
| 2 |
+
# ipython-input-10-d7dffa1aa475
|
| 3 |
+
# Load the saved model
|
| 4 |
+
from keras.models import load_model
|
| 5 |
+
from keras.losses import mean_squared_error # Import the MSE loss function
|
|
|
|
| 6 |
|
| 7 |
+
# Load the model with custom_objects
|
| 8 |
+
loaded_model = load_model('solar_irradiance_model.h5', custom_objects={'mse': mean_squared_error})
|
|
|
|
| 9 |
|
| 10 |
+
# ... (rest of the code remains the same)
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# Function to predict the irradiance for a given month, hour, and other features
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
def predict_irradiance(month, hour, latitude, longitude, panel_capacity, panel_efficiency, wind_speed, cloud_cover, temperature):
|
| 14 |
+
# Encode the month and hour
|
| 15 |
encoded_month_hour = encoder.transform([[month, hour]])
|
| 16 |
+
# Scale the numerical features
|
| 17 |
scaled_features = scaler.transform([[latitude, longitude, panel_capacity, panel_efficiency, wind_speed, cloud_cover, temperature]])
|
| 18 |
+
# Combine encoded categorical and scaled numerical features
|
| 19 |
processed_features = np.concatenate((encoded_month_hour, scaled_features), axis=1)
|
| 20 |
+
# Reshape the features to match the LSTM input shape
|
| 21 |
+
reshaped_features = np.reshape(processed_features, (1, 1, processed_features.shape[1]))
|
| 22 |
+
# Predict the irradiance
|
| 23 |
+
predicted_irradiance = loaded_model.predict(reshaped_features)
|
| 24 |
+
return max(predicted_irradiance[0][0], 0.0)
|
| 25 |
+
|
| 26 |
+
# Function to get the actual irradiance for a given month
|
| 27 |
+
def get_actual_irradiance(month):
|
| 28 |
+
return data[data['Month'] == month]['Irradiance(W/m^2)'].values
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
# Example usage: Predict the irradiance for July, hour 12 with additional features
|
| 32 |
+
month = 'January'
|
| 33 |
+
hour = 12
|
| 34 |
+
predicted_irradiance = predict_irradiance(month, hour, 28.570633, 77.327215, 500, 0.15, 6.43988, 17.7, 55)
|
| 35 |
+
print(f'Predicted irradiance for {month}, hour {hour}: {predicted_irradiance}')
|
| 36 |
+
|
| 37 |
+
# Plot Actual vs. Predicted Irradiance for a specific month
|
| 38 |
+
month = 'January'
|
| 39 |
+
actual_irradiance = get_actual_irradiance(month)
|
| 40 |
+
predicted_irradiances = []
|
| 41 |
+
|
| 42 |
+
for hour in range(24):
|
| 43 |
+
irradiance = predict_irradiance(month, hour, 28.570633, 77.327215, 500, 0.15, 6.43988, 17.7, 55)
|
| 44 |
+
predicted_irradiances.append(irradiance)
|
| 45 |
+
|
| 46 |
+
plt.figure(figsize=(12, 6))
|
| 47 |
+
plt.plot(range(24), actual_irradiance, label='Actual Irradiance')
|
| 48 |
+
plt.plot(range(24), predicted_irradiances, label='Predicted Irradiance')
|
| 49 |
+
plt.xlabel('Hour')
|
| 50 |
+
plt.ylabel('Irradiance (W/m^2)')
|
| 51 |
+
plt.title(f'Actual vs. Predicted Irradiance for {month}')
|
| 52 |
+
plt.legend()
|
| 53 |
+
plt.show()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|