From da65f74e88e8ccf5da00d43ab060096bd2ea5e86 Mon Sep 17 00:00:00 2001 From: srikeerthis Date: Fri, 3 Apr 2026 17:14:54 -0700 Subject: [PATCH 1/2] feat: addition of text box and button to load existing coordinates of polygon --- index.html | 4 ++++ styles.css | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/index.html b/index.html index 4e0655e..01c1673 100644 --- a/index.html +++ b/index.html @@ -47,6 +47,10 @@

How to use

  • Click to draw polygon points. Press enter to finish the polygon.
  • Coordinates

    +

    Load existing Polygon coordinates

    + + Load Polygon +

    Copy the points below, formatted as NumPy arrays, into your Python code.

    Copy Python to Clipboard diff --git a/styles.css b/styles.css index 2df8a20..e3e606c 100644 --- a/styles.css +++ b/styles.css @@ -309,4 +309,29 @@ li { .mt-3 { margin-top: 24px; +} + +#load-coordinates{ + width: 100%; + height: 120px; + background-color: rgba(17,24,39,0.1); + border: 2px solid transparent; + border-radius: 4px; + padding:10px; + box-sizing: border-box; + font-family: monospace; + font-size: 14px; + resize: vertical; + margin-bottom: 12px; + color: #1f2937; + transition: outline 0.15s ease-in-out; +} + +#load-coordinates:focus{ + outline: 2px solid #7733f4; +} + +#loadCoordinates{ + display: table; + margin-bottom: 30px; } \ No newline at end of file From 746624cf210fd5565446819bb198a0ce789d4df9 Mon Sep 17 00:00:00 2001 From: srikeerthis Date: Fri, 3 Apr 2026 18:37:26 -0700 Subject: [PATCH 2/2] add coordinate parsing and loading logic to script.js --- script.js | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 86 insertions(+), 1 deletion(-) diff --git a/script.js b/script.js index 5e068b3..14713a8 100644 --- a/script.js +++ b/script.js @@ -712,4 +712,89 @@ window.addEventListener('keydown', function(e) { if (e.key === 'f' || e.key === 'F') { toggleFullscreen(); } -}) \ No newline at end of file +}) + +document.querySelector('#loadCoordinates').addEventListener('click',function(e) { + e.preventDefault(); + + if(!img || !img.complete || img.naturalWidth === 0){ + alert("Please upload an image before loading points"); + } + + var inputVal = document.querySelector('#load-coordinates').value.trim(); + if(!inputVal) return; + + try{ + // Sanitize input + var sanitized = inputVal; + sanitized = sanitized.replace(/np\.array\(/g,'').replace(/\)/g,''); + + if(sanitized.startsWith('{') && sanitized.endsWith('}')){ + sanitized = '[' + sanitized.substring(1,sanitized.length-1)+']'; + } + sanitized = sanitized.replace(/,\s*([\]}])/g,'$1'); + var parsed = JSON.parse(sanitized); + + // convert object based points into array pairs + const objectToArray=(arr) => { + if(Array.isArray(arr)){ + if(arr.length >0 && typeof arr[0] === 'object' && arr[0] !== null && 'x' in arr[0]) { + return arr.map(point => [point.x,point.y]); + } + return arr.map(objectToArray); + } + return arr; + }; + parsed = objectToArray(parsed); + + if(parsed.length > 0 && Array.isArray(parsed[0]) && !Array.isArray(parsed[0][0])){ + parsed = [parsed]; + } + + if(!Array.isArray(parsed) || !Array.isArray(parsed[0]) ||!Array.isArray(parsed[0][0])){ + throw new Error("Invalid coordinate structure. Needs to be a list of polygons or single polygon."); + } + + // Detect and convert normalized points (0-1) to absolute pixels + var isNormalized = true; + for (var i=0;i 1 || parsed[i][j][1]>1){ + isNormalized = false; + break; + } + } + if(!isNormalized) break; + } + + if(isNormalized){ + for(var i=0; i