freealise commited on
Commit
4cf379e
1 Parent(s): 8e1625f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -72
app.py CHANGED
@@ -37,7 +37,6 @@ encoder2name = {
37
 
38
  blurin = "1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1"
39
  edge = []
40
- clrs = []
41
  gradient = None
42
  params = { "fnum":0, "l":16 }
43
  dcolor = []
@@ -226,6 +225,8 @@ def make_video(video_path, outdir='./vis_video_depth', encoder='vits', blur_data
226
  cv2.imwrite(f"f{count}_dmap.png", depth_color)
227
  depth_frames.append(f"f{count}_dmap.png")
228
 
 
 
229
  cv2.imwrite(f"f{count}_mask.png", depth_gray)
230
  masks.append(f"f{count}_mask.png")
231
  count += 1
@@ -414,16 +415,45 @@ def show_json(txt):
414
  return data[0]["video"]["path"], data[1]["path"], data[2], data[3]["background"]["path"], data[4], data[5]
415
 
416
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
417
  def select_frame(d, evt: gr.SelectData):
418
  global dcolor
419
  global frame_selected
 
420
  global masks
421
  global edge
422
 
423
  if evt.index != frame_selected:
424
  edge = []
425
- mask = cv2.imread(depths[frame_selected]).astype(np.uint8)
426
- cv2.imwrite(masks[frame_selected], cv2.cvtColor(mask, cv2.COLOR_RGB2GRAY))
427
  frame_selected = evt.index
428
 
429
  if len(dcolor) == 0:
@@ -431,7 +461,7 @@ def select_frame(d, evt: gr.SelectData):
431
  else:
432
  bg = "[" + str(dcolor[frame_selected])[1:-1] + ", 255]"
433
 
434
- return masks[frame_selected], frame_selected, bg
435
 
436
  def switch_rows(v):
437
  global frames
@@ -516,7 +546,7 @@ def reset_mask():
516
  cv2.imwrite(masks[frame_selected], cv2.cvtColor(mask, cv2.COLOR_RGB2GRAY))
517
  return masks[frame_selected], depths
518
 
519
- def apply_mask(d, b):
520
  global frames
521
  global frame_selected
522
  global masks
@@ -525,29 +555,20 @@ def apply_mask(d, b):
525
 
526
  edge = []
527
  mask = cv2.cvtColor(d["layers"][0], cv2.COLOR_RGBA2GRAY)
528
- mask[mask<255] = 0
529
- b = b*2+1
530
- dilation = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2 * b + 1, 2 * b + 1), (b, b))
531
- mask = cv2.dilate(mask, dilation)
532
- mask_b = cv2.GaussianBlur(mask, (b,b), 0)
533
- b = b*2+1
534
- dilation = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2 * b + 1, 2 * b + 1), (b, b))
535
- dmask = cv2.dilate(mask, dilation)
536
- dmask_b = cv2.GaussianBlur(dmask, (b,b), 0)
537
-
538
- for k, mk in enumerate(masks):
539
- if k != frame_selected and k < len(depths):
540
- cv2.imwrite(masks[k], dmask)
541
- frame = cv2.imread(frames[k], cv2.IMREAD_UNCHANGED).astype(np.uint8)
542
- frame[:, :, 3] = dmask_b
543
- cv2.imwrite(frames[k], frame)
544
-
545
  frame = cv2.imread(frames[frame_selected], cv2.IMREAD_UNCHANGED).astype(np.uint8)
546
- frame[:, :, 3] = 255 - mask_b
547
  cv2.imwrite(frames[frame_selected], frame)
 
 
 
 
548
 
549
- cv2.imwrite(masks[frame_selected], mask) #d["background"]
550
- return masks[frame_selected], depths, frames
551
 
552
  def draw_mask(l, t, b, v, d, evt: gr.EventData):
553
  global frames
@@ -557,63 +578,24 @@ def draw_mask(l, t, b, v, d, evt: gr.EventData):
557
  global masks
558
  global gradient
559
  global edge
560
- global clrs
561
 
562
  points = json.loads(v)
563
  pts = np.array(points, np.int32)
564
  pts = pts.reshape((-1,1,2))
565
 
566
  if len(edge) == 0 or params["fnum"] != frame_selected or params["l"] != l:
567
- clrs = cv2.cvtColor(cv2.imread(frames[frame_selected]).astype(np.uint8), cv2.COLOR_BGR2HSV)
568
- clrs = cv2.medianBlur(clrs, b)
569
  if len(edge) > 0:
570
  d["background"] = cv2.imread(depths[frame_selected]).astype(np.uint8)
571
-
572
- if d["background"].shape[0] == 2048: #height
573
- gradient = cv2.imread('./gradient_large.png').astype(np.uint8)
574
- elif d["background"].shape[0] == 1024:
575
- gradient = cv2.imread('./gradient.png').astype(np.uint8)
576
- else:
577
- gradient = cv2.imread('./gradient_small.png').astype(np.uint8)
578
 
579
  bg = cv2.cvtColor(d["background"], cv2.COLOR_RGBA2GRAY)
580
- #bg = cv2.cvtColor(bg, cv2.COLOR_RGB2HSV)
581
- #bg[:,:,0] = clrs[:,:,0]
582
- #bg = cv2.cvtColor(bg, cv2.COLOR_HSV2RGB)
583
- bd = cv2.convertScaleAbs(cv2.Laplacian(bg, cv2.CV_64F, 7)).astype(np.uint8)
584
-
585
- 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)
586
- mask = cv2.inRange(diff, 0, t)
587
- #kernel = np.ones((c,c),np.float32)/(c*c)
588
- #mask = cv2.filter2D(mask,-1,kernel)
589
- dilation = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (15-(t*2+1), 15-(t*2+1)), (t, t))
590
- mask = cv2.dilate(mask, dilation)
591
-
592
- #indices = np.arange(0,256) # List of all colors
593
- #divider = np.linspace(0,255,l+1)[1] # we get a divider
594
- #quantiz = np.intp(np.linspace(0,255,l)) # we get quantization colors
595
- #color_levels = np.clip(np.intp(indices/divider),0,l-1) # color levels 0,1,2..
596
- #palette = quantiz[color_levels]
597
-
598
- #for i in range(l):
599
- # bg[(bg >= i*255/l) & (bg < (i+1)*255/l)] = i*255/(l-1)
600
- #bg = cv2.convertScaleAbs(palette[bg]).astype(np.uint8) # Converting image back to uint
601
-
602
- res = np.float32(bg.reshape((-1,1)))
603
- criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 4, 1.0)
604
- ret,label,center=cv2.kmeans(res,l,None,criteria,4,cv2.KMEANS_PP_CENTERS)
605
- center = np.uint8(center)
606
- res = center[label.flatten()]
607
- bg = res.reshape((bg.shape))
608
-
609
- bg[bd>4] = 0
610
- bg[mask>0] = 0
611
  bg[bg==255] = 0
612
 
613
  params["fnum"] = frame_selected
614
  params["l"] = l
615
 
616
- d["layers"][0] = cv2.cvtColor(bg, cv2.COLOR_GRAY2RGBA)
617
  edge = bg.copy()
618
  else:
619
  bg = edge.copy()
@@ -621,12 +603,11 @@ def draw_mask(l, t, b, v, d, evt: gr.EventData):
621
  x = points[len(points)-1][0]
622
  y = points[len(points)-1][1]
623
 
624
- #int(t*256/l)
625
- mask = cv2.floodFill(bg, None, (x, y), 1, 0, 256, (4 | cv2.FLOODFILL_FIXED_RANGE))[2] #(4 | cv2.FLOODFILL_FIXED_RANGE | cv2.FLOODFILL_MASK_ONLY | 255 << 8)
626
  # 255 << 8 tells to fill with the value 255)
627
- mask = mask[1:mask.shape[0]-1, 1:mask.shape[1]-1]
628
-
629
- d["layers"][0][mask>0] = (255,255,255,255)
630
 
631
  return gr.ImageEditor(value=d)
632
 
 
37
 
38
  blurin = "1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1"
39
  edge = []
 
40
  gradient = None
41
  params = { "fnum":0, "l":16 }
42
  dcolor = []
 
225
  cv2.imwrite(f"f{count}_dmap.png", depth_color)
226
  depth_frames.append(f"f{count}_dmap.png")
227
 
228
+ depth_gray = seg_frame(depth_gray)
229
+
230
  cv2.imwrite(f"f{count}_mask.png", depth_gray)
231
  masks.append(f"f{count}_mask.png")
232
  count += 1
 
415
  return data[0]["video"]["path"], data[1]["path"], data[2], data[3]["background"]["path"], data[4], data[5]
416
 
417
 
418
+ def seg_frame(newmask):
419
+
420
+ if depth.shape[0] == 2048: #height
421
+ gd = cv2.imread('./gradient_large.png', cv2.IMREAD_GRAYSCALE).astype(np.uint8)
422
+ elif depth.shape[0] == 1024:
423
+ gd = cv2.imread('./gradient.png', cv2.IMREAD_GRAYSCALE).astype(np.uint8)
424
+ else:
425
+ gd = cv2.imread('./gradient_small.png', cv2.IMREAD_GRAYSCALE).astype(np.uint8)
426
+
427
+ newmask[np.absolute(newmask.astype(np.int16)-gd.astype(np.int16))<16] = 0
428
+ ret,newmask = cv2.threshold(newmask,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
429
+
430
+ b = 1
431
+ d = 32
432
+ element = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2 * b + 1, 2 * b + 1), (b, b))
433
+ bd = cv2.erode(newmask, element)
434
+ element = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2 * d + 1, 2 * d + 1), (d, d))
435
+ bg = cv2.dilate(newmask, element)
436
+ bg[bg.shape[0]-64:bg.shape[0],0:bg.shape[1]] = 0
437
+
438
+ mask = np.zeros(depth.shape[:2],np.uint8)
439
+ # https://docs.opencv.org/4.x/d8/d83/tutorial_py_grabcut.html
440
+ # wherever it is marked white (sure foreground), change mask=1
441
+ # wherever it is marked black (sure background), change mask=0
442
+ mask[bg == 255] = 3
443
+ mask[bd == 255] = 1 #2: probable bg, 3: probable fg
444
+
445
+ return mask
446
+
447
+
448
  def select_frame(d, evt: gr.SelectData):
449
  global dcolor
450
  global frame_selected
451
+ global depths
452
  global masks
453
  global edge
454
 
455
  if evt.index != frame_selected:
456
  edge = []
 
 
457
  frame_selected = evt.index
458
 
459
  if len(dcolor) == 0:
 
461
  else:
462
  bg = "[" + str(dcolor[frame_selected])[1:-1] + ", 255]"
463
 
464
+ return depths[frame_selected], frame_selected, bg
465
 
466
  def switch_rows(v):
467
  global frames
 
546
  cv2.imwrite(masks[frame_selected], cv2.cvtColor(mask, cv2.COLOR_RGB2GRAY))
547
  return masks[frame_selected], depths
548
 
549
+ def apply_mask(d,b):
550
  global frames
551
  global frame_selected
552
  global masks
 
555
 
556
  edge = []
557
  mask = cv2.cvtColor(d["layers"][0], cv2.COLOR_RGBA2GRAY)
558
+ mask = (mask - 128).astype(np.uint8)
559
+
560
+ mask, bgdModel, fgdModel = cv2.grabCut(d["background"], mask, None,None,None,15, cv2.GC_INIT_WITH_MASK)
561
+ mask = np.where((mask==2)|(mask==0),1,0).astype('uint8')
562
+
 
 
 
 
 
 
 
 
 
 
 
 
563
  frame = cv2.imread(frames[frame_selected], cv2.IMREAD_UNCHANGED).astype(np.uint8)
564
+ frame[:, :, 3] = mask * 255
565
  cv2.imwrite(frames[frame_selected], frame)
566
+
567
+ mask = cv2.imread(masks[frame_selected], cv2.IMREAD_GRAYSCALE).astype(np.uint8) + 128
568
+ mask[mask==128] = 0
569
+ d["layers"][0] = cv2.cvtColor(mask, cv2.COLOR_GRAY2RGBA)
570
 
571
+ return gr.ImageEditor(value=d), depths, frames
 
572
 
573
  def draw_mask(l, t, b, v, d, evt: gr.EventData):
574
  global frames
 
578
  global masks
579
  global gradient
580
  global edge
 
581
 
582
  points = json.loads(v)
583
  pts = np.array(points, np.int32)
584
  pts = pts.reshape((-1,1,2))
585
 
586
  if len(edge) == 0 or params["fnum"] != frame_selected or params["l"] != l:
 
 
587
  if len(edge) > 0:
588
  d["background"] = cv2.imread(depths[frame_selected]).astype(np.uint8)
589
+ mask = cv2.imread(masks[frame_selected], cv2.IMREAD_GRAYSCALE).astype(np.uint8) + 128
590
+ mask[mask==128] = 0
 
 
 
 
 
591
 
592
  bg = cv2.cvtColor(d["background"], cv2.COLOR_RGBA2GRAY)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
593
  bg[bg==255] = 0
594
 
595
  params["fnum"] = frame_selected
596
  params["l"] = l
597
 
598
+ d["layers"][0] = cv2.cvtColor(mask, cv2.COLOR_GRAY2RGBA)
599
  edge = bg.copy()
600
  else:
601
  bg = edge.copy()
 
603
  x = points[len(points)-1][0]
604
  y = points[len(points)-1][1]
605
 
606
+ sel = cv2.floodFill(d["layers"][0], None, (x, y), 1, 2, 2, (4 | cv2.FLOODFILL_FIXED_RANGE))[2] #(4 | cv2.FLOODFILL_FIXED_RANGE | cv2.FLOODFILL_MASK_ONLY | 255 << 8)
 
607
  # 255 << 8 tells to fill with the value 255)
608
+ sel = sel[1:sel.shape[0]-1, 1:sel.shape[1]-1]
609
+
610
+ d["layers"][0][sel==0] = (0,0,0,0)
611
 
612
  return gr.ImageEditor(value=d)
613