freealise commited on
Commit
31fd26a
1 Parent(s): 1814958

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -12
app.py CHANGED
@@ -337,10 +337,12 @@ def loadurl(url):
337
  def select_frame(v, evt: gr.SelectData):
338
  global frame_selected
339
  global masks
 
340
 
341
  if evt.index != frame_selected:
342
  masks[frame_selected] = v
343
  frame_selected = evt.index
 
344
 
345
  return masks[frame_selected], frame_selected
346
 
@@ -380,10 +382,8 @@ def draw_mask(l, c, k, v, d, evt: gr.EventData):
380
  delta = 0
381
  ddepth = cv2.CV_16S
382
 
383
- if len(d["layers"]) == 1 or params["fnum"] != frame_selected or params["l"] != l or params["c"] != c or params["k"] != k:
384
- if len(d["layers"]) == 1:
385
- d["layers"].append(np.zeros_like(d["layers"][0]))
386
- else:
387
  d["background"] = cv2.imread(masks[frame_selected]).astype(np.uint8)
388
 
389
  bg = cv2.cvtColor(d["background"], cv2.COLOR_RGBA2GRAY)
@@ -394,10 +394,10 @@ def draw_mask(l, c, k, v, d, evt: gr.EventData):
394
  #mask = cv2.filter2D(mask,-1,kernel)
395
  grad_x = cv2.convertScaleAbs(cv2.Sobel(mask, ddepth, 1, 0, ksize=1, scale=scale, delta=delta, borderType=cv2.BORDER_DEFAULT))
396
  grad_y = cv2.convertScaleAbs(cv2.Sobel(mask, ddepth, 0, 1, ksize=1, scale=scale, delta=delta, borderType=cv2.BORDER_DEFAULT))
397
- grad_z = cv2.convertScaleAbs(cv2.Sobel(mask, ddepth, 1, 1, ksize=1, scale=scale, delta=delta, borderType=cv2.BORDER_DEFAULT))
398
 
399
- grad = cv2.addWeighted(grad_x, 0.5, grad_y, 0.5, 0)
400
- grad = cv2.addWeighted(grad, 0.5, grad_z, 0.5, 0)
401
 
402
  mask = mask + cv2.inRange(grad, 1, 255)
403
 
@@ -417,13 +417,10 @@ def draw_mask(l, c, k, v, d, evt: gr.EventData):
417
  params["c"] = c
418
  params["k"] = k
419
 
420
- d["layers"][0] = cv2.cvtColor(bg, cv2.COLOR_GRAY2RGBA)
421
  edge = bg.copy()
422
  else:
423
  bg = edge.copy()
424
-
425
- #bg_ = cv2.copyMakeBorder(bg, 1, 1, 1, 1, cv2.BORDER_DEFAULT)
426
- #bg = np.zeros_like(bg)
427
 
428
  mask = cv2.floodFill(bg, None, (x, y), 1, 0, 0)[2] #, (4 | cv2.FLOODFILL_FIXED_RANGE | cv2.FLOODFILL_MASK_ONLY | 255 << 8)
429
  # 255 << 8 tells to fill with the value 255)
@@ -434,7 +431,7 @@ def draw_mask(l, c, k, v, d, evt: gr.EventData):
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"][1][mask>0] = (0,0,0,255)
438
  d["background"][mask>0] = (0,0,0,255)
439
 
440
  return gr.ImageEditor(value=d)
 
337
  def select_frame(v, evt: gr.SelectData):
338
  global frame_selected
339
  global masks
340
+ global edge
341
 
342
  if evt.index != frame_selected:
343
  masks[frame_selected] = v
344
  frame_selected = evt.index
345
+ edge = None
346
 
347
  return masks[frame_selected], frame_selected
348
 
 
382
  delta = 0
383
  ddepth = cv2.CV_16S
384
 
385
+ if edge == None or params["fnum"] != frame_selected or params["l"] != l or params["c"] != c or params["k"] != k:
386
+ if edge != None:
 
 
387
  d["background"] = cv2.imread(masks[frame_selected]).astype(np.uint8)
388
 
389
  bg = cv2.cvtColor(d["background"], cv2.COLOR_RGBA2GRAY)
 
394
  #mask = cv2.filter2D(mask,-1,kernel)
395
  grad_x = cv2.convertScaleAbs(cv2.Sobel(mask, ddepth, 1, 0, ksize=1, scale=scale, delta=delta, borderType=cv2.BORDER_DEFAULT))
396
  grad_y = cv2.convertScaleAbs(cv2.Sobel(mask, ddepth, 0, 1, ksize=1, scale=scale, delta=delta, borderType=cv2.BORDER_DEFAULT))
397
+ grad = cv2.convertScaleAbs(cv2.Sobel(mask, ddepth, 1, 1, ksize=1, scale=scale, delta=delta, borderType=cv2.BORDER_DEFAULT))
398
 
399
+ grad_y = cv2.addWeighted(grad_x, 0.5, grad_y, 0.5, 0)
400
+ grad = cv2.addWeighted(grad_y, 0.5, grad, 0.5, 0)
401
 
402
  mask = mask + cv2.inRange(grad, 1, 255)
403
 
 
417
  params["c"] = c
418
  params["k"] = k
419
 
420
+ #d["layers"][0] = cv2.cvtColor(bg, cv2.COLOR_GRAY2RGBA)
421
  edge = bg.copy()
422
  else:
423
  bg = edge.copy()
 
 
 
424
 
425
  mask = cv2.floodFill(bg, None, (x, y), 1, 0, 0)[2] #, (4 | cv2.FLOODFILL_FIXED_RANGE | cv2.FLOODFILL_MASK_ONLY | 255 << 8)
426
  # 255 << 8 tells to fill with the value 255)
 
431
  mask[cv2.inRange(grad, 1, 255)>0] = 1
432
  mask = mask[1:mask.shape[0]-1, 1:mask.shape[1]-1]
433
 
434
+ d["layers"][0][mask>0] = (0,0,0,255)
435
  d["background"][mask>0] = (0,0,0,255)
436
 
437
  return gr.ImageEditor(value=d)