|
import os |
|
os.environ["NUMBA_CACHE_DIR"] = "/tmp/numba_cache" |
|
|
|
from fastapi import FastAPI, HTTPException |
|
from fastapi.responses import StreamingResponse |
|
import requests |
|
from io import BytesIO |
|
from PIL import Image |
|
import rembg |
|
|
|
app = FastAPI() |
|
|
|
@app.get("/remove-background") |
|
async def remove_background(image_url: str): |
|
try: |
|
|
|
response = requests.get(image_url) |
|
response.raise_for_status() |
|
|
|
|
|
image = Image.open(BytesIO(response.content)) |
|
|
|
|
|
output = rembg.remove(image) |
|
|
|
|
|
img_byte_arr = BytesIO() |
|
output.save(img_byte_arr, format='PNG') |
|
img_byte_arr.seek(0) |
|
|
|
|
|
return StreamingResponse(img_byte_arr, media_type="image/png") |
|
|
|
except Exception as e: |
|
raise HTTPException(status_code=400, detail=str(e)) |