freealise commited on
Commit
b166ebd
1 Parent(s): db98133

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -6
app.py CHANGED
@@ -336,7 +336,7 @@ def switch_rows(v):
336
  print(frames[0])
337
  return frames
338
 
339
- def draw_mask(v, d, evt: gr.EventData):
340
  print(v)
341
  vv = v.split(", ")
342
  x = int(vv[0])
@@ -349,16 +349,15 @@ def draw_mask(v, d, evt: gr.EventData):
349
  ddepth = cv2.CV_16S
350
 
351
  gray = cv2.cvtColor(d["background"], cv2.COLOR_BGR2GRAY)
352
- grad_x = cv2.Sobel(gray, ddepth, 1, 0, ksize=5, scale=scale, delta=delta, borderType=cv2.BORDER_DEFAULT)
353
  # Gradient-Y
354
  # grad_y = cv2.Scharr(gray,ddepth,0,1)
355
- grad_y = cv2.Sobel(gray, ddepth, 0, 1, ksize=5, scale=scale, delta=delta, borderType=cv2.BORDER_DEFAULT)
356
  abs_grad_x = cv2.convertScaleAbs(grad_x)
357
  abs_grad_y = cv2.convertScaleAbs(grad_y)
358
 
359
  grad = cv2.addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0)
360
- thresh = 127
361
- bw = cv2.threshold(255-grad, thresh, 255, cv2.THRESH_BINARY)[1]
362
 
363
  d["background"] = cv2.cvtColor(bw, cv2.COLOR_GRAY2RGB)
364
  d["composite"] = cv2.cvtColor(bw, cv2.COLOR_GRAY2RGB)
@@ -416,6 +415,8 @@ with gr.Blocks(css=css) as demo:
416
  output_switch = gr.Checkbox(label="Show depths")
417
  output_switch.input(fn=switch_rows, inputs=[output_switch], outputs=[output_frame])
418
  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")
 
 
419
  selector = gr.HTML(value="""<div id='select_frame' style='position:absolute;border:1px dashed #808080;width:0;height:0'></div>
420
  <input type='number' id='mousedown' style='width:10em;color:auto;background-color:transparent;border:1px solid lightgray;'/>
421
  <input type='number' id='mouseup' style='width:10em;color:auto;background-color:transparent;border:1px solid lightgray;'/>
@@ -455,7 +456,7 @@ with gr.Blocks(css=css) as demo:
455
  document.getElementById(\"mouse\").getElementsByTagName(\"textarea\")[0].dispatchEvent(evt);
456
  }' title='select'>⬚</a>""")
457
  mouse = gr.Textbox(elem_id="mouse", value="""0, 0, 0, 0""")
458
- output_mask.apply(fn=draw_mask, inputs=[mouse, output_mask], outputs=[output_mask])
459
  submit = gr.Button("Submit")
460
  with gr.Column():
461
  model_type = gr.Dropdown([("small", "vits"), ("base", "vitb"), ("large", "vitl")], type="value", value="vits", label='Model Type')
 
336
  print(frames[0])
337
  return frames
338
 
339
+ def draw_mask(ksize, th, v, d, evt: gr.EventData):
340
  print(v)
341
  vv = v.split(", ")
342
  x = int(vv[0])
 
349
  ddepth = cv2.CV_16S
350
 
351
  gray = cv2.cvtColor(d["background"], cv2.COLOR_BGR2GRAY)
352
+ grad_x = cv2.Sobel(gray, ddepth, 1, 0, ksize=ksize, scale=scale, delta=delta, borderType=cv2.BORDER_DEFAULT)
353
  # Gradient-Y
354
  # grad_y = cv2.Scharr(gray,ddepth,0,1)
355
+ grad_y = cv2.Sobel(gray, ddepth, 0, 1, ksize=ksize, scale=scale, delta=delta, borderType=cv2.BORDER_DEFAULT)
356
  abs_grad_x = cv2.convertScaleAbs(grad_x)
357
  abs_grad_y = cv2.convertScaleAbs(grad_y)
358
 
359
  grad = cv2.addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0)
360
+ bw = cv2.threshold(255-grad, th, 255, cv2.THRESH_BINARY)[1]
 
361
 
362
  d["background"] = cv2.cvtColor(bw, cv2.COLOR_GRAY2RGB)
363
  d["composite"] = cv2.cvtColor(bw, cv2.COLOR_GRAY2RGB)
 
415
  output_switch = gr.Checkbox(label="Show depths")
416
  output_switch.input(fn=switch_rows, inputs=[output_switch], outputs=[output_frame])
417
  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")
418
+ ksize = gr.Slider(label="Kernel", value=5, minimum=1, maximum=15, step=2)
419
+ threshold = gr.Slider(label="Threshold", value=127, minimum=0, maximum=255, step=1)
420
  selector = gr.HTML(value="""<div id='select_frame' style='position:absolute;border:1px dashed #808080;width:0;height:0'></div>
421
  <input type='number' id='mousedown' style='width:10em;color:auto;background-color:transparent;border:1px solid lightgray;'/>
422
  <input type='number' id='mouseup' style='width:10em;color:auto;background-color:transparent;border:1px solid lightgray;'/>
 
456
  document.getElementById(\"mouse\").getElementsByTagName(\"textarea\")[0].dispatchEvent(evt);
457
  }' title='select'>⬚</a>""")
458
  mouse = gr.Textbox(elem_id="mouse", value="""0, 0, 0, 0""")
459
+ output_mask.apply(fn=draw_mask, inputs=[ksize, threshold, mouse, output_mask], outputs=[output_mask])
460
  submit = gr.Button("Submit")
461
  with gr.Column():
462
  model_type = gr.Dropdown([("small", "vits"), ("base", "vitb"), ("large", "vitl")], type="value", value="vits", label='Model Type')