Update app.py
Browse files
app.py
CHANGED
@@ -549,6 +549,7 @@ def apply_mask(d, b):
|
|
549 |
return masks[frame_selected], depths, frames
|
550 |
|
551 |
def draw_mask(l, t, th, v, d, evt: gr.EventData):
|
|
|
552 |
global depths
|
553 |
global params
|
554 |
global frame_selected
|
@@ -562,6 +563,8 @@ def draw_mask(l, t, th, v, d, evt: gr.EventData):
|
|
562 |
|
563 |
if len(edge) == 0 or params["fnum"] != frame_selected or params["l"] != l:
|
564 |
if len(edge) > 0:
|
|
|
|
|
565 |
d["background"] = cv2.imread(depths[frame_selected]).astype(np.uint8)
|
566 |
|
567 |
if d["background"].shape[0] == 2048: #height
|
@@ -571,19 +574,16 @@ def draw_mask(l, t, th, v, d, evt: gr.EventData):
|
|
571 |
else:
|
572 |
gradient = cv2.imread('./gradient_small.png').astype(np.uint8)
|
573 |
|
574 |
-
bg = cv2.cvtColor(d["background"], cv2.
|
|
|
575 |
|
576 |
-
diff = np.abs(
|
577 |
mask = cv2.inRange(diff, 0, t)
|
578 |
#kernel = np.ones((c,c),np.float32)/(c*c)
|
579 |
#mask = cv2.filter2D(mask,-1,kernel)
|
580 |
dilation = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (15-(t*2+1), 15-(t*2+1)), (t, t))
|
581 |
mask = cv2.dilate(mask, dilation)
|
582 |
|
583 |
-
#bd = cv2.convertScaleAbs(cv2.Laplacian(bg, cv2.CV_64F, 5))
|
584 |
-
|
585 |
-
bg = ((bg - 127) * 2).astype(np.uint8)
|
586 |
-
|
587 |
#indices = np.arange(0,256) # List of all colors
|
588 |
#divider = np.linspace(0,255,l+1)[1] # we get a divider
|
589 |
#quantiz = np.intp(np.linspace(0,255,l)) # we get quantization colors
|
@@ -601,13 +601,13 @@ def draw_mask(l, t, th, v, d, evt: gr.EventData):
|
|
601 |
res = center[label.flatten()]
|
602 |
bg = res.reshape((bg.shape))
|
603 |
|
604 |
-
bg[mask>0] = 0
|
605 |
-
bg[bg==255] = 0
|
606 |
|
607 |
params["fnum"] = frame_selected
|
608 |
params["l"] = l
|
609 |
|
610 |
-
d["layers"][0] = cv2.cvtColor(bg, cv2.
|
611 |
edge = bg.copy()
|
612 |
else:
|
613 |
bg = edge.copy()
|
|
|
549 |
return masks[frame_selected], depths, frames
|
550 |
|
551 |
def draw_mask(l, t, th, v, d, evt: gr.EventData):
|
552 |
+
global frames
|
553 |
global depths
|
554 |
global params
|
555 |
global frame_selected
|
|
|
563 |
|
564 |
if len(edge) == 0 or params["fnum"] != frame_selected or params["l"] != l:
|
565 |
if len(edge) > 0:
|
566 |
+
clr = cv2.cvtColor(cv2.imread(frames[frame_selected]).astype(np.uint8), cv2.COLOR_BGR2HSV)
|
567 |
+
clr = cb2.medianBlur(clr, th)
|
568 |
d["background"] = cv2.imread(depths[frame_selected]).astype(np.uint8)
|
569 |
|
570 |
if d["background"].shape[0] == 2048: #height
|
|
|
574 |
else:
|
575 |
gradient = cv2.imread('./gradient_small.png').astype(np.uint8)
|
576 |
|
577 |
+
bg = cv2.cvtColor(d["background"], cv2.COLOR_RGBA2HSV)
|
578 |
+
bg[:,:,0] = clr[:,:,0]
|
579 |
|
580 |
+
diff = np.abs(cv2.cvtColor(d["background"], cv2.COLOR_RGBA2GRAY).astype(np.int16)-cv2.cvtColor(gradient, cv2.COLOR_RGBA2GRAY).astype(np.int16)).astype(np.uint8)
|
581 |
mask = cv2.inRange(diff, 0, t)
|
582 |
#kernel = np.ones((c,c),np.float32)/(c*c)
|
583 |
#mask = cv2.filter2D(mask,-1,kernel)
|
584 |
dilation = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (15-(t*2+1), 15-(t*2+1)), (t, t))
|
585 |
mask = cv2.dilate(mask, dilation)
|
586 |
|
|
|
|
|
|
|
|
|
587 |
#indices = np.arange(0,256) # List of all colors
|
588 |
#divider = np.linspace(0,255,l+1)[1] # we get a divider
|
589 |
#quantiz = np.intp(np.linspace(0,255,l)) # we get quantization colors
|
|
|
601 |
res = center[label.flatten()]
|
602 |
bg = res.reshape((bg.shape))
|
603 |
|
604 |
+
bg[mask>0] = (0,0,0)
|
605 |
+
bg[bg[:,:,2]==255] = (0,0,0)
|
606 |
|
607 |
params["fnum"] = frame_selected
|
608 |
params["l"] = l
|
609 |
|
610 |
+
d["layers"][0] = cv2.cvtColor(bg, cv2.COLOR_HSV2RGBA)
|
611 |
edge = bg.copy()
|
612 |
else:
|
613 |
bg = edge.copy()
|