129 lines
4.3 KiB
Markdown
129 lines
4.3 KiB
Markdown
# 🪄 FOBG — AI Background Remover
|
||
|
||
Remove backgrounds from **images** and **videos** using AI, powered by [rembg](https://github.com/danielzhanika/rembg).
|
||
|
||
## Features
|
||
|
||
- **Image BG Removal** — Upload any image and get back a transparent PNG
|
||
- **Video BG Removal** — Frame-by-frame processing with true alpha channel output
|
||
- **Multiple AI Models** — u2net, u2netp, u2net_human_seg, isnet-general-use, isnet-anime
|
||
- **Gradio UI** — Clean, interactive web interface with fine-tuning controls
|
||
- **CLI Mode** — Script-friendly command-line interface for batch processing
|
||
- **Fine-tune Controls** — Alpha matting, foreground/background thresholds, erosion mask
|
||
|
||
## Installation
|
||
|
||
### Prerequisites
|
||
|
||
- [Python](https://www.python.org/) 3.9 or later
|
||
- [ffmpeg](https://ffmpeg.org/) — required for video processing (Mac: `brew install ffmpeg`; Debian/Ubuntu: `sudo apt install ffmpeg`)
|
||
|
||
### Option 1 — Virtual Environment (Recommended)
|
||
|
||
```bash
|
||
cd /mnt/games/Coding/Python/AI/FOBG
|
||
python -m venv .venv
|
||
source .venv/bin/activate
|
||
pip install -r requirements.txt
|
||
```
|
||
|
||
### Option 2 — Global Install
|
||
|
||
```bash
|
||
pip install -r requirements.txt
|
||
```
|
||
|
||
> **Note:** The first run will automatically download the selected AI model(s) (~40–170 MB depending on the model).
|
||
|
||
## Usage
|
||
|
||
### Web UI (Gradio)
|
||
|
||
```bash
|
||
python main.py
|
||
```
|
||
|
||
Opens at [http://localhost:7860](http://localhost:7860).
|
||
|
||
Additional options:
|
||
|
||
```bash
|
||
python main.py --host 0.0.0.0 --port 7860 # Bind to a specific host/port
|
||
python main.py --share # Create a public share link
|
||
python main.py --no-browser # Do not open browser automatically
|
||
```
|
||
|
||
### CLI Mode
|
||
|
||
```bash
|
||
python main.py --cli input.jpg # Remove image background
|
||
python main.py --cli input.jpg -o result.png # Specify output path
|
||
python main.py --cli video.mp4 --video # Remove background from video
|
||
python main.py --cli video.mp4 --video -o out.mov # .mov output preserves alpha channel
|
||
```
|
||
|
||
CLI options:
|
||
|
||
| Flag | Description |
|
||
|------|-------------|
|
||
| `--model <name>` | AI model to use (default: `u2net`) |
|
||
| `--alpha-matting` | Enable alpha matting for fine edges |
|
||
| `--no-alpha-matting` | Disable alpha matting |
|
||
| `--video` | Treat input as video (frame-by-frame processing) |
|
||
|
||
## Available Models
|
||
|
||
| Model | Description | Speed |
|
||
|-------|-------------|-------|
|
||
| `u2net` | Default, balanced quality/speed | ⚡ Fast |
|
||
| `u2netp` | Lightweight, slightly lower quality | 🚀 Fastest |
|
||
| `u2net_human_seg` | Optimized for human segmentation | ⚡ Fast |
|
||
| `u2net_3b` | Heavy, higher accuracy | 🐢 Slower |
|
||
| `isnet-general-use` | General purpose, high accuracy | 🐢 Slower |
|
||
| `isnet-anime` | Optimized for anime illustrations | ⚡ Fast |
|
||
|
||
## Gradio Controls
|
||
|
||
### Image Tab
|
||
|
||
- **Upload Image** — Accepts PNG, JPEG, or WebP
|
||
- **Model** — Choose the AI model
|
||
- **Alpha Matting** — Fine-tune edges (slower but cleaner results)
|
||
- **Foreground Threshold** — How much of the image is considered foreground (1–255)
|
||
- **Background Threshold** — How much is considered background (1–255)
|
||
- **Erosion Mask Dilation** — Morphological operation to refine the mask (0–32)
|
||
|
||
### Video Tab
|
||
|
||
- **Upload Video** — Accepts MP4, AVI, WebM, and MOV
|
||
- Same model and fine-tuning controls as the image tab
|
||
|
||
## Notes
|
||
|
||
- **Image processing** typically takes 1–5 seconds depending on resolution and model
|
||
- **Video processing** is frame-by-frame and can take minutes for longer clips
|
||
- Image output is saved as **PNG with transparency**
|
||
- Video output uses **.mov with ProRes 4444 alpha** when alpha is desired, or **MP4 with libx264** (black background where alpha is zero)
|
||
- All results are stored in the `output/` directory
|
||
|
||
## Project Structure
|
||
|
||
```
|
||
FOBG/
|
||
├── main.py # Entry point (Gradio + CLI)
|
||
├── requirements.txt # Python dependencies
|
||
├── src/
|
||
│ ├── core/
|
||
│ │ ├── bg_remove.py # Image background removal (rembg)
|
||
│ │ └── video_bg_remove.py# Video background removal (OpenCV + rembg + ffmpeg)
|
||
│ └── ui/
|
||
│ └── app.py # Gradio web interface
|
||
├── output/ # Processed results saved here
|
||
├── tests/ # Unit tests
|
||
└── docs/ # Extended documentation
|
||
```
|
||
|
||
## License
|
||
|
||
MIT License — see [LICENSE](LICENSE) for details.
|