← Home

Image Segmenting using SAM2

August 8, 2024 by Mandar Wagh
Image Segmenting using SAM2

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

Getting Started

Prerequisites

Installation

  1. Clone the Repository:
    git clone https://github.com/mandarwagh9/Image-segmenting-using-SAM2.git
    cd Image-segmenting-using-SAM2
  2. Install Dependencies:
    1. Create a virtual environment and activate it:
      python -m venv venv
      source venv/bin/activate  # On Windows use `venv\Scripts\activate`
    2. Install the required packages:
      pip install -r requirements.txt

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

  1. Navigate to the Application
  2. 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.

  3. 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

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.

Built by Mandar Wagh