Update app.py
Browse files
app.py
CHANGED
|
@@ -9,7 +9,6 @@ from io import BytesIO
|
|
| 9 |
from PIL import Image, ImageFilter, ImageEnhance
|
| 10 |
import rembg
|
| 11 |
import onnxruntime as ort
|
| 12 |
-
from functools import lru_cache
|
| 13 |
|
| 14 |
app = FastAPI()
|
| 15 |
|
|
@@ -18,11 +17,6 @@ options = ort.SessionOptions()
|
|
| 18 |
options.intra_op_num_threads = 2 # Limita o número de threads para evitar sobrecarga
|
| 19 |
options.execution_mode = ort.ExecutionMode.ORT_SEQUENTIAL # Execução sequencial para melhor desempenho em CPU
|
| 20 |
|
| 21 |
-
# Cache do modelo u2net (carrega apenas uma vez)
|
| 22 |
-
@lru_cache(maxsize=1)
|
| 23 |
-
def load_u2net_model():
|
| 24 |
-
return rembg.get_model("u2net")
|
| 25 |
-
|
| 26 |
def resize_image(image, max_size=512):
|
| 27 |
"""Redimensiona a imagem para uma largura máxima de 512px, mantendo a proporção."""
|
| 28 |
width, height = image.size
|
|
@@ -55,8 +49,7 @@ async def remove_background(image_url: str):
|
|
| 55 |
image = adjust_brightness_contrast(image)
|
| 56 |
|
| 57 |
# Remove o fundo da imagem usando rembg
|
| 58 |
-
|
| 59 |
-
output = rembg.remove(image, model=model, session_options=options)
|
| 60 |
|
| 61 |
# Pós-processamento: suaviza as bordas
|
| 62 |
output = output.filter(ImageFilter.SMOOTH_MORE)
|
|
|
|
| 9 |
from PIL import Image, ImageFilter, ImageEnhance
|
| 10 |
import rembg
|
| 11 |
import onnxruntime as ort
|
|
|
|
| 12 |
|
| 13 |
app = FastAPI()
|
| 14 |
|
|
|
|
| 17 |
options.intra_op_num_threads = 2 # Limita o número de threads para evitar sobrecarga
|
| 18 |
options.execution_mode = ort.ExecutionMode.ORT_SEQUENTIAL # Execução sequencial para melhor desempenho em CPU
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
def resize_image(image, max_size=512):
|
| 21 |
"""Redimensiona a imagem para uma largura máxima de 512px, mantendo a proporção."""
|
| 22 |
width, height = image.size
|
|
|
|
| 49 |
image = adjust_brightness_contrast(image)
|
| 50 |
|
| 51 |
# Remove o fundo da imagem usando rembg
|
| 52 |
+
output = rembg.remove(image, session_options=options)
|
|
|
|
| 53 |
|
| 54 |
# Pós-processamento: suaviza as bordas
|
| 55 |
output = output.filter(ImageFilter.SMOOTH_MORE)
|