Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -21,14 +21,20 @@ def extract_frames(gif_path):
|
|
| 21 |
return None, f"Error loading GIF: {str(e)}"
|
| 22 |
|
| 23 |
def preprocess_frame(frame):
|
| 24 |
-
"""Preprocess a frame:
|
| 25 |
# Apply Gaussian blur to reduce noise
|
| 26 |
blurred = cv2.GaussianBlur(frame, (9, 9), 0)
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
def detect_circles(frame_diff, image_center, min_radius=20, max_radius=200):
|
| 34 |
"""Detect circles in a frame difference image, centered at the Sun."""
|
|
@@ -37,8 +43,8 @@ def detect_circles(frame_diff, image_center, min_radius=20, max_radius=200):
|
|
| 37 |
cv2.HOUGH_GRADIENT,
|
| 38 |
dp=1.5, # Resolution for better detection
|
| 39 |
minDist=100, # Prevent overlapping circles
|
| 40 |
-
param1=
|
| 41 |
-
param2=
|
| 42 |
minRadius=min_radius,
|
| 43 |
maxRadius=max_radius
|
| 44 |
)
|
|
@@ -56,7 +62,7 @@ def detect_circles(frame_diff, image_center, min_radius=20, max_radius=200):
|
|
| 56 |
return None
|
| 57 |
|
| 58 |
def analyze_gif(gif_file):
|
| 59 |
-
"""Analyze a GIF for growing concentric circles
|
| 60 |
try:
|
| 61 |
# Handle Gradio file input
|
| 62 |
gif_path = gif_file.name if hasattr(gif_file, 'name') else gif_file
|
|
@@ -85,7 +91,7 @@ def analyze_gif(gif_file):
|
|
| 85 |
|
| 86 |
# Compute absolute difference between consecutive frames
|
| 87 |
frame_diff = cv2.absdiff(frame2, frame1)
|
| 88 |
-
# Enhance contrast for
|
| 89 |
frame_diff = cv2.convertScaleAbs(frame_diff, alpha=3.0, beta=0)
|
| 90 |
|
| 91 |
# Detect circles centered at the Sun
|
|
@@ -114,9 +120,9 @@ def analyze_gif(gif_file):
|
|
| 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)
|
|
@@ -126,9 +132,9 @@ def analyze_gif(gif_file):
|
|
| 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:
|
|
@@ -143,7 +149,7 @@ iface = gr.Interface(
|
|
| 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)."
|
| 147 |
)
|
| 148 |
|
| 149 |
if __name__ == "__main__":
|
|
|
|
| 21 |
return None, f"Error loading GIF: {str(e)}"
|
| 22 |
|
| 23 |
def preprocess_frame(frame):
|
| 24 |
+
"""Preprocess a frame: isolate mid-to-light pixels and enhance circular patterns."""
|
| 25 |
# Apply Gaussian blur to reduce noise
|
| 26 |
blurred = cv2.GaussianBlur(frame, (9, 9), 0)
|
| 27 |
+
|
| 28 |
+
# Isolate mid-to-light pixels (intensity range 100–200 in grayscale)
|
| 29 |
+
lower_bound = 100
|
| 30 |
+
upper_bound = 200
|
| 31 |
+
mask = cv2.inRange(blurred, lower_bound, upper_bound)
|
| 32 |
+
|
| 33 |
+
# Apply morphological operation to enhance circular patterns
|
| 34 |
+
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
|
| 35 |
+
enhanced = cv2.dilate(mask, kernel, iterations=2)
|
| 36 |
+
|
| 37 |
+
return enhanced
|
| 38 |
|
| 39 |
def detect_circles(frame_diff, image_center, min_radius=20, max_radius=200):
|
| 40 |
"""Detect circles in a frame difference image, centered at the Sun."""
|
|
|
|
| 43 |
cv2.HOUGH_GRADIENT,
|
| 44 |
dp=1.5, # Resolution for better detection
|
| 45 |
minDist=100, # Prevent overlapping circles
|
| 46 |
+
param1=80, # Lower edge threshold to capture fainter edges
|
| 47 |
+
param2=15, # Lower accumulator threshold to detect faint circles
|
| 48 |
minRadius=min_radius,
|
| 49 |
maxRadius=max_radius
|
| 50 |
)
|
|
|
|
| 62 |
return None
|
| 63 |
|
| 64 |
def analyze_gif(gif_file):
|
| 65 |
+
"""Analyze a GIF for growing concentric circles of mid-to-light pixels."""
|
| 66 |
try:
|
| 67 |
# Handle Gradio file input
|
| 68 |
gif_path = gif_file.name if hasattr(gif_file, 'name') else gif_file
|
|
|
|
| 91 |
|
| 92 |
# Compute absolute difference between consecutive frames
|
| 93 |
frame_diff = cv2.absdiff(frame2, frame1)
|
| 94 |
+
# Enhance contrast for the difference image
|
| 95 |
frame_diff = cv2.convertScaleAbs(frame_diff, alpha=3.0, beta=0)
|
| 96 |
|
| 97 |
# Detect circles centered at the Sun
|
|
|
|
| 120 |
|
| 121 |
# Generate output frames and report
|
| 122 |
results = []
|
| 123 |
+
report = "Analysis Report (as of 07:34 PM PDT, May 24, 2025):\n"
|
| 124 |
if growing_circle_data:
|
| 125 |
+
report += f"Detected {len(growing_circle_data)} frames with growing concentric circles of mid-to-light pixels:\n"
|
| 126 |
for c in growing_circle_data:
|
| 127 |
# Visualize the frame with detected circle
|
| 128 |
output_frame = cv2.cvtColor(c["output_frame"], cv2.COLOR_GRAY2RGB)
|
|
|
|
| 132 |
results.append(output_frame)
|
| 133 |
|
| 134 |
report += f"Frame {c['frame']}: Center at {c['center']}, Radius {c['radius']} pixels\n"
|
| 135 |
+
report += "\nConclusion: Growing concentric circles of mid-to-light pixels detected, indicative of a potential Earth-directed CME."
|
| 136 |
else:
|
| 137 |
+
report += "No growing concentric circles of mid-to-light pixels detected. CME may not be Earth-directed."
|
| 138 |
|
| 139 |
return report, results
|
| 140 |
except Exception as e:
|
|
|
|
| 149 |
gr.Gallery(label="Frames with Growing Circles")
|
| 150 |
],
|
| 151 |
title="Solar CME Detection",
|
| 152 |
+
description="Upload a GIF of solar images to detect growing concentric circles of mid-to-light pixels indicative of Earth-directed coronal mass ejections (CMEs)."
|
| 153 |
)
|
| 154 |
|
| 155 |
if __name__ == "__main__":
|