Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -383,9 +383,13 @@ def switch_rows(v):
|
|
383 |
print(frames[0])
|
384 |
return frames
|
385 |
|
386 |
-
def apply_mask(d):
|
387 |
global frame_selected
|
388 |
global masks
|
|
|
|
|
|
|
|
|
389 |
|
390 |
masks[frame_selected] = d["background"]
|
391 |
cv2.imwrite(f"f{frame_selected}_dmap.png", masks[frame_selected])
|
@@ -446,8 +450,8 @@ def draw_mask(l, t, v, d, evt: gr.EventData):
|
|
446 |
# 255 << 8 tells to fill with the value 255)
|
447 |
mask = mask[1:mask.shape[0]-1, 1:mask.shape[1]-1]
|
448 |
|
449 |
-
d["layers"][0][mask>0] = (
|
450 |
-
d["background"][mask>0] = (
|
451 |
|
452 |
return gr.ImageEditor(value=d)
|
453 |
|
@@ -560,12 +564,13 @@ with gr.Blocks(css=css, js=js) as demo:
|
|
560 |
output_switch.input(fn=switch_rows, inputs=[output_switch], outputs=[output_frame])
|
561 |
output_mask = gr.ImageEditor(layers=False, sources=('upload', 'clipboard'), show_download_button=True, type="numpy", interactive=True, transforms=(None,), eraser=gr.Eraser(), brush=gr.Brush(default_size=0, colors=['black', '#505050', '#a0a0a0', 'white']), elem_id="image_edit")
|
562 |
depth_file = gr.File(label="Edited depth")
|
563 |
-
output_mask.apply(fn=apply_mask, inputs=[output_mask], outputs=[output_mask, depth_file])
|
564 |
with gr.Accordion(label="Edge", open=False):
|
565 |
levels = gr.Slider(label="Color levels", value=16, maximum=32, minimum=2, step=1)
|
566 |
tolerance = gr.Slider(label="Tolerance", value=1, maximum=7, minimum=0, step=1)
|
|
|
567 |
mouse = gr.Textbox(elem_id="mouse", value="""[]""", interactive=False)
|
568 |
mouse.input(fn=draw_mask, show_progress="minimal", inputs=[levels, tolerance, mouse, output_mask], outputs=[output_mask])
|
|
|
569 |
selector = gr.HTML(value="""
|
570 |
<a href='#' id='selector' onclick='this.style.fontWeight=\"bold\";
|
571 |
document.getElementById(\"image_edit\").getElementsByTagName(\"canvas\")[0].oncontextmenu = function(e){e.preventDefault();}
|
|
|
383 |
print(frames[0])
|
384 |
return frames
|
385 |
|
386 |
+
def apply_mask(d, b):
|
387 |
global frame_selected
|
388 |
global masks
|
389 |
+
|
390 |
+
dilation = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2 * b + 1, 2 * b + 1), (b, b))
|
391 |
+
d["layers"][0] = cv2.dilate(d["layers"][0], dilation)
|
392 |
+
d["background"][d["layers"][0]==(255,255,255,255)] = (255,255,255,255)
|
393 |
|
394 |
masks[frame_selected] = d["background"]
|
395 |
cv2.imwrite(f"f{frame_selected}_dmap.png", masks[frame_selected])
|
|
|
450 |
# 255 << 8 tells to fill with the value 255)
|
451 |
mask = mask[1:mask.shape[0]-1, 1:mask.shape[1]-1]
|
452 |
|
453 |
+
d["layers"][0][mask>0] = (255,255,255,255)
|
454 |
+
d["background"][mask>0] = (255,255,255,255)
|
455 |
|
456 |
return gr.ImageEditor(value=d)
|
457 |
|
|
|
564 |
output_switch.input(fn=switch_rows, inputs=[output_switch], outputs=[output_frame])
|
565 |
output_mask = gr.ImageEditor(layers=False, sources=('upload', 'clipboard'), show_download_button=True, type="numpy", interactive=True, transforms=(None,), eraser=gr.Eraser(), brush=gr.Brush(default_size=0, colors=['black', '#505050', '#a0a0a0', 'white']), elem_id="image_edit")
|
566 |
depth_file = gr.File(label="Edited depth")
|
|
|
567 |
with gr.Accordion(label="Edge", open=False):
|
568 |
levels = gr.Slider(label="Color levels", value=16, maximum=32, minimum=2, step=1)
|
569 |
tolerance = gr.Slider(label="Tolerance", value=1, maximum=7, minimum=0, step=1)
|
570 |
+
bsize = gr.Slider(label="Border size", value=1, maximum=256, minimum=1, step=2)
|
571 |
mouse = gr.Textbox(elem_id="mouse", value="""[]""", interactive=False)
|
572 |
mouse.input(fn=draw_mask, show_progress="minimal", inputs=[levels, tolerance, mouse, output_mask], outputs=[output_mask])
|
573 |
+
output_mask.apply(fn=apply_mask, inputs=[output_mask, bsize], outputs=[output_mask, depth_file])
|
574 |
selector = gr.HTML(value="""
|
575 |
<a href='#' id='selector' onclick='this.style.fontWeight=\"bold\";
|
576 |
document.getElementById(\"image_edit\").getElementsByTagName(\"canvas\")[0].oncontextmenu = function(e){e.preventDefault();}
|