Spaces:
Running
on
Zero
Running
on
Zero
tori29umai
commited on
Commit
โข
f77dc2b
1
Parent(s):
2dd8a35
Update utils/image_utils.py
Browse files- utils/image_utils.py +8 -7
utils/image_utils.py
CHANGED
@@ -6,8 +6,8 @@ from rembg import remove
|
|
6 |
def background_removal(input_image_path):
|
7 |
"""
|
8 |
ๆๅฎใใใ็ปๅใใ่ๆฏใ้คๅปใใ้ๆ้จๅใ็ฝ่ๆฏใซใใฌใณใใใฆ่ฟใ้ขๆฐใ
|
9 |
-
|
10 |
-
|
11 |
Parameters:
|
12 |
- input_image_path: ่ๆฏ้คๅปใใ็ปๅใฎใใน
|
13 |
"""
|
@@ -18,21 +18,22 @@ def background_removal(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 |
-
|
32 |
|
33 |
# ๆๅฎใใ้พๅคไปฅไธใฎ้ ๅใใใๅ ดๅใฎใฟใใชใใณใฐ
|
34 |
-
if
|
35 |
result = result.crop(bbox)
|
|
|
|
|
36 |
|
37 |
# ็ตๆใไธๆใใกใคใซใซไฟๅญ
|
38 |
result_path = "tmp.png"
|
|
|
6 |
def background_removal(input_image_path):
|
7 |
"""
|
8 |
ๆๅฎใใใ็ปๅใใ่ๆฏใ้คๅปใใ้ๆ้จๅใ็ฝ่ๆฏใซใใฌใณใใใฆ่ฟใ้ขๆฐใ
|
9 |
+
ๅฐใใชใใใใๅฐใใๅ้ๆ้ ๅใ็ก่ฆใใฆใใชใใณใฐใ่กใใพใใ
|
10 |
+
|
11 |
Parameters:
|
12 |
- input_image_path: ่ๆฏ้คๅปใใ็ปๅใฎใใน
|
13 |
"""
|
|
|
18 |
return None
|
19 |
|
20 |
area_threshold=100 # ็ก่ฆใใๅฐใใ้ ๅใฎใใฏใปใซๆฐใฎ้พๅค
|
|
|
21 |
# ่ๆฏ้คๅปๅฆ็
|
22 |
result = remove(input_image)
|
23 |
|
24 |
+
# ใขใซใใกใใฃใณใใซๅๅพ
|
25 |
alpha = result.split()[-1] # ใขใซใใกใใฃใณใใซใๅๅพ
|
26 |
bbox = alpha.getbbox()
|
27 |
if bbox:
|
28 |
+
# bbox ๅ
ใฎๅฎๅ
จ้้ใงใชใใใฏใปใซใใซใฆใณใ
|
29 |
cropped_alpha = alpha.crop(bbox)
|
30 |
+
non_transparent_pixel_count = sum(1 for pixel in cropped_alpha.getdata() if pixel > 0)
|
31 |
|
32 |
# ๆๅฎใใ้พๅคไปฅไธใฎ้ ๅใใใๅ ดๅใฎใฟใใชใใณใฐ
|
33 |
+
if non_transparent_pixel_count >= area_threshold:
|
34 |
result = result.crop(bbox)
|
35 |
+
else:
|
36 |
+
print("Small transparent region ignored")
|
37 |
|
38 |
# ็ตๆใไธๆใใกใคใซใซไฟๅญ
|
39 |
result_path = "tmp.png"
|