Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -347,7 +347,7 @@ def switch_rows(v):
|
|
347 |
print(frames[0])
|
348 |
return frames
|
349 |
|
350 |
-
def draw_mask(f, n, v, d, evt: gr.EventData):
|
351 |
print(v)
|
352 |
vv = json.loads(v)
|
353 |
pts = np.array(vv, np.int32)
|
@@ -372,10 +372,25 @@ def draw_mask(f, n, v, d, evt: gr.EventData):
|
|
372 |
gd = cv2.cvtColor(gradient, cv2.COLOR_RGB2GRAY)
|
373 |
|
374 |
diff = (bg-gd).astype(np.uint8)
|
375 |
-
mask = cv2.inRange(diff, -int(
|
376 |
d["layers"][0][mask>0] = (0,0,0,255)
|
377 |
|
378 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
379 |
d["layers"][0] = cv2.fillPoly(d["layers"][0], [pts], (0,0,0,255))
|
380 |
|
381 |
return gr.ImageEditor(value=d)
|
@@ -431,7 +446,8 @@ with gr.Blocks(css=css) as demo:
|
|
431 |
output_switch.input(fn=switch_rows, inputs=[output_switch], outputs=[output_frame])
|
432 |
output_mask = gr.ImageEditor(layers=True, sources=None, type="numpy", interactive=True, transforms=(None,), eraser=gr.Eraser(), brush=gr.Brush(default_size=0, colors=['black', '#505050', '#a0a0a0', 'white']), elem_id="image_edit")
|
433 |
gradients = gr.Files(visible=False, value=['./gradient_large.png', './gradient.png', './gradient_small.png'])
|
434 |
-
levels = gr.
|
|
|
435 |
selector = gr.HTML(value="""<div id='select_cursor' style='width:1px;height:1px;position:absolute;border:8px dotted #ffffff;opacity:50%;'></div>
|
436 |
<a href='#' id='selector' onclick='this.style.fontWeight=\"bold\";
|
437 |
document.body.appendChild(document.getElementById(\"select_cursor\"));
|
@@ -488,7 +504,7 @@ with gr.Blocks(css=css) as demo:
|
|
488 |
document.getElementById(\"select_cursor\").style.top = 0;
|
489 |
' title='Clear selection' style='text-decoration:none;color:white;'>[x] Clear selection</a>""")
|
490 |
mouse = gr.Textbox(elem_id="mouse", value="""[]""", interactive=False)
|
491 |
-
output_mask.apply(fn=draw_mask, inputs=[gradients, levels, mouse, output_mask], outputs=[output_mask])
|
492 |
submit = gr.Button("Submit")
|
493 |
with gr.Column():
|
494 |
model_type = gr.Dropdown([("small", "vits"), ("base", "vitb"), ("large", "vitl")], type="value", value="vits", label='Model Type')
|
|
|
347 |
print(frames[0])
|
348 |
return frames
|
349 |
|
350 |
+
def draw_mask(f, n, c, v, d, evt: gr.EventData):
|
351 |
print(v)
|
352 |
vv = json.loads(v)
|
353 |
pts = np.array(vv, np.int32)
|
|
|
372 |
gd = cv2.cvtColor(gradient, cv2.COLOR_RGB2GRAY)
|
373 |
|
374 |
diff = (bg-gd).astype(np.uint8)
|
375 |
+
mask = cv2.inRange(diff, -int(c/2), int(c/2))
|
376 |
d["layers"][0][mask>0] = (0,0,0,255)
|
377 |
|
378 |
+
bg = cv2.convertScaleAbs(palette[bg]) # Converting image back to uint
|
379 |
+
|
380 |
+
scale = 1
|
381 |
+
delta = 0
|
382 |
+
ddepth = cv2.CV_16S
|
383 |
+
|
384 |
+
grad = cv2.Sobel(bg, ddepth, 1, 1, ksize=1, scale=scale, delta=delta, borderType=cv2.BORDER_DEFAULT)
|
385 |
+
# Gradient-Y
|
386 |
+
# grad_y = cv.Scharr(gray,ddepth,0,1)
|
387 |
+
#grad_y = cv.Sobel(gray, ddepth, 0, 1, ksize=3, scale=scale, delta=delta, borderType=cv.BORDER_DEFAULT)
|
388 |
+
|
389 |
+
abs_grad = cv2.convertScaleAbs(grad)
|
390 |
+
#abs_grad_y = cv.convertScaleAbs(grad_y)
|
391 |
+
#grad = cv.addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0)
|
392 |
+
|
393 |
+
th, d["layers"][0] = cv2.threshold(abs_grad, 127, 255, cv2.THRESH_BINARY)
|
394 |
d["layers"][0] = cv2.fillPoly(d["layers"][0], [pts], (0,0,0,255))
|
395 |
|
396 |
return gr.ImageEditor(value=d)
|
|
|
446 |
output_switch.input(fn=switch_rows, inputs=[output_switch], outputs=[output_frame])
|
447 |
output_mask = gr.ImageEditor(layers=True, sources=None, type="numpy", interactive=True, transforms=(None,), eraser=gr.Eraser(), brush=gr.Brush(default_size=0, colors=['black', '#505050', '#a0a0a0', 'white']), elem_id="image_edit")
|
448 |
gradients = gr.Files(visible=False, value=['./gradient_large.png', './gradient.png', './gradient_small.png'])
|
449 |
+
levels = gr.Number(label="Color levels", value=16, maximum=32, minimum=2, step=1)
|
450 |
+
contour = gr.Number(label="Contour width", value=2, maximum=16, minimum=0, step=2)
|
451 |
selector = gr.HTML(value="""<div id='select_cursor' style='width:1px;height:1px;position:absolute;border:8px dotted #ffffff;opacity:50%;'></div>
|
452 |
<a href='#' id='selector' onclick='this.style.fontWeight=\"bold\";
|
453 |
document.body.appendChild(document.getElementById(\"select_cursor\"));
|
|
|
504 |
document.getElementById(\"select_cursor\").style.top = 0;
|
505 |
' title='Clear selection' style='text-decoration:none;color:white;'>[x] Clear selection</a>""")
|
506 |
mouse = gr.Textbox(elem_id="mouse", value="""[]""", interactive=False)
|
507 |
+
output_mask.apply(fn=draw_mask, inputs=[gradients, levels, contour, mouse, output_mask], outputs=[output_mask])
|
508 |
submit = gr.Button("Submit")
|
509 |
with gr.Column():
|
510 |
model_type = gr.Dropdown([("small", "vits"), ("base", "vitb"), ("large", "vitl")], type="value", value="vits", label='Model Type')
|