Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,8 +3,7 @@ os.environ["NUMBA_CACHE_DIR"] = "/tmp/numba_cache"
|
|
3 |
os.environ["U2NET_HOME"] = "/tmp/u2net"
|
4 |
|
5 |
from fastapi import FastAPI, HTTPException
|
6 |
-
from
|
7 |
-
import requests
|
8 |
from io import BytesIO
|
9 |
from PIL import Image, ImageFilter
|
10 |
import rembg
|
@@ -12,6 +11,7 @@ import onnxruntime as ort
|
|
12 |
from concurrent.futures import ThreadPoolExecutor
|
13 |
import asyncio
|
14 |
import gc
|
|
|
15 |
|
16 |
app = FastAPI()
|
17 |
|
@@ -32,13 +32,9 @@ def resize_image(image, max_size=512):
|
|
32 |
image = image.resize(new_size, Image.Resampling.LANCZOS)
|
33 |
return image
|
34 |
|
35 |
-
def process_image(
|
36 |
-
#
|
37 |
-
|
38 |
-
response.raise_for_status()
|
39 |
-
|
40 |
-
# Abre a imagem usando Pillow
|
41 |
-
image = Image.open(BytesIO(response.content))
|
42 |
|
43 |
# Pré-processamento: apenas redimensiona para 512px
|
44 |
image = resize_image(image, max_size=512)
|
@@ -54,17 +50,21 @@ def process_image(image_url):
|
|
54 |
output.save(img_byte_arr, format='PNG')
|
55 |
img_byte_arr.seek(0)
|
56 |
|
57 |
-
|
|
|
|
|
|
|
|
|
58 |
|
59 |
-
@app.
|
60 |
-
async def remove_background(
|
61 |
try:
|
62 |
# Executa o processamento da imagem em um thread separado
|
63 |
loop = asyncio.get_event_loop()
|
64 |
-
|
65 |
|
66 |
-
# Retorna a imagem processada
|
67 |
-
return
|
68 |
|
69 |
except Exception as e:
|
70 |
raise HTTPException(status_code=400, detail=str(e))
|
|
|
3 |
os.environ["U2NET_HOME"] = "/tmp/u2net"
|
4 |
|
5 |
from fastapi import FastAPI, HTTPException
|
6 |
+
from pydantic import BaseModel
|
|
|
7 |
from io import BytesIO
|
8 |
from PIL import Image, ImageFilter
|
9 |
import rembg
|
|
|
11 |
from concurrent.futures import ThreadPoolExecutor
|
12 |
import asyncio
|
13 |
import gc
|
14 |
+
import base64
|
15 |
|
16 |
app = FastAPI()
|
17 |
|
|
|
32 |
image = image.resize(new_size, Image.Resampling.LANCZOS)
|
33 |
return image
|
34 |
|
35 |
+
def process_image(image_data):
|
36 |
+
# Decodifica a imagem base64
|
37 |
+
image = Image.open(BytesIO(base64.b64decode(image_data)))
|
|
|
|
|
|
|
|
|
38 |
|
39 |
# Pré-processamento: apenas redimensiona para 512px
|
40 |
image = resize_image(image, max_size=512)
|
|
|
50 |
output.save(img_byte_arr, format='PNG')
|
51 |
img_byte_arr.seek(0)
|
52 |
|
53 |
+
# Codifica a imagem processada em base64
|
54 |
+
return base64.b64encode(img_byte_arr.getvalue()).decode('utf-8')
|
55 |
+
|
56 |
+
class ImageBase64(BaseModel):
|
57 |
+
image: str # A imagem em base64
|
58 |
|
59 |
+
@app.post("/remove-background")
|
60 |
+
async def remove_background(image_data: ImageBase64):
|
61 |
try:
|
62 |
# Executa o processamento da imagem em um thread separado
|
63 |
loop = asyncio.get_event_loop()
|
64 |
+
processed_image_base64 = await loop.run_in_executor(executor, process_image, image_data.image)
|
65 |
|
66 |
+
# Retorna a imagem processada em base64
|
67 |
+
return {"processed_image": processed_image_base64}
|
68 |
|
69 |
except Exception as e:
|
70 |
raise HTTPException(status_code=400, detail=str(e))
|