Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -18,7 +18,7 @@ center_tol = 30
|
|
18 |
morph_dia = 5
|
19 |
min_rad = 70
|
20 |
|
21 |
-
def fetch_sdo_images(max_images,
|
22 |
"""Fetch SDO images from NOAA URL directory."""
|
23 |
try:
|
24 |
# Construct the directory URL
|
@@ -33,22 +33,21 @@ def fetch_sdo_images(max_images, ident="0745", size="1024by960", tool="ccor1"):
|
|
33 |
soup = BeautifulSoup(response.text, 'html.parser')
|
34 |
links = soup.find_all('a')
|
35 |
|
36 |
-
# Extract image filenames matching the pattern {
|
37 |
image_files = []
|
38 |
for link in links:
|
39 |
-
print(link)
|
40 |
href = link.get('href')
|
41 |
-
if href and href.endswith(f"_{size}.jpg"):
|
42 |
-
# Check if the filename starts with a valid timestamp (
|
43 |
-
if len(href) >=
|
44 |
image_files.append(href)
|
45 |
|
46 |
# Sort images by timestamp (most recent first)
|
47 |
-
image_files = sorted(image_files, key=lambda x: x[:
|
48 |
total_images = len(image_files)
|
49 |
|
50 |
if not image_files:
|
51 |
-
return None, f"No images found with
|
52 |
|
53 |
# Fetch up to max_images
|
54 |
frames = []
|
@@ -131,9 +130,9 @@ def create_gif(frames, output_path, duration=0.5):
|
|
131 |
)
|
132 |
return output_path
|
133 |
|
134 |
-
def handle_fetch(max_images,
|
135 |
"""Fetch SDO images and return frames for preview and state."""
|
136 |
-
frames, message, total_images = fetch_sdo_images(max_images,
|
137 |
if frames is None:
|
138 |
return message, [], frames, total_images
|
139 |
preview_frames = [Image.fromarray(frame) for frame in frames]
|
@@ -240,7 +239,7 @@ def analyze_images(frames, lower_bound, upper_bound, param1, param2, center_tole
|
|
240 |
except Exception as e:
|
241 |
return f"Error during analysis: {str(e)}", [], None
|
242 |
|
243 |
-
def process_input(gif_file, max_images,
|
244 |
"""Process either uploaded GIF or fetched SDO images."""
|
245 |
if gif_file:
|
246 |
frames, error = extract_frames(gif_file.name)
|
@@ -277,7 +276,6 @@ with gr.Blocks(title="Solar CME Detection") as demo:
|
|
277 |
gr.Markdown("### Input Options")
|
278 |
gif_input = gr.File(label="Upload Solar GIF (optional)", file_types=[".gif"])
|
279 |
max_images = gr.Slider(minimum=1, maximum=100, value=10, step=1, label="Max Images to Fetch")
|
280 |
-
ident = gr.Textbox(label="Image Identifier", value="0745")
|
281 |
size = gr.Textbox(label="Image Size", value="1024by960")
|
282 |
tool = gr.Textbox(label="Instrument", value="ccor1")
|
283 |
fetch_button = gr.Button("Fetch Images from URL")
|
@@ -309,8 +307,7 @@ with gr.Blocks(title="Solar CME Detection") as demo:
|
|
309 |
# Fetch button action
|
310 |
fetch_button.click(
|
311 |
fn=handle_fetch,
|
312 |
-
inputs=[max_images,
|
313 |
-
#inputs=[max_images],
|
314 |
outputs=[report, preview, fetched_frames_state, total_images]
|
315 |
)
|
316 |
|
@@ -318,7 +315,7 @@ with gr.Blocks(title="Solar CME Detection") as demo:
|
|
318 |
analyze_button.click(
|
319 |
fn=process_input,
|
320 |
inputs=[
|
321 |
-
|
322 |
lower_bound, upper_bound, param1, param2, center_tolerance, morph_iterations, min_rad, display_mode, fetched_frames_state
|
323 |
],
|
324 |
outputs=[report, gallery, gif_output, preview, total_images]
|
|
|
18 |
morph_dia = 5
|
19 |
min_rad = 70
|
20 |
|
21 |
+
def fetch_sdo_images(max_images, size="1024by960", tool="ccor1"):
|
22 |
"""Fetch SDO images from NOAA URL directory."""
|
23 |
try:
|
24 |
# Construct the directory URL
|
|
|
33 |
soup = BeautifulSoup(response.text, 'html.parser')
|
34 |
links = soup.find_all('a')
|
35 |
|
36 |
+
# Extract image filenames matching the pattern YYYYMMDD_HHMM_{tool}_{size}.jpg
|
37 |
image_files = []
|
38 |
for link in links:
|
|
|
39 |
href = link.get('href')
|
40 |
+
if href and href.endswith(f"_{tool}_{size}.jpg"):
|
41 |
+
# Check if the filename starts with a valid timestamp (YYYYMMDD_HHMM)
|
42 |
+
if len(href) >= 12 and href[:8].isdigit() and href[9:13].isdigit():
|
43 |
image_files.append(href)
|
44 |
|
45 |
# Sort images by timestamp (most recent first)
|
46 |
+
image_files = sorted(image_files, key=lambda x: x[:13], reverse=True)
|
47 |
total_images = len(image_files)
|
48 |
|
49 |
if not image_files:
|
50 |
+
return None, f"No images found with size={size}, tool={tool}.", total_images
|
51 |
|
52 |
# Fetch up to max_images
|
53 |
frames = []
|
|
|
130 |
)
|
131 |
return output_path
|
132 |
|
133 |
+
def handle_fetch(max_images, size, tool):
|
134 |
"""Fetch SDO images and return frames for preview and state."""
|
135 |
+
frames, message, total_images = fetch_sdo_images(max_images, size, tool)
|
136 |
if frames is None:
|
137 |
return message, [], frames, total_images
|
138 |
preview_frames = [Image.fromarray(frame) for frame in frames]
|
|
|
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)
|
|
|
276 |
gr.Markdown("### Input Options")
|
277 |
gif_input = gr.File(label="Upload Solar GIF (optional)", file_types=[".gif"])
|
278 |
max_images = gr.Slider(minimum=1, maximum=100, value=10, step=1, label="Max Images to Fetch")
|
|
|
279 |
size = gr.Textbox(label="Image Size", value="1024by960")
|
280 |
tool = gr.Textbox(label="Instrument", value="ccor1")
|
281 |
fetch_button = gr.Button("Fetch Images from URL")
|
|
|
307 |
# Fetch button action
|
308 |
fetch_button.click(
|
309 |
fn=handle_fetch,
|
310 |
+
inputs=[max_images, size, tool],
|
|
|
311 |
outputs=[report, preview, fetched_frames_state, total_images]
|
312 |
)
|
313 |
|
|
|
315 |
analyze_button.click(
|
316 |
fn=process_input,
|
317 |
inputs=[
|
318 |
+
gif_file, max_images, size, tool,
|
319 |
lower_bound, upper_bound, param1, param2, center_tolerance, morph_iterations, min_rad, display_mode, fetched_frames_state
|
320 |
],
|
321 |
outputs=[report, gallery, gif_output, preview, total_images]
|