Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,11 +6,31 @@ from fastapi import FastAPI, HTTPException
|
|
6 |
from fastapi.responses import StreamingResponse
|
7 |
import requests
|
8 |
from io import BytesIO
|
9 |
-
from PIL import Image
|
10 |
import rembg
|
|
|
11 |
|
12 |
app = FastAPI()
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
@app.get("/remove-background")
|
15 |
async def remove_background(image_url: str):
|
16 |
try:
|
@@ -21,8 +41,15 @@ async def remove_background(image_url: str):
|
|
21 |
# Abre a imagem usando Pillow
|
22 |
image = Image.open(BytesIO(response.content))
|
23 |
|
|
|
|
|
|
|
|
|
24 |
# Remove o fundo da imagem usando rembg
|
25 |
-
output = rembg.remove(image)
|
|
|
|
|
|
|
26 |
|
27 |
# Converte a imagem de volta para bytes
|
28 |
img_byte_arr = BytesIO()
|
|
|
6 |
from fastapi.responses import StreamingResponse
|
7 |
import requests
|
8 |
from io import BytesIO
|
9 |
+
from PIL import Image, ImageFilter, ImageEnhance
|
10 |
import rembg
|
11 |
+
import onnxruntime as ort
|
12 |
|
13 |
app = FastAPI()
|
14 |
|
15 |
+
# Limita o n煤mero de threads do onnxruntime
|
16 |
+
options = ort.SessionOptions()
|
17 |
+
options.intra_op_num_threads = 2
|
18 |
+
|
19 |
+
def resize_image(image, max_size=1024):
|
20 |
+
width, height = image.size
|
21 |
+
if width > max_size or height > max_size:
|
22 |
+
ratio = min(max_size / width, max_size / height)
|
23 |
+
new_size = (int(width * ratio), int(height * ratio))
|
24 |
+
image = image.resize(new_size, Image.ANTIALIAS)
|
25 |
+
return image
|
26 |
+
|
27 |
+
def adjust_brightness_contrast(image, brightness=1.2, contrast=1.2):
|
28 |
+
enhancer = ImageEnhance.Brightness(image)
|
29 |
+
image = enhancer.enhance(brightness)
|
30 |
+
enhancer = ImageEnhance.Contrast(image)
|
31 |
+
image = enhancer.enhance(contrast)
|
32 |
+
return image
|
33 |
+
|
34 |
@app.get("/remove-background")
|
35 |
async def remove_background(image_url: str):
|
36 |
try:
|
|
|
41 |
# Abre a imagem usando Pillow
|
42 |
image = Image.open(BytesIO(response.content))
|
43 |
|
44 |
+
# Pr茅-processamento: redimensiona e ajusta brilho/contraste
|
45 |
+
image = resize_image(image, max_size=1024)
|
46 |
+
image = adjust_brightness_contrast(image)
|
47 |
+
|
48 |
# Remove o fundo da imagem usando rembg
|
49 |
+
output = rembg.remove(image, model="u2net", session_options=options)
|
50 |
+
|
51 |
+
# P贸s-processamento: suaviza as bordas
|
52 |
+
output = output.filter(ImageFilter.SMOOTH_MORE)
|
53 |
|
54 |
# Converte a imagem de volta para bytes
|
55 |
img_byte_arr = BytesIO()
|