HoeioUser commited on
Commit
cf40356
ยท
verified ยท
1 Parent(s): 6306de1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -38
app.py CHANGED
@@ -209,6 +209,35 @@ class ClimatePredictor:
209
  transforms.Normalize(mean=[0.5], std=[0.5])
210
  ])
211
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
212
  def convert_to_single_channel(self, image_array):
213
  if len(image_array.shape) == 3:
214
  return np.dot(image_array[...,:3], [0.2989, 0.5870, 0.1140])
@@ -292,13 +321,13 @@ class ClimatePredictor:
292
  solar_map = solar_pred.cpu().numpy()[0, 0]
293
 
294
  # ๊ฒฐ๊ณผ ์‹œ๊ฐํ™”
295
- fig = plt.figure(figsize=(20, 10))
296
 
297
- # 1. ์›๋ณธ ์ด๋ฏธ์ง€์™€ ์ƒ์œ„ 5% ์ด๋ชจ์ง€ ์˜ค๋ฒ„๋ ˆ์ด
298
  ax1 = plt.subplot(2, 2, 1)
299
- overlay_img = self.overlay_emojis(rgb_image, wind_map, solar_map)
300
- ax1.imshow(overlay_img)
301
- ax1.set_title('Top 5% Potential Sites')
302
  ax1.axis('off')
303
 
304
  # 2. ํ’๋ ฅ ๋ฐœ์ „ ์ž ์žฌ๋Ÿ‰ ํžˆํŠธ๋งต
@@ -313,13 +342,13 @@ class ClimatePredictor:
313
  cbar_kws={'label': 'Solar Power Potential'})
314
  ax3.set_title('Solar Power Potential Map')
315
 
316
- # 4. ์ƒ์œ„ 5% ํฌํ…์…œ ์ง€์—ญ ํ‘œ์‹œ
317
  ax4 = plt.subplot(2, 2, 4)
318
- wind_top = np.where(get_top_percentile_mask(wind_map), wind_map, 0)
319
- solar_top = np.where(get_top_percentile_mask(solar_map), solar_map, 0)
320
  combined_map = np.stack([solar_top, wind_top, np.zeros_like(wind_map)], axis=-1)
321
  ax4.imshow(combined_map)
322
- ax4.set_title('Top 5% Potential Sites (Red: Solar, Green: Wind)')
323
  ax4.axis('off')
324
 
325
  plt.tight_layout()
@@ -375,18 +404,17 @@ def load_examples_from_directory(base_dir):
375
 
376
  def create_gradio_interface():
377
  predictor = ClimatePredictor('best_model.pth')
378
-
379
  def process_elevation_file(file_obj):
380
- if isinstance(file_obj, str): # ํŒŒ์ผ ๊ฒฝ๋กœ์ธ ๊ฒฝ์šฐ
381
  return np.load(file_obj)
382
- else: # UploadedFile ๊ฐ์ฒด์ธ ๊ฒฝ์šฐ
383
  return np.load(file_obj.name)
384
 
385
  def predict_with_processing(*args):
386
  rgb_image, ndvi_image, terrain_image, elevation_file = args[:4]
387
  weather_params = args[4:]
388
 
389
- # elevation ํŒŒ์ผ ์ฒ˜๋ฆฌ
390
  elevation_data = process_elevation_file(elevation_file)
391
 
392
  return predictor.predict_from_inputs(
@@ -395,36 +423,30 @@ def create_gradio_interface():
395
  )
396
 
397
  with gr.Blocks() as interface:
398
- gr.Markdown("# Renewable Energy Potential Predictor")
399
- gr.Markdown("Upload satellite imagery and environmental data to predict wind and solar power potential.")
400
-
401
- with gr.Row():
402
- # ์ž…๋ ฅ ์„น์…˜ (1/3 ํฌ๊ธฐ)
403
- with gr.Column(scale=1):
404
- rgb_input = gr.Image(label="RGB Satellite Image", type="numpy", height=200)
405
- ndvi_input = gr.Image(label="NDVI Image", type="numpy", height=200)
406
- terrain_input = gr.Image(label="Terrain Map", type="numpy", height=200)
407
- elevation_input = gr.File(label="Elevation Data (NPY file)", height=50)
408
 
409
- with gr.Row():
 
410
  wind_speed = gr.Number(label="Wind Speed (m/s)", value=5.0)
411
  wind_direction = gr.Number(label="Wind Direction (ยฐ)", value=180.0)
412
-
413
- with gr.Row():
414
  temperature = gr.Number(label="Temperature (ยฐC)", value=25.0)
415
  humidity = gr.Number(label="Humidity (%)", value=60.0)
416
-
417
- predict_btn = gr.Button("Generate Predictions", variant="primary")
418
 
419
- # ์ถœ๋ ฅ ์„น์…˜ (2/3 ํฌ๊ธฐ)
420
- with gr.Column(scale=2):
421
- output_plot = gr.Plot(label="Prediction Results")
422
-
423
- # ์˜ˆ์ œ ๋ฐ์ดํ„ฐ ๋กœ๋“œ
424
- examples = load_examples_from_directory("filtered_climate_data")
425
-
426
- # ์˜ˆ์ œ ๊ฐค๋Ÿฌ๋ฆฌ ์ƒ์„ฑ
427
- with gr.Row():
428
  gr.Examples(
429
  examples=examples,
430
  inputs=[rgb_input, ndvi_input, terrain_input, elevation_input,
@@ -436,7 +458,6 @@ def create_gradio_interface():
436
  examples_per_page=5
437
  )
438
 
439
- # ์˜ˆ์ธก ๋ฒ„ํŠผ ํด๋ฆญ ์ด๋ฒคํŠธ ์—ฐ๊ฒฐ
440
  predict_btn.click(
441
  fn=predict_with_processing,
442
  inputs=[rgb_input, ndvi_input, terrain_input, elevation_input,
 
209
  transforms.Normalize(mean=[0.5], std=[0.5])
210
  ])
211
 
212
+ def highlight_top_potential(self, rgb_image, wind_map, solar_map, percentile=95):
213
+ """์ƒ์œ„ ํฌํ…์…œ ์ง€์—ญ์„ ํ•˜์ด๋ผ์ดํŠธ๋กœ ํ‘œ์‹œ"""
214
+ # RGB ์ด๋ฏธ์ง€๋ฅผ ๊ธฐ๋ณธ์œผ๋กœ ์‚ฌ์šฉ
215
+ result = np.copy(rgb_image)
216
+
217
+ # ํžˆํŠธ๋งต ๋ฆฌ์‚ฌ์ด์ฆˆ
218
+ h, w = rgb_image.shape[:2]
219
+ wind_map_resized = np.array(Image.fromarray((wind_map * 255).astype(np.uint8)).resize((w, h))) / 255.0
220
+ solar_map_resized = np.array(Image.fromarray((solar_map * 255).astype(np.uint8)).resize((w, h))) / 255.0
221
+
222
+ # ์ƒ์œ„ N% ๋งˆ์Šคํฌ ์ƒ์„ฑ
223
+ wind_threshold = np.percentile(wind_map_resized, percentile)
224
+ solar_threshold = np.percentile(solar_map_resized, percentile)
225
+
226
+ wind_mask = wind_map_resized >= wind_threshold
227
+ solar_mask = solar_map_resized >= solar_threshold
228
+
229
+ # ํ•˜์ด๋ผ์ดํŠธ ์ƒ‰์ƒ ์„ค์ •
230
+ wind_color = np.array([0, 255, 0]) # ๋…น์ƒ‰
231
+ solar_color = np.array([255, 0, 0]) # ๋นจ๊ฐ„์ƒ‰
232
+
233
+ # ๋ฐ˜ํˆฌ๋ช… ์˜ค๋ฒ„๋ ˆ์ด ์ ์šฉ
234
+ alpha = 0.3
235
+ result[wind_mask] = result[wind_mask] * (1 - alpha) + wind_color * alpha
236
+ result[solar_mask] = result[solar_mask] * (1 - alpha) + solar_color * alpha
237
+
238
+ return result.astype(np.uint8)
239
+
240
+
241
  def convert_to_single_channel(self, image_array):
242
  if len(image_array.shape) == 3:
243
  return np.dot(image_array[...,:3], [0.2989, 0.5870, 0.1140])
 
321
  solar_map = solar_pred.cpu().numpy()[0, 0]
322
 
323
  # ๊ฒฐ๊ณผ ์‹œ๊ฐํ™”
324
+ fig = plt.figure(figsize=(20, 12))
325
 
326
+ # 1. ์›๋ณธ ์ด๋ฏธ์ง€์™€ ์ƒ์œ„ 5% ํ•˜์ด๋ผ์ดํŠธ ์˜ค๋ฒ„๋ ˆ์ด
327
  ax1 = plt.subplot(2, 2, 1)
328
+ highlighted_img = self.highlight_top_potential(rgb_image, wind_map, solar_map)
329
+ ax1.imshow(highlighted_img)
330
+ ax1.set_title('Top 5% Potential Sites\n(Red: Solar, Green: Wind)', pad=20)
331
  ax1.axis('off')
332
 
333
  # 2. ํ’๋ ฅ ๋ฐœ์ „ ์ž ์žฌ๋Ÿ‰ ํžˆํŠธ๋งต
 
342
  cbar_kws={'label': 'Solar Power Potential'})
343
  ax3.set_title('Solar Power Potential Map')
344
 
345
+ # 4. ์ƒ์œ„ 5% ํฌํ…์…œ๋งŒ ํ‘œ์‹œํ•œ ํžˆํŠธ๋งต
346
  ax4 = plt.subplot(2, 2, 4)
347
+ wind_top = np.where(wind_map >= np.percentile(wind_map, 95), wind_map, 0)
348
+ solar_top = np.where(solar_map >= np.percentile(solar_map, 95), solar_map, 0)
349
  combined_map = np.stack([solar_top, wind_top, np.zeros_like(wind_map)], axis=-1)
350
  ax4.imshow(combined_map)
351
+ ax4.set_title('Top 5% Potential Sites Heatmap\n(Red: Solar, Green: Wind)', pad=20)
352
  ax4.axis('off')
353
 
354
  plt.tight_layout()
 
404
 
405
  def create_gradio_interface():
406
  predictor = ClimatePredictor('best_model.pth')
407
+
408
  def process_elevation_file(file_obj):
409
+ if isinstance(file_obj, str):
410
  return np.load(file_obj)
411
+ else:
412
  return np.load(file_obj.name)
413
 
414
  def predict_with_processing(*args):
415
  rgb_image, ndvi_image, terrain_image, elevation_file = args[:4]
416
  weather_params = args[4:]
417
 
 
418
  elevation_data = process_elevation_file(elevation_file)
419
 
420
  return predictor.predict_from_inputs(
 
423
  )
424
 
425
  with gr.Blocks() as interface:
426
+ with gr.Column():
427
+ gr.Markdown("# Renewable Energy Potential Predictor")
428
+
429
+ # ์ž…๋ ฅ ์„น์…˜ - ๋ชจ๋“  ์ž…๋ ฅ์„ ํ•˜๋‚˜์˜ Row์— ๋ฐฐ์น˜
430
+ with gr.Row(equal_height=True):
431
+ # ์ด๋ฏธ์ง€/ํŒŒ์ผ ์ž…๋ ฅ
432
+ rgb_input = gr.Image(label="RGB Satellite Image", type="numpy", height=150, scale=1)
433
+ ndvi_input = gr.Image(label="NDVI Image", type="numpy", height=150, scale=1)
434
+ terrain_input = gr.Image(label="Terrain Map", type="numpy", height=150, scale=1)
435
+ elevation_input = gr.File(label="Elevation Data (NPY)", height=150, scale=1)
436
 
437
+ # ๋‚ ์”จ ํŒŒ๋ผ๋ฏธํ„ฐ ์ž…๋ ฅ
438
+ with gr.Column(scale=1):
439
  wind_speed = gr.Number(label="Wind Speed (m/s)", value=5.0)
440
  wind_direction = gr.Number(label="Wind Direction (ยฐ)", value=180.0)
 
 
441
  temperature = gr.Number(label="Temperature (ยฐC)", value=25.0)
442
  humidity = gr.Number(label="Humidity (%)", value=60.0)
443
+ predict_btn = gr.Button("Generate Predictions", variant="primary", size="lg")
 
444
 
445
+ # ์ถœ๋ ฅ ์„น์…˜ - ์ „์ฒด ๋„ˆ๋น„ ์‚ฌ์šฉ
446
+ output_plot = gr.Plot(label="Prediction Results", container=True, height=600)
447
+
448
+ # ์˜ˆ์ œ ์„น์…˜
449
+ examples = load_examples_from_directory("filtered_climate_data")
 
 
 
 
450
  gr.Examples(
451
  examples=examples,
452
  inputs=[rgb_input, ndvi_input, terrain_input, elevation_input,
 
458
  examples_per_page=5
459
  )
460
 
 
461
  predict_btn.click(
462
  fn=predict_with_processing,
463
  inputs=[rgb_input, ndvi_input, terrain_input, elevation_input,