makakwastaken commited on
Commit
8802f45
·
1 Parent(s): b049fc2

Output mask instead of visual

Browse files
Files changed (4) hide show
  1. .gitignore +1 -0
  2. CutLER +1 -1
  3. app.py +30 -26
  4. model.py +0 -7
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ __pycache__
CutLER CHANGED
@@ -1 +1 @@
1
- Subproject commit 077938c626341723050a1971107af552a6ca6697
 
1
+ Subproject commit 19ca89e1ce39a786e459f64a9d7f6dcfbcdb6d6f
app.py CHANGED
@@ -6,40 +6,44 @@ import gradio as gr
6
  import numpy as np
7
  import PIL.Image as Image
8
 
9
- from model import Model, random_color, vis_mask
 
 
10
 
11
  model = Model()
12
 
13
 
14
- def run(image_path, threshold, max_num_mask):
15
  image = np.asarray(Image.open(image_path).convert('RGB'))
16
- masks = model(image_path, threshold, max_num_mask)
17
- for mask in masks:
18
- image = vis_mask(image, mask, random_color(rgb=True))
19
- return image
 
 
 
 
 
 
 
20
 
21
 
22
  TITLE = 'MaskCut'
23
  DESCRIPTION = 'This is an unofficial demo for https://github.com/facebookresearch/CutLER.'
24
 
25
  paths = sorted(pathlib.Path('CutLER/maskcut/imgs').glob('*.jpg'))
26
- demo = gr.Interface(
27
- fn=run,
28
- inputs=[
29
- gr.Image(label='Input image', type='filepath'),
30
- gr.Slider(label='Threshold used for producing binary graph',
31
- minimum=0,
32
- maximum=1,
33
- value=0.15,
34
- step=0.01),
35
- gr.Slider(label='The maximum number of pseudo-masks per image',
36
- minimum=1,
37
- maximum=20,
38
- value=6,
39
- step=1),
40
- ],
41
- outputs=gr.Image(label='Result', type='numpy'),
42
- examples=[[path.as_posix(), 0.15, 6] for path in paths],
43
- title=TITLE,
44
- description=DESCRIPTION)
45
- demo.queue().launch()
 
6
  import numpy as np
7
  import PIL.Image as Image
8
 
9
+ from model import Model
10
+ import base64
11
+ from io import BytesIO
12
 
13
  model = Model()
14
 
15
 
16
+ def run(image_path, threshold):
17
  image = np.asarray(Image.open(image_path).convert('RGB'))
18
+ # We copy the image that and fill it with black, to get the dimensions
19
+ rgb = np.copy(image)
20
+ rgb.fill(0)
21
+ masks = model(image_path, threshold, 1)
22
+
23
+ mask = masks[0]
24
+ fg = mask > 0.5
25
+ rgb[fg] = 255
26
+ img = Image.fromarray(rgb)
27
+
28
+ return img
29
 
30
 
31
  TITLE = 'MaskCut'
32
  DESCRIPTION = 'This is an unofficial demo for https://github.com/facebookresearch/CutLER.'
33
 
34
  paths = sorted(pathlib.Path('CutLER/maskcut/imgs').glob('*.jpg'))
35
+ demo = gr.Interface(fn=run,
36
+ inputs=[
37
+ gr.Image(label='Input image', type='filepath'),
38
+ gr.Slider(
39
+ label='Threshold used for producing binary graph',
40
+ minimum=0,
41
+ maximum=1,
42
+ value=0.15,
43
+ step=0.01),
44
+ ],
45
+ outputs=gr.Image(label='Output image', type="pil"),
46
+ examples=[[path.as_posix(), 0.15] for path in paths],
47
+ title=TITLE,
48
+ description=DESCRIPTION)
49
+ demo.queue().launch(share=True)
 
 
 
 
 
model.py CHANGED
@@ -17,13 +17,6 @@ from maskcut import maskcut
17
  from third_party.TokenCut.unsupervised_saliency_detection import metric
18
 
19
 
20
- def vis_mask(input, mask, mask_color):
21
- fg = mask > 0.5
22
- rgb = np.copy(input)
23
- rgb[fg] = (rgb[fg] * 0.3 + np.array(mask_color) * 0.7).astype(np.uint8)
24
- return Image.fromarray(rgb)
25
-
26
-
27
  class Model:
28
  def __init__(self):
29
  self.device = torch.device(
 
17
  from third_party.TokenCut.unsupervised_saliency_detection import metric
18
 
19
 
 
 
 
 
 
 
 
20
  class Model:
21
  def __init__(self):
22
  self.device = torch.device(