Spaces:
Running on Zero
Running on Zero
Brush colours that match how the editor shows the trimap
Browse files29 unmodified lines3031323333343536373867 unmodified lines1061071081071091101111121132 unmodified lines116117118116117118119120119120121122123124125126127128182 unmodified lines31131231330931431531231631731831531932032132229 unmodified linessegmenter = ClickSegmenter(SEGMENTERS, device=DEVICE, token=TOKEN)
BACKGROUNDS = {"Checkerboard": None, "White": (255, 255, 255), "Black": (0, 0, 0), "Green": (0, 177, 64), "Blue": (40, 90, 200)}BRUSHES = {"#FFFFFF": 255, "#808080": 128, "#000000": 0} # brush colour -> trimap value# Brushes of the trimap editor, in the colours the editor shows the trimap in: yellow = unknown, light = foreground, dark =# background. They are see-through, so the photo stays visible under a stroke.BRUSHES = [("#FFD200", 0.45), ("#FFFFFF", 0.6), ("#000000", 0.7)]
# ------------------------------------------------------------------ pictures67 unmodified lines
def apply_strokes(trimap: np.ndarray, editor) -> np.ndarray: """the trimap with the user's brush strokes painted over it (white = foreground, grey = unknown, black = background)""" """the trimap with the user's brush strokes painted over it (yellow = unknown, white = foreground, black = background); where see-through strokes overlap, the colours mix and the stroke on top decides""" if not editor or not editor.get("layers"): return trimap out = trimap.copy()2 unmodified lines if layer is None: continue rgba = np.asarray(layer.convert("RGBA").resize((W, H), Image.NEAREST)) painted = rgba[..., 3] > 127 level = rgba[..., :3].astype(np.float32).mean(-1) out[painted & (level > 192)] = 255 out[painted & (level < 64)] = 0 out[painted & (level >= 64) & (level <= 192)] = 128 painted = rgba[..., 3] > 16 rgb = rgba[..., :3].astype(np.float32) yellow = rgb[..., 0] - rgb[..., 2] > 96 light = rgb.mean(-1) >= 128 out[painted & yellow] = 128 out[painted & ~yellow & light] = 255 out[painted & ~yellow & ~light] = 0 return out
182 unmodified lines with gr.Tab("Upload a trimap"): trimap_upload = gr.Image(type="pil", image_mode="L", height=260, sources=["upload", "clipboard"], label="Trimap (black = background, grey = unknown, white = foreground)") # default brush colour grey: the colour button takes the current colour and a white one is invisible on the toolbar editor = gr.ImageEditor(type="pil", label="2. Trimap (bright = foreground, dark = background, yellow = unknown)", height=420, sources=(), transforms=(), layers=False, brush=gr.Brush(colors=list(BRUSHES), default_color="#808080", color_mode="fixed", default_size=24), brush=gr.Brush(colors=BRUSHES, default_color=BRUSHES[0], color_mode="fixed", default_size=24), eraser=gr.Eraser(default_size=24)) gr.Markdown("To correct the trimap, paint on it: select the brush, pick its colour with the palette button under the eraser " "(white = foreground, grey = unknown, black = background) and draw. The eraser removes your strokes.") "(yellow = unknown, white = foreground, black = background) and draw. The eraser removes your strokes.") with gr.Accordion("Options", open=False): bg = gr.Dropdown(list(BACKGROUNDS), value="Checkerboard", label="Background of the preview") seed = gr.Number(value=42, precision=0, label="Noise seed")app.py+14-10