lolzysiu commited on
Commit
419fc83
·
verified ·
1 Parent(s): 2defc24

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -21
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 data from CSV file
117
- data = pd.read_csv(csv_file.name) # Corrected from Solar_Irradiance.csv to use the file input
118
 
119
- # Ensure that the CSV has 7 columns (features) and at least 43 rows (timesteps)
120
- if data.shape[1] != 7:
121
- return "Error: CSV should have exactly 7 columns corresponding to the 7 features"
 
 
 
122
 
123
- # Check if there are enough rows (43 days of data)
124
  if data.shape[0] < 43:
125
- return "Error: CSV should have at least 43 rows of data"
126
 
127
- # Select the last 43 rows of data
128
- last_43_days = data.tail(43)
 
 
129
 
130
- # Convert the data to a numpy array (we expect 43 days, 7 features)
131
- data_array = last_43_days.values.astype(np.float32)
 
132
 
133
- # Reshape the data to match the input shape (1, 43, 7)
134
- sequence = np.reshape(data_array, (1, 43, 7))
 
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 from CSV data
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): # If an error message is returned
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 if found
173
  if roof_contour is not None:
174
  cv2.drawContours(image, [roof_contour], -1, (0, 255, 0), 2)
175
 
176
- # Return results as text
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,