
Introduction
The SAM2 (Segment Anything Model) is a cutting-edge tool for object detection and segmentation. In this blog, I'll guide you through the process of creating an image segmentation application that uses SAM2 to perform object detection and segmentation on uploaded images.
Features
- Image Upload: Users can upload images for processing.
- Segmentation: The SAM2 model segments objects within the uploaded image.
- Results: Segmented masks and a combined mask of the image are displayed.
Getting Started
Prerequisites
- Python 3.6 or later
- Flask
- Replicate Python Client
Installation
- Clone the Repository:
git clone https://github.com/mandarwagh9/Image-segmenting-using-SAM2.git cd Image-segmenting-using-SAM2
- Install Dependencies:
- Create a virtual environment and activate it:
python -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
- Install the required packages:
pip install -r requirements.txt
- Create a virtual environment and activate it:
Set Up Environment Variables
Create a .env
file in the project root and add your Replicate API token:
REPLICATE_API_TOKEN=your_replicate_api_token_here
Running the Application
Start the Flask Server
python app.py
Access the Application
Open a web browser and navigate to http://127.0.0.1:5000/ to access the application.
Usage
- Navigate to the Application
- Upload an Image
Use the upload form to select and upload an image. Ensure the image is in one of the allowed formats: PNG, JPG, JPEG, or GIF, and has been uploaded to an image hosting site so that you can provide a link in the
app.py
for further processing. - View Results
After uploading, the application will process the image using the SAM2 model and display the segmentation results, including combined and individual masks in your terminal.
File Structure
app.py
: Main Flask application file.templates/
: Directory containing HTML templates for the web pages.uploads/
: Directory for storing uploaded images.requirements.txt
: File listing the Python dependencies.
Contributing
Feel free to fork the repository and submit pull requests. For any issues or feature requests, please open an issue on GitHub.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Acknowledgements
Special thanks to Replicate for providing the SAM2 model and to Flask for the web framework.