broadfield-dev commited on
Commit
2aa7ef4
·
verified ·
1 Parent(s): 902ba86

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -14
app.py CHANGED
@@ -2,7 +2,6 @@ import gradio as gr
2
  import numpy as np
3
  import cv2
4
  from PIL import Image
5
- import matplotlib.pyplot as plt
6
  import requests
7
  from datetime import datetime, timedelta
8
  import io
@@ -111,13 +110,12 @@ def create_gif(frames, output_path, duration=0.5):
111
  return output_path
112
 
113
  def handle_fetch(start_date, end_date, ident, size, tool):
114
- """Fetch SDO images and return frames for preview."""
115
  frames, error = fetch_sdo_images(start_date, end_date, ident, size, tool)
116
  if error:
117
- return error, []
118
- # Convert frames to PIL Images for preview
119
  preview_frames = [Image.fromarray(frame) for frame in frames]
120
- return "Fetched {} images successfully.".format(len(frames)), preview_frames
121
 
122
  def analyze_images(frames, lower_bound, upper_bound, param1, param2, center_tolerance, morph_iterations, min_rad, display_mode):
123
  """Analyze frames for concentric circles, highlighting growing series."""
@@ -171,10 +169,8 @@ def analyze_images(frames, lower_bound, upper_bound, param1, param2, center_tole
171
 
172
  # Prepare output based on display mode
173
  if display_mode == "All Frames":
174
- # Show pure, unprocessed frames
175
  results = [Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_GRAY2RGB)) for frame in frames]
176
  elif display_mode == "Detected Frames":
177
- # Show only frames with detected circles
178
  for c in all_circle_data:
179
  output_frame = cv2.cvtColor(c["output_frame"], cv2.COLOR_GRAY2RGB)
180
  cv2.circle(output_frame, c["center"], c["radius"], (0, 255, 0), 2) # Green for detected
@@ -182,7 +178,6 @@ def analyze_images(frames, lower_bound, upper_bound, param1, param2, center_tole
182
  cv2.circle(output_frame, c["center"], c["radius"] + 2, (255, 165, 0), 2) # Orange for growing
183
  results.append(Image.fromarray(output_frame))
184
  elif display_mode == "Both (Detected Replaces Original)":
185
- # Show all frames, replacing detected frames with circles
186
  for i, frame in enumerate(frames):
187
  if i + 1 in [c["frame"] for c in all_circle_data]:
188
  for c in all_circle_data:
@@ -293,12 +288,7 @@ with gr.Blocks(title="Solar CME Detection") as demo:
293
  fetch_button.click(
294
  fn=handle_fetch,
295
  inputs=[start_date, end_date, ident, size, tool],
296
- outputs=[report, preview],
297
- js="() => {return {fetched_frames_state: []}}"
298
- ).then(
299
- fn=lambda report, preview_frames: (preview_frames, report),
300
- inputs=[report, preview],
301
- outputs=[fetched_frames_state, report]
302
  )
303
 
304
  # Analyze button action
 
2
  import numpy as np
3
  import cv2
4
  from PIL import Image
 
5
  import requests
6
  from datetime import datetime, timedelta
7
  import io
 
110
  return output_path
111
 
112
  def handle_fetch(start_date, end_date, ident, size, tool):
113
+ """Fetch SDO images and return frames for preview and state."""
114
  frames, error = fetch_sdo_images(start_date, end_date, ident, size, tool)
115
  if error:
116
+ return error, [], frames
 
117
  preview_frames = [Image.fromarray(frame) for frame in frames]
118
+ return f"Fetched {len(frames)} images successfully.", preview_frames, frames
119
 
120
  def analyze_images(frames, lower_bound, upper_bound, param1, param2, center_tolerance, morph_iterations, min_rad, display_mode):
121
  """Analyze frames for concentric circles, highlighting growing series."""
 
169
 
170
  # Prepare output based on display mode
171
  if display_mode == "All Frames":
 
172
  results = [Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_GRAY2RGB)) for frame in frames]
173
  elif display_mode == "Detected Frames":
 
174
  for c in all_circle_data:
175
  output_frame = cv2.cvtColor(c["output_frame"], cv2.COLOR_GRAY2RGB)
176
  cv2.circle(output_frame, c["center"], c["radius"], (0, 255, 0), 2) # Green for detected
 
178
  cv2.circle(output_frame, c["center"], c["radius"] + 2, (255, 165, 0), 2) # Orange for growing
179
  results.append(Image.fromarray(output_frame))
180
  elif display_mode == "Both (Detected Replaces Original)":
 
181
  for i, frame in enumerate(frames):
182
  if i + 1 in [c["frame"] for c in all_circle_data]:
183
  for c in all_circle_data:
 
288
  fetch_button.click(
289
  fn=handle_fetch,
290
  inputs=[start_date, end_date, ident, size, tool],
291
+ outputs=[report, preview, fetched_frames_state]
 
 
 
 
 
292
  )
293
 
294
  # Analyze button action