An advanced object detection and segmentation pipeline that leverages YOLOv8 for tumor localization and SAM (Segment Anything Model) for precise mask generation. Developed using PyTorch, OpenCV, and integrated with an optional Streamlit UI for real-time interaction.
This project combines the power of YOLOv8 (by Ultralytics) and SAM (by Meta AI) to detect and segment brain tumors from MRI scans.
- 🔍 Detects multiple objects with YOLOv8
- 🧠 Generates pixel-accurate tumor masks using SAM
- 💾 Outputs bounding box and segmentation coordinates
- 📊 Supports general object detection from COCO too
Original brain scan used as input:
After YOLOv8 detection + SAM segmentation:
- YOLOv8 detects all bounding boxes of interest (e.g., tumors).
- Each bounding box is passed to SAM, which generates a detailed segmentation mask.
- Both bounding boxes and polygonal masks are saved in
.txt
files for further use or annotation.
Clone the repository and install the necessary libraries:
git clone https://github.com/your-username/brain-tumor-yolo-sam.git
cd brain-tumor-yolo-sam
pip install torch torchvision torchaudio
pip install opencv-python
pip install ultralytics
pip install git+https://github.com/facebookresearch/segment-anything.git
pip install streamlit
cd YOLOV8_SAM
python detect_multi_object_SAM.py
streamlit run appy.py
Model | Description | Source |
---|---|---|
YOLOv8s | Object detection for bounding boxes | Ultralytics YOLOv8 |
SAM ViT-H | Segmentation from bounding boxes | Meta AI Segment Anything |
- BraTS 2021: Brain Tumor Segmentation Challenge
- MRI-based brain tumor scans
- Converted to YOLOv8 format inside
yolo_brain_dataset/
- Masks saved in
output/masks/
File Name | Description |
---|---|
bounding_box_image1.txt |
YOLOv8 bounding boxes for test image |
yolo_mask_image1.txt |
Normalized mask polygon coordinates |
output_detection.jpg |
Annotated image with box + mask overlay |
The pipeline also works on general object detection tasks. For example:
- Detected: Class 74 = Book
You can try feeding other images to test its generalizability.
Abdul Rafay
📚 BS Software Engineering | 🎯 AI & ML Enthusiast
🔗 LinkedIn
This repository is licensed under the MIT License.
If you found this helpful:
- ⭐ Star the repo
- 🍴 Fork it and contribute
- 📢 Share on LinkedIn and tag me!
🔍 Accurate detection. 🎯 Precise segmentation. 🚀 Built with passion.