Team Ai
Apppublic

vishnuselvam/Clipseg-AI

sourceHugging Faceupdated 9mo agoView on Hugging Face
1likes
App README

CLIPSeg AI - Advanced Image Segmentation & Editing

A professional-grade web application for zero-shot image segmentation and editing. Combine the power of CLIPSeg (text understanding) and SAM (Segment Anything Model) for pixel-perfect results.

CLIPSeg AI SAM GPU React FastAPI

๐ŸŒŸ Features

  • โ€”๐ŸŽฏ Zero-Shot Segmentation: Type any object name (e.g., "car", "person", "sky") and get instant segmentation.
  • โ€”๐ŸŽจ Magic Brush Editor: Manually refine masks using a paint/erase tool.
  • โ€”โœจ Studio Mode: Apply professional effects like Portrait Blur, Color Pop (B&W Background), and Neon Glow.
  • โ€”๐Ÿช„ Generative Fill: Replace objects with AI-generated content (SDXL / SD 1.5).
  • โ€”๐Ÿงน Magic Eraser: Remove objects from images with AI-powered inpainting.
  • โ€”๐ŸŽฏ High Precision Mode: Uses SAM (Segment Anything Model) for crisp, industry-standard edges.
  • โ€”โœจ One-Click Background Removal: Instantly download objects with transparent backgrounds.
  • โ€”๐ŸŽš๏ธ Sensitivity Control: Fine-tune segmentation with an adjustable threshold slider.
  • โ€”๐Ÿš€ Hybrid Acceleration: Automatic CUDA (GPU) and CPU optimization for all models.

๐Ÿš€ Quick Start

Prerequisites

  • โ€”Python 3.10+ (3.11 recommended)
  • โ€”Node.js 18+
  • โ€”NVIDIA GPU (Optional, but robust CPU fallback is included)

โšก One-Click Start (Windows)

powershell
cd "Image Segmatation"
.\start_app.ps1

This script will:

  1. 1.Create the backend virtual environment (if missing).
  2. 2.Install all dependencies (backend & frontend).
  3. 3.Start both the Backend and Frontend servers in new windows.

Manual Installation

1. Backend Setup
bash
cd backend

# Create virtual environment
python -m venv venv

# Activate (Windows)
.\venv\Scripts\Activate.ps1

# Install dependencies 
pip install -r requirements.txt

# For NVIDIA GPU (Recommended - Fast):
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu121

# For CPU only:
# pip install torch torchvision --index-url https://download.pytorch.org/whl/cpu
2. Frontend Setup
bash
cd frontend
npm install

Running the Application

Terminal 1 - Backend:

bash
cd backend
.\venv\Scripts\Activate.ps1
python -m uvicorn main:app --reload

Terminal 2 - Frontend:

bash
cd frontend
npm run dev

Open Browser: http://localhost:5173

๐Ÿ“– Usage Guide

1. Basic Segmentation

  • โ€”Upload an image
  • โ€”Type a prompt (e.g., "car", "person", "dog")
  • โ€”Click Generate Segmentation
  • โ€”Use the slider to compare results

2. Generative Fill (New! โœจ)

  • โ€”Go to the Studio โœจ tab after segmenting an object.
  • โ€”Select Style: Choose from Realism, Cinematic, Cyberpunk, Anime, or Art.
  • โ€”Describe: Type what you want to see (e.g., "red bus").
  • โ€”Creativity: Adjust strength (Higher = less like original shape).
  • โ€”Generate: The system automatically enhances your prompt and uses Negative Prompts for high quality.
  • โ€”GPU Users: Uses SDXL Inpainting (1024x1024, High Quality).
  • โ€”CPU Users: Automatically falls back to SD 1.5 Inpainting (512x512, Faster).

3. High Precision Mode (SAM)

  • โ€”Toggle "High Precision Mode" ON
  • โ€”Generates pixel-perfect masks using the Segment Anything Model
  • โ€”Note: First run downloads the SAM model (~40MB).

4. Magic Brush Editing

  • โ€”Click "Editor" tab in the results area
  • โ€”Use the + Brush to add areas to the mask
  • โ€”Use the - Brush to erase areas
  • โ€”Click Save (โœ“) to apply changes

5. Studio Mode Effects

  • โ€”Click "Studio โœจ" tab for creative effects:
  • โ€”Background Blur: Blur the background for portrait effect
  • โ€”B&W Background: Keep subject in color, make background grayscale
  • โ€”Neon Glow: Add a glowing effect around the subject
  • โ€”Sticker Border: Add a white border for sticker effect
  • โ€”Custom Background: Upload your own background image

6. Background Removal

  • โ€”Click "Remove BG" to download the object as a transparent PNG
  • โ€”Perfect for creating assets for design work

๐Ÿ—๏ธ Architecture

Hybrid AI Pipeline

  1. 1.CLIPSeg: Understands what to segment based on text (e.g., "red car")
  2. 2.Bounding Box Extraction: Finds the region of the object
  3. 3.SAM (Segment Anything): Takes the bbox and generates a high-quality mask
  4. 4.Generative Pipeline:
  5. 5.SDXL Inpainting (GPU): For high-fidelity object replacement.
  6. 6.SD 1.5 Inpainting (CPU): Optimized fallback for non-GPU systems.
  7. 7.Post-Processing: Morphological operations for clean edges

Tech Stack

  • โ€”Frontend: React 19, Tailwind CSS, HTML5 Canvas
  • โ€”Backend: FastAPI, PyTorch, Ultralytics (SAM), Transformers (CLIPSeg), Diffusers (SDXL/SD1.5)
  • โ€”Processing: OpenCV, NumPy, PIL

๐Ÿ”ง Configuration

GPU & CPU Support

The application includes a smart hardware detection system:

  • โ€”GPU (CUDA):
  • โ€”CLIPSeg (Fast)
  • โ€”SAM (Fast)
  • โ€”SDXL Inpainting (High Quality, fp16 mode)
  • โ€”CPU:
  • โ€”CLIPSeg (Standard)
  • โ€”SAM (Standard)
  • โ€”SD 1.5 Inpainting (Optimized, fp32 mode, ~1-3 min generation)

Model Files

Models are downloaded automatically:

  • โ€”CLIPSeg: ~/.cache/huggingface/
  • โ€”SAM: backend/sam2.1_s.pt (~40MB)

๐Ÿ“ Project Structure

Image Segmatation/
โ”œโ”€โ”€ backend/
โ”‚   โ”œโ”€โ”€ main.py           # FastAPI server
โ”‚   โ”œโ”€โ”€ requirements.txt  # Python dependencies
โ”‚   โ””โ”€โ”€ venv/             # Virtual environment
โ”œโ”€โ”€ frontend/
โ”‚   โ”œโ”€โ”€ src/
โ”‚   โ”‚   โ”œโ”€โ”€ App.jsx       # Main React component
โ”‚   โ”‚   โ”œโ”€โ”€ index.css     # Tailwind styles
โ”‚   โ”‚   โ””โ”€โ”€ main.jsx      # React entry point
โ”‚   โ”œโ”€โ”€ package.json      # Node dependencies
โ”‚   โ””โ”€โ”€ vite.config.js    # Vite configuration
โ”œโ”€โ”€ start_app.ps1         # One-click start script
โ”œโ”€โ”€ README.md             # This file
โ””โ”€โ”€ .gitignore

๐Ÿ› Troubleshooting

"Module not found: ultralytics"

Run the backend with virtual environment activated:

bash
.\venv\Scripts\Activate.ps1
python -m uvicorn main:app --reload

Slow performance

  • โ€”Enable GPU: Install CUDA version of PyTorch
  • โ€”Disable SAM: Uncheck "High Precision Mode" for faster (but less accurate) results

Magic Eraser not working

  • โ€”Make sure you've generated a segmentation first
  • โ€”Check browser console for error messages

๐Ÿค Contributing

Contributions are welcome! See UPGRADE_GUIDE.md for the roadmap.

๐Ÿ“„ License

MIT License