Spaces:
Running
on
Zero
Running
on
Zero
tori29umai
commited on
Commit
โข
2dd8a35
1
Parent(s):
16a8ae4
Update utils/image_utils.py
Browse files- utils/image_utils.py +17 -5
utils/image_utils.py
CHANGED
@@ -6,21 +6,33 @@ from rembg import remove
|
|
6 |
def background_removal(input_image_path):
|
7 |
"""
|
8 |
ๆๅฎใใใ็ปๅใใ่ๆฏใ้คๅปใใ้ๆ้จๅใ็ฝ่ๆฏใซใใฌใณใใใฆ่ฟใ้ขๆฐใ
|
9 |
-
|
|
|
|
|
|
|
10 |
"""
|
11 |
try:
|
12 |
input_image = Image.open(input_image_path).convert("RGBA")
|
13 |
except IOError:
|
14 |
print(f"Error: Cannot open {input_image_path}")
|
15 |
return None
|
16 |
-
|
|
|
|
|
17 |
# ่ๆฏ้คๅปๅฆ็
|
18 |
result = remove(input_image)
|
19 |
|
20 |
-
#
|
21 |
-
|
|
|
22 |
if bbox:
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
# ็ตๆใไธๆใใกใคใซใซไฟๅญ
|
26 |
result_path = "tmp.png"
|
|
|
6 |
def background_removal(input_image_path):
|
7 |
"""
|
8 |
ๆๅฎใใใ็ปๅใใ่ๆฏใ้คๅปใใ้ๆ้จๅใ็ฝ่ๆฏใซใใฌใณใใใฆ่ฟใ้ขๆฐใ
|
9 |
+
ๅฐใใชใใใใ็ก่ฆใใฆใใชใใณใฐใ่กใใพใใ
|
10 |
+
|
11 |
+
Parameters:
|
12 |
+
- input_image_path: ่ๆฏ้คๅปใใ็ปๅใฎใใน
|
13 |
"""
|
14 |
try:
|
15 |
input_image = Image.open(input_image_path).convert("RGBA")
|
16 |
except IOError:
|
17 |
print(f"Error: Cannot open {input_image_path}")
|
18 |
return None
|
19 |
+
|
20 |
+
area_threshold=100 # ็ก่ฆใใๅฐใใ้ ๅใฎใใฏใปใซๆฐใฎ้พๅค
|
21 |
+
|
22 |
# ่ๆฏ้คๅปๅฆ็
|
23 |
result = remove(input_image)
|
24 |
|
25 |
+
# ้้้จๅใฎใในใฏใไฝๆใใๅฐใใช้ ๅใ็ก่ฆ
|
26 |
+
alpha = result.split()[-1] # ใขใซใใกใใฃใณใใซใๅๅพ
|
27 |
+
bbox = alpha.getbbox()
|
28 |
if bbox:
|
29 |
+
# bbox ๅ
ใฎ้้ใใใฆใใชใใใฏใปใซใๆฐใใ
|
30 |
+
cropped_alpha = alpha.crop(bbox)
|
31 |
+
non_transparent_pixels = sum(cropped_alpha.getdata()) // 255
|
32 |
+
|
33 |
+
# ๆๅฎใใ้พๅคไปฅไธใฎ้ ๅใใใๅ ดๅใฎใฟใใชใใณใฐ
|
34 |
+
if non_transparent_pixels >= area_threshold:
|
35 |
+
result = result.crop(bbox)
|
36 |
|
37 |
# ็ตๆใไธๆใใกใคใซใซไฟๅญ
|
38 |
result_path = "tmp.png"
|