Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -154,8 +154,6 @@ def predict():
|
|
154 |
output[0, class_index].backward()
|
155 |
cam = gradcam.generate(class_index)
|
156 |
|
157 |
-
|
158 |
-
|
159 |
# β
Ensure cam is 2D
|
160 |
if cam.ndim == 3:
|
161 |
cam = cam[0]
|
@@ -164,18 +162,22 @@ def predict():
|
|
164 |
cam = np.uint8(255 * cam)
|
165 |
cam = cv2.resize(cam, (224, 224))
|
166 |
|
167 |
-
# β
Create overlay
|
168 |
original_img = np.asarray(img.resize((224, 224)))
|
169 |
heatmap = cv2.applyColorMap(cam, cv2.COLORMAP_JET)
|
170 |
-
|
171 |
-
# β
Final overlay
|
172 |
overlay = cv2.addWeighted(original_img, 0.6, heatmap, 0.4, 0)
|
173 |
-
|
|
|
174 |
gradcam_filename = f"gradcam_{timestamp}.png"
|
175 |
gradcam_file_path = os.path.join(OUTPUT_DIR, gradcam_filename)
|
176 |
cv2.imwrite(gradcam_file_path, overlay)
|
177 |
-
|
178 |
-
# β
Save
|
|
|
|
|
|
|
|
|
|
|
179 |
conn = sqlite3.connect(DB_PATH)
|
180 |
cursor = conn.cursor()
|
181 |
cursor.execute("""
|
@@ -184,7 +186,8 @@ def predict():
|
|
184 |
""", (uploaded_filename, result, confidence, gradcam_filename, datetime.now().isoformat()))
|
185 |
conn.commit()
|
186 |
conn.close()
|
187 |
-
|
|
|
188 |
return jsonify({
|
189 |
'prediction': result,
|
190 |
'confidence': confidence,
|
@@ -192,9 +195,9 @@ def predict():
|
|
192 |
'early_glaucoma_probability': float(probabilities[1]),
|
193 |
'advanced_glaucoma_probability': float(probabilities[2]),
|
194 |
'gradcam_image': gradcam_filename,
|
|
|
195 |
'image_filename': uploaded_filename
|
196 |
})
|
197 |
-
|
198 |
except Exception as e:
|
199 |
return jsonify({'error': str(e)}), 500
|
200 |
|
|
|
154 |
output[0, class_index].backward()
|
155 |
cam = gradcam.generate(class_index)
|
156 |
|
|
|
|
|
157 |
# β
Ensure cam is 2D
|
158 |
if cam.ndim == 3:
|
159 |
cam = cam[0]
|
|
|
162 |
cam = np.uint8(255 * cam)
|
163 |
cam = cv2.resize(cam, (224, 224))
|
164 |
|
165 |
+
# β
Create color overlay
|
166 |
original_img = np.asarray(img.resize((224, 224)))
|
167 |
heatmap = cv2.applyColorMap(cam, cv2.COLORMAP_JET)
|
|
|
|
|
168 |
overlay = cv2.addWeighted(original_img, 0.6, heatmap, 0.4, 0)
|
169 |
+
|
170 |
+
# β
Save color overlay
|
171 |
gradcam_filename = f"gradcam_{timestamp}.png"
|
172 |
gradcam_file_path = os.path.join(OUTPUT_DIR, gradcam_filename)
|
173 |
cv2.imwrite(gradcam_file_path, overlay)
|
174 |
+
|
175 |
+
# β
Save grayscale overlay
|
176 |
+
gray_filename = f"gradcam_gray_{timestamp}.png"
|
177 |
+
gray_file_path = os.path.join(OUTPUT_DIR, gray_filename)
|
178 |
+
cv2.imwrite(gray_file_path, cam)
|
179 |
+
|
180 |
+
# β
Save results to database
|
181 |
conn = sqlite3.connect(DB_PATH)
|
182 |
cursor = conn.cursor()
|
183 |
cursor.execute("""
|
|
|
186 |
""", (uploaded_filename, result, confidence, gradcam_filename, datetime.now().isoformat()))
|
187 |
conn.commit()
|
188 |
conn.close()
|
189 |
+
|
190 |
+
# β
Return results
|
191 |
return jsonify({
|
192 |
'prediction': result,
|
193 |
'confidence': confidence,
|
|
|
195 |
'early_glaucoma_probability': float(probabilities[1]),
|
196 |
'advanced_glaucoma_probability': float(probabilities[2]),
|
197 |
'gradcam_image': gradcam_filename,
|
198 |
+
'gradcam_gray_image': gray_filename,
|
199 |
'image_filename': uploaded_filename
|
200 |
})
|
|
|
201 |
except Exception as e:
|
202 |
return jsonify({'error': str(e)}), 500
|
203 |
|