Skip to content

A lightweight web server built with FastAPI that wraps the powerful rembg library, allowing you to remove image backgrounds through simple API calls.

License

Notifications You must be signed in to change notification settings

Yorick-Ryu/rembg-server

Repository files navigation

Rembg Background Removal Server

This project is a lightweight web server built with FastAPI that wraps the powerful rembg library, allowing you to remove image backgrounds through simple API calls.

It has been tested and confirmed to run smoothly on a server with 2 CPU cores and 2GB of RAM.

We have also developed a companion browser extension, Banana Peel, which allows you to right-click on any image on a webpage to remove its background. Feel free to try it out.

中文

Usage Guide

Step 1: Installation and Running

You can install and run this service in one of two ways.

Method 1: Local Installation

  1. Clone this repository to your local machine:
    git clone https://github.com/Yorick-Ryu/rembg-server.git
    cd rembg-server
  2. Install the required dependencies:
    pip install "rembg[cpu,cli]"
  3. Start the server:
    python main.py
    The server will start on port 7001 by default.

Method 2: Deploy with Docker

  1. Pull the pre-built Docker image from ghcr.io:

    docker pull ghcr.io/yorick-ryu/rembg-server:latest
  2. Run the Docker container:

    docker run -d --name rembg-service -p 7001:7001 ghcr.io/yorick-ryu/rembg-server:latest

    If you need to use a custom port (e.g., 8080), you can run it like this:

    docker run -d -p 8080:8080 -e PORT=8080 --name rembg-service ghcr.io/yorick-ryu/rembg-server:latest

Step 2: Configure Models

The server uses a models.json file to manage and configure the available AI models.

  • models: An array of model configurations. Each configuration includes the model's name, desc (description), and enabled status.
  • default_model: This model will be used when an API request does not specify one.

Example models.json:

{
  "models": [
    {
      "name": "u2net",
      "desc": "General-purpose model",
      "enabled": false
    },
    {
      "name": "silueta",
      "desc": "Lightweight general-purpose model",
      "enabled": true
    },
    {
      "name": "isnet-general-use",
      "desc": "General-purpose model",
      "enabled": true
    },
    {
      "name": "isnet-anime",
      "desc": "Model for anime-style images",
      "enabled": true
    }
  ],
  "default_model": "silueta"
}

Note: Only models with enabled set to true will be loaded and accessible via the API.

Step 3: Use with the Browser Extension

For a more convenient experience, you can use our browser extension, Banana Peel.

  1. Visit the Banana Peel project page and follow the instructions to install the extension.
  2. After installation, click the extension icon in your browser's toolbar and enter your server address in the settings (e.g., http://127.0.0.1:7001).

Once configured, you can right-click on any image on any webpage to quickly remove its background.

API Documentation

After starting the server, you can access interactive API documentation, automatically generated by FastAPI, by visiting the /docs or /redoc paths.

GET /

This endpoint is used to check if the server is running correctly. It returns a welcome message.

  • Method: GET
  • URL: /

Example Response

{
  "message": "Welcome to the rembg background removal server."
}

GET /models

Retrieves a list of all currently enabled models.

  • Method: GET
  • URL: /models

Example cURL Request

curl http://your-server-ip:7001/models

Example Response

{
  "models": [
    {
      "name": "silueta",
      "desc": "Lightweight general-purpose"
    },
    {
      "name": "isnet-general-use",
      "desc": "General-purpose"
    },
    {
      "name": "isnet-anime",
      "desc": "Anime"
    }
  ]
}

Available Models

This project supports all models from the rembg library (15+), catering to various needs. For a complete list of models, their descriptions, and performance comparisons, please visit the official rembg documentation:

📋 View All Available Models

Here are some popular models:

  • u2net - A general-purpose background removal model.
  • silueta - A lightweight model for faster processing (only 43MB).
  • isnet-anime - A high-precision model optimized for anime and cartoon-style images.
  • birefnet-portrait - A model designed specifically for portrait segmentation.
  • sam - A powerful, advanced model suitable for various complex scenarios.

POST /remove

This is the core endpoint for background removal.

  • Method: POST
  • URL: /remove
  • Content-Type: multipart/form-data
  • Form Data:
    • file: The image file to be processed (required).
    • model: The name of the model to use (optional, defaults to the one specified in default_model). The model must be enabled in models.json.

Request Parameters

Parameter Type Required Default Description
file File Yes - Image file (e.g., JPG, PNG, WebP)
model String No silueta The name of the model to use

Response

  • Success: Returns the processed image in PNG format.
  • Failure: Returns a JSON object with an error message.

Example cURL Requests

1. Using the default model (silueta):

curl -X POST 
  -F "file=@/path/to/your/image.jpg" 
  http://your-server-ip:7001/remove 
  -o output.png

2. Specifying the isnet-anime model for an anime image:

curl -X POST 
  -F "file=@/path/to/your/anime-image.jpg" 
  -F "model=isnet-anime" 
  http://your-server-ip:7001/remove 
  -o output_anime.png

3. Using the isnet-general-use model for a general image:

curl -X POST 
  -F "file=@/path/to/your/image.jpg" 
  -F "model=isnet-general-use" 
  http://your-server-ip:7001/remove 
  -o output_general.png

Example Error Response

If you request an invalid or disabled model, the server will return an error similar to this:

{
  "detail": "Model 'invalid-model' is not available or not enabled. Available models: ['silueta', 'isnet-general-use', 'isnet-anime']"
}

Acknowledgments

This project is built upon the excellent rembg library created by @danielgatis. We extend our sincere gratitude to the author and all contributors of rembg for making powerful AI image processing technology accessible to everyone.

🙏 Special thanks to:

  • danielgatis/rembg - The core background removal library.
  • The rembg community and all its contributors.
  • The researchers behind all the AI models that support this project (e.g., U²-Net, IS-Net, BiRefNet, SAM).

About

A lightweight web server built with FastAPI that wraps the powerful rembg library, allowing you to remove image backgrounds through simple API calls.

Resources

License

Stars

Watchers

Forks

Packages