|
from fastapi import FastAPI, HTTPException |
|
import httpx |
|
from rembg import remove |
|
from io import BytesIO |
|
from fastapi.responses import StreamingResponse |
|
|
|
app = FastAPI() |
|
|
|
@app.get("/remove-background/") |
|
async def remove_background(image_url: str): |
|
try: |
|
|
|
async with httpx.AsyncClient() as client: |
|
response = await client.get(image_url) |
|
response.raise_for_status() |
|
image_data = response.content |
|
|
|
|
|
output_image = remove(image_data) |
|
|
|
|
|
return StreamingResponse(BytesIO(output_image), media_type="image/png") |
|
|
|
except httpx.RequestError as e: |
|
raise HTTPException(status_code=400, detail="Erro ao baixar a imagem: " + str(e)) |
|
except Exception as e: |
|
raise HTTPException(status_code=500, detail="Erro ao processar a imagem: " + str(e)) |