Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -99,16 +99,23 @@ def test_file():
|
|
99 |
|
100 |
@app.route('/predict', methods=['POST'])
|
101 |
def predict():
|
102 |
-
"""Perform prediction and save
|
103 |
if 'file' not in request.files:
|
104 |
return jsonify({'error': 'No file uploaded'}), 400
|
105 |
|
106 |
-
|
107 |
-
if
|
108 |
return jsonify({'error': 'No file selected'}), 400
|
109 |
|
110 |
try:
|
111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
img_array = preprocess_image(img)
|
113 |
|
114 |
prediction = model.predict(img_array)[0]
|
@@ -117,18 +124,18 @@ def predict():
|
|
117 |
result = 'Glaucoma' if glaucoma_prob > normal_prob else 'Normal'
|
118 |
confidence = float(glaucoma_prob) if result == 'Glaucoma' else float(normal_prob)
|
119 |
|
120 |
-
# Grad-CAM
|
121 |
heatmap = make_gradcam(img_array, model, last_conv_layer_name='Conv_1_bn')
|
122 |
-
gradcam_filename = f"gradcam_{
|
123 |
save_gradcam_image(img, heatmap, filename=gradcam_filename)
|
124 |
|
125 |
-
# Save results to SQLite
|
126 |
conn = sqlite3.connect(DB_PATH)
|
127 |
cursor = conn.cursor()
|
128 |
cursor.execute("""
|
129 |
INSERT INTO results (image_filename, prediction, confidence, gradcam_filename, timestamp)
|
130 |
VALUES (?, ?, ?, ?, ?)
|
131 |
-
""", (
|
132 |
conn.commit()
|
133 |
conn.close()
|
134 |
|
@@ -137,7 +144,8 @@ def predict():
|
|
137 |
'confidence': confidence,
|
138 |
'normal_probability': float(normal_prob),
|
139 |
'glaucoma_probability': float(glaucoma_prob),
|
140 |
-
'gradcam_image': gradcam_filename
|
|
|
141 |
})
|
142 |
|
143 |
except Exception as e:
|
@@ -174,5 +182,14 @@ def get_gradcam(filename):
|
|
174 |
else:
|
175 |
return jsonify({'error': 'File not found'}), 404
|
176 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
177 |
if __name__ == '__main__':
|
178 |
app.run(host='0.0.0.0', port=7860)
|
|
|
99 |
|
100 |
@app.route('/predict', methods=['POST'])
|
101 |
def predict():
|
102 |
+
"""Perform prediction, save results (including uploaded image), and save to SQLite database."""
|
103 |
if 'file' not in request.files:
|
104 |
return jsonify({'error': 'No file uploaded'}), 400
|
105 |
|
106 |
+
uploaded_file = request.files['file']
|
107 |
+
if uploaded_file.filename == '':
|
108 |
return jsonify({'error': 'No file selected'}), 400
|
109 |
|
110 |
try:
|
111 |
+
# β
Save the uploaded image
|
112 |
+
timestamp = int(datetime.now().timestamp())
|
113 |
+
uploaded_filename = f"uploaded_{timestamp}.png"
|
114 |
+
uploaded_file_path = os.path.join(OUTPUT_DIR, uploaded_filename)
|
115 |
+
uploaded_file.save(uploaded_file_path)
|
116 |
+
|
117 |
+
# β
Perform prediction
|
118 |
+
img = Image.open(uploaded_file_path).convert('RGB')
|
119 |
img_array = preprocess_image(img)
|
120 |
|
121 |
prediction = model.predict(img_array)[0]
|
|
|
124 |
result = 'Glaucoma' if glaucoma_prob > normal_prob else 'Normal'
|
125 |
confidence = float(glaucoma_prob) if result == 'Glaucoma' else float(normal_prob)
|
126 |
|
127 |
+
# β
Grad-CAM
|
128 |
heatmap = make_gradcam(img_array, model, last_conv_layer_name='Conv_1_bn')
|
129 |
+
gradcam_filename = f"gradcam_{timestamp}.png"
|
130 |
save_gradcam_image(img, heatmap, filename=gradcam_filename)
|
131 |
|
132 |
+
# β
Save results to SQLite
|
133 |
conn = sqlite3.connect(DB_PATH)
|
134 |
cursor = conn.cursor()
|
135 |
cursor.execute("""
|
136 |
INSERT INTO results (image_filename, prediction, confidence, gradcam_filename, timestamp)
|
137 |
VALUES (?, ?, ?, ?, ?)
|
138 |
+
""", (uploaded_filename, result, confidence, gradcam_filename, datetime.now().isoformat()))
|
139 |
conn.commit()
|
140 |
conn.close()
|
141 |
|
|
|
144 |
'confidence': confidence,
|
145 |
'normal_probability': float(normal_prob),
|
146 |
'glaucoma_probability': float(glaucoma_prob),
|
147 |
+
'gradcam_image': gradcam_filename,
|
148 |
+
'image_filename': uploaded_filename
|
149 |
})
|
150 |
|
151 |
except Exception as e:
|
|
|
182 |
else:
|
183 |
return jsonify({'error': 'File not found'}), 404
|
184 |
|
185 |
+
@app.route('/image/<filename>')
|
186 |
+
def get_image(filename):
|
187 |
+
"""Serve the original uploaded image."""
|
188 |
+
filepath = os.path.join(OUTPUT_DIR, filename)
|
189 |
+
if os.path.exists(filepath):
|
190 |
+
return send_file(filepath, mimetype='image/png')
|
191 |
+
else:
|
192 |
+
return jsonify({'error': 'File not found'}), 404
|
193 |
+
|
194 |
if __name__ == '__main__':
|
195 |
app.run(host='0.0.0.0', port=7860)
|