freealise commited on
Commit
93dce2b
1 Parent(s): e057879

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -42
app.py CHANGED
@@ -336,7 +336,7 @@ def switch_rows(v):
336
  print(frames[0])
337
  return frames
338
 
339
- def draw_mask(ksize, th, v, d, evt: gr.EventData):
340
  print(v)
341
  vv = json.loads(v)
342
  pts = np.array(vv, np.int32)
@@ -344,36 +344,6 @@ def draw_mask(ksize, th, v, d, evt: gr.EventData):
344
 
345
  d["layers"][0] = cv2.fillPoly(d["layers"][0], [pts], (0,0,0,255))
346
  d["composite"] = cv2.fillPoly(d["composite"], [pts], (0,0,0,255))
347
-
348
- img = cv2.cvtColor(d["background"], cv2.COLOR_RGB2GRAY)
349
- rows, cols = img.shape
350
- crow, ccol = rows//2, cols//2
351
- f = np.fft.fft2(img)
352
- fshift = np.fft.fftshift(f)
353
- fshift[crow-int(ksize/2):crow+int(ksize/2+1), ccol-int(ksize/2):ccol+int(ksize/2+1)] = 0
354
- f_ishift = np.fft.ifftshift(fshift)
355
- img_back = np.fft.ifft2(f_ishift)
356
- img_back = np.real(img_back).astype(np.uint8)
357
-
358
-
359
- #scale = 1
360
- #delta = 0
361
- #ddepth = cv2.CV_16S
362
-
363
- #gray = cv2.cvtColor(d["background"], cv2.COLOR_BGR2GRAY)
364
- #grad_x = cv2.Sobel(gray, ddepth, 1, 0, ksize=ksize, scale=scale, delta=delta, borderType=cv2.BORDER_DEFAULT)
365
- # Gradient-Y
366
- # grad_y = cv2.Scharr(gray,ddepth,0,1)
367
- #grad_y = cv2.Sobel(gray, ddepth, 0, 1, ksize=ksize, scale=scale, delta=delta, borderType=cv2.BORDER_DEFAULT)
368
- #abs_grad_x = cv2.convertScaleAbs(grad_x)
369
- #abs_grad_y = cv2.convertScaleAbs(grad_y)
370
-
371
- #grad = cv2.addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0)
372
- #bw = cv2.cvtColor(cv2.threshold(255-img_back, th, 255, cv2.THRESH_BINARY)[1], cv2.COLOR_GRAY2RGB)
373
- bw = cv2.cvtColor(255-img_back, cv2.COLOR_GRAY2RGB)
374
-
375
- d["background"] = bw
376
- d["composite"] = bw
377
  print(d["composite"])
378
 
379
  return gr.ImageEditor(value=d)
@@ -428,9 +398,7 @@ with gr.Blocks(css=css) as demo:
428
  output_switch = gr.Checkbox(label="Show depths")
429
  output_switch.input(fn=switch_rows, inputs=[output_switch], outputs=[output_frame])
430
  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")
431
- ksize = gr.Slider(label="Kernel", value=128, minimum=0, maximum=1024, step=1)
432
- threshold = gr.Slider(label="Threshold", value=128, minimum=0, maximum=255, step=1)
433
- selector = gr.HTML(value="""<div id='select_cursor' style='width:1px;height:1px;position:absolute;border:3px dotted #ffffff;'></div>
434
  <a href='#' id='selector' onclick='this.style.fontWeight=\"bold\";
435
  document.body.appendChild(document.getElementById(\"select_cursor\"));
436
  document.getElementById(\"image_edit\").getElementsByTagName(\"canvas\")[0].oncontextmenu = function(e){e.preventDefault();}
@@ -474,20 +442,19 @@ with gr.Blocks(css=css) as demo:
474
  document.getElementById(\"select_cursor\").style.top = e.clientY-1 + \"px\";
475
  }
476
  }
477
- document.getElementById(\"image_edit\").getElementsByTagName(\"canvas\")[0].onblur = function(e) {
478
- document.getElementById(\"mouse\").getElementsByTagName(\"textarea\")[0].value = \"[]\";
479
-
480
- document.getElementById(\"select_cursor\").style.left = 0;
481
- document.getElementById(\"select_cursor\").style.top = 0;
482
- }
483
  document.getElementById(\"image_edit\").getElementsByTagName(\"canvas\")[0].onpointerdown = function(e) {
484
  document.getElementById(\"select_cursor\").style.borderColor = \"#808080\";
485
  }
486
  document.getElementById(\"image_edit\").getElementsByTagName(\"canvas\")[0].onpointerup = function(e) {
487
  document.getElementById(\"select_cursor\").style.borderColor = \"#ffffff\";
488
- }' title='select' style='text-decoration:none;color:white;'>⬚ Select polygon</a>""")
 
 
 
 
 
489
  mouse = gr.Textbox(elem_id="mouse", value="""[]""", interactive=False)
490
- output_mask.apply(fn=draw_mask, inputs=[ksize, threshold, mouse, output_mask], outputs=[output_mask])
491
  submit = gr.Button("Submit")
492
  with gr.Column():
493
  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(v, d, evt: gr.EventData):
340
  print(v)
341
  vv = json.loads(v)
342
  pts = np.array(vv, np.int32)
 
344
 
345
  d["layers"][0] = cv2.fillPoly(d["layers"][0], [pts], (0,0,0,255))
346
  d["composite"] = cv2.fillPoly(d["composite"], [pts], (0,0,0,255))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
347
  print(d["composite"])
348
 
349
  return gr.ImageEditor(value=d)
 
398
  output_switch = gr.Checkbox(label="Show depths")
399
  output_switch.input(fn=switch_rows, inputs=[output_switch], outputs=[output_frame])
400
  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")
401
+ selector = gr.HTML(value="""<div id='select_cursor' style='width:1px;height:1px;position:absolute;border:8px dotted #ffffff;'></div>
 
 
402
  <a href='#' id='selector' onclick='this.style.fontWeight=\"bold\";
403
  document.body.appendChild(document.getElementById(\"select_cursor\"));
404
  document.getElementById(\"image_edit\").getElementsByTagName(\"canvas\")[0].oncontextmenu = function(e){e.preventDefault();}
 
442
  document.getElementById(\"select_cursor\").style.top = e.clientY-1 + \"px\";
443
  }
444
  }
 
 
 
 
 
 
445
  document.getElementById(\"image_edit\").getElementsByTagName(\"canvas\")[0].onpointerdown = function(e) {
446
  document.getElementById(\"select_cursor\").style.borderColor = \"#808080\";
447
  }
448
  document.getElementById(\"image_edit\").getElementsByTagName(\"canvas\")[0].onpointerup = function(e) {
449
  document.getElementById(\"select_cursor\").style.borderColor = \"#ffffff\";
450
+ }' title='select' style='text-decoration:none;color:white;'>⬚ Select polygon</a> <a href='#' id='clear_select' onclick='
451
+ document.getElementById(\"mouse\").getElementsByTagName(\"textarea\")[0].value = \"[]\";
452
+
453
+ document.getElementById(\"select_cursor\").style.left = 0;
454
+ document.getElementById(\"select_cursor\").style.top = 0;
455
+ '>[x] Clear selection</a>""")
456
  mouse = gr.Textbox(elem_id="mouse", value="""[]""", interactive=False)
457
+ output_mask.apply(fn=draw_mask, inputs=[mouse, output_mask], outputs=[output_mask])
458
  submit = gr.Button("Submit")
459
  with gr.Column():
460
  model_type = gr.Dropdown([("small", "vits"), ("base", "vitb"), ("large", "vitl")], type="value", value="vits", label='Model Type')