tori29umai commited on
Commit
477dcb8
โ€ข
1 Parent(s): 5d7ff09

Update utils/image_utils.py

Browse files
Files changed (1) hide show
  1. 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
- def background_removal(input_image_path):
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=1000 # ็„ก่ฆ–ใ™ใ‚‹ๅฐใ•ใ„้ ˜ๅŸŸใฎใƒ”ใ‚ฏใ‚ปใƒซๆ•ฐใฎ้–พๅ€ค
 
 
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 > 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"
 
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"