freealise commited on
Commit
e926245
1 Parent(s): 41864e5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -5
app.py CHANGED
@@ -341,9 +341,7 @@ def draw_mask(ksize, th, v, d, evt: gr.EventData):
341
  vv = json.loads(v)
342
  pts = np.array(vv, np.int32)
343
  pts = pts.reshape((-1,1,2))
344
- d["layers"][0] = cv2.polylines(d["layers"][0],[pts],True,(0,0,0,255))
345
- d["composite"] = cv2.polylines(d["composite"],[pts],True,(0,0,0,255))
346
-
347
  d["layers"][0] = cv2.fillPoly(d["layers"][0], [pts], (0,0,0,255))
348
  d["composite"] = cv2.fillPoly(d["composite"], [pts], (0,0,0,255))
349
 
@@ -451,14 +449,35 @@ with gr.Blocks(css=css) as demo:
451
  evt.initEvent(\"input\", true, false);
452
  document.getElementById(\"mouse\").getElementsByTagName(\"textarea\")[0].dispatchEvent(evt);
453
 
454
- document.getElementById(\"select_cursor\").style.left = e.clientX;
455
- document.getElementById(\"select_cursor\").style.top = e.clientY;
456
  }
457
  document.getElementById(\"image_edit\").getElementsByTagName(\"canvas\")[0].onpointermove = function(e) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
458
  document.getElementById(\"mouse\").getElementsByTagName(\"textarea\")[0].value = \"[]\";
459
 
460
  document.getElementById(\"select_cursor\").style.left = 0;
461
  document.getElementById(\"select_cursor\").style.top = 0;
 
 
 
 
 
 
462
  }' title='select' style='text-decoration:none;color:white;'>⬚ Select polygon</a>""")
463
  mouse = gr.Textbox(elem_id="mouse", value="""[]""", interactive=False)
464
  output_mask.apply(fn=draw_mask, inputs=[ksize, threshold, mouse, output_mask], outputs=[output_mask])
 
341
  vv = json.loads(v)
342
  pts = np.array(vv, np.int32)
343
  pts = pts.reshape((-1,1,2))
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
 
 
449
  evt.initEvent(\"input\", true, false);
450
  document.getElementById(\"mouse\").getElementsByTagName(\"textarea\")[0].dispatchEvent(evt);
451
 
452
+ document.getElementById(\"select_cursor\").style.left = e.clientX + \"px\";
453
+ document.getElementById(\"select_cursor\").style.top = e.clientY + \"px\";
454
  }
455
  document.getElementById(\"image_edit\").getElementsByTagName(\"canvas\")[0].onpointermove = function(e) {
456
+ if (document.getElementById(\"select_cursor\").style.borderColor != \"#ffffff\") {
457
+ var x = parseInt((e.clientX-e.target.getBoundingClientRect().x)*e.target.width/e.target.getBoundingClientRect().width);
458
+ var y = parseInt((e.clientY-e.target.getBoundingClientRect().y)*e.target.height/e.target.getBoundingClientRect().height);
459
+
460
+ var p = document.getElementById(\"mouse\").getElementsByTagName(\"textarea\")[0].value.slice(1, -1);
461
+ if (p == \"\") {
462
+ p = \"[\" + x + \", \" + y + \"]\";
463
+ } else {
464
+ p = p.substr(0, p.lastIndexOf(\"[\"));
465
+ p += \"[\" + x + \", \" + y + \"]\";
466
+ }
467
+ document.getElementById(\"mouse\").getElementsByTagName(\"textarea\")[0].value = \"[\" + p + \"]\";
468
+ }
469
+ }
470
+ document.getElementById(\"image_edit\").getElementsByTagName(\"canvas\")[0].oncontextmenu = function(e) {
471
  document.getElementById(\"mouse\").getElementsByTagName(\"textarea\")[0].value = \"[]\";
472
 
473
  document.getElementById(\"select_cursor\").style.left = 0;
474
  document.getElementById(\"select_cursor\").style.top = 0;
475
+ }
476
+ document.getElementById(\"image_edit\").getElementsByTagName(\"canvas\")[0].onpointerdown = function(e) {
477
+ document.getElementById(\"select_cursor\").style.borderColor = \"#808080\";
478
+ }
479
+ document.getElementById(\"image_edit\").getElementsByTagName(\"canvas\")[0].onpointerup = function(e) {
480
+ document.getElementById(\"select_cursor\").style.borderColor = \"#ffffff\";
481
  }' title='select' style='text-decoration:none;color:white;'>⬚ Select polygon</a>""")
482
  mouse = gr.Textbox(elem_id="mouse", value="""[]""", interactive=False)
483
  output_mask.apply(fn=draw_mask, inputs=[ksize, threshold, mouse, output_mask], outputs=[output_mask])