freealise commited on
Commit
c0b01c5
1 Parent(s): 2a522dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -4
app.py CHANGED
@@ -347,14 +347,21 @@ def switch_rows(v):
347
  print(frames[0])
348
  return frames
349
 
350
- def draw_mask(th, v, d, evt: gr.EventData):
351
  print(v)
352
  vv = json.loads(v)
353
  pts = np.array(vv, np.int32)
354
  pts = pts.reshape((-1,1,2))
355
 
 
 
 
 
 
 
 
356
  bg = cv2.cvtColor(d["background"], cv2.COLOR_RGB2GRAY)
357
- gd = ((cv2.cvtColor(cv2.imread(masks[len(masks)-1]), cv2.COLOR_RGB2GRAY)-th)*2).astype(np.uint8)
358
  diff = abs(bg-gd).astype(np.uint8)
359
  mask = cv2.inRange(diff, 0, 5)
360
  d["background"][mask>0] = (0,0,0,255)
@@ -427,7 +434,7 @@ with gr.Blocks(css=css) as demo:
427
  output_switch = gr.Checkbox(label="Show depths")
428
  output_switch.input(fn=switch_rows, inputs=[output_switch], outputs=[output_frame])
429
  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")
430
- threshold = gr.Slider(value=127, minimum=1, maximum=255, step=1)
431
  selector = gr.HTML(value="""<div id='select_cursor' style='width:1px;height:1px;position:absolute;border:8px dotted #ffffff;opacity:50%;'></div>
432
  <a href='#' id='selector' onclick='this.style.fontWeight=\"bold\";
433
  document.body.appendChild(document.getElementById(\"select_cursor\"));
@@ -484,7 +491,7 @@ with gr.Blocks(css=css) as demo:
484
  document.getElementById(\"select_cursor\").style.top = 0;
485
  ' title='Clear selection' style='text-decoration:none;color:white;'>[x] Clear selection</a>""")
486
  mouse = gr.Textbox(elem_id="mouse", value="""[]""", interactive=False)
487
- output_mask.apply(fn=draw_mask, inputs=[threshold, mouse, output_mask], outputs=[output_mask])
488
  submit = gr.Button("Submit")
489
  with gr.Column():
490
  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, v, d, evt: gr.EventData):
351
  print(v)
352
  vv = json.loads(v)
353
  pts = np.array(vv, np.int32)
354
  pts = pts.reshape((-1,1,2))
355
 
356
+ if d["background"].shape[0] == 2048: #height
357
+ gradient = cv2.imread(f[0]).astype(np.uint8)
358
+ elif d["background"].shape[0] == 1024:
359
+ gradient = cv2.imread(f[1]).astype(np.uint8)
360
+ else:
361
+ gradient = cv2.imread(f[2]).astype(np.uint8)
362
+
363
  bg = cv2.cvtColor(d["background"], cv2.COLOR_RGB2GRAY)
364
+ gd = cv2.cvtColor(gradient, cv2.COLOR_RGB2GRAY)
365
  diff = abs(bg-gd).astype(np.uint8)
366
  mask = cv2.inRange(diff, 0, 5)
367
  d["background"][mask>0] = (0,0,0,255)
 
434
  output_switch = gr.Checkbox(label="Show depths")
435
  output_switch.input(fn=switch_rows, inputs=[output_switch], outputs=[output_frame])
436
  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")
437
+ gradients = gr.Files(value=['./gradient_large.png', './gradient.png', './gradient_small.png'])
438
  selector = gr.HTML(value="""<div id='select_cursor' style='width:1px;height:1px;position:absolute;border:8px dotted #ffffff;opacity:50%;'></div>
439
  <a href='#' id='selector' onclick='this.style.fontWeight=\"bold\";
440
  document.body.appendChild(document.getElementById(\"select_cursor\"));
 
491
  document.getElementById(\"select_cursor\").style.top = 0;
492
  ' title='Clear selection' style='text-decoration:none;color:white;'>[x] Clear selection</a>""")
493
  mouse = gr.Textbox(elem_id="mouse", value="""[]""", interactive=False)
494
+ output_mask.apply(fn=draw_mask, inputs=[gradients, mouse, output_mask], outputs=[output_mask])
495
  submit = gr.Button("Submit")
496
  with gr.Column():
497
  model_type = gr.Dropdown([("small", "vits"), ("base", "vitb"), ("large", "vitl")], type="value", value="vits", label='Model Type')