Spaces:
Sleeping
Sleeping
Upload 3 files
Browse filesbackground-removal
- README.md +100 -7
- app.py +96 -0
- requirements.txt +4 -0
README.md
CHANGED
|
@@ -1,13 +1,106 @@
|
|
| 1 |
---
|
| 2 |
-
title: Background Removal
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
-
|
| 11 |
---
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: ClosetConnect Background Removal API
|
| 3 |
+
emoji: π¨
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: purple
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 4.0.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
+
license: mit
|
| 11 |
---
|
| 12 |
|
| 13 |
+
# π¨ Background Removal API
|
| 14 |
+
|
| 15 |
+
Fast background removal service using **rembg** (u2net model) optimized for Hugging Face Spaces with GPU support.
|
| 16 |
+
|
| 17 |
+
## Features
|
| 18 |
+
|
| 19 |
+
- β‘ **GPU-accelerated** processing (1-3 seconds per image)
|
| 20 |
+
- π **Model session reuse** for optimal performance
|
| 21 |
+
- π **REST API** endpoint for external services
|
| 22 |
+
- π― **Optimized for clothing images** (used in ClosetConnect project)
|
| 23 |
+
|
| 24 |
+
## Usage
|
| 25 |
+
|
| 26 |
+
### Web Interface
|
| 27 |
+
|
| 28 |
+
Simply upload an image and get the background removed instantly.
|
| 29 |
+
|
| 30 |
+
### API Endpoint
|
| 31 |
+
|
| 32 |
+
You can call this service from your applications:
|
| 33 |
+
|
| 34 |
+
```python
|
| 35 |
+
import requests
|
| 36 |
+
import base64
|
| 37 |
+
from PIL import Image
|
| 38 |
+
import io
|
| 39 |
+
|
| 40 |
+
# Your Hugging Face Space URL
|
| 41 |
+
API_URL = "https://YOUR-USERNAME-background-removal.hf.space/api/predict"
|
| 42 |
+
|
| 43 |
+
# Read image
|
| 44 |
+
with open("your_image.png", "rb") as f:
|
| 45 |
+
image_bytes = f.read()
|
| 46 |
+
|
| 47 |
+
# Call API
|
| 48 |
+
response = requests.post(
|
| 49 |
+
API_URL,
|
| 50 |
+
files={"data": ("image.png", image_bytes, "image/png")},
|
| 51 |
+
timeout=60
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
# Parse response
|
| 55 |
+
result = response.json()
|
| 56 |
+
if "data" in result and len(result["data"]) > 0:
|
| 57 |
+
# Extract base64 image data
|
| 58 |
+
image_data_url = result["data"][0]
|
| 59 |
+
base64_data = image_data_url.split(",")[1]
|
| 60 |
+
image_data = base64.b64decode(base64_data)
|
| 61 |
+
|
| 62 |
+
# Save result
|
| 63 |
+
output_image = Image.open(io.BytesIO(image_data))
|
| 64 |
+
output_image.save("output.png")
|
| 65 |
+
```
|
| 66 |
+
|
| 67 |
+
### Environment Variables for CloudRun Worker
|
| 68 |
+
|
| 69 |
+
Set this in your CloudRun Worker environment:
|
| 70 |
+
|
| 71 |
+
```bash
|
| 72 |
+
REMBG_API_URL=https://YOUR-USERNAME-background-removal.hf.space
|
| 73 |
+
```
|
| 74 |
+
|
| 75 |
+
## Performance
|
| 76 |
+
|
| 77 |
+
- **First request**: 5-10s (model loading)
|
| 78 |
+
- **Subsequent requests**: 1-3s (GPU)
|
| 79 |
+
- **Memory usage**: ~500MB (model + runtime)
|
| 80 |
+
|
| 81 |
+
## Model
|
| 82 |
+
|
| 83 |
+
- **Model**: u2net (176MB)
|
| 84 |
+
- **Framework**: rembg 2.0.55+
|
| 85 |
+
- **Inference**: ONNX Runtime with GPU support
|
| 86 |
+
|
| 87 |
+
## Deployment to Hugging Face Spaces
|
| 88 |
+
|
| 89 |
+
1. Create a new Space on [Hugging Face](https://huggingface.co/spaces)
|
| 90 |
+
2. Choose **Gradio** SDK
|
| 91 |
+
3. Enable **GPU** (Settings β Hardware β GPU)
|
| 92 |
+
4. Upload these files:
|
| 93 |
+
- `app.py`
|
| 94 |
+
- `requirements.txt`
|
| 95 |
+
- `README.md`
|
| 96 |
+
5. The Space will automatically build and deploy
|
| 97 |
+
|
| 98 |
+
## License
|
| 99 |
+
|
| 100 |
+
MIT License - Free to use for personal and commercial projects
|
| 101 |
+
|
| 102 |
+
## Related
|
| 103 |
+
|
| 104 |
+
- Part of the **ClosetConnect** project
|
| 105 |
+
- Used for clothing image preprocessing
|
| 106 |
+
- Integrated with CloudRun Worker pipeline
|
app.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Background Removal API using rembg
|
| 3 |
+
Optimized for Hugging Face Spaces with GPU support
|
| 4 |
+
|
| 5 |
+
This Gradio app provides a simple API endpoint for background removal
|
| 6 |
+
that can be called from CloudRun Worker or other services.
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
import gradio as gr
|
| 10 |
+
from rembg import remove, new_session
|
| 11 |
+
from PIL import Image
|
| 12 |
+
import io
|
| 13 |
+
import time
|
| 14 |
+
|
| 15 |
+
# Global session for model reuse (GPU optimized)
|
| 16 |
+
print("π₯ Loading rembg model...")
|
| 17 |
+
start_time = time.time()
|
| 18 |
+
rembg_session = new_session(model_name='u2net')
|
| 19 |
+
load_time = time.time() - start_time
|
| 20 |
+
print(f"β
rembg model loaded in {load_time:.2f}s")
|
| 21 |
+
|
| 22 |
+
def remove_background(image):
|
| 23 |
+
"""
|
| 24 |
+
Remove background from image using rembg
|
| 25 |
+
|
| 26 |
+
Args:
|
| 27 |
+
image: PIL Image
|
| 28 |
+
|
| 29 |
+
Returns:
|
| 30 |
+
PIL Image with transparent background
|
| 31 |
+
"""
|
| 32 |
+
print(f"π₯ Received image: {image.size}, mode: {image.mode}")
|
| 33 |
+
|
| 34 |
+
try:
|
| 35 |
+
# Convert PIL Image to bytes
|
| 36 |
+
img_byte_arr = io.BytesIO()
|
| 37 |
+
image.save(img_byte_arr, format='PNG')
|
| 38 |
+
img_byte_arr.seek(0)
|
| 39 |
+
image_bytes = img_byte_arr.getvalue()
|
| 40 |
+
|
| 41 |
+
# Remove background using rembg with session reuse
|
| 42 |
+
print("π Processing with rembg...")
|
| 43 |
+
start_time = time.time()
|
| 44 |
+
output_bytes = remove(image_bytes, session=rembg_session)
|
| 45 |
+
process_time = time.time() - start_time
|
| 46 |
+
print(f"β
Background removed in {process_time:.2f}s")
|
| 47 |
+
|
| 48 |
+
# Convert back to PIL Image
|
| 49 |
+
output_image = Image.open(io.BytesIO(output_bytes)).convert("RGBA")
|
| 50 |
+
print(f"π€ Output image: {output_image.size}, mode: {output_image.mode}")
|
| 51 |
+
|
| 52 |
+
return output_image
|
| 53 |
+
|
| 54 |
+
except Exception as e:
|
| 55 |
+
print(f"β Error: {e}")
|
| 56 |
+
raise e
|
| 57 |
+
|
| 58 |
+
# Create Gradio Interface
|
| 59 |
+
demo = gr.Interface(
|
| 60 |
+
fn=remove_background,
|
| 61 |
+
inputs=gr.Image(type="pil", label="Upload Image"),
|
| 62 |
+
outputs=gr.Image(type="pil", label="Background Removed"),
|
| 63 |
+
title="π¨ Background Removal API",
|
| 64 |
+
description="""
|
| 65 |
+
Fast background removal using rembg (u2net model).
|
| 66 |
+
|
| 67 |
+
**Features:**
|
| 68 |
+
- GPU-accelerated processing (on HF Spaces)
|
| 69 |
+
- Session reuse for faster processing
|
| 70 |
+
- API endpoint available at `/api/predict`
|
| 71 |
+
|
| 72 |
+
**Usage:**
|
| 73 |
+
```python
|
| 74 |
+
import requests
|
| 75 |
+
|
| 76 |
+
response = requests.post(
|
| 77 |
+
"YOUR_SPACE_URL/api/predict",
|
| 78 |
+
files={"data": open("image.png", "rb")}
|
| 79 |
+
)
|
| 80 |
+
result = response.json()
|
| 81 |
+
```
|
| 82 |
+
""",
|
| 83 |
+
examples=[
|
| 84 |
+
# You can add example images here if you want
|
| 85 |
+
],
|
| 86 |
+
api_name="predict", # API endpoint name
|
| 87 |
+
allow_flagging="never"
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
if __name__ == "__main__":
|
| 91 |
+
# Launch with API mode enabled
|
| 92 |
+
demo.launch(
|
| 93 |
+
server_name="0.0.0.0",
|
| 94 |
+
server_port=7860,
|
| 95 |
+
show_api=True
|
| 96 |
+
)
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.0.0
|
| 2 |
+
rembg>=2.0.55
|
| 3 |
+
pillow>=10.1.0
|
| 4 |
+
onnxruntime-gpu>=1.16.0
|