Upload 3 files
Browse files- cutter.py +98 -0
- requirements (1).txt +4 -0
- utils.py +6 -0
cutter.py
ADDED
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import PIL
|
2 |
+
import numpy as np
|
3 |
+
from PIL import Image, ImageColor, ImageDraw
|
4 |
+
from PIL.Image import Image as PILImage
|
5 |
+
from pymatting.alpha.estimate_alpha_cf import estimate_alpha_cf
|
6 |
+
from pymatting.foreground.estimate_foreground_ml import estimate_foreground_ml
|
7 |
+
from pymatting.util.util import stack_images
|
8 |
+
from rembg.bg import post_process, naive_cutout, apply_background_color
|
9 |
+
from scipy.ndimage import binary_erosion
|
10 |
+
|
11 |
+
|
12 |
+
def alpha_matting_cutout(img: PILImage, trimap: np.ndarray) -> PILImage:
|
13 |
+
if img.mode == "RGBA" or img.mode == "CMYK":
|
14 |
+
img = img.convert("RGB")
|
15 |
+
|
16 |
+
img = np.asarray(img)
|
17 |
+
|
18 |
+
img_normalized = img / 255.0
|
19 |
+
trimap_normalized = trimap / 255.0
|
20 |
+
|
21 |
+
alpha = estimate_alpha_cf(img_normalized, trimap_normalized)
|
22 |
+
foreground = estimate_foreground_ml(img_normalized, alpha)
|
23 |
+
cutout = stack_images(foreground, alpha)
|
24 |
+
|
25 |
+
cutout = np.clip(cutout * 255, 0, 255).astype(np.uint8)
|
26 |
+
return Image.fromarray(cutout)
|
27 |
+
|
28 |
+
|
29 |
+
def generate_trimap(
|
30 |
+
mask: PILImage,
|
31 |
+
foreground_threshold: int,
|
32 |
+
background_threshold: int,
|
33 |
+
erode_structure_size: int,
|
34 |
+
) -> np.ndarray:
|
35 |
+
mask = np.asarray(mask)
|
36 |
+
|
37 |
+
is_foreground = mask > foreground_threshold
|
38 |
+
is_background = mask < background_threshold
|
39 |
+
|
40 |
+
structure = None
|
41 |
+
if erode_structure_size > 0:
|
42 |
+
structure = np.ones(
|
43 |
+
(erode_structure_size, erode_structure_size), dtype=np.uint8
|
44 |
+
)
|
45 |
+
|
46 |
+
is_foreground = binary_erosion(is_foreground, structure=structure)
|
47 |
+
is_background = binary_erosion(is_background, structure=structure, border_value=1)
|
48 |
+
|
49 |
+
trimap = np.full(mask.shape, dtype=np.uint8, fill_value=128)
|
50 |
+
trimap[is_foreground] = 255
|
51 |
+
trimap[is_background] = 0
|
52 |
+
|
53 |
+
return trimap
|
54 |
+
|
55 |
+
|
56 |
+
def get_background_dominant_color(img: PILImage, mask: PILImage) -> tuple:
|
57 |
+
negative_img = img.copy()
|
58 |
+
negative_mask = PIL.ImageOps.invert(mask)
|
59 |
+
negative_img.putalpha(negative_mask)
|
60 |
+
negative_img = negative_img.resize((1, 1))
|
61 |
+
r, g, b, a = negative_img.getpixel((0, 0))
|
62 |
+
return r, g, b, 255
|
63 |
+
|
64 |
+
|
65 |
+
def remove(session, img: PILImage, smoot: bool, matting: tuple, color) -> (PILImage, PILImage):
|
66 |
+
mask = session.predict(img)[0]
|
67 |
+
|
68 |
+
if smoot:
|
69 |
+
mask = PIL.Image.fromarray(post_process(np.array(mask)))
|
70 |
+
|
71 |
+
fg_t, bg_t, erode = matting
|
72 |
+
|
73 |
+
if fg_t > 0 or bg_t > 0 or erode > 0:
|
74 |
+
mask = generate_trimap(mask, *matting)
|
75 |
+
try:
|
76 |
+
cutout = alpha_matting_cutout(img, mask)
|
77 |
+
mask = PIL.Image.fromarray(mask)
|
78 |
+
except ValueError as err:
|
79 |
+
raise err
|
80 |
+
else:
|
81 |
+
cutout = naive_cutout(img, mask)
|
82 |
+
|
83 |
+
if color is True:
|
84 |
+
color = get_background_dominant_color(img, mask)
|
85 |
+
cutout = apply_background_color(cutout, color)
|
86 |
+
elif isinstance(color, str):
|
87 |
+
r, g, b = ImageColor.getcolor(color, "RGB")
|
88 |
+
cutout = apply_background_color(cutout, (r, g, b, 255))
|
89 |
+
|
90 |
+
return cutout, mask
|
91 |
+
|
92 |
+
|
93 |
+
def make_label(text, width=600, height=200, color="black") -> PILImage:
|
94 |
+
image = Image.new("RGB", (width, height), color)
|
95 |
+
draw = ImageDraw.Draw(image)
|
96 |
+
text_width, text_height = draw.textsize(text)
|
97 |
+
draw.text(((width-text_width)/2, height/2), text)
|
98 |
+
return image
|
requirements (1).txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
rembg~=2.0.47
|
2 |
+
pillow~=9.5.0
|
3 |
+
pymatting
|
4 |
+
opencv-python-headless
|
utils.py
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
def keys(dictionary: dict):
|
2 |
+
return [k for k, v in dictionary.items()]
|
3 |
+
|
4 |
+
|
5 |
+
def split_numbers(numbers: str):
|
6 |
+
return [int(i) for i in numbers.split(",")]
|