WhiteYC commited on
Commit
0af2c19
·
verified ·
1 Parent(s): 2923ad5

Brush colours that match how the editor shows the trimap

Browse files
Files changed (1) hide show
  1. app.py +14 -10
app.py CHANGED
@@ -30,7 +30,9 @@ pipe = PixelMattePipeline.from_pretrained(MODEL, device="cpu", token=TOKEN).to(D
30
  segmenter = ClickSegmenter(SEGMENTERS, device=DEVICE, token=TOKEN)
31
 
32
  BACKGROUNDS = {"Checkerboard": None, "White": (255, 255, 255), "Black": (0, 0, 0), "Green": (0, 177, 64), "Blue": (40, 90, 200)}
33
- BRUSHES = {"#FFFFFF": 255, "#808080": 128, "#000000": 0} # brush colour -> trimap value
 
 
34
 
35
 
36
  # ------------------------------------------------------------------ pictures
@@ -104,7 +106,8 @@ def show_trimap(image: Image.Image, trimap: np.ndarray) -> Image.Image:
104
 
105
 
106
  def apply_strokes(trimap: np.ndarray, editor) -> np.ndarray:
107
- """the trimap with the user's brush strokes painted over it (white = foreground, grey = unknown, black = background)"""
 
108
  if not editor or not editor.get("layers"):
109
  return trimap
110
  out = trimap.copy()
@@ -113,11 +116,13 @@ def apply_strokes(trimap: np.ndarray, editor) -> np.ndarray:
113
  if layer is None:
114
  continue
115
  rgba = np.asarray(layer.convert("RGBA").resize((W, H), Image.NEAREST))
116
- painted = rgba[..., 3] > 127
117
- level = rgba[..., :3].astype(np.float32).mean(-1)
118
- out[painted & (level > 192)] = 255
119
- out[painted & (level < 64)] = 0
120
- out[painted & (level >= 64) & (level <= 192)] = 128
 
 
121
  return out
122
 
123
 
@@ -306,13 +311,12 @@ with gr.Blocks(title="PixelMatte") as demo:
306
  with gr.Tab("Upload a trimap"):
307
  trimap_upload = gr.Image(type="pil", image_mode="L", height=260, sources=["upload", "clipboard"],
308
  label="Trimap (black = background, grey = unknown, white = foreground)")
309
- # default brush colour grey: the colour button takes the current colour and a white one is invisible on the toolbar
310
  editor = gr.ImageEditor(type="pil", label="2. Trimap (bright = foreground, dark = background, yellow = unknown)",
311
  height=420, sources=(), transforms=(), layers=False,
312
- brush=gr.Brush(colors=list(BRUSHES), default_color="#808080", color_mode="fixed", default_size=24),
313
  eraser=gr.Eraser(default_size=24))
314
  gr.Markdown("To correct the trimap, paint on it: select the brush, pick its colour with the palette button under the eraser "
315
- "(white = foreground, grey = unknown, black = background) and draw. The eraser removes your strokes.")
316
  with gr.Accordion("Options", open=False):
317
  bg = gr.Dropdown(list(BACKGROUNDS), value="Checkerboard", label="Background of the preview")
318
  seed = gr.Number(value=42, precision=0, label="Noise seed")
 
30
  segmenter = ClickSegmenter(SEGMENTERS, device=DEVICE, token=TOKEN)
31
 
32
  BACKGROUNDS = {"Checkerboard": None, "White": (255, 255, 255), "Black": (0, 0, 0), "Green": (0, 177, 64), "Blue": (40, 90, 200)}
33
+ # Brushes of the trimap editor, in the colours the editor shows the trimap in: yellow = unknown, light = foreground, dark =
34
+ # background. They are see-through, so the photo stays visible under a stroke.
35
+ BRUSHES = [("#FFD200", 0.45), ("#FFFFFF", 0.6), ("#000000", 0.7)]
36
 
37
 
38
  # ------------------------------------------------------------------ pictures
 
106
 
107
 
108
  def apply_strokes(trimap: np.ndarray, editor) -> np.ndarray:
109
+ """the trimap with the user's brush strokes painted over it (yellow = unknown, white = foreground, black = background);
110
+ where see-through strokes overlap, the colours mix and the stroke on top decides"""
111
  if not editor or not editor.get("layers"):
112
  return trimap
113
  out = trimap.copy()
 
116
  if layer is None:
117
  continue
118
  rgba = np.asarray(layer.convert("RGBA").resize((W, H), Image.NEAREST))
119
+ painted = rgba[..., 3] > 16
120
+ rgb = rgba[..., :3].astype(np.float32)
121
+ yellow = rgb[..., 0] - rgb[..., 2] > 96
122
+ light = rgb.mean(-1) >= 128
123
+ out[painted & yellow] = 128
124
+ out[painted & ~yellow & light] = 255
125
+ out[painted & ~yellow & ~light] = 0
126
  return out
127
 
128
 
 
311
  with gr.Tab("Upload a trimap"):
312
  trimap_upload = gr.Image(type="pil", image_mode="L", height=260, sources=["upload", "clipboard"],
313
  label="Trimap (black = background, grey = unknown, white = foreground)")
 
314
  editor = gr.ImageEditor(type="pil", label="2. Trimap (bright = foreground, dark = background, yellow = unknown)",
315
  height=420, sources=(), transforms=(), layers=False,
316
+ brush=gr.Brush(colors=BRUSHES, default_color=BRUSHES[0], color_mode="fixed", default_size=24),
317
  eraser=gr.Eraser(default_size=24))
318
  gr.Markdown("To correct the trimap, paint on it: select the brush, pick its colour with the palette button under the eraser "
319
+ "(yellow = unknown, white = foreground, black = background) and draw. The eraser removes your strokes.")
320
  with gr.Accordion("Options", open=False):
321
  bg = gr.Dropdown(list(BACKGROUNDS), value="Checkerboard", label="Background of the preview")
322
  seed = gr.Number(value=42, precision=0, label="Noise seed")