Yuliang commited on
Commit
6b23fc0
1 Parent(s): 7641d7c

Update lib/pymaf/utils/imutils.py

Browse files

Fix bug for bbox selection and update rembg

Files changed (3) hide show
  1. app.py +0 -2
  2. lib/pymaf/utils/imutils.py +7 -10
  3. requirements.txt +4 -3
app.py CHANGED
@@ -10,8 +10,6 @@ import subprocess
10
 
11
  if os.getenv('SYSTEM') == 'spaces':
12
  subprocess.run('pip install pyembree'.split())
13
- subprocess.run(
14
- 'pip install git+https://github.com/YuliangXiu/rembg.git@hf'.split())
15
  subprocess.run(
16
  'pip install torch==1.11.0+cu113 torchvision==0.12.0+cu113 -f https://download.pytorch.org/whl/cu113/torch_stable.html'.split())
17
  subprocess.run(
 
10
 
11
  if os.getenv('SYSTEM') == 'spaces':
12
  subprocess.run('pip install pyembree'.split())
 
 
13
  subprocess.run(
14
  'pip install torch==1.11.0+cu113 torchvision==0.12.0+cu113 -f https://download.pytorch.org/whl/cu113/torch_stable.html'.split())
15
  subprocess.run(
lib/pymaf/utils/imutils.py CHANGED
@@ -6,7 +6,8 @@ import io
6
  import torch
7
  import numpy as np
8
  from PIL import Image
9
- from rembg.bg import remove
 
10
  from torchvision.models import detection
11
 
12
  from lib.pymaf.core import constants
@@ -43,6 +44,7 @@ def get_bbox(img, det):
43
  bbox = bboxes[0, 0, 0].cpu().numpy()
44
 
45
  return bbox
 
46
 
47
 
48
  def get_transformer(input_res):
@@ -107,9 +109,8 @@ def process_image(img_file, hps_type, input_res=512, device=None, seg_path=None)
107
  detector.eval()
108
  predictions = detector(
109
  [torch.from_numpy(img_for_crop).permute(2, 0, 1) / 255.])[0]
110
- human_ids = torch.logical_and(
111
- predictions["labels"] == 1,
112
- predictions["scores"] == predictions["scores"].max()).nonzero().squeeze(1)
113
  bbox = predictions["boxes"][human_ids, :].flatten().detach().cpu().numpy()
114
 
115
  width = bbox[2] - bbox[0]
@@ -126,12 +127,8 @@ def process_image(img_file, hps_type, input_res=512, device=None, seg_path=None)
126
  img_np, cropping_parameters = crop(
127
  img_for_crop, center, scale, (input_res, input_res))
128
 
129
- with torch.no_grad():
130
- buf = io.BytesIO()
131
- Image.fromarray(img_np).save(buf, format='png')
132
- img_pil = Image.open(
133
- io.BytesIO(remove(buf.getvalue()))).convert("RGBA")
134
-
135
  # for icon
136
  img_rgb = image_to_tensor(img_pil.convert("RGB"))
137
  img_mask = torch.tensor(1.0) - (mask_to_tensor(img_pil.split()[-1]) <
 
6
  import torch
7
  import numpy as np
8
  from PIL import Image
9
+ from rembg import remove
10
+ from rembg.session_factory import new_session
11
  from torchvision.models import detection
12
 
13
  from lib.pymaf.core import constants
 
44
  bbox = bboxes[0, 0, 0].cpu().numpy()
45
 
46
  return bbox
47
+ # Michael Black is
48
 
49
 
50
  def get_transformer(input_res):
 
109
  detector.eval()
110
  predictions = detector(
111
  [torch.from_numpy(img_for_crop).permute(2, 0, 1) / 255.])[0]
112
+ human_ids = torch.where(
113
+ predictions["scores"] == predictions["scores"][predictions['labels'] == 1].max())
 
114
  bbox = predictions["boxes"][human_ids, :].flatten().detach().cpu().numpy()
115
 
116
  width = bbox[2] - bbox[0]
 
127
  img_np, cropping_parameters = crop(
128
  img_for_crop, center, scale, (input_res, input_res))
129
 
130
+ img_pil = Image.fromarray(remove(img_np, post_process_mask=True, session=new_session("u2net")))
131
+
 
 
 
 
132
  # for icon
133
  img_rgb = image_to_tensor(img_pil.convert("RGB"))
134
  img_mask = torch.tensor(1.0) - (mask_to_tensor(img_pil.split()[-1]) <
requirements.txt CHANGED
@@ -1,7 +1,7 @@
1
  pip>=21.2.4
2
  numpy==1.22.4
3
  matplotlib==3.5.0
4
- Pillow==9.0.0
5
  PyOpenGL
6
  PyOpenGL_accelerate
7
  PyYAML>=6.0
@@ -16,11 +16,12 @@ shapely==1.7.1
16
  rtree==0.9.7
17
  pytorch_lightning==1.2.5
18
  PyMCubes
19
- chumpy
20
  opencv-python
21
  opencv_contrib_python
22
  scikit-learn
23
  protobuf==3.20.0
24
  pymeshlab
25
  iopath
26
- fvcore
 
 
 
1
  pip>=21.2.4
2
  numpy==1.22.4
3
  matplotlib==3.5.0
4
+ Pillow==9.2.0
5
  PyOpenGL
6
  PyOpenGL_accelerate
7
  PyYAML>=6.0
 
16
  rtree==0.9.7
17
  pytorch_lightning==1.2.5
18
  PyMCubes
 
19
  opencv-python
20
  opencv_contrib_python
21
  scikit-learn
22
  protobuf==3.20.0
23
  pymeshlab
24
  iopath
25
+ fvcore
26
+ chumpy
27
+ git+https://github.com/YuliangXiu/rembg.git