A comprehensive deep learning-based web application for automated brain tumor detection, classification, and segmentation from MRI scans. This project leverages convolutional neural networks (CNN) and U-Net architecture to assist in medical diagnosis.
- Overview
- Features
- Project Structure
- Technologies Used
- Models
- Installation
- Usage
- Dataset
- Model Performance
- Screenshots
- Future Enhancements
- Contributing
- License
This web application provides an end-to-end solution for brain tumor analysis using MRI images. It combines two powerful deep learning models:
- Classification Model: Identifies the type of brain tumor (Glioma, Meningioma, Pituitary, or No Tumor)
- Segmentation Model: Precisely delineates tumor boundaries within the MRI scan
The system is designed to assist medical professionals in making faster and more accurate diagnoses by providing both tumor classification and visual segmentation results.
-
๐ Multi-class Tumor Classification: Classifies brain MRI scans into four categories:
- Glioma
- Meningioma
- Pituitary Tumor
- No Tumor
-
๐จ Tumor Segmentation: Provides pixel-level segmentation of tumor regions using U-Net architecture
-
๐ค Patient Information Management: Captures and displays patient demographics (name, age, gender)
-
๐ Visual Results: Side-by-side display of original MRI and segmented output
-
๐ Web-Based Interface: User-friendly Flask web application accessible via browser
-
โก Real-time Prediction: Fast inference on uploaded MRI images
brain-tumor-webapp/
โ
โโโ app.py # Main Flask application
โโโ readme.md # Project documentation
โ
โโโ models/ # Trained model files and prediction scripts
โ โโโ brain_tumor_segmentation.h5 # U-Net segmentation model
โ โโโ Model m2.h5 # CNN classification model
โ โโโ predict.py # Prediction functions (inference logic)
โ
โโโ static/ # Static files
โ โโโ uploads/ # Uploaded MRI images
โ โโโ results/ # Segmentation results
โ
โโโ templates/ # HTML templates
โ โโโ index.html # Main web interface
โ
โโโ training_scripts/ # Model training notebooks
โโโ Brain_Tumor_Classification.ipynb # Classification model training
โโโ Tumor_Segmentation.ipynb # Segmentation model training
- Flask: Web framework for Python
- TensorFlow/Keras: Deep learning framework for model training and inference
- NumPy: Numerical computing
- OpenCV: Image processing
- Pillow: Image manipulation
- HTML5: Structure
- Bootstrap 5.3.2: Responsive UI design
- CSS: Styling
- Convolutional Neural Networks (CNN): For tumor classification
- U-Net Architecture: For tumor segmentation
- Image Data Generators: For data augmentation during training
Architecture:
- Custom CNN with multiple convolutional blocks
- Architecture layers:
- Conv2D (64 filters, 3x3 kernel) โ ReLU
- Conv2D (64 filters, 3x3 kernel) โ ReLU
- MaxPooling2D (2x2)
- Conv2D (128 filters, 3x3 kernel) โ ReLU
- Conv2D (128 filters, 3x3 kernel) โ ReLU
- MaxPooling2D (2x2)
- Conv2D (256 filters, 3x3 kernel) โ ReLU
- Conv2D (256 filters, 3x3 kernel) โ ReLU
- MaxPooling2D (2x2)
- Conv2D (512 filters, 3x3 kernel) โ ReLU
- Conv2D (512 filters, 3x3 kernel) โ ReLU
- MaxPooling2D (2x2)
- Flatten
- Dense (256 units) โ ReLU โ Dropout
- Dense (64 units) โ ReLU โ Dropout
- Dense (4 units) โ Softmax
Training Details:
- Optimizer: Adamax (learning rate: 0.001)
- Loss Function: Categorical Crossentropy
- Metrics: Accuracy
- Output Classes: 4 (Glioma, Meningioma, No Tumor, Pituitary)
Architecture:
- U-Net based architecture with encoder-decoder structure
- Image Size: 256ร256 pixels
- Architecture components:
- Encoder:
- Multiple convolutional blocks with downsampling
- Captures contextual information at different scales
- Decoder:
- Upsampling layers with skip connections
- Reconstructs spatial resolution
- Skip Connections:
- Connects encoder and decoder layers
- Preserves fine-grained details
- Encoder:
Training Details:
- Optimizer: Adam
- Loss Function: Binary Crossentropy
- Metrics: Accuracy
- Input/Output Size: 256ร256ร3 โ 256ร256ร1
- Python 3.8 or higher
- pip package manager
- GPU (optional, but recommended for faster inference)
git clone <repository-url>
cd brain-tumor-webapppython -m venv venv
# On Windows
venv\Scripts\activate
# On macOS/Linux
source venv/bin/activatepip install flask
pip install tensorflow
pip install keras
pip install numpy
pip install opencv-python
pip install pillow
pip install werkzeugOr create a requirements.txt file:
flask==3.0.0
tensorflow==2.15.0
keras==3.0.0
numpy==1.26.0
opencv-python==4.9.0
Pillow==10.2.0
werkzeug==3.0.1Then install:
pip install -r requirements.txtmodels/predict.py file is missing. You need to create it with the following content:
# models/predict.py
import tensorflow as tf
import numpy as np
import cv2
from PIL import Image
import os
# Load models
classification_model = tf.keras.models.load_model('models/Model m2.h5')
segmentation_model = tf.keras.models.load_model('models/brain_tumor_segmentation.h5')
# Class names for classification
class_names = ['glioma', 'meningioma', 'notumor', 'pituitary']
def classify_tumor(image_path):
"""
Classify the type of brain tumor from MRI image
Args:
image_path: Path to the MRI image
Returns:
tumor_type: String indicating the tumor type
"""
# Load and preprocess image
img = Image.open(image_path)
img = img.resize((224, 224)) # Adjust size based on your model
img_array = np.array(img) / 255.0
img_array = np.expand_dims(img_array, axis=0)
# Predict
predictions = classification_model.predict(img_array)
predicted_class = np.argmax(predictions, axis=1)[0]
return class_names[predicted_class]
def segment_tumor(image_path):
"""
Segment tumor region from MRI image
Args:
image_path: Path to the MRI image
Returns:
segmented_path: Path to the saved segmented image
"""
# Load and preprocess image
img = cv2.imread(image_path)
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img_resized = cv2.resize(img_rgb, (256, 256))
img_normalized = img_resized / 255.0
img_input = np.expand_dims(img_normalized, axis=0)
# Predict segmentation mask
prediction = segmentation_model.predict(img_input)[0]
mask = (prediction > 0.5).astype(np.uint8) * 255
# Apply mask to original image
mask_3channel = cv2.merge([mask[:,:,0], mask[:,:,0], mask[:,:,0]])
segmented = cv2.bitwise_and(img_resized, mask_3channel)
# Save segmented image
filename = os.path.basename(image_path)
save_dir = os.path.dirname(image_path)
segmented_filename = f"segmented_{filename}"
segmented_path = os.path.join(save_dir, segmented_filename)
cv2.imwrite(segmented_path, cv2.cvtColor(segmented, cv2.COLOR_RGB2BGR))
return segmented_path- Start the Flask server:
python app.py- Open your browser and navigate to:
http://localhost:5000
- Using the Application:
- Enter patient information (name, age, gender)
- Upload an MRI brain scan image (JPEG, PNG formats supported)
- Click "Analyze" button
- View results:
- Original MRI image
- Predicted tumor type
- Segmented tumor visualization
GET /: Displays the main web interfacePOST /: Handles image upload and returns prediction results
The models were trained on a comprehensive brain tumor MRI dataset containing:
- Total Images: 5,732+ MRI scans
- Classes:
- Glioma
- Meningioma
- Pituitary Tumor
- No Tumor
Data Split:
- Training Set: ~70%
- Validation Set: ~15%
- Test Set: ~15%
Data Source: The dataset was sourced from publicly available medical imaging repositories, including Kaggle and medical imaging databases.
Training utilized data augmentation techniques:
- Rotation
- Width/Height shifts
- Zoom
- Horizontal flip
- Brightness adjustment
- Training Accuracy: ~95%+
- Validation Accuracy: ~93%+
- Test Accuracy: ~92%+
- Training Accuracy: ~96%+
- Loss: Binary Crossentropy
- Epochs: 20-50 (varies based on training configuration)
Note: Exact metrics can be found in the training notebooks in the training_scripts/ directory.
The application features a clean, Bootstrap-based interface with:
- Patient information form
- File upload button
- Result display area with original and segmented images
- Tumor classification label
- Add confidence scores for predictions
- Support for DICOM format medical images
- Batch processing of multiple MRI scans
- 3D visualization of tumor regions
- Integration with hospital information systems (HIS)
- Export reports in PDF format
- User authentication and patient record management
- Support for additional tumor types
- Model explainability (Grad-CAM visualization)
- REST API for integration with other systems
- Mobile application version
- Real-time streaming analysis
Issue 1: Model file not found
Solution: Ensure model files are in the models/ directory:
- brain_tumor_segmentation.h5
- Model m2.h5
Issue 2: Import errors
Solution: Install all required dependencies:
pip install -r requirements.txt
Issue 3: Prediction errors
Solution: Check that predict.py exists in models/ directory
Verify image format is supported (JPEG, PNG)
Issue 4: Upload folder errors
Solution: Create static/uploads/ directory if it doesn't exist
Ensure write permissions are set correctly
Contributions are welcome! Please follow these steps:
- Fork the repository
- Create a new branch (
git checkout -b feature/improvement) - Make your changes
- Commit your changes (
git commit -am 'Add new feature') - Push to the branch (
git push origin feature/improvement) - Create a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
This application is designed for educational and research purposes only. It should NOT be used as a replacement for professional medical diagnosis. Always consult qualified healthcare professionals for medical advice and diagnosis.
- TensorFlow/Keras team for the deep learning framework
- Medical imaging community for providing datasets
- Flask framework developers
- Bootstrap team for the UI framework
- All contributors and researchers in medical AI