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