Update app.py
Browse files
app.py
CHANGED
|
@@ -113,25 +113,32 @@ def forecast_solar_energy(historical_data):
|
|
| 113 |
# Read and preprocess CSV data for prediction
|
| 114 |
def preprocess_csv_for_prediction(csv_file):
|
| 115 |
try:
|
| 116 |
-
# Load the
|
| 117 |
-
data = pd.read_csv(csv_file.name)
|
| 118 |
|
| 119 |
-
#
|
| 120 |
-
|
| 121 |
-
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
-
#
|
| 124 |
if data.shape[0] < 43:
|
| 125 |
-
return "Error: CSV
|
| 126 |
|
| 127 |
-
# Select the
|
| 128 |
-
|
|
|
|
|
|
|
| 129 |
|
| 130 |
-
#
|
| 131 |
-
|
|
|
|
| 132 |
|
| 133 |
-
#
|
| 134 |
-
|
|
|
|
| 135 |
|
| 136 |
return sequence
|
| 137 |
except Exception as e:
|
|
@@ -140,13 +147,13 @@ def preprocess_csv_for_prediction(csv_file):
|
|
| 140 |
|
| 141 |
# Function to process image and forecast energy using CSV file
|
| 142 |
# Function to process image and forecast energy using CSV file
|
| 143 |
-
def process_image_and_forecast(image, min_area, pixel_area, clip_limit, tile_size, panel_type,
|
| 144 |
panel_length, panel_width, irradiance, electricity_rate, historical_data_csv):
|
| 145 |
try:
|
|
|
|
| 146 |
if image is None:
|
| 147 |
return None, "Error: No image provided."
|
| 148 |
|
| 149 |
-
# Process image
|
| 150 |
preprocessed = preprocess_image(image, clip_limit, tile_size)
|
| 151 |
roof_contour = find_roof_contour(preprocessed, min_area)
|
| 152 |
roof_area = calculate_roof_area(roof_contour, pixel_area)
|
|
@@ -155,25 +162,24 @@ def process_image_and_forecast(image, min_area, pixel_area, clip_limit, tile_siz
|
|
| 155 |
num_panels, total_energy_kwh, total_cost_inr = calculate_panels_and_energy(
|
| 156 |
roof_area, panel_length, panel_width, panel_type, irradiance
|
| 157 |
)
|
| 158 |
-
|
| 159 |
yearly_savings = estimate_savings(total_energy_kwh, electricity_rate)
|
| 160 |
|
| 161 |
-
# Forecast energy
|
| 162 |
forecast_text = ""
|
| 163 |
if historical_data_csv:
|
| 164 |
-
# Preprocess and predict from CSV data
|
| 165 |
sequence = preprocess_csv_for_prediction(historical_data_csv)
|
| 166 |
-
if isinstance(sequence, str): #
|
| 167 |
return image, sequence
|
| 168 |
|
|
|
|
| 169 |
predicted_energy = model.predict(sequence)
|
| 170 |
forecast_text = f"Predicted Energy Output for Next Day: {predicted_energy[0][0]:.2f} kWh"
|
| 171 |
|
| 172 |
-
# Draw the roof contour
|
| 173 |
if roof_contour is not None:
|
| 174 |
cv2.drawContours(image, [roof_contour], -1, (0, 255, 0), 2)
|
| 175 |
|
| 176 |
-
#
|
| 177 |
result_text = (
|
| 178 |
f"Roof Area: {roof_area:.2f} sq.m | Panels: {num_panels} | "
|
| 179 |
f"Energy Output: {total_energy_kwh:.2f} kWh | Cost: ₹{total_cost_inr:.2f} | "
|
|
@@ -185,6 +191,7 @@ def process_image_and_forecast(image, min_area, pixel_area, clip_limit, tile_siz
|
|
| 185 |
return image, f"Error: {str(e)}"
|
| 186 |
|
| 187 |
|
|
|
|
| 188 |
# Gradio Interface
|
| 189 |
interface = gr.Interface(
|
| 190 |
fn=process_image_and_forecast,
|
|
|
|
| 113 |
# Read and preprocess CSV data for prediction
|
| 114 |
def preprocess_csv_for_prediction(csv_file):
|
| 115 |
try:
|
| 116 |
+
# Load the dataset from CSV
|
| 117 |
+
data = pd.read_csv(csv_file.name)
|
| 118 |
|
| 119 |
+
# Validate column names
|
| 120 |
+
expected_columns = ['Month', 'Hour', 'Irradiance(W/m^2)', 'Latitude', 'Longitude',
|
| 121 |
+
'Panel_Capacity(W)', 'Panel_Efficiency', 'Wind_Speed(km/h)',
|
| 122 |
+
'Cloud_Cover(%)', 'temperature (°f)']
|
| 123 |
+
if list(data.columns) != expected_columns:
|
| 124 |
+
return f"Error: Expected columns {expected_columns}, but got {list(data.columns)}"
|
| 125 |
|
| 126 |
+
# Ensure there are at least 43 rows for prediction
|
| 127 |
if data.shape[0] < 43:
|
| 128 |
+
return "Error: CSV must have at least 43 rows of data for prediction"
|
| 129 |
|
| 130 |
+
# Select the relevant columns for the LSTM model
|
| 131 |
+
relevant_columns = ['Irradiance(W/m^2)', 'Panel_Efficiency', 'Wind_Speed(km/h)',
|
| 132 |
+
'Cloud_Cover(%)', 'temperature (°f)']
|
| 133 |
+
last_43_days = data.tail(43)[relevant_columns]
|
| 134 |
|
| 135 |
+
# Handle missing values (replace NaNs with 0)
|
| 136 |
+
if last_43_days.isnull().any().any():
|
| 137 |
+
last_43_days.fillna(0, inplace=True)
|
| 138 |
|
| 139 |
+
# Convert to numpy array and reshape for LSTM input
|
| 140 |
+
data_array = last_43_days.values.astype(np.float32)
|
| 141 |
+
sequence = np.reshape(data_array, (1, 43, len(relevant_columns)))
|
| 142 |
|
| 143 |
return sequence
|
| 144 |
except Exception as e:
|
|
|
|
| 147 |
|
| 148 |
# Function to process image and forecast energy using CSV file
|
| 149 |
# Function to process image and forecast energy using CSV file
|
| 150 |
+
def process_image_and_forecast(image, min_area, pixel_area, clip_limit, tile_size, panel_type,
|
| 151 |
panel_length, panel_width, irradiance, electricity_rate, historical_data_csv):
|
| 152 |
try:
|
| 153 |
+
# Process the uploaded image
|
| 154 |
if image is None:
|
| 155 |
return None, "Error: No image provided."
|
| 156 |
|
|
|
|
| 157 |
preprocessed = preprocess_image(image, clip_limit, tile_size)
|
| 158 |
roof_contour = find_roof_contour(preprocessed, min_area)
|
| 159 |
roof_area = calculate_roof_area(roof_contour, pixel_area)
|
|
|
|
| 162 |
num_panels, total_energy_kwh, total_cost_inr = calculate_panels_and_energy(
|
| 163 |
roof_area, panel_length, panel_width, panel_type, irradiance
|
| 164 |
)
|
|
|
|
| 165 |
yearly_savings = estimate_savings(total_energy_kwh, electricity_rate)
|
| 166 |
|
| 167 |
+
# Forecast energy using the CSV data
|
| 168 |
forecast_text = ""
|
| 169 |
if historical_data_csv:
|
|
|
|
| 170 |
sequence = preprocess_csv_for_prediction(historical_data_csv)
|
| 171 |
+
if isinstance(sequence, str): # Handle errors during preprocessing
|
| 172 |
return image, sequence
|
| 173 |
|
| 174 |
+
# Predict energy output
|
| 175 |
predicted_energy = model.predict(sequence)
|
| 176 |
forecast_text = f"Predicted Energy Output for Next Day: {predicted_energy[0][0]:.2f} kWh"
|
| 177 |
|
| 178 |
+
# Draw the roof contour on the image
|
| 179 |
if roof_contour is not None:
|
| 180 |
cv2.drawContours(image, [roof_contour], -1, (0, 255, 0), 2)
|
| 181 |
|
| 182 |
+
# Compile results
|
| 183 |
result_text = (
|
| 184 |
f"Roof Area: {roof_area:.2f} sq.m | Panels: {num_panels} | "
|
| 185 |
f"Energy Output: {total_energy_kwh:.2f} kWh | Cost: ₹{total_cost_inr:.2f} | "
|
|
|
|
| 191 |
return image, f"Error: {str(e)}"
|
| 192 |
|
| 193 |
|
| 194 |
+
|
| 195 |
# Gradio Interface
|
| 196 |
interface = gr.Interface(
|
| 197 |
fn=process_image_and_forecast,
|