Spaces:
Starting
Starting
Update app.py
Browse files
app.py
CHANGED
@@ -22,7 +22,7 @@ from moviepy.editor import *
|
|
22 |
|
23 |
edge = []
|
24 |
gradient = None
|
25 |
-
params = { "fnum":0, "l":16, "c":3
|
26 |
frame_selected = 0
|
27 |
frames = []
|
28 |
depths = []
|
@@ -367,7 +367,7 @@ def apply_mask(d):
|
|
367 |
cv2.imwrite(f"f{frame_selected}_dmap.png", masks[frame_selected])
|
368 |
return masks[frame_selected], f"f{frame_selected}_dmap.png"
|
369 |
|
370 |
-
def draw_mask(l, c,
|
371 |
global params
|
372 |
global frame_selected
|
373 |
global masks
|
@@ -385,7 +385,7 @@ def draw_mask(l, c, k, v, d, evt: gr.EventData):
|
|
385 |
delta = 0
|
386 |
ddepth = cv2.CV_16S
|
387 |
|
388 |
-
if len(edge) == 0 or params["fnum"] != frame_selected or params["l"] != l or params["c"] != c
|
389 |
if len(edge) > 0:
|
390 |
d["background"] = cv2.imread(masks[frame_selected]).astype(np.uint8)
|
391 |
|
@@ -393,7 +393,7 @@ def draw_mask(l, c, k, v, d, evt: gr.EventData):
|
|
393 |
|
394 |
diff = (bg-cv2.cvtColor(gradient, cv2.COLOR_RGBA2GRAY)).astype(np.uint8)
|
395 |
mask = cv2.inRange(diff, -int(c/2), int(c/2))
|
396 |
-
#kernel = np.ones((
|
397 |
#mask = cv2.filter2D(mask,-1,kernel)
|
398 |
grad_x = cv2.convertScaleAbs(cv2.Sobel(mask, ddepth, 1, 0, ksize=1, scale=scale, delta=delta, borderType=cv2.BORDER_DEFAULT))
|
399 |
grad_y = cv2.convertScaleAbs(cv2.Sobel(mask, ddepth, 0, 1, ksize=1, scale=scale, delta=delta, borderType=cv2.BORDER_DEFAULT))
|
@@ -418,20 +418,14 @@ def draw_mask(l, c, k, v, d, evt: gr.EventData):
|
|
418 |
params["fnum"] = frame_selected
|
419 |
params["l"] = l
|
420 |
params["c"] = c
|
421 |
-
params["k"] = k
|
422 |
|
423 |
#d["layers"][0] = cv2.cvtColor(bg, cv2.COLOR_GRAY2RGBA)
|
424 |
edge = bg.copy()
|
425 |
else:
|
426 |
bg = edge.copy()
|
427 |
|
428 |
-
mask = cv2.floodFill(bg, None, (x, y), 1,
|
429 |
# 255 << 8 tells to fill with the value 255)
|
430 |
-
grad_x = cv2.convertScaleAbs(cv2.Sobel(mask, ddepth, 1, 0, ksize=3, scale=scale, delta=delta, borderType=cv2.BORDER_DEFAULT))
|
431 |
-
grad_y = cv2.convertScaleAbs(cv2.Sobel(mask, ddepth, 0, 1, ksize=3, scale=scale, delta=delta, borderType=cv2.BORDER_DEFAULT))
|
432 |
-
|
433 |
-
grad = cv2.addWeighted(grad_x, 0.5, grad_y, 0.5, 0)
|
434 |
-
mask[cv2.inRange(grad, 1, 255)>0] = 1
|
435 |
mask = mask[1:mask.shape[0]-1, 1:mask.shape[1]-1]
|
436 |
|
437 |
d["layers"][0][mask>0] = (0,0,0,255)
|
@@ -494,9 +488,9 @@ with gr.Blocks(css=css) as demo:
|
|
494 |
with gr.Accordion(label="Edge", open=False):
|
495 |
levels = gr.Slider(label="Color levels", value=16, maximum=32, minimum=2, step=1)
|
496 |
contour = gr.Slider(label="Contour width", value=3, maximum=15, minimum=1, step=2)
|
497 |
-
|
498 |
mouse = gr.Textbox(elem_id="mouse", value="""[]""", interactive=False)
|
499 |
-
mouse.input(fn=draw_mask, show_progress="minimal", inputs=[levels, contour,
|
500 |
selector = gr.HTML(value="""
|
501 |
<a href='#' id='selector' onclick='this.style.fontWeight=\"bold\";
|
502 |
document.getElementById(\"image_edit\").getElementsByTagName(\"canvas\")[0].oncontextmenu = function(e){e.preventDefault();}
|
|
|
22 |
|
23 |
edge = []
|
24 |
gradient = None
|
25 |
+
params = { "fnum":0, "l":16, "c":3 }
|
26 |
frame_selected = 0
|
27 |
frames = []
|
28 |
depths = []
|
|
|
367 |
cv2.imwrite(f"f{frame_selected}_dmap.png", masks[frame_selected])
|
368 |
return masks[frame_selected], f"f{frame_selected}_dmap.png"
|
369 |
|
370 |
+
def draw_mask(l, c, t, v, d, evt: gr.EventData):
|
371 |
global params
|
372 |
global frame_selected
|
373 |
global masks
|
|
|
385 |
delta = 0
|
386 |
ddepth = cv2.CV_16S
|
387 |
|
388 |
+
if len(edge) == 0 or params["fnum"] != frame_selected or params["l"] != l or params["c"] != c:
|
389 |
if len(edge) > 0:
|
390 |
d["background"] = cv2.imread(masks[frame_selected]).astype(np.uint8)
|
391 |
|
|
|
393 |
|
394 |
diff = (bg-cv2.cvtColor(gradient, cv2.COLOR_RGBA2GRAY)).astype(np.uint8)
|
395 |
mask = cv2.inRange(diff, -int(c/2), int(c/2))
|
396 |
+
#kernel = np.ones((c,c),np.float32)/(c*c)
|
397 |
#mask = cv2.filter2D(mask,-1,kernel)
|
398 |
grad_x = cv2.convertScaleAbs(cv2.Sobel(mask, ddepth, 1, 0, ksize=1, scale=scale, delta=delta, borderType=cv2.BORDER_DEFAULT))
|
399 |
grad_y = cv2.convertScaleAbs(cv2.Sobel(mask, ddepth, 0, 1, ksize=1, scale=scale, delta=delta, borderType=cv2.BORDER_DEFAULT))
|
|
|
418 |
params["fnum"] = frame_selected
|
419 |
params["l"] = l
|
420 |
params["c"] = c
|
|
|
421 |
|
422 |
#d["layers"][0] = cv2.cvtColor(bg, cv2.COLOR_GRAY2RGBA)
|
423 |
edge = bg.copy()
|
424 |
else:
|
425 |
bg = edge.copy()
|
426 |
|
427 |
+
mask = cv2.floodFill(bg, None, (x, y), 1, int(t*256/l), int(t*256/l))[2] #, (4 | cv2.FLOODFILL_FIXED_RANGE | cv2.FLOODFILL_MASK_ONLY | 255 << 8)
|
428 |
# 255 << 8 tells to fill with the value 255)
|
|
|
|
|
|
|
|
|
|
|
429 |
mask = mask[1:mask.shape[0]-1, 1:mask.shape[1]-1]
|
430 |
|
431 |
d["layers"][0][mask>0] = (0,0,0,255)
|
|
|
488 |
with gr.Accordion(label="Edge", open=False):
|
489 |
levels = gr.Slider(label="Color levels", value=16, maximum=32, minimum=2, step=1)
|
490 |
contour = gr.Slider(label="Contour width", value=3, maximum=15, minimum=1, step=2)
|
491 |
+
tolerance = gr.Slider(label="Flood fill tolerance", value=0, maximum=15, minimum=0, step=1)
|
492 |
mouse = gr.Textbox(elem_id="mouse", value="""[]""", interactive=False)
|
493 |
+
mouse.input(fn=draw_mask, show_progress="minimal", inputs=[levels, contour, tolerance, mouse, output_mask], outputs=[output_mask])
|
494 |
selector = gr.HTML(value="""
|
495 |
<a href='#' id='selector' onclick='this.style.fontWeight=\"bold\";
|
496 |
document.getElementById(\"image_edit\").getElementsByTagName(\"canvas\")[0].oncontextmenu = function(e){e.preventDefault();}
|