Skip to content

About

Remove humans from room images and reconstruct the background using FastAPI, YOLOv11, SAM 2, and LaMa inpainting with IOPaint.

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Repository files navigation

CleanSlate AI

A production-grade FastAPI application that intelligently removes humans from room images and seamlessly reconstructs the background using state-of-the-art computer vision models.

Python FastAPI PyTorch License


Overview

CleanSlate AI uses a three-stage AI pipeline to detect, segment, and remove humans from images:

Stage Model Purpose
1. Detection YOLOv11 Nano Locates humans in the image with bounding boxes
2. Segmentation SAM 2 (Segment Anything Model 2) Creates pixel-perfect masks of detected humans
3. Inpainting IOPaint (LaMa) Reconstructs the background seamlessly

Features

  • Human Detection - Fine-tuned YOLOv11 model for accurate person detection
  • Precise Segmentation - SAM 2 generates pixel-level masks with smooth edges
  • Background Reconstruction - LaMa-based inpainting fills removed areas naturally
  • RESTful API - Clean FastAPI endpoints with automatic OpenAPI documentation
  • Session Management - Isolated temporary directories per request with auto-cleanup
  • CORS Enabled - Ready for cross-origin frontend integration

Project Structure

CleanSlate-AI/
├── app/
│   ├── api/
│   │   └── routes.py           # API endpoint definitions
│   ├── core/
│   │   ├── config.py           # Configuration settings
│   │   └── models.py           # Model loaders (YOLO + SAM)
│   ├── services/
│   │   ├── processing.py       # Core image processing pipeline
│   │   ├── response.py         # Response formatting utilities
│   │   └── utils.py            # Helper functions
│   └── main.py                 # FastAPI application entry
├── runs/
│   └── detect/
│       └── train/
│           ├── weights/
│           │   ├── best.pt     # Best model checkpoint
│           │   └── last.pt     # Latest model checkpoint
│           ├── results.csv     # Training metrics
│           └── args.yaml       # Training configuration
├── sam2_b.pt                   # SAM 2 Base model weights
├── requirements.txt            # Python dependencies
├── run.py                      # Server startup script
└── readme.md

Installation

Prerequisites

  • Python 3.10 or higher
  • pip package manager
  • Git

Step 1: Clone the Repository

git clone https://github.com/stellainternational/SIMPLIFIED_AI_PEOPLE.git
cd SIMPLIFIED_AI_PEOPLE

Step 2: Create Virtual Environment

Windows:

python -m venv removal-env
.\removal-env\Scripts\activate

Linux/macOS:

python -m venv removal-env
source removal-env/bin/activate

Step 3: Install Dependencies

pip install -r requirements.txt

Step 4: Run the Server

python run.py

Or using uvicorn directly:

uvicorn app.main:app --reload --host 0.0.0.0 --port 8000

The API will be available at http://localhost:8000


API Reference

Interactive Documentation

Once the server is running, access the interactive API docs:

Endpoints

POST /remove-person/

Removes humans from an uploaded image and returns the processed result.

Request:

  • Content-Type: multipart/form-data
  • Body: file - Image file (JPEG or PNG)

Response:

{
  "status": "success",
  "status_code": 200,
  "response": {
    "message": "Person Removed from the image",
    "mask_image_base64": "<base64-encoded-mask>",
    "output_image_base64": "<base64-encoded-result>"
  }
}

Example using cURL:

curl -X POST "http://localhost:8000/remove-person/" \
  -H "accept: application/json" \
  -H "Content-Type: multipart/form-data" \
  -F "file=@room_with_person.jpg"

Example using Python:

import requests
import base64

url = "http://localhost:8000/remove-person/"
files = {"file": open("room_with_person.jpg", "rb")}

response = requests.post(url, files=files)
result = response.json()

# Decode and save the output image
output_data = base64.b64decode(result["response"]["output_image_base64"])
with open("cleaned_room.jpg", "wb") as f:
    f.write(output_data)

GET /clear-session-cache/

Clears temporary session data from the server.

Response:

{
  "message": "All folders inside 'data' directory have been cleaned up."
}

Processing Pipeline

┌─────────────┐     ┌─────────────┐     ┌─────────────┐     ┌─────────────┐
│   Upload    │───▶│    YOLO     │────▶│    SAM 2    │────▶│   IOPaint   │
│   Image     │     │  Detection  │     │ Segmentation│     │  Inpainting │
└─────────────┘     └─────────────┘     └─────────────┘     └─────────────┘
                           │                   │                   │
                           ▼                   ▼                   ▼
                      Bounding           Pixel-level          Reconstructed
                        Boxes              Masks               Background

Post-Processing Steps:

  1. Combine multiple masks into a single binary mask
  2. Apply morphological closing to fill gaps
  3. Dilate mask to ensure complete coverage
  4. Gaussian blur for smooth edges
  5. Threshold to create final binary mask

Tech Stack

Category Technology
Web Framework FastAPI 0.108.0
Deep Learning PyTorch 2.6.0, Torchvision 0.21.0
Object Detection Ultralytics YOLOv11
Segmentation SAM 2 (Segment Anything Model 2)
Inpainting IOPaint 1.6.0 (LaMa)
Image Processing OpenCV 4.11.0, Pillow 9.5.0
Async I/O aiofiles

Configuration

The application uses the following model paths (configurable in app/core/models.py):

yolo_model = YOLO("runs/detect/train/weights/best.pt")
sam_model = SAM("sam2_b.pt")

Troubleshooting

Common Issues

1. CUDA/GPU not detected

  • The application defaults to CPU. For GPU support, ensure CUDA-compatible PyTorch is installed.

2. Out of memory errors

  • Try processing smaller images (resize before upload)
  • Ensure sufficient RAM (8GB+ recommended)

3. IOPaint command fails

  • Verify IOPaint is installed: pip install iopaint
  • Check that the LaMa model downloads successfully on first run

4. Model files not found

  • Ensure runs/detect/train/weights/best.pt exists
  • Ensure sam2_b.pt is in the project root

License

This project is licensed under the MIT License.


Acknowledgments

About

Remove humans from room images and reconstruct the background using FastAPI, YOLOv11, SAM 2, and LaMa inpainting with IOPaint.

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages