Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ import cv2
|
|
2 |
import numpy as np
|
3 |
import gradio as gr
|
4 |
import tensorflow as tf
|
|
|
5 |
import matplotlib.pyplot as plt
|
6 |
from mpl_toolkits.mplot3d import Axes3D
|
7 |
|
@@ -19,8 +20,6 @@ USD_TO_INR_CONVERSION_RATE = 83 # Convert USD to INR
|
|
19 |
# Load the pre-trained LSTM model
|
20 |
model = tf.keras.models.load_model('./solar_irradiance_model.keras') # Or .h5
|
21 |
|
22 |
-
|
23 |
-
|
24 |
# Preprocessing image
|
25 |
def preprocess_image(image, clip_limit=2.0, tile_size=8):
|
26 |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
@@ -102,10 +101,20 @@ def visualize_roof_panels(roof_area, panel_length, panel_width, num_panels):
|
|
102 |
return fig
|
103 |
|
104 |
# Process uploaded image and calculate solar panel details
|
105 |
-
def process_image_and_forecast(image, min_area, pixel_area, clip_limit, tile_size, panel_type, panel_length, panel_width, irradiance, electricity_rate,
|
106 |
if image is None:
|
107 |
return None, "Error: No image provided."
|
108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
# Preprocess the image
|
110 |
preprocessed = preprocess_image(image, clip_limit, tile_size)
|
111 |
|
@@ -116,16 +125,13 @@ def process_image_and_forecast(image, min_area, pixel_area, clip_limit, tile_siz
|
|
116 |
roof_area = calculate_roof_area(roof_contour, pixel_area)
|
117 |
|
118 |
# Calculate the number of panels, energy output, and cost
|
119 |
-
num_panels, total_energy_kwh, total_cost_inr = calculate_panels_and_energy(
|
120 |
-
roof_area, panel_length, panel_width, panel_type, irradiance
|
121 |
-
)
|
122 |
|
123 |
# Estimate yearly cost savings
|
124 |
yearly_savings = estimate_savings(total_energy_kwh, electricity_rate)
|
125 |
|
126 |
# Forecast future energy production using the LSTM model
|
127 |
try:
|
128 |
-
historical_data = list(map(float, historical_energy_data.split(','))) # Convert input to a list of floats
|
129 |
predicted_energy = forecast_solar_energy(historical_data)
|
130 |
forecast_text = f"Predicted Energy Output for Next Day: {predicted_energy:.2f} kWh"
|
131 |
except Exception as e:
|
@@ -147,7 +153,7 @@ def process_image_and_forecast(image, min_area, pixel_area, clip_limit, tile_siz
|
|
147 |
interface = gr.Interface(
|
148 |
fn=process_image_and_forecast,
|
149 |
inputs=[
|
150 |
-
gr.Image(type="numpy", label="Upload Image"),
|
151 |
gr.Slider(500, 5000, value=1000, step=100, label="Minimum Contour Area"),
|
152 |
gr.Number(value=0.1, label="Pixel to Area Conversion (sq. meters)"),
|
153 |
gr.Slider(1.0, 5.0, value=2.0, step=0.1, label="CLAHE Clip Limit"),
|
@@ -157,7 +163,7 @@ interface = gr.Interface(
|
|
157 |
gr.Number(value=1.0, label="Panel Width (m)"),
|
158 |
gr.Number(value=1000, label="Average Solar Irradiance (W/m²)"),
|
159 |
gr.Number(value=0.15, label="Electricity Rate (₹/kWh)"),
|
160 |
-
gr.
|
161 |
],
|
162 |
outputs=[
|
163 |
gr.Image(type="numpy", label="Processed Image with Contours"),
|
@@ -165,7 +171,7 @@ interface = gr.Interface(
|
|
165 |
gr.Plot(label="3D Visualization of Panels")
|
166 |
],
|
167 |
title="Advanced Solar Panel Placement Estimator with Energy Forecasting",
|
168 |
-
description="Upload an image and
|
169 |
)
|
170 |
|
171 |
if __name__ == '__main__':
|
|
|
2 |
import numpy as np
|
3 |
import gradio as gr
|
4 |
import tensorflow as tf
|
5 |
+
import pandas as pd
|
6 |
import matplotlib.pyplot as plt
|
7 |
from mpl_toolkits.mplot3d import Axes3D
|
8 |
|
|
|
20 |
# Load the pre-trained LSTM model
|
21 |
model = tf.keras.models.load_model('./solar_irradiance_model.keras') # Or .h5
|
22 |
|
|
|
|
|
23 |
# Preprocessing image
|
24 |
def preprocess_image(image, clip_limit=2.0, tile_size=8):
|
25 |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
|
|
101 |
return fig
|
102 |
|
103 |
# Process uploaded image and calculate solar panel details
|
104 |
+
def process_image_and_forecast(image, min_area, pixel_area, clip_limit, tile_size, panel_type, panel_length, panel_width, irradiance, electricity_rate, historical_file):
|
105 |
if image is None:
|
106 |
return None, "Error: No image provided."
|
107 |
|
108 |
+
# Process historical file and extract energy data
|
109 |
+
if historical_file is None:
|
110 |
+
return None, "Error: No dataset provided."
|
111 |
+
|
112 |
+
try:
|
113 |
+
df = pd.read_csv(historical_file.name)
|
114 |
+
historical_data = df['energy_kWh'].tolist() # Extract energy values (assuming the column name is 'energy_kWh')
|
115 |
+
except Exception as e:
|
116 |
+
return None, f"Error reading file: {str(e)}"
|
117 |
+
|
118 |
# Preprocess the image
|
119 |
preprocessed = preprocess_image(image, clip_limit, tile_size)
|
120 |
|
|
|
125 |
roof_area = calculate_roof_area(roof_contour, pixel_area)
|
126 |
|
127 |
# Calculate the number of panels, energy output, and cost
|
128 |
+
num_panels, total_energy_kwh, total_cost_inr = calculate_panels_and_energy(roof_area, panel_length, panel_width, panel_type, irradiance)
|
|
|
|
|
129 |
|
130 |
# Estimate yearly cost savings
|
131 |
yearly_savings = estimate_savings(total_energy_kwh, electricity_rate)
|
132 |
|
133 |
# Forecast future energy production using the LSTM model
|
134 |
try:
|
|
|
135 |
predicted_energy = forecast_solar_energy(historical_data)
|
136 |
forecast_text = f"Predicted Energy Output for Next Day: {predicted_energy:.2f} kWh"
|
137 |
except Exception as e:
|
|
|
153 |
interface = gr.Interface(
|
154 |
fn=process_image_and_forecast,
|
155 |
inputs=[
|
156 |
+
gr.Image(type="numpy", label="Upload Roof Image"),
|
157 |
gr.Slider(500, 5000, value=1000, step=100, label="Minimum Contour Area"),
|
158 |
gr.Number(value=0.1, label="Pixel to Area Conversion (sq. meters)"),
|
159 |
gr.Slider(1.0, 5.0, value=2.0, step=0.1, label="CLAHE Clip Limit"),
|
|
|
163 |
gr.Number(value=1.0, label="Panel Width (m)"),
|
164 |
gr.Number(value=1000, label="Average Solar Irradiance (W/m²)"),
|
165 |
gr.Number(value=0.15, label="Electricity Rate (₹/kWh)"),
|
166 |
+
gr.File(label="Upload CSV Dataset (Historical Energy Data)")
|
167 |
],
|
168 |
outputs=[
|
169 |
gr.Image(type="numpy", label="Processed Image with Contours"),
|
|
|
171 |
gr.Plot(label="3D Visualization of Panels")
|
172 |
],
|
173 |
title="Advanced Solar Panel Placement Estimator with Energy Forecasting",
|
174 |
+
description="Upload an image and a CSV file to estimate the roof area, panel placement, energy output, cost, savings, and forecast future energy production."
|
175 |
)
|
176 |
|
177 |
if __name__ == '__main__':
|