Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -35,10 +35,10 @@ def detect_circles(frame_diff, image_center, min_radius=20, max_radius=200):
|
|
35 |
circles = cv2.HoughCircles(
|
36 |
frame_diff,
|
37 |
cv2.HOUGH_GRADIENT,
|
38 |
-
dp=1.5, #
|
39 |
minDist=100, # Prevent overlapping circles
|
40 |
-
param1=100, #
|
41 |
-
param2=20, #
|
42 |
minRadius=min_radius,
|
43 |
maxRadius=max_radius
|
44 |
)
|
@@ -74,12 +74,11 @@ def analyze_gif(gif_file):
|
|
74 |
image_center = (width // 2, height // 2) # Assume Sun is at the center
|
75 |
|
76 |
# Initialize results
|
77 |
-
|
78 |
-
circle_data = []
|
79 |
min_radius = 20
|
80 |
max_radius = min(height, width) // 2 # Limit max radius to half the image size
|
81 |
|
82 |
-
# Process frames
|
83 |
for i in range(len(frames) - 1):
|
84 |
frame1 = preprocess_frame(frames[i])
|
85 |
frame2 = preprocess_frame(frames[i + 1])
|
@@ -96,45 +95,40 @@ def analyze_gif(gif_file):
|
|
96 |
# Take the largest circle (most prominent CME feature)
|
97 |
largest_circle = max(circles, key=lambda c: c[2]) # Sort by radius
|
98 |
x, y, r = largest_circle
|
99 |
-
|
100 |
"frame": i + 1,
|
101 |
"center": (x, y),
|
102 |
-
"radius": r
|
|
|
103 |
})
|
104 |
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
|
|
|
|
115 |
report = "Analysis Report:\n"
|
116 |
-
if
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
for i in range(1, len(centers))
|
127 |
-
) if len(centers) > 1 else False
|
128 |
-
|
129 |
-
report += f"Detected {len(circle_data)} circles across frames.\n"
|
130 |
-
for c in circle_data:
|
131 |
report += f"Frame {c['frame']}: Center at {c['center']}, Radius {c['radius']} pixels\n"
|
132 |
-
|
133 |
-
report += "\nConclusion: Growing concentric circles detected, indicative of a potential Earth-directed CME."
|
134 |
-
else:
|
135 |
-
report += "\nConclusion: Detected circles, but growth pattern or center consistency does not confirm a clear CME."
|
136 |
else:
|
137 |
-
report += "No concentric circles detected."
|
138 |
|
139 |
return report, results
|
140 |
except Exception as e:
|
@@ -146,7 +140,7 @@ iface = gr.Interface(
|
|
146 |
inputs=gr.File(label="Upload Solar GIF", file_types=[".gif"]),
|
147 |
outputs=[
|
148 |
gr.Textbox(label="Analysis Report"),
|
149 |
-
gr.Gallery(label="Frames with
|
150 |
],
|
151 |
title="Solar CME Detection",
|
152 |
description="Upload a GIF of solar images to detect growing concentric circles indicative of Earth-directed coronal mass ejections (CMEs)."
|
|
|
35 |
circles = cv2.HoughCircles(
|
36 |
frame_diff,
|
37 |
cv2.HOUGH_GRADIENT,
|
38 |
+
dp=1.5, # Resolution for better detection
|
39 |
minDist=100, # Prevent overlapping circles
|
40 |
+
param1=100, # Edge threshold to reduce noise
|
41 |
+
param2=20, # Accumulator threshold to detect faint circles
|
42 |
minRadius=min_radius,
|
43 |
maxRadius=max_radius
|
44 |
)
|
|
|
74 |
image_center = (width // 2, height // 2) # Assume Sun is at the center
|
75 |
|
76 |
# Initialize results
|
77 |
+
all_circle_data = [] # Store all detected circles
|
|
|
78 |
min_radius = 20
|
79 |
max_radius = min(height, width) // 2 # Limit max radius to half the image size
|
80 |
|
81 |
+
# Process frames and detect circles
|
82 |
for i in range(len(frames) - 1):
|
83 |
frame1 = preprocess_frame(frames[i])
|
84 |
frame2 = preprocess_frame(frames[i + 1])
|
|
|
95 |
# Take the largest circle (most prominent CME feature)
|
96 |
largest_circle = max(circles, key=lambda c: c[2]) # Sort by radius
|
97 |
x, y, r = largest_circle
|
98 |
+
all_circle_data.append({
|
99 |
"frame": i + 1,
|
100 |
"center": (x, y),
|
101 |
+
"radius": r,
|
102 |
+
"output_frame": frames[i + 1] # Store the frame for visualization
|
103 |
})
|
104 |
|
105 |
+
# Filter frames where the circle is growing
|
106 |
+
growing_circle_data = []
|
107 |
+
if all_circle_data:
|
108 |
+
# Start with the first detection
|
109 |
+
growing_circle_data.append(all_circle_data[0])
|
110 |
+
for i in range(1, len(all_circle_data)):
|
111 |
+
# Compare radius with the last growing circle
|
112 |
+
if all_circle_data[i]["radius"] > growing_circle_data[-1]["radius"]:
|
113 |
+
growing_circle_data.append(all_circle_data[i])
|
114 |
+
|
115 |
+
# Generate output frames and report
|
116 |
+
results = []
|
117 |
report = "Analysis Report:\n"
|
118 |
+
if growing_circle_data:
|
119 |
+
report += f"Detected {len(growing_circle_data)} frames with growing concentric circles:\n"
|
120 |
+
for c in growing_circle_data:
|
121 |
+
# Visualize the frame with detected circle
|
122 |
+
output_frame = cv2.cvtColor(c["output_frame"], cv2.COLOR_GRAY2RGB)
|
123 |
+
cv2.circle(output_frame, (c["center"][0], c["center"][1]), c["radius"], (0, 255, 0), 2)
|
124 |
+
# Convert to PIL Image for Gradio
|
125 |
+
output_frame = Image.fromarray(output_frame)
|
126 |
+
results.append(output_frame)
|
127 |
+
|
|
|
|
|
|
|
|
|
|
|
128 |
report += f"Frame {c['frame']}: Center at {c['center']}, Radius {c['radius']} pixels\n"
|
129 |
+
report += "\nConclusion: Growing concentric circles detected, indicative of a potential Earth-directed CME."
|
|
|
|
|
|
|
130 |
else:
|
131 |
+
report += "No growing concentric circles detected. CME may not be Earth-directed."
|
132 |
|
133 |
return report, results
|
134 |
except Exception as e:
|
|
|
140 |
inputs=gr.File(label="Upload Solar GIF", file_types=[".gif"]),
|
141 |
outputs=[
|
142 |
gr.Textbox(label="Analysis Report"),
|
143 |
+
gr.Gallery(label="Frames with Growing Circles")
|
144 |
],
|
145 |
title="Solar CME Detection",
|
146 |
description="Upload a GIF of solar images to detect growing concentric circles indicative of Earth-directed coronal mass ejections (CMEs)."
|