Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -213,6 +213,62 @@ def predict():
|
|
| 213 |
traceback.print_exc()
|
| 214 |
return jsonify({'error': str(e)}), 500
|
| 215 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
if __name__ == '__main__':
|
| 217 |
print("\n" + "="*60)
|
| 218 |
print("🧠 Brain Tumor Segmentation App")
|
|
|
|
| 213 |
traceback.print_exc()
|
| 214 |
return jsonify({'error': str(e)}), 500
|
| 215 |
|
| 216 |
+
@app.route('/test-example', methods=['POST'])
|
| 217 |
+
def test_example():
|
| 218 |
+
"""Test with example MHA file"""
|
| 219 |
+
try:
|
| 220 |
+
example_path = 'image/VSD.Brain.XX.O.MR_Flair.35796.mha'
|
| 221 |
+
|
| 222 |
+
if not os.path.exists(example_path):
|
| 223 |
+
return jsonify({'error': 'Example MHA file not found. Please add VSD.Brain.XX.O.MR_Flair.35796.mha to image/ folder'}), 404
|
| 224 |
+
|
| 225 |
+
print(f"Testing with example file: {example_path}")
|
| 226 |
+
|
| 227 |
+
# Preprocess MHA file
|
| 228 |
+
img_input, original_slice = preprocess_image(example_path)
|
| 229 |
+
|
| 230 |
+
# Predict
|
| 231 |
+
print("Making prediction on example...")
|
| 232 |
+
prediction = model.predict(img_input, verbose=0)
|
| 233 |
+
|
| 234 |
+
# Postprocess mask
|
| 235 |
+
mask = postprocess_mask(prediction[0], original_slice.shape)
|
| 236 |
+
|
| 237 |
+
# Normalize original slice for display
|
| 238 |
+
original_display = ((original_slice - original_slice.min()) /
|
| 239 |
+
(original_slice.max() - original_slice.min() + 1e-8) * 255).astype(np.uint8)
|
| 240 |
+
|
| 241 |
+
# Create overlay
|
| 242 |
+
overlay = create_overlay(original_display, mask)
|
| 243 |
+
|
| 244 |
+
# Convert to base64
|
| 245 |
+
original_base64 = img_to_base64(original_display)
|
| 246 |
+
mask_base64 = img_to_base64(mask)
|
| 247 |
+
overlay_base64 = img_to_base64(overlay)
|
| 248 |
+
|
| 249 |
+
# Calculate statistics
|
| 250 |
+
tumor_pixels = np.sum(mask > 127)
|
| 251 |
+
total_pixels = mask.shape[0] * mask.shape[1]
|
| 252 |
+
tumor_percentage = (tumor_pixels / total_pixels) * 100
|
| 253 |
+
|
| 254 |
+
result = {
|
| 255 |
+
'original': original_base64,
|
| 256 |
+
'mask': mask_base64,
|
| 257 |
+
'overlay': overlay_base64,
|
| 258 |
+
'tumor_percentage': float(tumor_percentage),
|
| 259 |
+
'image_size': f"{mask.shape[1]}x{mask.shape[0]}"
|
| 260 |
+
}
|
| 261 |
+
|
| 262 |
+
print(f"✓ Example prediction completed: {tumor_percentage:.2f}% tumor detected")
|
| 263 |
+
|
| 264 |
+
return jsonify(result)
|
| 265 |
+
|
| 266 |
+
except Exception as e:
|
| 267 |
+
print(f"Error during example prediction: {e}")
|
| 268 |
+
import traceback
|
| 269 |
+
traceback.print_exc()
|
| 270 |
+
return jsonify({'error': str(e)}), 500
|
| 271 |
+
|
| 272 |
if __name__ == '__main__':
|
| 273 |
print("\n" + "="*60)
|
| 274 |
print("🧠 Brain Tumor Segmentation App")
|