Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -118,9 +118,11 @@ def detect_circles(frame_diff, image_center, center_tolerance, param1, param2, m
|
|
118 |
return filtered_circles if filtered_circles else None
|
119 |
return None
|
120 |
|
121 |
-
def create_gif(frames, output_path, duration=0.5):
|
122 |
-
"""Create a GIF from a list of frames."""
|
123 |
pil_frames = [Image.fromarray(frame) for frame in frames]
|
|
|
|
|
124 |
pil_frames[0].save(
|
125 |
output_path,
|
126 |
save_all=True,
|
@@ -138,7 +140,7 @@ def handle_fetch(max_images, size, tool):
|
|
138 |
preview_frames = [Image.fromarray(frame) for frame in frames]
|
139 |
return message, preview_frames, frames, total_images
|
140 |
|
141 |
-
def analyze_images(frames, lower_bound, upper_bound, param1, param2, center_tolerance, morph_iterations, min_rad, display_mode):
|
142 |
"""Analyze frames for concentric circles, highlighting growing series."""
|
143 |
try:
|
144 |
if not frames or len(frames) < 2:
|
@@ -233,13 +235,13 @@ def analyze_images(frames, lower_bound, upper_bound, param1, param2, center_tole
|
|
233 |
if results:
|
234 |
gif_frames = [np.array(img) for img in results]
|
235 |
gif_path = "output.gif"
|
236 |
-
create_gif(gif_frames, gif_path)
|
237 |
|
238 |
return report, results, gif_path
|
239 |
except Exception as e:
|
240 |
return f"Error during analysis: {str(e)}", [], None
|
241 |
|
242 |
-
def process_input(gif_file, max_images, size, tool, lower_bound, upper_bound, param1, param2, center_tolerance, morph_iterations, min_rad, display_mode, fetched_frames_state):
|
243 |
"""Process either uploaded GIF or fetched SDO images."""
|
244 |
if gif_file:
|
245 |
frames, error = extract_frames(gif_file.name)
|
@@ -255,10 +257,11 @@ def process_input(gif_file, max_images, size, tool, lower_bound, upper_bound, pa
|
|
255 |
|
256 |
# Analyze frames
|
257 |
report, results, gif_path = analyze_images(
|
258 |
-
frames, lower_bound, upper_bound, param1, param2, center_tolerance, morph_iterations, min_rad, display_mode
|
259 |
)
|
260 |
|
261 |
return report, results, gif_path, preview, len(frames)
|
|
|
262 |
def load_demo_2(gif_file, max_images, size, tool, lower_bound, upper_bound, param1, param2, center_tolerance, morph_iterations, min_rad, display_mode, fetched_frames_state):
|
263 |
gif_file="./demo_gif.gif"
|
264 |
if gif_file:
|
@@ -296,6 +299,7 @@ def load_demo():
|
|
296 |
return message, preview, frames, total_images
|
297 |
|
298 |
|
|
|
299 |
# Gradio Blocks interface
|
300 |
with gr.Blocks(title="Solar CME Detection") as demo:
|
301 |
gr.Markdown("""
|
@@ -307,7 +311,6 @@ with gr.Blocks(title="Solar CME Detection") as demo:
|
|
307 |
# State to store fetched frames
|
308 |
fetched_frames_state = gr.State(value=[])
|
309 |
with gr.Sidebar(open=False):
|
310 |
-
|
311 |
gr.Markdown("### Analysis Parameters")
|
312 |
size = gr.Textbox(label="Image Size", value="1024by960")
|
313 |
tool = gr.Textbox(label="Instrument", value="ccor1")
|
@@ -323,20 +326,22 @@ with gr.Blocks(title="Solar CME Detection") as demo:
|
|
323 |
value="Both (Detected Replaces Original)",
|
324 |
label="Display Mode"
|
325 |
)
|
|
|
|
|
326 |
with gr.Row():
|
327 |
with gr.Column():
|
328 |
gr.Markdown("### Input Options")
|
329 |
demo_btn = gr.Button("Load Demo")
|
330 |
gif_input = gr.File(label="Upload Solar GIF (optional)", file_types=[".gif"])
|
331 |
max_images = gr.Slider(minimum=1, maximum=100, value=10, step=1, label="Max Images to Fetch")
|
332 |
-
|
333 |
fetch_button = gr.Button("Fetch Images from URL")
|
334 |
analyze_button = gr.Button("Analyze")
|
335 |
|
336 |
with gr.Column():
|
337 |
gr.Markdown("### Outputs")
|
338 |
report = gr.Textbox(label="Analysis Report", lines=10)
|
339 |
-
|
|
|
340 |
with gr.Row():
|
341 |
with gr.Column():
|
342 |
gif_output = gr.File(label="Download Resulting GIF")
|
@@ -345,6 +350,7 @@ with gr.Blocks(title="Solar CME Detection") as demo:
|
|
345 |
with gr.Column():
|
346 |
total_images = gr.Textbox(label="Total Images Available in Directory", value="0")
|
347 |
preview = gr.Gallery(label="Input Preview (All Frames)")
|
|
|
348 |
# Fetch button action
|
349 |
fetch_button.click(
|
350 |
fn=handle_fetch,
|
@@ -357,14 +363,15 @@ with gr.Blocks(title="Solar CME Detection") as demo:
|
|
357 |
inputs=[],
|
358 |
outputs=[report, preview, fetched_frames_state, total_images]
|
359 |
)
|
|
|
360 |
# Analyze button action
|
361 |
analyze_button.click(
|
362 |
fn=process_input,
|
363 |
inputs=[
|
364 |
gif_input, max_images, size, tool,
|
365 |
-
lower_bound, upper_bound, param1, param2, center_tolerance, morph_iterations, min_rad, display_mode, fetched_frames_state
|
366 |
],
|
367 |
-
outputs=[report, gallery, gif_output, preview, total_images]
|
368 |
)
|
369 |
|
370 |
if __name__ == "__main__":
|
|
|
118 |
return filtered_circles if filtered_circles else None
|
119 |
return None
|
120 |
|
121 |
+
def create_gif(frames, output_path, duration=0.5, scale_to_512=False):
|
122 |
+
"""Create a GIF from a list of frames, optionally scaling to 512x512."""
|
123 |
pil_frames = [Image.fromarray(frame) for frame in frames]
|
124 |
+
if scale_to_512:
|
125 |
+
pil_frames = [frame.resize((512, 512), Image.Resampling.LANCZOS) for frame in pil_frames]
|
126 |
pil_frames[0].save(
|
127 |
output_path,
|
128 |
save_all=True,
|
|
|
140 |
preview_frames = [Image.fromarray(frame) for frame in frames]
|
141 |
return message, preview_frames, frames, total_images
|
142 |
|
143 |
+
def analyze_images(frames, lower_bound, upper_bound, param1, param2, center_tolerance, morph_iterations, min_rad, display_mode, scale_to_512):
|
144 |
"""Analyze frames for concentric circles, highlighting growing series."""
|
145 |
try:
|
146 |
if not frames or len(frames) < 2:
|
|
|
235 |
if results:
|
236 |
gif_frames = [np.array(img) for img in results]
|
237 |
gif_path = "output.gif"
|
238 |
+
create_gif(gif_frames, gif_path, scale_to_512=scale_to_512)
|
239 |
|
240 |
return report, results, gif_path
|
241 |
except Exception as e:
|
242 |
return f"Error during analysis: {str(e)}", [], None
|
243 |
|
244 |
+
def process_input(gif_file, max_images, size, tool, lower_bound, upper_bound, param1, param2, center_tolerance, morph_iterations, min_rad, display_mode, fetched_frames_state, scale_to_512):
|
245 |
"""Process either uploaded GIF or fetched SDO images."""
|
246 |
if gif_file:
|
247 |
frames, error = extract_frames(gif_file.name)
|
|
|
257 |
|
258 |
# Analyze frames
|
259 |
report, results, gif_path = analyze_images(
|
260 |
+
frames, lower_bound, upper_bound, param1, param2, center_tolerance, morph_iterations, min_rad, display_mode, scale_to_512
|
261 |
)
|
262 |
|
263 |
return report, results, gif_path, preview, len(frames)
|
264 |
+
|
265 |
def load_demo_2(gif_file, max_images, size, tool, lower_bound, upper_bound, param1, param2, center_tolerance, morph_iterations, min_rad, display_mode, fetched_frames_state):
|
266 |
gif_file="./demo_gif.gif"
|
267 |
if gif_file:
|
|
|
299 |
return message, preview, frames, total_images
|
300 |
|
301 |
|
302 |
+
# Gradio Blocks interface
|
303 |
# Gradio Blocks interface
|
304 |
with gr.Blocks(title="Solar CME Detection") as demo:
|
305 |
gr.Markdown("""
|
|
|
311 |
# State to store fetched frames
|
312 |
fetched_frames_state = gr.State(value=[])
|
313 |
with gr.Sidebar(open=False):
|
|
|
314 |
gr.Markdown("### Analysis Parameters")
|
315 |
size = gr.Textbox(label="Image Size", value="1024by960")
|
316 |
tool = gr.Textbox(label="Instrument", value="ccor1")
|
|
|
326 |
value="Both (Detected Replaces Original)",
|
327 |
label="Display Mode"
|
328 |
)
|
329 |
+
scale_to_512 = gr.Checkbox(label="Scale Output GIF to 512x512", value=False)
|
330 |
+
|
331 |
with gr.Row():
|
332 |
with gr.Column():
|
333 |
gr.Markdown("### Input Options")
|
334 |
demo_btn = gr.Button("Load Demo")
|
335 |
gif_input = gr.File(label="Upload Solar GIF (optional)", file_types=[".gif"])
|
336 |
max_images = gr.Slider(minimum=1, maximum=100, value=10, step=1, label="Max Images to Fetch")
|
|
|
337 |
fetch_button = gr.Button("Fetch Images from URL")
|
338 |
analyze_button = gr.Button("Analyze")
|
339 |
|
340 |
with gr.Column():
|
341 |
gr.Markdown("### Outputs")
|
342 |
report = gr.Textbox(label="Analysis Report", lines=10)
|
343 |
+
gif_viewer = gr.Image(label="Output GIF Preview", type="filepath")
|
344 |
+
|
345 |
with gr.Row():
|
346 |
with gr.Column():
|
347 |
gif_output = gr.File(label="Download Resulting GIF")
|
|
|
350 |
with gr.Column():
|
351 |
total_images = gr.Textbox(label="Total Images Available in Directory", value="0")
|
352 |
preview = gr.Gallery(label="Input Preview (All Frames)")
|
353 |
+
|
354 |
# Fetch button action
|
355 |
fetch_button.click(
|
356 |
fn=handle_fetch,
|
|
|
363 |
inputs=[],
|
364 |
outputs=[report, preview, fetched_frames_state, total_images]
|
365 |
)
|
366 |
+
|
367 |
# Analyze button action
|
368 |
analyze_button.click(
|
369 |
fn=process_input,
|
370 |
inputs=[
|
371 |
gif_input, max_images, size, tool,
|
372 |
+
lower_bound, upper_bound, param1, param2, center_tolerance, morph_iterations, min_rad, display_mode, fetched_frames_state, scale_to_512
|
373 |
],
|
374 |
+
outputs=[report, gallery, gif_output, preview, total_images, gif_viewer]
|
375 |
)
|
376 |
|
377 |
if __name__ == "__main__":
|