|
| 1 | +<html> |
| 2 | + <!-- <script type="text/javascript" src="../../dist/webworker-worker.js"></script> --> |
| 3 | + <body> |
| 4 | + <p> |
| 5 | + To run this file, start a server in the autoseg directory, e.g. "python -m |
| 6 | + http.server") then navigate to |
| 7 | + http://localhost:8000/tests/webworker/webworker.html |
| 8 | + </p> |
| 9 | + <img |
| 10 | + id="img" |
| 11 | + src="https://s3.amazonaws.com/datasets.workaround.online/hands/01cb3085-3d42-4186-b52c-dcc52d9beb4c.mp4_frame012.jpg" |
| 12 | + /> |
| 13 | + <script type="text/javascript"> |
| 14 | + let lastRequestId = -1 |
| 15 | + |
| 16 | + const blob = new Blob( |
| 17 | + [ |
| 18 | + "importScripts('https://unpkg.com/[email protected]/webworker-worker-bundle.js')", |
| 19 | + ], |
| 20 | + { type: "application/javascript" } |
| 21 | + ) |
| 22 | + const blobUrl = window.URL.createObjectURL(blob) |
| 23 | + const webworker = new Worker(blobUrl) |
| 24 | + |
| 25 | + webworker.addEventListener("message", console.log) |
| 26 | + const functions = ["setConfig", "loadImage", "getMask"] |
| 27 | + |
| 28 | + const obj = {} |
| 29 | + |
| 30 | + for (const functionName of functions) { |
| 31 | + const id = lastRequestId++ |
| 32 | + obj[functionName] = (...args) => { |
| 33 | + webworker.postMessage({ functionName, args, id }) |
| 34 | + return new Promise((resolve, reject) => { |
| 35 | + const possibleResponse = (e) => { |
| 36 | + const { data } = e |
| 37 | + if (!data) return |
| 38 | + if (data.id === id) { |
| 39 | + webworker.removeEventListener("message", possibleResponse) |
| 40 | + if (data.error) { |
| 41 | + reject(new Error(data.error)) |
| 42 | + } else { |
| 43 | + // There's some issue with the serialization of the ImageData |
| 44 | + // for getMask |
| 45 | + if (functionName === "getMask") { |
| 46 | + const { data: pixels, width, height } = data.returnValue |
| 47 | + resolve(new ImageData(pixels, width, height)) |
| 48 | + } else { |
| 49 | + resolve(data.returnValue) |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + webworker.addEventListener("message", possibleResponse) |
| 55 | + }) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + window.autoseg = obj |
| 60 | + |
| 61 | + // ----------------------------- |
| 62 | + // APPLICATION CODE |
| 63 | + // ----------------------------- |
| 64 | + |
| 65 | + const img = document.getElementById("img") |
| 66 | + img.crossOrigin = "anonymous" |
| 67 | + |
| 68 | + img.onload = () => { |
| 69 | + const canvas = document.createElement("canvas") |
| 70 | + canvas.width = img.naturalWidth |
| 71 | + canvas.height = img.naturalHeight |
| 72 | + document.body.appendChild(canvas) |
| 73 | + const context = canvas.getContext("2d") |
| 74 | + |
| 75 | + context.drawImage(img, 0, 0) |
| 76 | + |
| 77 | + const imageData = context.getImageData( |
| 78 | + 0, |
| 79 | + 0, |
| 80 | + img.naturalWidth, |
| 81 | + img.naturalHeight |
| 82 | + ) |
| 83 | + |
| 84 | + async function doStuff() { |
| 85 | + await window.autoseg.setConfig({ |
| 86 | + // mode: "simple", |
| 87 | + maxClusters: 10000, |
| 88 | + }) |
| 89 | + await window.autoseg.loadImage(imageData) |
| 90 | + const maskImageData = await window.autoseg.getMask([ |
| 91 | + { |
| 92 | + regionType: "polygon", |
| 93 | + cls: 0, |
| 94 | + points: [ |
| 95 | + { |
| 96 | + x: 0.48117539026629935, |
| 97 | + y: 0.9876543209876543, |
| 98 | + }, |
| 99 | + { |
| 100 | + x: 0.4986225895316804, |
| 101 | + y: 0.5126007550249975, |
| 102 | + }, |
| 103 | + { |
| 104 | + x: 0.4701561065197429, |
| 105 | + y: 0.3803693500663198, |
| 106 | + }, |
| 107 | + { |
| 108 | + x: 0.43067033976124885, |
| 109 | + y: 0.3624119987756351, |
| 110 | + }, |
| 111 | + { |
| 112 | + x: 0.40955004591368227, |
| 113 | + y: 0.3101724313845526, |
| 114 | + }, |
| 115 | + { |
| 116 | + x: 0.32966023875114786, |
| 117 | + y: 0.31833486378940923, |
| 118 | + }, |
| 119 | + { |
| 120 | + x: 0.3076216712580349, |
| 121 | + y: 0.28405264768901134, |
| 122 | + }, |
| 123 | + { |
| 124 | + x: 0.37373737373737376, |
| 125 | + y: 0.262830323436384, |
| 126 | + }, |
| 127 | + { |
| 128 | + x: 0.4214876033057851, |
| 129 | + y: 0.1828384858687889, |
| 130 | + }, |
| 131 | + { |
| 132 | + x: 0.3149678604224059, |
| 133 | + y: 0.17957351290684623, |
| 134 | + }, |
| 135 | + { |
| 136 | + x: 0.317722681359045, |
| 137 | + y: 0.15671870217324763, |
| 138 | + }, |
| 139 | + { |
| 140 | + x: 0.42056932966023874, |
| 141 | + y: 0.14202632384450567, |
| 142 | + }, |
| 143 | + { |
| 144 | + x: 0.480257116620753, |
| 145 | + y: 0.21385572900724417, |
| 146 | + }, |
| 147 | + { |
| 148 | + x: 0.5528007346189164, |
| 149 | + y: 0.4244464850525457, |
| 150 | + }, |
| 151 | + { |
| 152 | + x: 0.6776859504132231, |
| 153 | + y: 0.966431996735027, |
| 154 | + }, |
| 155 | + ], |
| 156 | + }, |
| 157 | + { |
| 158 | + regionType: "polygon", |
| 159 | + cls: 1, |
| 160 | + points: [ |
| 161 | + { |
| 162 | + x: 0.3581267217630854, |
| 163 | + y: 0.9762269156208551, |
| 164 | + }, |
| 165 | + { |
| 166 | + x: 0.41597796143250687, |
| 167 | + y: 0.435873890419345, |
| 168 | + }, |
| 169 | + { |
| 170 | + x: 0.26905417814508725, |
| 171 | + y: 0.3787368635853484, |
| 172 | + }, |
| 173 | + { |
| 174 | + x: 0.2892561983471074, |
| 175 | + y: 0.2660952963983267, |
| 176 | + }, |
| 177 | + { |
| 178 | + x: 0.36455463728191, |
| 179 | + y: 0.22528313437404346, |
| 180 | + }, |
| 181 | + { |
| 182 | + x: 0.2910927456382002, |
| 183 | + y: 0.20732578308335883, |
| 184 | + }, |
| 185 | + { |
| 186 | + x: 0.24517906336088155, |
| 187 | + y: 0.1599836751351903, |
| 188 | + }, |
| 189 | + { |
| 190 | + x: 0.4361799816345271, |
| 191 | + y: 0.019589837771655953, |
| 192 | + }, |
| 193 | + { |
| 194 | + x: 0.24058769513314968, |
| 195 | + y: 0.024487297214569943, |
| 196 | + }, |
| 197 | + { |
| 198 | + x: 0.04040404040404041, |
| 199 | + y: 0.04244464850525457, |
| 200 | + }, |
| 201 | + { |
| 202 | + x: 0.011937557392102846, |
| 203 | + y: 0.9370472400775431, |
| 204 | + }, |
| 205 | + ], |
| 206 | + }, |
| 207 | + { |
| 208 | + regionType: "polygon", |
| 209 | + cls: 1, |
| 210 | + points: [ |
| 211 | + { |
| 212 | + x: 0.7658402203856749, |
| 213 | + y: 0.971329456177941, |
| 214 | + }, |
| 215 | + { |
| 216 | + x: 0.6749311294765841, |
| 217 | + y: 0.6203448627691052, |
| 218 | + }, |
| 219 | + { |
| 220 | + x: 0.45362718089990817, |
| 221 | + y: 0.03101724313845526, |
| 222 | + }, |
| 223 | + { |
| 224 | + x: 0.9623507805325987, |
| 225 | + y: 0.06856443220079583, |
| 226 | + }, |
| 227 | + { |
| 228 | + x: 0.9338842975206612, |
| 229 | + y: 0.9647995102540557, |
| 230 | + }, |
| 231 | + ], |
| 232 | + }, |
| 233 | + ]) |
| 234 | + context.putImageData(maskImageData, 0, 0) |
| 235 | + } |
| 236 | + doStuff().catch((err) => { |
| 237 | + console.error(err) |
| 238 | + }) |
| 239 | + } |
| 240 | + </script> |
| 241 | + </body> |
| 242 | +</html> |
0 commit comments