jimmy60504 commited on
Commit
fc9d4ef
·
1 Parent(s): 2d4076c

add function to load Ground Truth images and update prediction display

Browse files
Files changed (1) hide show
  1. app.py +44 -12
app.py CHANGED
@@ -729,6 +729,29 @@ def create_intensity_map(pga_list, target_names, epicenter_lat=None, epicenter_l
729
  return m
730
 
731
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
732
  def load_and_display_waveform(event_name, start_time, end_time, epicenter_lon, epicenter_lat):
733
  """載入並顯示波形,讓使用者確認範圍"""
734
  try:
@@ -855,7 +878,10 @@ def predict_intensity(event_name, start_time, end_time, epicenter_lon, epicenter
855
  intensity_map = create_intensity_map(pga_list, target_names, epicenter_lat, epicenter_lon)
856
  map_html = intensity_map._repr_html_()
857
 
858
- # 9. 統計資訊
 
 
 
859
  max_intensity = max([calculate_intensity(pga, label=True) for pga in pga_list])
860
  stats = f"✅ 預測完成!\n"
861
  stats += f"選取時間範圍: {start_time:.1f} - {end_time:.1f} 秒\n"
@@ -864,13 +890,13 @@ def predict_intensity(event_name, start_time, end_time, epicenter_lon, epicenter
864
  stats += f"預測最大震度: {max_intensity}"
865
 
866
  logger.info("預測完成!")
867
- return map_html, stats
868
 
869
  except Exception as e:
870
  logger.error(f"預測過程發生錯誤: {e}")
871
  import traceback
872
  traceback.print_exc()
873
- return None, f"錯誤: {str(e)}"
874
 
875
 
876
  # ============ Gradio 介面 ============
@@ -919,18 +945,24 @@ with gr.Blocks(title="TTSAM 震度預測系統") as demo:
919
  """)
920
 
921
  info_output = gr.Textbox(label="狀態資訊", lines=5, interactive=False)
 
922
 
923
- # 右側:震度分布圖
924
  with gr.Column(scale=1):
925
- gr.Markdown("## 預測震度分布")
926
- intensity_map = gr.HTML(label="互動式震度地圖")
927
- stats_output = gr.Textbox(label="預測統計", lines=4)
928
 
929
- # 下方:波形圖
930
  with gr.Row():
931
- gr.Markdown("## 輸入波形")
932
- with gr.Row():
933
- waveform_plot = gr.Plot(label="地震波形(前 10 個最近測站)")
 
 
 
 
 
 
934
 
935
  # 綁定事件
936
  # 第一步:載入波形
@@ -944,7 +976,7 @@ with gr.Blocks(title="TTSAM 震度預測系統") as demo:
944
  predict_btn.click(
945
  fn=predict_intensity,
946
  inputs=[event_dropdown, start_slider, end_slider, epicenter_lon_input, epicenter_lat_input, vs30_input],
947
- outputs=[intensity_map, stats_output]
948
  )
949
 
950
  demo.launch()
 
729
  return m
730
 
731
 
732
+ def load_ground_truth_image(event_name):
733
+ """從 ground_truth 資料夾載入對應的 Ground Truth 圖片"""
734
+ import os
735
+
736
+ # 根據事件名稱找對應的圖片
737
+ # 假設圖片命名格式為:20240403.png 或類似
738
+ event_file = EARTHQUAKE_EVENTS[event_name]
739
+ event_date = os.path.basename(event_file).replace('.mseed', '')
740
+
741
+ # 嘗試不同的圖片格式
742
+ ground_truth_dir = "ground_truth"
743
+ possible_extensions = ['.png', '.jpg', '.jpeg', '.gif']
744
+
745
+ for ext in possible_extensions:
746
+ image_path = os.path.join(ground_truth_dir, f"{event_date}{ext}")
747
+ if os.path.exists(image_path):
748
+ logger.info(f"載入 Ground Truth 圖片: {image_path}")
749
+ return image_path
750
+
751
+ logger.warning(f"找不到 Ground Truth 圖片: {event_date}")
752
+ return None
753
+
754
+
755
  def load_and_display_waveform(event_name, start_time, end_time, epicenter_lon, epicenter_lat):
756
  """載入並顯示波形,讓使用者確認範圍"""
757
  try:
 
878
  intensity_map = create_intensity_map(pga_list, target_names, epicenter_lat, epicenter_lon)
879
  map_html = intensity_map._repr_html_()
880
 
881
+ # 9. 載入 Ground Truth 圖片
882
+ ground_truth_path = load_ground_truth_image(event_name)
883
+
884
+ # 10. 統計資訊
885
  max_intensity = max([calculate_intensity(pga, label=True) for pga in pga_list])
886
  stats = f"✅ 預測完成!\n"
887
  stats += f"選取時間範圍: {start_time:.1f} - {end_time:.1f} 秒\n"
 
890
  stats += f"預測最大震度: {max_intensity}"
891
 
892
  logger.info("預測完成!")
893
+ return ground_truth_path, map_html, stats
894
 
895
  except Exception as e:
896
  logger.error(f"預測過程發生錯誤: {e}")
897
  import traceback
898
  traceback.print_exc()
899
+ return None, None, f"錯誤: {str(e)}"
900
 
901
 
902
  # ============ Gradio 介面 ============
 
945
  """)
946
 
947
  info_output = gr.Textbox(label="狀態資訊", lines=5, interactive=False)
948
+ stats_output = gr.Textbox(label="預測統計", lines=4)
949
 
950
+ # 右側:波形圖
951
  with gr.Column(scale=1):
952
+ gr.Markdown("## 輸入波形")
953
+ waveform_plot = gr.Plot(label="地震波形(選定的最近測站)")
 
954
 
955
+ # 下方:Ground Truth vs 預測結果
956
  with gr.Row():
957
+ # 左側:Ground Truth
958
+ with gr.Column(scale=1):
959
+ gr.Markdown("## Ground Truth 震度分布")
960
+ ground_truth_image = gr.Image(label="實際觀測震度", type="filepath")
961
+
962
+ # 右側:預測震度地圖
963
+ with gr.Column(scale=1):
964
+ gr.Markdown("## 預測震度分布")
965
+ intensity_map = gr.HTML(label="互動式震度地圖")
966
 
967
  # 綁定事件
968
  # 第一步:載入波形
 
976
  predict_btn.click(
977
  fn=predict_intensity,
978
  inputs=[event_dropdown, start_slider, end_slider, epicenter_lon_input, epicenter_lat_input, vs30_input],
979
+ outputs=[ground_truth_image, intensity_map, stats_output]
980
  )
981
 
982
  demo.launch()