luigi12345
commited on
Commit
β’
f192f0b
1
Parent(s):
131b493
app.py
CHANGED
@@ -112,117 +112,83 @@ def get_confidence_level(confidence):
|
|
112 |
def main():
|
113 |
st.set_page_config(layout="wide", page_title="Glaucoma Screening Tool")
|
114 |
|
115 |
-
# Header with better styling
|
116 |
st.markdown("""
|
117 |
<h1 style='text-align: center;'>Glaucoma Screening from Retinal Fundus Images</h1>
|
118 |
<p style='text-align: center; color: gray;'>Upload retinal images for automated glaucoma detection and optic disc/cup segmentation</p>
|
119 |
""", unsafe_allow_html=True)
|
120 |
|
121 |
-
# Sidebar
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
help="Images with confidence above this threshold will be marked as reliable predictions")
|
135 |
|
136 |
if uploaded_files:
|
137 |
for uploaded_file in uploaded_files:
|
138 |
image = Image.open(uploaded_file).convert('RGB')
|
139 |
image_np = np.array(image).astype(np.uint8)
|
140 |
|
|
|
|
|
141 |
with st.spinner(f'π Processing {uploaded_file.name}...'):
|
142 |
model = GlaucomaModel(device=torch.device("cuda:0" if torch.cuda.is_available() else "cpu"))
|
143 |
disease_idx, disc_cup_image, vcdr, cls_conf, cup_conf, disc_conf, cropped_image = model.process(image_np)
|
144 |
|
145 |
-
#
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
st.warning("β οΈ Elevated CDR (>0.7)")
|
196 |
-
elif vcdr > 0.5:
|
197 |
-
st.info("βΉοΈ Borderline CDR (0.5-0.7)")
|
198 |
-
else:
|
199 |
-
st.success("β
Normal CDR (<0.5)")
|
200 |
-
|
201 |
-
# Download section
|
202 |
-
if download_confident_images:
|
203 |
-
st.sidebar.markdown("---")
|
204 |
-
st.sidebar.markdown("### Download Results")
|
205 |
-
with zipfile.ZipFile("confident_cropped_images.zip", "w") as zf:
|
206 |
-
for cropped_image, name in download_confident_images:
|
207 |
-
img_buffer = io.BytesIO()
|
208 |
-
Image.fromarray(cropped_image).save(img_buffer, format="PNG")
|
209 |
-
zf.writestr(f"{name}_cropped.png", img_buffer.getvalue())
|
210 |
-
|
211 |
-
st.sidebar.download_button(
|
212 |
-
label="π₯ Download Analysis Results",
|
213 |
-
data=open("confident_cropped_images.zip", "rb"),
|
214 |
-
file_name="glaucoma_analysis_results.zip",
|
215 |
-
mime="application/zip",
|
216 |
-
help="Download cropped images and analysis results"
|
217 |
-
)
|
218 |
-
else:
|
219 |
-
# Welcome message when no files are uploaded
|
220 |
-
st.markdown("""
|
221 |
-
<div style='text-align: center; padding: 50px;'>
|
222 |
-
<h3>π Welcome to the Glaucoma Screening Tool</h3>
|
223 |
-
<p>Upload retinal fundus images using the sidebar to begin analysis</p>
|
224 |
-
</div>
|
225 |
-
""", unsafe_allow_html=True)
|
226 |
-
|
227 |
-
if __name__ == '__main__':
|
228 |
-
main()
|
|
|
112 |
def main():
|
113 |
st.set_page_config(layout="wide", page_title="Glaucoma Screening Tool")
|
114 |
|
|
|
115 |
st.markdown("""
|
116 |
<h1 style='text-align: center;'>Glaucoma Screening from Retinal Fundus Images</h1>
|
117 |
<p style='text-align: center; color: gray;'>Upload retinal images for automated glaucoma detection and optic disc/cup segmentation</p>
|
118 |
""", unsafe_allow_html=True)
|
119 |
|
120 |
+
# Sidebar settings
|
121 |
+
st.sidebar.markdown("### Upload Settings")
|
122 |
+
uploaded_files = st.sidebar.file_uploader("Upload Retinal Images",
|
123 |
+
type=['png', 'jpeg', 'jpg'],
|
124 |
+
accept_multiple_files=True,
|
125 |
+
help="Support multiple images in PNG, JPEG formats")
|
126 |
+
|
127 |
+
st.sidebar.markdown("### Analysis Settings")
|
128 |
+
st.sidebar.info("π Set confidence threshold to filter results")
|
129 |
+
confidence_threshold = st.sidebar.slider(
|
130 |
+
"Classification Confidence Threshold (%)",
|
131 |
+
0, 100, 70,
|
132 |
+
help="Images with confidence above this threshold will be marked as reliable predictions")
|
|
|
133 |
|
134 |
if uploaded_files:
|
135 |
for uploaded_file in uploaded_files:
|
136 |
image = Image.open(uploaded_file).convert('RGB')
|
137 |
image_np = np.array(image).astype(np.uint8)
|
138 |
|
139 |
+
st.markdown(f"## π Analysis Results: {uploaded_file.name}")
|
140 |
+
|
141 |
with st.spinner(f'π Processing {uploaded_file.name}...'):
|
142 |
model = GlaucomaModel(device=torch.device("cuda:0" if torch.cuda.is_available() else "cpu"))
|
143 |
disease_idx, disc_cup_image, vcdr, cls_conf, cup_conf, disc_conf, cropped_image = model.process(image_np)
|
144 |
|
145 |
+
# Image display section
|
146 |
+
st.subheader("Original Image")
|
147 |
+
st.image(image_np, use_column_width=True)
|
148 |
+
st.subheader("Segmentation Overlay")
|
149 |
+
st.image(disc_cup_image, use_column_width=True)
|
150 |
+
st.subheader("Region of Interest")
|
151 |
+
st.image(cropped_image, use_column_width=True)
|
152 |
+
|
153 |
+
st.markdown("---")
|
154 |
+
|
155 |
+
# Classification Results
|
156 |
+
st.markdown("### π Classification")
|
157 |
+
diagnosis = model.cls_id2label[disease_idx]
|
158 |
+
is_confident = cls_conf >= confidence_threshold
|
159 |
+
|
160 |
+
if diagnosis == "Glaucoma":
|
161 |
+
st.markdown(f"<div style='padding: 10px; background-color: #ffebee; border-radius: 5px;'>"
|
162 |
+
f"<h4 style='color: #c62828;'>Diagnosis: {diagnosis}</h4></div>",
|
163 |
+
unsafe_allow_html=True)
|
164 |
+
else:
|
165 |
+
st.markdown(f"<div style='padding: 10px; background-color: #e8f5e9; border-radius: 5px;'>"
|
166 |
+
f"<h4 style='color: #2e7d32;'>Diagnosis: {diagnosis}</h4></div>",
|
167 |
+
unsafe_allow_html=True)
|
168 |
+
|
169 |
+
st.write(f"Classification Confidence: {cls_conf:.1f}%")
|
170 |
+
if not is_confident:
|
171 |
+
st.warning("β οΈ Below confidence threshold")
|
172 |
+
|
173 |
+
# Segmentation Results
|
174 |
+
st.markdown("### π― Segmentation Quality")
|
175 |
+
st.write(f"Optic Cup Confidence: {cup_conf:.1f}%")
|
176 |
+
st.write(f"Optic Disc Confidence: {disc_conf:.1f}%")
|
177 |
+
|
178 |
+
cup_level = get_confidence_level(cup_conf)
|
179 |
+
disc_level = get_confidence_level(disc_conf)
|
180 |
+
st.info(f"Cup Detection: {cup_level}\nDisc Detection: {disc_level}")
|
181 |
+
|
182 |
+
# Clinical Metrics
|
183 |
+
st.markdown("### π Clinical Metrics")
|
184 |
+
st.write(f"Cup-to-Disc Ratio (CDR): {vcdr:.3f}")
|
185 |
+
|
186 |
+
if vcdr > 0.7:
|
187 |
+
st.warning("β οΈ Elevated CDR (>0.7)")
|
188 |
+
elif vcdr > 0.5:
|
189 |
+
st.info("βΉοΈ Borderline CDR (0.5-0.7)")
|
190 |
+
else:
|
191 |
+
st.success("β
Normal CDR (<0.5)")
|
192 |
+
|
193 |
+
st.markdown("---")
|
194 |
+
# ... rest of the code remains the same ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|