Spaces:
Running
on
Zero
Running
on
Zero
tori29umai
commited on
Commit
โข
0aa210d
1
Parent(s):
05d9ddd
Update utils/image_utils.py
Browse files- utils/image_utils.py +22 -10
utils/image_utils.py
CHANGED
@@ -3,16 +3,28 @@ import numpy as np
|
|
3 |
import cv2
|
4 |
from rembg import remove
|
5 |
|
6 |
-
def
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
def resize_image_aspect_ratio(image):
|
18 |
# ๅ
ใฎ็ปๅใตใคใบใๅๅพ
|
|
|
3 |
import cv2
|
4 |
from rembg import remove
|
5 |
|
6 |
+
def background_removal(input_image_path):
|
7 |
+
"""
|
8 |
+
ๆๅฎใใใ็ปๅใใ่ๆฏใ้คๅปใใ็ฝ่ๆฏใซ็ฝฎใๆใใ็ปๅใ่ฟใ้ขๆฐ
|
9 |
+
"""
|
10 |
+
try:
|
11 |
+
input_image = Image.open(input_image_path)
|
12 |
+
except IOError:
|
13 |
+
print(f"Error: Cannot open {input_image_path}")
|
14 |
+
return None
|
15 |
+
|
16 |
+
# ่ๆฏ้คๅปๅฆ็
|
17 |
+
rgba_image = remove(input_image).convert("RGBA")
|
18 |
+
rgba_np = np.array(rgba_image)
|
19 |
+
alpha_channel = rgba_np[:, :, 3]
|
20 |
+
|
21 |
+
# ็ฝ่ๆฏใฎ็ๆใจๅๆ
|
22 |
+
background = np.ones_like(rgba_np[:, :, :3], dtype=np.uint8) * 255
|
23 |
+
foreground = cv2.bitwise_and(rgba_np[:, :, :3], rgba_np[:, :, :3], mask=alpha_channel)
|
24 |
+
background_masked = cv2.bitwise_and(background, background, mask=cv2.bitwise_not(alpha_channel))
|
25 |
+
result = cv2.add(foreground, background_masked)
|
26 |
+
|
27 |
+
return Image.fromarray(result)
|
28 |
|
29 |
def resize_image_aspect_ratio(image):
|
30 |
# ๅ
ใฎ็ปๅใตใคใบใๅๅพ
|