Spaces:
mashroo
/
Running on Zero

YoussefAnso commited on
Commit
0ac4eff
·
1 Parent(s): 3112ed0

Refactor background removal in app.py to utilize dis_bg_remover for improved image processing. Removed rembg dependency and updated related logic in remove_background function. Updated requirements.txt to reflect the change in background removal library.

Browse files
Files changed (2) hide show
  1. app.py +5 -14
  2. requirements.txt +2 -1
app.py CHANGED
@@ -9,7 +9,6 @@ import PIL
9
  from pipelines import TwoStagePipeline
10
  from huggingface_hub import hf_hub_download
11
  import os
12
- import rembg
13
  from typing import Any
14
  import json
15
  import os
@@ -18,9 +17,9 @@ import argparse
18
 
19
  from model import CRM
20
  from inference import generate3d
 
21
 
22
  pipeline = None
23
- rembg_session = rembg.new_session()
24
 
25
 
26
  def expand_to_square(image, bg_color=(0, 0, 0, 0)):
@@ -45,17 +44,9 @@ def remove_background(
45
  force: bool = False,
46
  **rembg_kwargs,
47
  ) -> PIL.Image.Image:
48
- do_remove = True
49
- if image.mode == "RGBA" and image.getextrema()[3][0] < 255:
50
- # explain why current do not rm bg
51
- print("alhpa channl not enpty, skip remove background, using alpha channel as mask")
52
- background = Image.new("RGBA", image.size, (0, 0, 0, 0))
53
- image = Image.alpha_composite(background, image)
54
- do_remove = False
55
- do_remove = do_remove or force
56
- if do_remove:
57
- image = rembg.remove(image, session=rembg_session, **rembg_kwargs)
58
- return image
59
 
60
  def do_resize_content(original_image: Image, scale_rate):
61
  # resize image content wile retain the original image size
@@ -87,7 +78,7 @@ def preprocess_image(image, background_choice, foreground_ratio, backgroud_color
87
  background = Image.new("RGBA", image.size, (0, 0, 0, 0))
88
  image = Image.alpha_composite(background, image)
89
  else:
90
- image = remove_background(image, rembg_session, force=True)
91
  image = do_resize_content(image, foreground_ratio)
92
  image = expand_to_square(image)
93
  image = add_background(image, backgroud_color)
 
9
  from pipelines import TwoStagePipeline
10
  from huggingface_hub import hf_hub_download
11
  import os
 
12
  from typing import Any
13
  import json
14
  import os
 
17
 
18
  from model import CRM
19
  from inference import generate3d
20
+ from dis_bg_remover import remove_background as dis_remove_background
21
 
22
  pipeline = None
 
23
 
24
 
25
  def expand_to_square(image, bg_color=(0, 0, 0, 0)):
 
44
  force: bool = False,
45
  **rembg_kwargs,
46
  ) -> PIL.Image.Image:
47
+ model_path = "isnet_dis.onnx" # Update with the correct path to the ONNX model
48
+ extracted_img, mask = dis_remove_background(model_path, image)
49
+ return extracted_img
 
 
 
 
 
 
 
 
50
 
51
  def do_resize_content(original_image: Image, scale_rate):
52
  # resize image content wile retain the original image size
 
78
  background = Image.new("RGBA", image.size, (0, 0, 0, 0))
79
  image = Image.alpha_composite(background, image)
80
  else:
81
+ image = remove_background(image, force=True)
82
  image = do_resize_content(image, foreground_ratio)
83
  image = expand_to_square(image)
84
  image = add_background(image, backgroud_color)
requirements.txt CHANGED
@@ -9,7 +9,8 @@ opencv-contrib-python-headless==4.9.0.80
9
  opencv-python-headless==4.9.0.80
10
  xformers
11
  omegaconf
12
- rembg
 
13
  git+https://github.com/NVlabs/nvdiffrast
14
  pygltflib
15
  kiui
 
9
  opencv-python-headless==4.9.0.80
10
  xformers
11
  omegaconf
12
+ # rembg
13
+ dis_bg_remover
14
  git+https://github.com/NVlabs/nvdiffrast
15
  pygltflib
16
  kiui