|
import spaces |
|
import gradio as gr |
|
from transparent_background import Remover |
|
from PIL import Image |
|
import numpy as np |
|
import tempfile |
|
|
|
|
|
remover = Remover() |
|
|
|
@spaces.GPU |
|
def remove_background(image): |
|
""" |
|
Remove background from an image and return the path to the result |
|
""" |
|
if isinstance(image, Image.Image): |
|
output = remover.process(image) |
|
elif isinstance(image, np.ndarray): |
|
image_pil = Image.fromarray(image) |
|
output = remover.process(image_pil) |
|
else: |
|
raise TypeError("Unsupported image type") |
|
|
|
|
|
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png") |
|
output.save(temp_file, format="PNG") |
|
temp_file.close() |
|
return temp_file.name |
|
|
|
|
|
iface = gr.Interface( |
|
fn=remove_background, |
|
inputs=gr.Image(label="Upload Image"), |
|
outputs=gr.Image(label="Output Image", type="filepath"), |
|
title="AI Background Remover - Automatically Remove Image Backgrounds", |
|
description="Upload an image and our AI-powered background remover will automatically make the background transparent. Perfect for various needs requiring transparent images.", |
|
article=""" |
|
## How to Use |
|
1. Click **Upload Image** or drag and drop an image into the upload area. |
|
2. Click the **Submit** button and wait for a moment. The processed image will appear in the output area on the right. |
|
3. The background of the processed image will be transparent, and the image will be in PNG format for easy use. |
|
### Application Scenarios |
|
- **E-commerce**: Remove backgrounds from product images to create clean product photos. |
|
- **Graphic Design**: Quickly get images with transparent backgrounds for composition and design. |
|
- **Social Media**: Create avatars or icons with transparent backgrounds that blend perfectly on various backgrounds. |
|
Our tool leverages the latest AI technology to efficiently and accurately remove image backgrounds, saving you time and effort. |
|
""", |
|
examples=[["example.jpg"]], |
|
allow_flagging="never", |
|
analytics_enabled=False, |
|
api_name="predict" |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
iface.launch(show_error=True, share=False, server_name="0.0.0.0", server_port=7860) |