|
@@ -18,13 +18,7 @@ def alpha_matting_cutout(
|
|
foreground_threshold: int,
|
|
foreground_threshold: int,
|
|
background_threshold: int,
|
|
background_threshold: int,
|
|
erode_structure_size: int,
|
|
erode_structure_size: int,
|
|
- base_size: int,
|
|
|
|
) -> Image:
|
|
) -> Image:
|
|
- size = img.size
|
|
|
|
-
|
|
|
|
- img.thumbnail((base_size, base_size), Image.LANCZOS)
|
|
|
|
- mask = mask.resize(img.size, Image.LANCZOS)
|
|
|
|
-
|
|
|
|
img = np.asarray(img)
|
|
img = np.asarray(img)
|
|
mask = np.asarray(mask)
|
|
mask = np.asarray(mask)
|
|
|
|
|
|
@@ -60,45 +54,37 @@ def alpha_matting_cutout(
|
|
|
|
|
|
cutout = np.clip(cutout * 255, 0, 255).astype(np.uint8)
|
|
cutout = np.clip(cutout * 255, 0, 255).astype(np.uint8)
|
|
cutout = Image.fromarray(cutout)
|
|
cutout = Image.fromarray(cutout)
|
|
- cutout = cutout.resize(size, Image.LANCZOS)
|
|
|
|
|
|
|
|
return cutout
|
|
return cutout
|
|
|
|
|
|
|
|
|
|
def naive_cutout(img: Image, mask: Image) -> Image:
|
|
def naive_cutout(img: Image, mask: Image) -> Image:
|
|
empty = Image.new("RGBA", (img.size), 0)
|
|
empty = Image.new("RGBA", (img.size), 0)
|
|
- cutout = Image.composite(img, empty, mask.resize(img.size, Image.LANCZOS))
|
|
|
|
|
|
+ cutout = Image.composite(img, empty, mask)
|
|
return cutout
|
|
return cutout
|
|
|
|
|
|
|
|
|
|
-def resize_image(img: Image, width: Optional[int], height: Optional[int]) -> Image:
|
|
|
|
- original_width, original_height = img.size
|
|
|
|
- width = original_width if width is None else width
|
|
|
|
- height = original_height if height is None else height
|
|
|
|
- return img.resize((width, height))
|
|
|
|
-
|
|
|
|
-
|
|
|
|
def remove(
|
|
def remove(
|
|
data: bytes,
|
|
data: bytes,
|
|
alpha_matting: bool = False,
|
|
alpha_matting: bool = False,
|
|
alpha_matting_foreground_threshold: int = 240,
|
|
alpha_matting_foreground_threshold: int = 240,
|
|
alpha_matting_background_threshold: int = 10,
|
|
alpha_matting_background_threshold: int = 10,
|
|
alpha_matting_erode_size: int = 10,
|
|
alpha_matting_erode_size: int = 10,
|
|
- alpha_matting_base_size: int = 1000,
|
|
|
|
session: Optional[ort.InferenceSession] = None,
|
|
session: Optional[ort.InferenceSession] = None,
|
|
- width: Optional[int] = None,
|
|
|
|
- height: Optional[int] = None,
|
|
|
|
|
|
+ only_mask: bool = False,
|
|
) -> bytes:
|
|
) -> bytes:
|
|
img = Image.open(io.BytesIO(data)).convert("RGB")
|
|
img = Image.open(io.BytesIO(data)).convert("RGB")
|
|
- if width is not None or height is not None:
|
|
|
|
- img = resize_image(img, width, height)
|
|
|
|
|
|
|
|
if session is None:
|
|
if session is None:
|
|
session = ort_session("u2net")
|
|
session = ort_session("u2net")
|
|
|
|
|
|
mask = predict(session, np.array(img)).convert("L")
|
|
mask = predict(session, np.array(img)).convert("L")
|
|
|
|
+ mask = mask.resize(img.size, Image.LANCZOS)
|
|
|
|
+
|
|
|
|
+ if only_mask:
|
|
|
|
+ cutout = mask
|
|
|
|
|
|
- if alpha_matting:
|
|
|
|
|
|
+ elif alpha_matting:
|
|
try:
|
|
try:
|
|
cutout = alpha_matting_cutout(
|
|
cutout = alpha_matting_cutout(
|
|
img,
|
|
img,
|
|
@@ -106,7 +92,6 @@ def remove(
|
|
alpha_matting_foreground_threshold,
|
|
alpha_matting_foreground_threshold,
|
|
alpha_matting_background_threshold,
|
|
alpha_matting_background_threshold,
|
|
alpha_matting_erode_size,
|
|
alpha_matting_erode_size,
|
|
- alpha_matting_base_size,
|
|
|
|
)
|
|
)
|
|
except Exception:
|
|
except Exception:
|
|
cutout = naive_cutout(img, mask)
|
|
cutout = naive_cutout(img, mask)
|