@whereby.com/camera-effects 0.0.0-canary-20251107145955
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENCE.md +23 -0
- package/README.md +4 -0
- package/dist/assets/backgrounds/brick-720p.jpg +0 -0
- package/dist/assets/backgrounds/bubbles.mp4 +0 -0
- package/dist/assets/backgrounds/cabin-720p.jpg +0 -0
- package/dist/assets/backgrounds/clay.png +0 -0
- package/dist/assets/backgrounds/concrete-720p.jpg +0 -0
- package/dist/assets/backgrounds/day-720p.png +0 -0
- package/dist/assets/backgrounds/focus.png +0 -0
- package/dist/assets/backgrounds/glow.png +0 -0
- package/dist/assets/backgrounds/haven.png +0 -0
- package/dist/assets/backgrounds/neon.mp4 +0 -0
- package/dist/assets/backgrounds/night-720p.png +0 -0
- package/dist/assets/backgrounds/pulse.png +0 -0
- package/dist/assets/backgrounds/studio.png +0 -0
- package/dist/assets/backgrounds/sunrise-720p.png +0 -0
- package/dist/assets/models/segm_full_v679.tflite +0 -0
- package/dist/assets/models/segm_lite_v681.tflite +0 -0
- package/dist/assets/models/selfiesegmentation_mlkit-256x256-2021_01_19-v1215.f16.tflite +0 -0
- package/dist/assets/tflite/models/segm_full_v679.tflite +0 -0
- package/dist/assets/tflite/models/segm_lite_v681.tflite +0 -0
- package/dist/assets/tflite/models/selfiesegmentation_mlkit-256x256-2021_01_19-v1215.f16.tflite +0 -0
- package/dist/assets/tflite/tflite-simd.wasm +0 -0
- package/dist/assets/tflite/tflite.wasm +0 -0
- package/dist/index.cjs +3693 -0
- package/dist/index.d.cts +63 -0
- package/dist/index.d.mts +63 -0
- package/dist/index.d.ts +63 -0
- package/dist/index.mjs +3686 -0
- package/dist/legacy-esm.js +3686 -0
- package/dist/workers/ProcessorProxy.worker.js +3507 -0
- package/dist/workers/assets/backgrounds/brick-720p.jpg +0 -0
- package/dist/workers/assets/backgrounds/bubbles.mp4 +0 -0
- package/dist/workers/assets/backgrounds/cabin-720p.jpg +0 -0
- package/dist/workers/assets/backgrounds/clay.png +0 -0
- package/dist/workers/assets/backgrounds/concrete-720p.jpg +0 -0
- package/dist/workers/assets/backgrounds/day-720p.png +0 -0
- package/dist/workers/assets/backgrounds/focus.png +0 -0
- package/dist/workers/assets/backgrounds/glow.png +0 -0
- package/dist/workers/assets/backgrounds/haven.png +0 -0
- package/dist/workers/assets/backgrounds/neon.mp4 +0 -0
- package/dist/workers/assets/backgrounds/night-720p.png +0 -0
- package/dist/workers/assets/backgrounds/pulse.png +0 -0
- package/dist/workers/assets/backgrounds/studio.png +0 -0
- package/dist/workers/assets/backgrounds/sunrise-720p.png +0 -0
- package/dist/workers/assets/models/segm_full_v679.tflite +0 -0
- package/dist/workers/assets/models/segm_lite_v681.tflite +0 -0
- package/dist/workers/assets/models/selfiesegmentation_mlkit-256x256-2021_01_19-v1215.f16.tflite +0 -0
- package/dist/workers/assets/tflite/tflite-simd.wasm +0 -0
- package/dist/workers/assets/tflite/tflite.wasm +0 -0
- package/dist/workers/timer.worker.js +13 -0
- package/package.json +44 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,3693 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var adapter = require('webrtc-adapter');
|
|
4
|
+
var events = require('events');
|
|
5
|
+
var wasmFeatureDetect = require('wasm-feature-detect');
|
|
6
|
+
|
|
7
|
+
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
|
|
8
|
+
/******************************************************************************
|
|
9
|
+
Copyright (c) Microsoft Corporation.
|
|
10
|
+
|
|
11
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
12
|
+
purpose with or without fee is hereby granted.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
15
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
16
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
17
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
18
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
19
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
20
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
21
|
+
***************************************************************************** */
|
|
22
|
+
/* global Reflect, Promise, SuppressedError, Symbol, Iterator */
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
function __rest(s, e) {
|
|
26
|
+
var t = {};
|
|
27
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
28
|
+
t[p] = s[p];
|
|
29
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
30
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
31
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
32
|
+
t[p[i]] = s[p[i]];
|
|
33
|
+
}
|
|
34
|
+
return t;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
38
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
39
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
40
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
41
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
42
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
43
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
48
|
+
var e = new Error(message);
|
|
49
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
function fixBackgroundUrlPromise(params) {
|
|
53
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
54
|
+
if (params.backgroundUrl && params.backgroundUrl.then) {
|
|
55
|
+
const src = yield params.backgroundUrl;
|
|
56
|
+
const candidate = (src === null || src === void 0 ? void 0 : src.default) || src;
|
|
57
|
+
if (typeof candidate === "string") {
|
|
58
|
+
try {
|
|
59
|
+
if (!/^https?:\/\//.test(candidate) &&
|
|
60
|
+
!candidate.startsWith("data:") &&
|
|
61
|
+
!candidate.startsWith("blob:")) {
|
|
62
|
+
params.backgroundUrl = new URL(candidate, (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href))).href;
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
params.backgroundUrl = candidate;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
catch (_a) {
|
|
69
|
+
params.backgroundUrl = candidate;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
params.backgroundUrl = candidate;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
function createBackgroundElement(url, type) {
|
|
79
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
80
|
+
if (!url)
|
|
81
|
+
return;
|
|
82
|
+
let backgroundElement;
|
|
83
|
+
if (type === "video") {
|
|
84
|
+
backgroundElement = document.createElement("video");
|
|
85
|
+
backgroundElement.loop = true;
|
|
86
|
+
yield new Promise((resolve) => {
|
|
87
|
+
backgroundElement.addEventListener("playing", resolve);
|
|
88
|
+
backgroundElement.crossOrigin = "Anonymous";
|
|
89
|
+
backgroundElement.src = url;
|
|
90
|
+
backgroundElement.play();
|
|
91
|
+
});
|
|
92
|
+
backgroundElement.muted = true;
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
backgroundElement = new Image();
|
|
96
|
+
yield new Promise((resolve) => {
|
|
97
|
+
backgroundElement.addEventListener("load", resolve);
|
|
98
|
+
backgroundElement.crossOrigin = "Anonymous";
|
|
99
|
+
backgroundElement.src = url;
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
return backgroundElement;
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
function createBackgroundProvider(backgroundElement, targetWidth, targetHeight, type) {
|
|
106
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
107
|
+
if (!backgroundElement)
|
|
108
|
+
return () => __awaiter(this, void 0, void 0, function* () { return undefined; });
|
|
109
|
+
const backgroundWidth = backgroundElement.videoWidth || backgroundElement.naturalWidth || backgroundElement.width;
|
|
110
|
+
const backgroundHeight = backgroundElement.videoHeight || backgroundElement.naturalHeight || backgroundElement.height;
|
|
111
|
+
const backgroundAspectRatio = backgroundWidth / backgroundHeight;
|
|
112
|
+
const targetAspectRatio = targetWidth / targetHeight;
|
|
113
|
+
if (type === "imagedata") {
|
|
114
|
+
const backgroundCanvas = document.createElement("canvas");
|
|
115
|
+
backgroundCanvas.width = targetWidth;
|
|
116
|
+
backgroundCanvas.height = targetHeight;
|
|
117
|
+
const backgroundCanvasCtx = backgroundCanvas.getContext("2d");
|
|
118
|
+
let [sx, sy, sw, sh] = [0, 0, targetWidth, targetHeight];
|
|
119
|
+
if (backgroundAspectRatio > targetAspectRatio) {
|
|
120
|
+
const outsideX = targetHeight * backgroundAspectRatio - targetWidth;
|
|
121
|
+
sx -= Math.round(outsideX / 2);
|
|
122
|
+
sw += Math.round(outsideX);
|
|
123
|
+
}
|
|
124
|
+
else if (backgroundAspectRatio < targetAspectRatio) {
|
|
125
|
+
const outsideY = targetWidth / backgroundAspectRatio - targetHeight;
|
|
126
|
+
sy -= Math.round(outsideY / 2);
|
|
127
|
+
sh += Math.round(outsideY);
|
|
128
|
+
}
|
|
129
|
+
return () => __awaiter(this, void 0, void 0, function* () {
|
|
130
|
+
backgroundCanvasCtx.drawImage(backgroundElement, 0, 0, backgroundWidth, backgroundHeight, sx, sy, sw, sh);
|
|
131
|
+
return backgroundCanvasCtx.getImageData(0, 0, targetWidth, targetHeight).data.buffer;
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
else if (type === "imagebitmap") {
|
|
135
|
+
let [sx, sy, sw, sh] = [0, 0, backgroundWidth, backgroundHeight];
|
|
136
|
+
if (backgroundAspectRatio > targetAspectRatio) {
|
|
137
|
+
const outsideX = backgroundWidth - backgroundHeight * targetAspectRatio;
|
|
138
|
+
sx += Math.round(outsideX / 2);
|
|
139
|
+
sw -= Math.round(outsideX);
|
|
140
|
+
}
|
|
141
|
+
else if (backgroundAspectRatio < targetAspectRatio) {
|
|
142
|
+
const outsideY = backgroundHeight - backgroundWidth / targetAspectRatio;
|
|
143
|
+
sy += Math.round(outsideY / 2);
|
|
144
|
+
sh -= Math.round(outsideY);
|
|
145
|
+
}
|
|
146
|
+
return () => __awaiter(this, void 0, void 0, function* () {
|
|
147
|
+
return yield createImageBitmap(backgroundElement, sx, sy, sw, sh, {
|
|
148
|
+
resizeWidth: targetWidth,
|
|
149
|
+
resizeHeight: targetHeight,
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
else if (type === "canvas") {
|
|
154
|
+
return () => __awaiter(this, void 0, void 0, function* () {
|
|
155
|
+
return backgroundElement;
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
function createCanvas(width, height) {
|
|
161
|
+
if (self.document) {
|
|
162
|
+
const canvas = self.document.createElement("canvas");
|
|
163
|
+
canvas.width = width;
|
|
164
|
+
canvas.height = height;
|
|
165
|
+
return canvas;
|
|
166
|
+
}
|
|
167
|
+
else if (self.OffscreenCanvas) {
|
|
168
|
+
return new OffscreenCanvas(width, height);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
var createTFLiteModule = (function () {
|
|
173
|
+
var _scriptDir = typeof document !== "undefined" && document.currentScript ? document.currentScript.src : undefined;
|
|
174
|
+
if (typeof __filename !== "undefined")
|
|
175
|
+
_scriptDir = _scriptDir || __filename;
|
|
176
|
+
return function (createTFLiteModule) {
|
|
177
|
+
createTFLiteModule = createTFLiteModule || {};
|
|
178
|
+
var Module = typeof createTFLiteModule !== "undefined" ? createTFLiteModule : {};
|
|
179
|
+
var readyPromiseResolve, readyPromiseReject;
|
|
180
|
+
Module["ready"] = new Promise(function (resolve, reject) {
|
|
181
|
+
readyPromiseResolve = resolve;
|
|
182
|
+
readyPromiseReject = reject;
|
|
183
|
+
});
|
|
184
|
+
var moduleOverrides = {};
|
|
185
|
+
var key;
|
|
186
|
+
for (key in Module) {
|
|
187
|
+
if (Module.hasOwnProperty(key)) {
|
|
188
|
+
moduleOverrides[key] = Module[key];
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
var thisProgram = "./this.program";
|
|
192
|
+
var quit_ = function (status, toThrow) {
|
|
193
|
+
throw toThrow;
|
|
194
|
+
};
|
|
195
|
+
var ENVIRONMENT_IS_WEB = false;
|
|
196
|
+
var ENVIRONMENT_IS_WORKER = false;
|
|
197
|
+
var ENVIRONMENT_IS_NODE = false;
|
|
198
|
+
var ENVIRONMENT_IS_SHELL = false;
|
|
199
|
+
ENVIRONMENT_IS_WEB = typeof window === "object";
|
|
200
|
+
ENVIRONMENT_IS_WORKER = typeof importScripts === "function";
|
|
201
|
+
ENVIRONMENT_IS_NODE =
|
|
202
|
+
typeof process === "object" &&
|
|
203
|
+
typeof process.versions === "object" &&
|
|
204
|
+
typeof process.versions.node === "string";
|
|
205
|
+
ENVIRONMENT_IS_SHELL = !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_NODE && !ENVIRONMENT_IS_WORKER;
|
|
206
|
+
var scriptDirectory = "";
|
|
207
|
+
function locateFile(path) {
|
|
208
|
+
if (Module["locateFile"]) {
|
|
209
|
+
return Module["locateFile"](path, scriptDirectory);
|
|
210
|
+
}
|
|
211
|
+
return scriptDirectory + path;
|
|
212
|
+
}
|
|
213
|
+
var read_, readAsync, readBinary;
|
|
214
|
+
var nodeFS;
|
|
215
|
+
var nodePath;
|
|
216
|
+
if (ENVIRONMENT_IS_NODE) {
|
|
217
|
+
if (ENVIRONMENT_IS_WORKER) {
|
|
218
|
+
scriptDirectory = require("path").dirname(scriptDirectory) + "/";
|
|
219
|
+
}
|
|
220
|
+
else {
|
|
221
|
+
scriptDirectory = __dirname + "/";
|
|
222
|
+
}
|
|
223
|
+
read_ = function shell_read(filename, binary) {
|
|
224
|
+
if (!nodeFS)
|
|
225
|
+
nodeFS = require("fs");
|
|
226
|
+
if (!nodePath)
|
|
227
|
+
nodePath = require("path");
|
|
228
|
+
filename = nodePath["normalize"](filename);
|
|
229
|
+
return nodeFS["readFileSync"](filename, binary ? null : "utf8");
|
|
230
|
+
};
|
|
231
|
+
readBinary = function readBinary(filename) {
|
|
232
|
+
var ret = read_(filename, true);
|
|
233
|
+
if (!ret.buffer) {
|
|
234
|
+
ret = new Uint8Array(ret);
|
|
235
|
+
}
|
|
236
|
+
assert(ret.buffer);
|
|
237
|
+
return ret;
|
|
238
|
+
};
|
|
239
|
+
if (process["argv"].length > 1) {
|
|
240
|
+
thisProgram = process["argv"][1].replace(/\\/g, "/");
|
|
241
|
+
}
|
|
242
|
+
process["argv"].slice(2);
|
|
243
|
+
process["on"]("uncaughtException", function (ex) {
|
|
244
|
+
if (!(ex instanceof ExitStatus)) {
|
|
245
|
+
throw ex;
|
|
246
|
+
}
|
|
247
|
+
});
|
|
248
|
+
process["on"]("unhandledRejection", abort);
|
|
249
|
+
quit_ = function (status) {
|
|
250
|
+
process["exit"](status);
|
|
251
|
+
};
|
|
252
|
+
Module["inspect"] = function () {
|
|
253
|
+
return "[Emscripten Module object]";
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
else if (ENVIRONMENT_IS_SHELL) {
|
|
257
|
+
if (typeof read != "undefined") {
|
|
258
|
+
read_ = function shell_read(f) {
|
|
259
|
+
return read(f);
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
readBinary = function readBinary(f) {
|
|
263
|
+
var data;
|
|
264
|
+
if (typeof readbuffer === "function") {
|
|
265
|
+
return new Uint8Array(readbuffer(f));
|
|
266
|
+
}
|
|
267
|
+
data = read(f, "binary");
|
|
268
|
+
assert(typeof data === "object");
|
|
269
|
+
return data;
|
|
270
|
+
};
|
|
271
|
+
if (typeof scriptArgs != "undefined") {
|
|
272
|
+
scriptArgs;
|
|
273
|
+
}
|
|
274
|
+
if (typeof quit === "function") {
|
|
275
|
+
quit_ = function (status) {
|
|
276
|
+
quit(status);
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
if (typeof print !== "undefined") {
|
|
280
|
+
if (typeof console === "undefined")
|
|
281
|
+
console = {};
|
|
282
|
+
console.log = print;
|
|
283
|
+
console.warn = console.error = typeof printErr !== "undefined" ? printErr : print;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
else if (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER) {
|
|
287
|
+
if (ENVIRONMENT_IS_WORKER) {
|
|
288
|
+
scriptDirectory = self.location.href;
|
|
289
|
+
}
|
|
290
|
+
else if (typeof document !== "undefined" && document.currentScript) {
|
|
291
|
+
scriptDirectory = document.currentScript.src;
|
|
292
|
+
}
|
|
293
|
+
if (_scriptDir) {
|
|
294
|
+
scriptDirectory = _scriptDir;
|
|
295
|
+
}
|
|
296
|
+
if (scriptDirectory.indexOf("blob:") !== 0) {
|
|
297
|
+
scriptDirectory = scriptDirectory.substr(0, scriptDirectory.lastIndexOf("/") + 1);
|
|
298
|
+
}
|
|
299
|
+
else {
|
|
300
|
+
scriptDirectory = "";
|
|
301
|
+
}
|
|
302
|
+
{
|
|
303
|
+
read_ = function (url) {
|
|
304
|
+
var xhr = new XMLHttpRequest();
|
|
305
|
+
xhr.open("GET", url, false);
|
|
306
|
+
xhr.send(null);
|
|
307
|
+
return xhr.responseText;
|
|
308
|
+
};
|
|
309
|
+
if (ENVIRONMENT_IS_WORKER) {
|
|
310
|
+
readBinary = function (url) {
|
|
311
|
+
var xhr = new XMLHttpRequest();
|
|
312
|
+
xhr.open("GET", url, false);
|
|
313
|
+
xhr.responseType = "arraybuffer";
|
|
314
|
+
xhr.send(null);
|
|
315
|
+
return new Uint8Array(xhr.response);
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
readAsync = function (url, onload, onerror) {
|
|
319
|
+
var xhr = new XMLHttpRequest();
|
|
320
|
+
xhr.open("GET", url, true);
|
|
321
|
+
xhr.responseType = "arraybuffer";
|
|
322
|
+
xhr.onload = function () {
|
|
323
|
+
if (xhr.status == 200 || (xhr.status == 0 && xhr.response)) {
|
|
324
|
+
onload(xhr.response);
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
onerror();
|
|
328
|
+
};
|
|
329
|
+
xhr.onerror = onerror;
|
|
330
|
+
xhr.send(null);
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
else ;
|
|
335
|
+
var out = Module["print"] || console.log.bind(console);
|
|
336
|
+
var err = Module["printErr"] || console.warn.bind(console);
|
|
337
|
+
for (key in moduleOverrides) {
|
|
338
|
+
if (moduleOverrides.hasOwnProperty(key)) {
|
|
339
|
+
Module[key] = moduleOverrides[key];
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
moduleOverrides = null;
|
|
343
|
+
if (Module["arguments"])
|
|
344
|
+
Module["arguments"];
|
|
345
|
+
if (Module["thisProgram"])
|
|
346
|
+
thisProgram = Module["thisProgram"];
|
|
347
|
+
if (Module["quit"])
|
|
348
|
+
quit_ = Module["quit"];
|
|
349
|
+
var wasmBinary;
|
|
350
|
+
if (Module["wasmBinary"])
|
|
351
|
+
wasmBinary = Module["wasmBinary"];
|
|
352
|
+
Module["noExitRuntime"] || true;
|
|
353
|
+
if (typeof WebAssembly !== "object") {
|
|
354
|
+
abort("no native wasm support detected");
|
|
355
|
+
}
|
|
356
|
+
var wasmMemory;
|
|
357
|
+
var ABORT = false;
|
|
358
|
+
function assert(condition, text) {
|
|
359
|
+
if (!condition) {
|
|
360
|
+
abort("Assertion failed: " + text);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
var UTF8Decoder = typeof TextDecoder !== "undefined" ? new TextDecoder("utf8") : undefined;
|
|
364
|
+
function UTF8ArrayToString(heap, idx, maxBytesToRead) {
|
|
365
|
+
var endIdx = idx + maxBytesToRead;
|
|
366
|
+
var endPtr = idx;
|
|
367
|
+
while (heap[endPtr] && !(endPtr >= endIdx))
|
|
368
|
+
++endPtr;
|
|
369
|
+
if (endPtr - idx > 16 && heap.subarray && UTF8Decoder) {
|
|
370
|
+
return UTF8Decoder.decode(heap.subarray(idx, endPtr));
|
|
371
|
+
}
|
|
372
|
+
else {
|
|
373
|
+
var str = "";
|
|
374
|
+
while (idx < endPtr) {
|
|
375
|
+
var u0 = heap[idx++];
|
|
376
|
+
if (!(u0 & 128)) {
|
|
377
|
+
str += String.fromCharCode(u0);
|
|
378
|
+
continue;
|
|
379
|
+
}
|
|
380
|
+
var u1 = heap[idx++] & 63;
|
|
381
|
+
if ((u0 & 224) == 192) {
|
|
382
|
+
str += String.fromCharCode(((u0 & 31) << 6) | u1);
|
|
383
|
+
continue;
|
|
384
|
+
}
|
|
385
|
+
var u2 = heap[idx++] & 63;
|
|
386
|
+
if ((u0 & 240) == 224) {
|
|
387
|
+
u0 = ((u0 & 15) << 12) | (u1 << 6) | u2;
|
|
388
|
+
}
|
|
389
|
+
else {
|
|
390
|
+
u0 = ((u0 & 7) << 18) | (u1 << 12) | (u2 << 6) | (heap[idx++] & 63);
|
|
391
|
+
}
|
|
392
|
+
if (u0 < 65536) {
|
|
393
|
+
str += String.fromCharCode(u0);
|
|
394
|
+
}
|
|
395
|
+
else {
|
|
396
|
+
var ch = u0 - 65536;
|
|
397
|
+
str += String.fromCharCode(55296 | (ch >> 10), 56320 | (ch & 1023));
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
return str;
|
|
402
|
+
}
|
|
403
|
+
function UTF8ToString(ptr, maxBytesToRead) {
|
|
404
|
+
return ptr ? UTF8ArrayToString(HEAPU8, ptr, maxBytesToRead) : "";
|
|
405
|
+
}
|
|
406
|
+
function writeAsciiToMemory(str, buffer, dontAddNull) {
|
|
407
|
+
for (var i = 0; i < str.length; ++i) {
|
|
408
|
+
HEAP8[buffer++ >> 0] = str.charCodeAt(i);
|
|
409
|
+
}
|
|
410
|
+
HEAP8[buffer >> 0] = 0;
|
|
411
|
+
}
|
|
412
|
+
function alignUp(x, multiple) {
|
|
413
|
+
if (x % multiple > 0) {
|
|
414
|
+
x += multiple - (x % multiple);
|
|
415
|
+
}
|
|
416
|
+
return x;
|
|
417
|
+
}
|
|
418
|
+
var buffer, HEAP8, HEAPU8, HEAP32;
|
|
419
|
+
function updateGlobalBufferAndViews(buf) {
|
|
420
|
+
buffer = buf;
|
|
421
|
+
Module["HEAP8"] = HEAP8 = new Int8Array(buf);
|
|
422
|
+
Module["HEAP16"] = new Int16Array(buf);
|
|
423
|
+
Module["HEAP32"] = HEAP32 = new Int32Array(buf);
|
|
424
|
+
Module["HEAPU8"] = HEAPU8 = new Uint8Array(buf);
|
|
425
|
+
Module["HEAPU16"] = new Uint16Array(buf);
|
|
426
|
+
Module["HEAPU32"] = new Uint32Array(buf);
|
|
427
|
+
Module["HEAPF32"] = new Float32Array(buf);
|
|
428
|
+
Module["HEAPF64"] = new Float64Array(buf);
|
|
429
|
+
}
|
|
430
|
+
Module["INITIAL_MEMORY"] || 16777216;
|
|
431
|
+
var wasmTable;
|
|
432
|
+
var __ATPRERUN__ = [];
|
|
433
|
+
var __ATINIT__ = [];
|
|
434
|
+
var __ATMAIN__ = [];
|
|
435
|
+
var __ATPOSTRUN__ = [];
|
|
436
|
+
__ATINIT__.push({
|
|
437
|
+
func: function () {
|
|
438
|
+
___wasm_call_ctors();
|
|
439
|
+
},
|
|
440
|
+
});
|
|
441
|
+
function preRun() {
|
|
442
|
+
if (Module["preRun"]) {
|
|
443
|
+
if (typeof Module["preRun"] == "function")
|
|
444
|
+
Module["preRun"] = [Module["preRun"]];
|
|
445
|
+
while (Module["preRun"].length) {
|
|
446
|
+
addOnPreRun(Module["preRun"].shift());
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
callRuntimeCallbacks(__ATPRERUN__);
|
|
450
|
+
}
|
|
451
|
+
function initRuntime() {
|
|
452
|
+
callRuntimeCallbacks(__ATINIT__);
|
|
453
|
+
}
|
|
454
|
+
function preMain() {
|
|
455
|
+
callRuntimeCallbacks(__ATMAIN__);
|
|
456
|
+
}
|
|
457
|
+
function postRun() {
|
|
458
|
+
if (Module["postRun"]) {
|
|
459
|
+
if (typeof Module["postRun"] == "function")
|
|
460
|
+
Module["postRun"] = [Module["postRun"]];
|
|
461
|
+
while (Module["postRun"].length) {
|
|
462
|
+
addOnPostRun(Module["postRun"].shift());
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
callRuntimeCallbacks(__ATPOSTRUN__);
|
|
466
|
+
}
|
|
467
|
+
function addOnPreRun(cb) {
|
|
468
|
+
__ATPRERUN__.unshift(cb);
|
|
469
|
+
}
|
|
470
|
+
function addOnPostRun(cb) {
|
|
471
|
+
__ATPOSTRUN__.unshift(cb);
|
|
472
|
+
}
|
|
473
|
+
var runDependencies = 0;
|
|
474
|
+
var dependenciesFulfilled = null;
|
|
475
|
+
function addRunDependency(id) {
|
|
476
|
+
runDependencies++;
|
|
477
|
+
if (Module["monitorRunDependencies"]) {
|
|
478
|
+
Module["monitorRunDependencies"](runDependencies);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
function removeRunDependency(id) {
|
|
482
|
+
runDependencies--;
|
|
483
|
+
if (Module["monitorRunDependencies"]) {
|
|
484
|
+
Module["monitorRunDependencies"](runDependencies);
|
|
485
|
+
}
|
|
486
|
+
if (runDependencies == 0) {
|
|
487
|
+
if (dependenciesFulfilled) {
|
|
488
|
+
var callback = dependenciesFulfilled;
|
|
489
|
+
dependenciesFulfilled = null;
|
|
490
|
+
callback();
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
Module["preloadedImages"] = {};
|
|
495
|
+
Module["preloadedAudios"] = {};
|
|
496
|
+
function abort(what) {
|
|
497
|
+
if (Module["onAbort"]) {
|
|
498
|
+
Module["onAbort"](what);
|
|
499
|
+
}
|
|
500
|
+
what += "";
|
|
501
|
+
err(what);
|
|
502
|
+
ABORT = true;
|
|
503
|
+
what = "abort(" + what + "). Build with -s ASSERTIONS=1 for more info.";
|
|
504
|
+
var e = new WebAssembly.RuntimeError(what);
|
|
505
|
+
readyPromiseReject(e);
|
|
506
|
+
throw e;
|
|
507
|
+
}
|
|
508
|
+
function hasPrefix(str, prefix) {
|
|
509
|
+
return String.prototype.startsWith ? str.startsWith(prefix) : str.indexOf(prefix) === 0;
|
|
510
|
+
}
|
|
511
|
+
var dataURIPrefix = "data:application/octet-stream;base64,";
|
|
512
|
+
function isDataURI(filename) {
|
|
513
|
+
return hasPrefix(filename, dataURIPrefix);
|
|
514
|
+
}
|
|
515
|
+
var fileURIPrefix = "file://";
|
|
516
|
+
function isFileURI(filename) {
|
|
517
|
+
return hasPrefix(filename, fileURIPrefix);
|
|
518
|
+
}
|
|
519
|
+
var wasmBinaryFile = "tflite.wasm";
|
|
520
|
+
if (!isDataURI(wasmBinaryFile)) {
|
|
521
|
+
wasmBinaryFile = locateFile(wasmBinaryFile);
|
|
522
|
+
}
|
|
523
|
+
function getBinary(file) {
|
|
524
|
+
try {
|
|
525
|
+
if (file == wasmBinaryFile && wasmBinary) {
|
|
526
|
+
return new Uint8Array(wasmBinary);
|
|
527
|
+
}
|
|
528
|
+
if (readBinary) {
|
|
529
|
+
return readBinary(file);
|
|
530
|
+
}
|
|
531
|
+
else {
|
|
532
|
+
throw "both async and sync fetching of the wasm failed";
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
catch (err) {
|
|
536
|
+
abort(err);
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
function getBinaryPromise() {
|
|
540
|
+
if (!wasmBinary && (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER)) {
|
|
541
|
+
if (typeof fetch === "function" && !isFileURI(wasmBinaryFile)) {
|
|
542
|
+
return fetch(wasmBinaryFile, { credentials: "same-origin" })
|
|
543
|
+
.then(function (response) {
|
|
544
|
+
if (!response["ok"]) {
|
|
545
|
+
throw "failed to load wasm binary file at '" + wasmBinaryFile + "'";
|
|
546
|
+
}
|
|
547
|
+
return response["arrayBuffer"]();
|
|
548
|
+
})
|
|
549
|
+
.catch(function () {
|
|
550
|
+
return getBinary(wasmBinaryFile);
|
|
551
|
+
});
|
|
552
|
+
}
|
|
553
|
+
else {
|
|
554
|
+
if (readAsync) {
|
|
555
|
+
return new Promise(function (resolve, reject) {
|
|
556
|
+
readAsync(wasmBinaryFile, function (response) {
|
|
557
|
+
resolve(new Uint8Array(response));
|
|
558
|
+
}, reject);
|
|
559
|
+
});
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
return Promise.resolve().then(function () {
|
|
564
|
+
return getBinary(wasmBinaryFile);
|
|
565
|
+
});
|
|
566
|
+
}
|
|
567
|
+
function createWasm() {
|
|
568
|
+
var info = { a: asmLibraryArg };
|
|
569
|
+
function receiveInstance(instance, module) {
|
|
570
|
+
var exports = instance.exports;
|
|
571
|
+
Module["asm"] = exports;
|
|
572
|
+
wasmMemory = Module["asm"]["q"];
|
|
573
|
+
updateGlobalBufferAndViews(wasmMemory.buffer);
|
|
574
|
+
wasmTable = Module["asm"]["D"];
|
|
575
|
+
removeRunDependency();
|
|
576
|
+
}
|
|
577
|
+
addRunDependency();
|
|
578
|
+
function receiveInstantiatedSource(output) {
|
|
579
|
+
receiveInstance(output["instance"]);
|
|
580
|
+
}
|
|
581
|
+
function instantiateArrayBuffer(receiver) {
|
|
582
|
+
return getBinaryPromise()
|
|
583
|
+
.then(function (binary) {
|
|
584
|
+
return WebAssembly.instantiate(binary, info);
|
|
585
|
+
})
|
|
586
|
+
.then(receiver, function (reason) {
|
|
587
|
+
err("failed to asynchronously prepare wasm: " + reason);
|
|
588
|
+
abort(reason);
|
|
589
|
+
});
|
|
590
|
+
}
|
|
591
|
+
function instantiateAsync() {
|
|
592
|
+
if (!wasmBinary &&
|
|
593
|
+
typeof WebAssembly.instantiateStreaming === "function" &&
|
|
594
|
+
!isDataURI(wasmBinaryFile) &&
|
|
595
|
+
!isFileURI(wasmBinaryFile) &&
|
|
596
|
+
typeof fetch === "function") {
|
|
597
|
+
return fetch(wasmBinaryFile, { credentials: "same-origin" }).then(function (response) {
|
|
598
|
+
var result = WebAssembly.instantiateStreaming(response, info);
|
|
599
|
+
return result.then(receiveInstantiatedSource, function (reason) {
|
|
600
|
+
err("wasm streaming compile failed: " + reason);
|
|
601
|
+
err("falling back to ArrayBuffer instantiation");
|
|
602
|
+
return instantiateArrayBuffer(receiveInstantiatedSource);
|
|
603
|
+
});
|
|
604
|
+
});
|
|
605
|
+
}
|
|
606
|
+
else {
|
|
607
|
+
return instantiateArrayBuffer(receiveInstantiatedSource);
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
if (Module["instantiateWasm"]) {
|
|
611
|
+
try {
|
|
612
|
+
var exports = Module["instantiateWasm"](info, receiveInstance);
|
|
613
|
+
return exports;
|
|
614
|
+
}
|
|
615
|
+
catch (e) {
|
|
616
|
+
err("Module.instantiateWasm callback failed with error: " + e);
|
|
617
|
+
return false;
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
instantiateAsync().catch(readyPromiseReject);
|
|
621
|
+
return {};
|
|
622
|
+
}
|
|
623
|
+
function callRuntimeCallbacks(callbacks) {
|
|
624
|
+
while (callbacks.length > 0) {
|
|
625
|
+
var callback = callbacks.shift();
|
|
626
|
+
if (typeof callback == "function") {
|
|
627
|
+
callback(Module);
|
|
628
|
+
continue;
|
|
629
|
+
}
|
|
630
|
+
var func = callback.func;
|
|
631
|
+
if (typeof func === "number") {
|
|
632
|
+
if (callback.arg === undefined) {
|
|
633
|
+
wasmTable.get(func)();
|
|
634
|
+
}
|
|
635
|
+
else {
|
|
636
|
+
wasmTable.get(func)(callback.arg);
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
else {
|
|
640
|
+
func(callback.arg === undefined ? null : callback.arg);
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
function _abort() {
|
|
645
|
+
abort();
|
|
646
|
+
}
|
|
647
|
+
var _emscripten_get_now;
|
|
648
|
+
if (ENVIRONMENT_IS_NODE) {
|
|
649
|
+
_emscripten_get_now = function () {
|
|
650
|
+
var t = process["hrtime"]();
|
|
651
|
+
return t[0] * 1e3 + t[1] / 1e6;
|
|
652
|
+
};
|
|
653
|
+
}
|
|
654
|
+
else if (typeof dateNow !== "undefined") {
|
|
655
|
+
_emscripten_get_now = dateNow;
|
|
656
|
+
}
|
|
657
|
+
else
|
|
658
|
+
_emscripten_get_now = function () {
|
|
659
|
+
return performance.now();
|
|
660
|
+
};
|
|
661
|
+
var _emscripten_get_now_is_monotonic = true;
|
|
662
|
+
function setErrNo(value) {
|
|
663
|
+
HEAP32[___errno_location() >> 2] = value;
|
|
664
|
+
return value;
|
|
665
|
+
}
|
|
666
|
+
function _clock_gettime(clk_id, tp) {
|
|
667
|
+
var now;
|
|
668
|
+
if (clk_id === 0) {
|
|
669
|
+
now = Date.now();
|
|
670
|
+
}
|
|
671
|
+
else if ((clk_id === 1 || clk_id === 4) && _emscripten_get_now_is_monotonic) {
|
|
672
|
+
now = _emscripten_get_now();
|
|
673
|
+
}
|
|
674
|
+
else {
|
|
675
|
+
setErrNo(28);
|
|
676
|
+
return -1;
|
|
677
|
+
}
|
|
678
|
+
HEAP32[tp >> 2] = (now / 1e3) | 0;
|
|
679
|
+
HEAP32[(tp + 4) >> 2] = ((now % 1e3) * 1e3 * 1e3) | 0;
|
|
680
|
+
return 0;
|
|
681
|
+
}
|
|
682
|
+
function _dlopen(filename, flag) {
|
|
683
|
+
abort("To use dlopen, you need to use Emscripten's linking support, see https://github.com/emscripten-core/emscripten/wiki/Linking");
|
|
684
|
+
}
|
|
685
|
+
function _dlsym(handle, symbol) {
|
|
686
|
+
abort("To use dlopen, you need to use Emscripten's linking support, see https://github.com/emscripten-core/emscripten/wiki/Linking");
|
|
687
|
+
}
|
|
688
|
+
function _emscripten_memcpy_big(dest, src, num) {
|
|
689
|
+
HEAPU8.copyWithin(dest, src, src + num);
|
|
690
|
+
}
|
|
691
|
+
function _emscripten_get_heap_size() {
|
|
692
|
+
return HEAPU8.length;
|
|
693
|
+
}
|
|
694
|
+
function emscripten_realloc_buffer(size) {
|
|
695
|
+
try {
|
|
696
|
+
wasmMemory.grow((size - buffer.byteLength + 65535) >>> 16);
|
|
697
|
+
updateGlobalBufferAndViews(wasmMemory.buffer);
|
|
698
|
+
return 1;
|
|
699
|
+
}
|
|
700
|
+
catch (e) { }
|
|
701
|
+
}
|
|
702
|
+
function _emscripten_resize_heap(requestedSize) {
|
|
703
|
+
var oldSize = _emscripten_get_heap_size();
|
|
704
|
+
var maxHeapSize = 2147483648;
|
|
705
|
+
if (requestedSize > maxHeapSize) {
|
|
706
|
+
return false;
|
|
707
|
+
}
|
|
708
|
+
for (var cutDown = 1; cutDown <= 4; cutDown *= 2) {
|
|
709
|
+
var overGrownHeapSize = oldSize * (1 + 0.2 / cutDown);
|
|
710
|
+
overGrownHeapSize = Math.min(overGrownHeapSize, requestedSize + 100663296);
|
|
711
|
+
var newSize = Math.min(maxHeapSize, alignUp(Math.max(requestedSize, overGrownHeapSize), 65536));
|
|
712
|
+
var replacement = emscripten_realloc_buffer(newSize);
|
|
713
|
+
if (replacement) {
|
|
714
|
+
return true;
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
return false;
|
|
718
|
+
}
|
|
719
|
+
function _emscripten_thread_sleep(msecs) {
|
|
720
|
+
var start = _emscripten_get_now();
|
|
721
|
+
while (_emscripten_get_now() - start < msecs) { }
|
|
722
|
+
}
|
|
723
|
+
var ENV = {};
|
|
724
|
+
function getExecutableName() {
|
|
725
|
+
return thisProgram || "./this.program";
|
|
726
|
+
}
|
|
727
|
+
function getEnvStrings() {
|
|
728
|
+
if (!getEnvStrings.strings) {
|
|
729
|
+
var lang = ((typeof navigator === "object" && navigator.languages && navigator.languages[0]) || "C").replace("-", "_") + ".UTF-8";
|
|
730
|
+
var env = {
|
|
731
|
+
USER: "web_user",
|
|
732
|
+
LOGNAME: "web_user",
|
|
733
|
+
PATH: "/",
|
|
734
|
+
PWD: "/",
|
|
735
|
+
HOME: "/home/web_user",
|
|
736
|
+
LANG: lang,
|
|
737
|
+
_: getExecutableName(),
|
|
738
|
+
};
|
|
739
|
+
for (var x in ENV) {
|
|
740
|
+
env[x] = ENV[x];
|
|
741
|
+
}
|
|
742
|
+
var strings = [];
|
|
743
|
+
for (var x in env) {
|
|
744
|
+
strings.push(x + "=" + env[x]);
|
|
745
|
+
}
|
|
746
|
+
getEnvStrings.strings = strings;
|
|
747
|
+
}
|
|
748
|
+
return getEnvStrings.strings;
|
|
749
|
+
}
|
|
750
|
+
var SYSCALLS = {
|
|
751
|
+
mappings: {},
|
|
752
|
+
buffers: [null, [], []],
|
|
753
|
+
printChar: function (stream, curr) {
|
|
754
|
+
var buffer = SYSCALLS.buffers[stream];
|
|
755
|
+
if (curr === 0 || curr === 10) {
|
|
756
|
+
(stream === 1 ? out : err)(UTF8ArrayToString(buffer, 0));
|
|
757
|
+
buffer.length = 0;
|
|
758
|
+
}
|
|
759
|
+
else {
|
|
760
|
+
buffer.push(curr);
|
|
761
|
+
}
|
|
762
|
+
},
|
|
763
|
+
varargs: undefined,
|
|
764
|
+
get: function () {
|
|
765
|
+
SYSCALLS.varargs += 4;
|
|
766
|
+
var ret = HEAP32[(SYSCALLS.varargs - 4) >> 2];
|
|
767
|
+
return ret;
|
|
768
|
+
},
|
|
769
|
+
getStr: function (ptr) {
|
|
770
|
+
var ret = UTF8ToString(ptr);
|
|
771
|
+
return ret;
|
|
772
|
+
},
|
|
773
|
+
get64: function (low, high) {
|
|
774
|
+
return low;
|
|
775
|
+
},
|
|
776
|
+
};
|
|
777
|
+
function _environ_get(__environ, environ_buf) {
|
|
778
|
+
var bufSize = 0;
|
|
779
|
+
getEnvStrings().forEach(function (string, i) {
|
|
780
|
+
var ptr = environ_buf + bufSize;
|
|
781
|
+
HEAP32[(__environ + i * 4) >> 2] = ptr;
|
|
782
|
+
writeAsciiToMemory(string, ptr);
|
|
783
|
+
bufSize += string.length + 1;
|
|
784
|
+
});
|
|
785
|
+
return 0;
|
|
786
|
+
}
|
|
787
|
+
function _environ_sizes_get(penviron_count, penviron_buf_size) {
|
|
788
|
+
var strings = getEnvStrings();
|
|
789
|
+
HEAP32[penviron_count >> 2] = strings.length;
|
|
790
|
+
var bufSize = 0;
|
|
791
|
+
strings.forEach(function (string) {
|
|
792
|
+
bufSize += string.length + 1;
|
|
793
|
+
});
|
|
794
|
+
HEAP32[penviron_buf_size >> 2] = bufSize;
|
|
795
|
+
return 0;
|
|
796
|
+
}
|
|
797
|
+
function _exit(status) {
|
|
798
|
+
exit(status);
|
|
799
|
+
}
|
|
800
|
+
function _fd_close(fd) {
|
|
801
|
+
return 0;
|
|
802
|
+
}
|
|
803
|
+
function _fd_seek(fd, offset_low, offset_high, whence, newOffset) { }
|
|
804
|
+
function _fd_write(fd, iov, iovcnt, pnum) {
|
|
805
|
+
var num = 0;
|
|
806
|
+
for (var i = 0; i < iovcnt; i++) {
|
|
807
|
+
var ptr = HEAP32[(iov + i * 8) >> 2];
|
|
808
|
+
var len = HEAP32[(iov + (i * 8 + 4)) >> 2];
|
|
809
|
+
for (var j = 0; j < len; j++) {
|
|
810
|
+
SYSCALLS.printChar(fd, HEAPU8[ptr + j]);
|
|
811
|
+
}
|
|
812
|
+
num += len;
|
|
813
|
+
}
|
|
814
|
+
HEAP32[pnum >> 2] = num;
|
|
815
|
+
return 0;
|
|
816
|
+
}
|
|
817
|
+
function _pthread_create() {
|
|
818
|
+
return 6;
|
|
819
|
+
}
|
|
820
|
+
function _pthread_join() {
|
|
821
|
+
return 28;
|
|
822
|
+
}
|
|
823
|
+
function _sysconf(name) {
|
|
824
|
+
switch (name) {
|
|
825
|
+
case 30:
|
|
826
|
+
return 16384;
|
|
827
|
+
case 85:
|
|
828
|
+
var maxHeapSize = 2147483648;
|
|
829
|
+
return maxHeapSize / 16384;
|
|
830
|
+
case 132:
|
|
831
|
+
case 133:
|
|
832
|
+
case 12:
|
|
833
|
+
case 137:
|
|
834
|
+
case 138:
|
|
835
|
+
case 15:
|
|
836
|
+
case 235:
|
|
837
|
+
case 16:
|
|
838
|
+
case 17:
|
|
839
|
+
case 18:
|
|
840
|
+
case 19:
|
|
841
|
+
case 20:
|
|
842
|
+
case 149:
|
|
843
|
+
case 13:
|
|
844
|
+
case 10:
|
|
845
|
+
case 236:
|
|
846
|
+
case 153:
|
|
847
|
+
case 9:
|
|
848
|
+
case 21:
|
|
849
|
+
case 22:
|
|
850
|
+
case 159:
|
|
851
|
+
case 154:
|
|
852
|
+
case 14:
|
|
853
|
+
case 77:
|
|
854
|
+
case 78:
|
|
855
|
+
case 139:
|
|
856
|
+
case 82:
|
|
857
|
+
case 68:
|
|
858
|
+
case 67:
|
|
859
|
+
case 164:
|
|
860
|
+
case 11:
|
|
861
|
+
case 29:
|
|
862
|
+
case 47:
|
|
863
|
+
case 48:
|
|
864
|
+
case 95:
|
|
865
|
+
case 52:
|
|
866
|
+
case 51:
|
|
867
|
+
case 46:
|
|
868
|
+
return 200809;
|
|
869
|
+
case 27:
|
|
870
|
+
case 246:
|
|
871
|
+
case 127:
|
|
872
|
+
case 128:
|
|
873
|
+
case 23:
|
|
874
|
+
case 24:
|
|
875
|
+
case 160:
|
|
876
|
+
case 161:
|
|
877
|
+
case 181:
|
|
878
|
+
case 182:
|
|
879
|
+
case 242:
|
|
880
|
+
case 183:
|
|
881
|
+
case 184:
|
|
882
|
+
case 243:
|
|
883
|
+
case 244:
|
|
884
|
+
case 245:
|
|
885
|
+
case 165:
|
|
886
|
+
case 178:
|
|
887
|
+
case 179:
|
|
888
|
+
case 49:
|
|
889
|
+
case 50:
|
|
890
|
+
case 168:
|
|
891
|
+
case 169:
|
|
892
|
+
case 175:
|
|
893
|
+
case 170:
|
|
894
|
+
case 171:
|
|
895
|
+
case 172:
|
|
896
|
+
case 97:
|
|
897
|
+
case 76:
|
|
898
|
+
case 32:
|
|
899
|
+
case 173:
|
|
900
|
+
case 35:
|
|
901
|
+
case 80:
|
|
902
|
+
case 81:
|
|
903
|
+
case 79:
|
|
904
|
+
return -1;
|
|
905
|
+
case 176:
|
|
906
|
+
case 177:
|
|
907
|
+
case 7:
|
|
908
|
+
case 155:
|
|
909
|
+
case 8:
|
|
910
|
+
case 157:
|
|
911
|
+
case 125:
|
|
912
|
+
case 126:
|
|
913
|
+
case 92:
|
|
914
|
+
case 93:
|
|
915
|
+
case 129:
|
|
916
|
+
case 130:
|
|
917
|
+
case 131:
|
|
918
|
+
case 94:
|
|
919
|
+
case 91:
|
|
920
|
+
return 1;
|
|
921
|
+
case 74:
|
|
922
|
+
case 60:
|
|
923
|
+
case 69:
|
|
924
|
+
case 70:
|
|
925
|
+
case 4:
|
|
926
|
+
return 1024;
|
|
927
|
+
case 31:
|
|
928
|
+
case 42:
|
|
929
|
+
case 72:
|
|
930
|
+
return 32;
|
|
931
|
+
case 87:
|
|
932
|
+
case 26:
|
|
933
|
+
case 33:
|
|
934
|
+
return 2147483647;
|
|
935
|
+
case 34:
|
|
936
|
+
case 1:
|
|
937
|
+
return 47839;
|
|
938
|
+
case 38:
|
|
939
|
+
case 36:
|
|
940
|
+
return 99;
|
|
941
|
+
case 43:
|
|
942
|
+
case 37:
|
|
943
|
+
return 2048;
|
|
944
|
+
case 0:
|
|
945
|
+
return 2097152;
|
|
946
|
+
case 3:
|
|
947
|
+
return 65536;
|
|
948
|
+
case 28:
|
|
949
|
+
return 32768;
|
|
950
|
+
case 44:
|
|
951
|
+
return 32767;
|
|
952
|
+
case 75:
|
|
953
|
+
return 16384;
|
|
954
|
+
case 39:
|
|
955
|
+
return 1e3;
|
|
956
|
+
case 89:
|
|
957
|
+
return 700;
|
|
958
|
+
case 71:
|
|
959
|
+
return 256;
|
|
960
|
+
case 40:
|
|
961
|
+
return 255;
|
|
962
|
+
case 2:
|
|
963
|
+
return 100;
|
|
964
|
+
case 180:
|
|
965
|
+
return 64;
|
|
966
|
+
case 25:
|
|
967
|
+
return 20;
|
|
968
|
+
case 5:
|
|
969
|
+
return 16;
|
|
970
|
+
case 6:
|
|
971
|
+
return 6;
|
|
972
|
+
case 73:
|
|
973
|
+
return 4;
|
|
974
|
+
case 84: {
|
|
975
|
+
if (typeof navigator === "object")
|
|
976
|
+
return navigator["hardwareConcurrency"] || 1;
|
|
977
|
+
return 1;
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
setErrNo(28);
|
|
981
|
+
return -1;
|
|
982
|
+
}
|
|
983
|
+
var asmLibraryArg = {
|
|
984
|
+
a: _abort,
|
|
985
|
+
n: _clock_gettime,
|
|
986
|
+
i: _dlopen,
|
|
987
|
+
e: _dlsym,
|
|
988
|
+
l: _emscripten_memcpy_big,
|
|
989
|
+
m: _emscripten_resize_heap,
|
|
990
|
+
o: _emscripten_thread_sleep,
|
|
991
|
+
p: _environ_get,
|
|
992
|
+
g: _environ_sizes_get,
|
|
993
|
+
j: _exit,
|
|
994
|
+
h: _fd_close,
|
|
995
|
+
k: _fd_seek,
|
|
996
|
+
c: _fd_write,
|
|
997
|
+
d: _pthread_create,
|
|
998
|
+
f: _pthread_join,
|
|
999
|
+
b: _sysconf,
|
|
1000
|
+
};
|
|
1001
|
+
createWasm();
|
|
1002
|
+
var ___wasm_call_ctors = (Module["___wasm_call_ctors"] = function () {
|
|
1003
|
+
return (___wasm_call_ctors = Module["___wasm_call_ctors"] = Module["asm"]["r"]).apply(null, arguments);
|
|
1004
|
+
});
|
|
1005
|
+
(Module["_getModelBufferMemoryOffset"] = function () {
|
|
1006
|
+
return (Module["_getModelBufferMemoryOffset"] = Module["asm"]["s"]).apply(null, arguments);
|
|
1007
|
+
});
|
|
1008
|
+
(Module["_getInputMemoryOffset"] = function () {
|
|
1009
|
+
return (Module["_getInputMemoryOffset"] = Module["asm"]["t"]).apply(null, arguments);
|
|
1010
|
+
});
|
|
1011
|
+
(Module["_getInputHeight"] = function () {
|
|
1012
|
+
return (Module["_getInputHeight"] = Module["asm"]["u"]).apply(null, arguments);
|
|
1013
|
+
});
|
|
1014
|
+
(Module["_getInputWidth"] = function () {
|
|
1015
|
+
return (Module["_getInputWidth"] = Module["asm"]["v"]).apply(null, arguments);
|
|
1016
|
+
});
|
|
1017
|
+
(Module["_getInputChannelCount"] = function () {
|
|
1018
|
+
return (Module["_getInputChannelCount"] = Module["asm"]["w"]).apply(null, arguments);
|
|
1019
|
+
});
|
|
1020
|
+
(Module["_getOutputMemoryOffset"] = function () {
|
|
1021
|
+
return (Module["_getOutputMemoryOffset"] = Module["asm"]["x"]).apply(null, arguments);
|
|
1022
|
+
});
|
|
1023
|
+
(Module["_getOutputHeight"] = function () {
|
|
1024
|
+
return (Module["_getOutputHeight"] = Module["asm"]["y"]).apply(null, arguments);
|
|
1025
|
+
});
|
|
1026
|
+
(Module["_getOutputWidth"] = function () {
|
|
1027
|
+
return (Module["_getOutputWidth"] = Module["asm"]["z"]).apply(null, arguments);
|
|
1028
|
+
});
|
|
1029
|
+
(Module["_getOutputChannelCount"] = function () {
|
|
1030
|
+
return (Module["_getOutputChannelCount"] = Module["asm"]["A"]).apply(null, arguments);
|
|
1031
|
+
});
|
|
1032
|
+
(Module["_loadModel"] = function () {
|
|
1033
|
+
return (Module["_loadModel"] = Module["asm"]["B"]).apply(null, arguments);
|
|
1034
|
+
});
|
|
1035
|
+
(Module["_runInference"] = function () {
|
|
1036
|
+
return (Module["_runInference"] = Module["asm"]["C"]).apply(null, arguments);
|
|
1037
|
+
});
|
|
1038
|
+
var ___errno_location = (Module["___errno_location"] = function () {
|
|
1039
|
+
return (___errno_location = Module["___errno_location"] = Module["asm"]["E"]).apply(null, arguments);
|
|
1040
|
+
});
|
|
1041
|
+
var calledRun;
|
|
1042
|
+
function ExitStatus(status) {
|
|
1043
|
+
this.name = "ExitStatus";
|
|
1044
|
+
this.message = "Program terminated with exit(" + status + ")";
|
|
1045
|
+
this.status = status;
|
|
1046
|
+
}
|
|
1047
|
+
dependenciesFulfilled = function runCaller() {
|
|
1048
|
+
if (!calledRun)
|
|
1049
|
+
run();
|
|
1050
|
+
if (!calledRun)
|
|
1051
|
+
dependenciesFulfilled = runCaller;
|
|
1052
|
+
};
|
|
1053
|
+
function run(args) {
|
|
1054
|
+
if (runDependencies > 0) {
|
|
1055
|
+
return;
|
|
1056
|
+
}
|
|
1057
|
+
preRun();
|
|
1058
|
+
if (runDependencies > 0) {
|
|
1059
|
+
return;
|
|
1060
|
+
}
|
|
1061
|
+
function doRun() {
|
|
1062
|
+
if (calledRun)
|
|
1063
|
+
return;
|
|
1064
|
+
calledRun = true;
|
|
1065
|
+
Module["calledRun"] = true;
|
|
1066
|
+
if (ABORT)
|
|
1067
|
+
return;
|
|
1068
|
+
initRuntime();
|
|
1069
|
+
preMain();
|
|
1070
|
+
readyPromiseResolve(Module);
|
|
1071
|
+
if (Module["onRuntimeInitialized"])
|
|
1072
|
+
Module["onRuntimeInitialized"]();
|
|
1073
|
+
postRun();
|
|
1074
|
+
}
|
|
1075
|
+
if (Module["setStatus"]) {
|
|
1076
|
+
Module["setStatus"]("Running...");
|
|
1077
|
+
setTimeout(function () {
|
|
1078
|
+
setTimeout(function () {
|
|
1079
|
+
Module["setStatus"]("");
|
|
1080
|
+
}, 1);
|
|
1081
|
+
doRun();
|
|
1082
|
+
}, 1);
|
|
1083
|
+
}
|
|
1084
|
+
else {
|
|
1085
|
+
doRun();
|
|
1086
|
+
}
|
|
1087
|
+
}
|
|
1088
|
+
Module["run"] = run;
|
|
1089
|
+
function exit(status, implicit) {
|
|
1090
|
+
quit_(status, new ExitStatus(status));
|
|
1091
|
+
}
|
|
1092
|
+
if (Module["preInit"]) {
|
|
1093
|
+
if (typeof Module["preInit"] == "function")
|
|
1094
|
+
Module["preInit"] = [Module["preInit"]];
|
|
1095
|
+
while (Module["preInit"].length > 0) {
|
|
1096
|
+
Module["preInit"].pop()();
|
|
1097
|
+
}
|
|
1098
|
+
}
|
|
1099
|
+
run();
|
|
1100
|
+
return createTFLiteModule.ready;
|
|
1101
|
+
};
|
|
1102
|
+
})();
|
|
1103
|
+
|
|
1104
|
+
const createTFLiteSIMDModule = (function () {
|
|
1105
|
+
let _scriptDir = typeof document !== "undefined" && document.currentScript ? document.currentScript.src : undefined;
|
|
1106
|
+
if (typeof __filename !== "undefined")
|
|
1107
|
+
_scriptDir = _scriptDir || __filename;
|
|
1108
|
+
return function (createTFLiteSIMDModule) {
|
|
1109
|
+
createTFLiteSIMDModule = createTFLiteSIMDModule || {};
|
|
1110
|
+
var Module = typeof createTFLiteSIMDModule !== "undefined" ? createTFLiteSIMDModule : {};
|
|
1111
|
+
var readyPromiseResolve, readyPromiseReject;
|
|
1112
|
+
Module["ready"] = new Promise(function (resolve, reject) {
|
|
1113
|
+
readyPromiseResolve = resolve;
|
|
1114
|
+
readyPromiseReject = reject;
|
|
1115
|
+
});
|
|
1116
|
+
var moduleOverrides = {};
|
|
1117
|
+
var key;
|
|
1118
|
+
for (key in Module) {
|
|
1119
|
+
if (Module.hasOwnProperty(key)) {
|
|
1120
|
+
moduleOverrides[key] = Module[key];
|
|
1121
|
+
}
|
|
1122
|
+
}
|
|
1123
|
+
var thisProgram = "./this.program";
|
|
1124
|
+
var quit_ = function (status, toThrow) {
|
|
1125
|
+
throw toThrow;
|
|
1126
|
+
};
|
|
1127
|
+
var ENVIRONMENT_IS_WEB = false;
|
|
1128
|
+
var ENVIRONMENT_IS_WORKER = false;
|
|
1129
|
+
var ENVIRONMENT_IS_NODE = false;
|
|
1130
|
+
var ENVIRONMENT_IS_SHELL = false;
|
|
1131
|
+
ENVIRONMENT_IS_WEB = typeof window === "object";
|
|
1132
|
+
ENVIRONMENT_IS_WORKER = typeof importScripts === "function";
|
|
1133
|
+
ENVIRONMENT_IS_NODE =
|
|
1134
|
+
typeof process === "object" &&
|
|
1135
|
+
typeof process.versions === "object" &&
|
|
1136
|
+
typeof process.versions.node === "string";
|
|
1137
|
+
ENVIRONMENT_IS_SHELL = !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_NODE && !ENVIRONMENT_IS_WORKER;
|
|
1138
|
+
var scriptDirectory = "";
|
|
1139
|
+
function locateFile(path) {
|
|
1140
|
+
if (Module["locateFile"]) {
|
|
1141
|
+
return Module["locateFile"](path, scriptDirectory);
|
|
1142
|
+
}
|
|
1143
|
+
return scriptDirectory + path;
|
|
1144
|
+
}
|
|
1145
|
+
var read_, readAsync, readBinary;
|
|
1146
|
+
var nodeFS;
|
|
1147
|
+
var nodePath;
|
|
1148
|
+
if (ENVIRONMENT_IS_NODE) {
|
|
1149
|
+
if (ENVIRONMENT_IS_WORKER) {
|
|
1150
|
+
scriptDirectory = require("path").dirname(scriptDirectory) + "/";
|
|
1151
|
+
}
|
|
1152
|
+
else {
|
|
1153
|
+
scriptDirectory = __dirname + "/";
|
|
1154
|
+
}
|
|
1155
|
+
read_ = function shell_read(filename, binary) {
|
|
1156
|
+
if (!nodeFS)
|
|
1157
|
+
nodeFS = require("fs");
|
|
1158
|
+
if (!nodePath)
|
|
1159
|
+
nodePath = require("path");
|
|
1160
|
+
filename = nodePath["normalize"](filename);
|
|
1161
|
+
return nodeFS["readFileSync"](filename, binary ? null : "utf8");
|
|
1162
|
+
};
|
|
1163
|
+
readBinary = function readBinary(filename) {
|
|
1164
|
+
var ret = read_(filename, true);
|
|
1165
|
+
if (!ret.buffer) {
|
|
1166
|
+
ret = new Uint8Array(ret);
|
|
1167
|
+
}
|
|
1168
|
+
assert(ret.buffer);
|
|
1169
|
+
return ret;
|
|
1170
|
+
};
|
|
1171
|
+
if (process["argv"].length > 1) {
|
|
1172
|
+
thisProgram = process["argv"][1].replace(/\\/g, "/");
|
|
1173
|
+
}
|
|
1174
|
+
process["argv"].slice(2);
|
|
1175
|
+
process["on"]("uncaughtException", function (ex) {
|
|
1176
|
+
if (!(ex instanceof ExitStatus)) {
|
|
1177
|
+
throw ex;
|
|
1178
|
+
}
|
|
1179
|
+
});
|
|
1180
|
+
process["on"]("unhandledRejection", abort);
|
|
1181
|
+
quit_ = function (status) {
|
|
1182
|
+
process["exit"](status);
|
|
1183
|
+
};
|
|
1184
|
+
Module["inspect"] = function () {
|
|
1185
|
+
return "[Emscripten Module object]";
|
|
1186
|
+
};
|
|
1187
|
+
}
|
|
1188
|
+
else if (ENVIRONMENT_IS_SHELL) {
|
|
1189
|
+
if (typeof read != "undefined") {
|
|
1190
|
+
read_ = function shell_read(f) {
|
|
1191
|
+
return read(f);
|
|
1192
|
+
};
|
|
1193
|
+
}
|
|
1194
|
+
readBinary = function readBinary(f) {
|
|
1195
|
+
var data;
|
|
1196
|
+
if (typeof readbuffer === "function") {
|
|
1197
|
+
return new Uint8Array(readbuffer(f));
|
|
1198
|
+
}
|
|
1199
|
+
data = read(f, "binary");
|
|
1200
|
+
assert(typeof data === "object");
|
|
1201
|
+
return data;
|
|
1202
|
+
};
|
|
1203
|
+
if (typeof scriptArgs != "undefined") {
|
|
1204
|
+
scriptArgs;
|
|
1205
|
+
}
|
|
1206
|
+
if (typeof quit === "function") {
|
|
1207
|
+
quit_ = function (status) {
|
|
1208
|
+
quit(status);
|
|
1209
|
+
};
|
|
1210
|
+
}
|
|
1211
|
+
if (typeof print !== "undefined") {
|
|
1212
|
+
if (typeof console === "undefined")
|
|
1213
|
+
console = {};
|
|
1214
|
+
console.log = print;
|
|
1215
|
+
console.warn = console.error = typeof printErr !== "undefined" ? printErr : print;
|
|
1216
|
+
}
|
|
1217
|
+
}
|
|
1218
|
+
else if (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER) {
|
|
1219
|
+
if (ENVIRONMENT_IS_WORKER) {
|
|
1220
|
+
scriptDirectory = self.location.href;
|
|
1221
|
+
}
|
|
1222
|
+
else if (typeof document !== "undefined" && document.currentScript) {
|
|
1223
|
+
scriptDirectory = document.currentScript.src;
|
|
1224
|
+
}
|
|
1225
|
+
if (_scriptDir) {
|
|
1226
|
+
scriptDirectory = _scriptDir;
|
|
1227
|
+
}
|
|
1228
|
+
if (scriptDirectory.indexOf("blob:") !== 0) {
|
|
1229
|
+
scriptDirectory = scriptDirectory.substr(0, scriptDirectory.lastIndexOf("/") + 1);
|
|
1230
|
+
}
|
|
1231
|
+
else {
|
|
1232
|
+
scriptDirectory = "";
|
|
1233
|
+
}
|
|
1234
|
+
{
|
|
1235
|
+
read_ = function (url) {
|
|
1236
|
+
var xhr = new XMLHttpRequest();
|
|
1237
|
+
xhr.open("GET", url, false);
|
|
1238
|
+
xhr.send(null);
|
|
1239
|
+
return xhr.responseText;
|
|
1240
|
+
};
|
|
1241
|
+
if (ENVIRONMENT_IS_WORKER) {
|
|
1242
|
+
readBinary = function (url) {
|
|
1243
|
+
var xhr = new XMLHttpRequest();
|
|
1244
|
+
xhr.open("GET", url, false);
|
|
1245
|
+
xhr.responseType = "arraybuffer";
|
|
1246
|
+
xhr.send(null);
|
|
1247
|
+
return new Uint8Array(xhr.response);
|
|
1248
|
+
};
|
|
1249
|
+
}
|
|
1250
|
+
readAsync = function (url, onload, onerror) {
|
|
1251
|
+
var xhr = new XMLHttpRequest();
|
|
1252
|
+
xhr.open("GET", url, true);
|
|
1253
|
+
xhr.responseType = "arraybuffer";
|
|
1254
|
+
xhr.onload = function () {
|
|
1255
|
+
if (xhr.status == 200 || (xhr.status == 0 && xhr.response)) {
|
|
1256
|
+
onload(xhr.response);
|
|
1257
|
+
return;
|
|
1258
|
+
}
|
|
1259
|
+
onerror();
|
|
1260
|
+
};
|
|
1261
|
+
xhr.onerror = onerror;
|
|
1262
|
+
xhr.send(null);
|
|
1263
|
+
};
|
|
1264
|
+
}
|
|
1265
|
+
}
|
|
1266
|
+
else ;
|
|
1267
|
+
var out = Module["print"] || console.log.bind(console);
|
|
1268
|
+
var err = Module["printErr"] || console.warn.bind(console);
|
|
1269
|
+
for (key in moduleOverrides) {
|
|
1270
|
+
if (moduleOverrides.hasOwnProperty(key)) {
|
|
1271
|
+
Module[key] = moduleOverrides[key];
|
|
1272
|
+
}
|
|
1273
|
+
}
|
|
1274
|
+
moduleOverrides = null;
|
|
1275
|
+
if (Module["arguments"])
|
|
1276
|
+
Module["arguments"];
|
|
1277
|
+
if (Module["thisProgram"])
|
|
1278
|
+
thisProgram = Module["thisProgram"];
|
|
1279
|
+
if (Module["quit"])
|
|
1280
|
+
quit_ = Module["quit"];
|
|
1281
|
+
var wasmBinary;
|
|
1282
|
+
if (Module["wasmBinary"])
|
|
1283
|
+
wasmBinary = Module["wasmBinary"];
|
|
1284
|
+
Module["noExitRuntime"] || true;
|
|
1285
|
+
if (typeof WebAssembly !== "object") {
|
|
1286
|
+
abort("no native wasm support detected");
|
|
1287
|
+
}
|
|
1288
|
+
var wasmMemory;
|
|
1289
|
+
var ABORT = false;
|
|
1290
|
+
function assert(condition, text) {
|
|
1291
|
+
if (!condition) {
|
|
1292
|
+
abort("Assertion failed: " + text);
|
|
1293
|
+
}
|
|
1294
|
+
}
|
|
1295
|
+
var UTF8Decoder = typeof TextDecoder !== "undefined" ? new TextDecoder("utf8") : undefined;
|
|
1296
|
+
function UTF8ArrayToString(heap, idx, maxBytesToRead) {
|
|
1297
|
+
var endIdx = idx + maxBytesToRead;
|
|
1298
|
+
var endPtr = idx;
|
|
1299
|
+
while (heap[endPtr] && !(endPtr >= endIdx))
|
|
1300
|
+
++endPtr;
|
|
1301
|
+
if (endPtr - idx > 16 && heap.subarray && UTF8Decoder) {
|
|
1302
|
+
return UTF8Decoder.decode(heap.subarray(idx, endPtr));
|
|
1303
|
+
}
|
|
1304
|
+
else {
|
|
1305
|
+
var str = "";
|
|
1306
|
+
while (idx < endPtr) {
|
|
1307
|
+
var u0 = heap[idx++];
|
|
1308
|
+
if (!(u0 & 128)) {
|
|
1309
|
+
str += String.fromCharCode(u0);
|
|
1310
|
+
continue;
|
|
1311
|
+
}
|
|
1312
|
+
var u1 = heap[idx++] & 63;
|
|
1313
|
+
if ((u0 & 224) == 192) {
|
|
1314
|
+
str += String.fromCharCode(((u0 & 31) << 6) | u1);
|
|
1315
|
+
continue;
|
|
1316
|
+
}
|
|
1317
|
+
var u2 = heap[idx++] & 63;
|
|
1318
|
+
if ((u0 & 240) == 224) {
|
|
1319
|
+
u0 = ((u0 & 15) << 12) | (u1 << 6) | u2;
|
|
1320
|
+
}
|
|
1321
|
+
else {
|
|
1322
|
+
u0 = ((u0 & 7) << 18) | (u1 << 12) | (u2 << 6) | (heap[idx++] & 63);
|
|
1323
|
+
}
|
|
1324
|
+
if (u0 < 65536) {
|
|
1325
|
+
str += String.fromCharCode(u0);
|
|
1326
|
+
}
|
|
1327
|
+
else {
|
|
1328
|
+
var ch = u0 - 65536;
|
|
1329
|
+
str += String.fromCharCode(55296 | (ch >> 10), 56320 | (ch & 1023));
|
|
1330
|
+
}
|
|
1331
|
+
}
|
|
1332
|
+
}
|
|
1333
|
+
return str;
|
|
1334
|
+
}
|
|
1335
|
+
function UTF8ToString(ptr, maxBytesToRead) {
|
|
1336
|
+
return ptr ? UTF8ArrayToString(HEAPU8, ptr, maxBytesToRead) : "";
|
|
1337
|
+
}
|
|
1338
|
+
function writeAsciiToMemory(str, buffer, dontAddNull) {
|
|
1339
|
+
for (var i = 0; i < str.length; ++i) {
|
|
1340
|
+
HEAP8[buffer++ >> 0] = str.charCodeAt(i);
|
|
1341
|
+
}
|
|
1342
|
+
HEAP8[buffer >> 0] = 0;
|
|
1343
|
+
}
|
|
1344
|
+
function alignUp(x, multiple) {
|
|
1345
|
+
if (x % multiple > 0) {
|
|
1346
|
+
x += multiple - (x % multiple);
|
|
1347
|
+
}
|
|
1348
|
+
return x;
|
|
1349
|
+
}
|
|
1350
|
+
var buffer, HEAP8, HEAPU8, HEAP32;
|
|
1351
|
+
function updateGlobalBufferAndViews(buf) {
|
|
1352
|
+
buffer = buf;
|
|
1353
|
+
Module["HEAP8"] = HEAP8 = new Int8Array(buf);
|
|
1354
|
+
Module["HEAP16"] = new Int16Array(buf);
|
|
1355
|
+
Module["HEAP32"] = HEAP32 = new Int32Array(buf);
|
|
1356
|
+
Module["HEAPU8"] = HEAPU8 = new Uint8Array(buf);
|
|
1357
|
+
Module["HEAPU16"] = new Uint16Array(buf);
|
|
1358
|
+
Module["HEAPU32"] = new Uint32Array(buf);
|
|
1359
|
+
Module["HEAPF32"] = new Float32Array(buf);
|
|
1360
|
+
Module["HEAPF64"] = new Float64Array(buf);
|
|
1361
|
+
}
|
|
1362
|
+
Module["INITIAL_MEMORY"] || 16777216;
|
|
1363
|
+
var wasmTable;
|
|
1364
|
+
var __ATPRERUN__ = [];
|
|
1365
|
+
var __ATINIT__ = [];
|
|
1366
|
+
var __ATMAIN__ = [];
|
|
1367
|
+
var __ATPOSTRUN__ = [];
|
|
1368
|
+
__ATINIT__.push({
|
|
1369
|
+
func: function () {
|
|
1370
|
+
___wasm_call_ctors();
|
|
1371
|
+
},
|
|
1372
|
+
});
|
|
1373
|
+
function preRun() {
|
|
1374
|
+
if (Module["preRun"]) {
|
|
1375
|
+
if (typeof Module["preRun"] == "function")
|
|
1376
|
+
Module["preRun"] = [Module["preRun"]];
|
|
1377
|
+
while (Module["preRun"].length) {
|
|
1378
|
+
addOnPreRun(Module["preRun"].shift());
|
|
1379
|
+
}
|
|
1380
|
+
}
|
|
1381
|
+
callRuntimeCallbacks(__ATPRERUN__);
|
|
1382
|
+
}
|
|
1383
|
+
function initRuntime() {
|
|
1384
|
+
callRuntimeCallbacks(__ATINIT__);
|
|
1385
|
+
}
|
|
1386
|
+
function preMain() {
|
|
1387
|
+
callRuntimeCallbacks(__ATMAIN__);
|
|
1388
|
+
}
|
|
1389
|
+
function postRun() {
|
|
1390
|
+
if (Module["postRun"]) {
|
|
1391
|
+
if (typeof Module["postRun"] == "function")
|
|
1392
|
+
Module["postRun"] = [Module["postRun"]];
|
|
1393
|
+
while (Module["postRun"].length) {
|
|
1394
|
+
addOnPostRun(Module["postRun"].shift());
|
|
1395
|
+
}
|
|
1396
|
+
}
|
|
1397
|
+
callRuntimeCallbacks(__ATPOSTRUN__);
|
|
1398
|
+
}
|
|
1399
|
+
function addOnPreRun(cb) {
|
|
1400
|
+
__ATPRERUN__.unshift(cb);
|
|
1401
|
+
}
|
|
1402
|
+
function addOnPostRun(cb) {
|
|
1403
|
+
__ATPOSTRUN__.unshift(cb);
|
|
1404
|
+
}
|
|
1405
|
+
var runDependencies = 0;
|
|
1406
|
+
var dependenciesFulfilled = null;
|
|
1407
|
+
function addRunDependency(id) {
|
|
1408
|
+
runDependencies++;
|
|
1409
|
+
if (Module["monitorRunDependencies"]) {
|
|
1410
|
+
Module["monitorRunDependencies"](runDependencies);
|
|
1411
|
+
}
|
|
1412
|
+
}
|
|
1413
|
+
function removeRunDependency(id) {
|
|
1414
|
+
runDependencies--;
|
|
1415
|
+
if (Module["monitorRunDependencies"]) {
|
|
1416
|
+
Module["monitorRunDependencies"](runDependencies);
|
|
1417
|
+
}
|
|
1418
|
+
if (runDependencies == 0) {
|
|
1419
|
+
if (dependenciesFulfilled) {
|
|
1420
|
+
var callback = dependenciesFulfilled;
|
|
1421
|
+
dependenciesFulfilled = null;
|
|
1422
|
+
callback();
|
|
1423
|
+
}
|
|
1424
|
+
}
|
|
1425
|
+
}
|
|
1426
|
+
Module["preloadedImages"] = {};
|
|
1427
|
+
Module["preloadedAudios"] = {};
|
|
1428
|
+
function abort(what) {
|
|
1429
|
+
if (Module["onAbort"]) {
|
|
1430
|
+
Module["onAbort"](what);
|
|
1431
|
+
}
|
|
1432
|
+
what += "";
|
|
1433
|
+
err(what);
|
|
1434
|
+
ABORT = true;
|
|
1435
|
+
what = "abort(" + what + "). Build with -s ASSERTIONS=1 for more info.";
|
|
1436
|
+
var e = new WebAssembly.RuntimeError(what);
|
|
1437
|
+
readyPromiseReject(e);
|
|
1438
|
+
throw e;
|
|
1439
|
+
}
|
|
1440
|
+
function hasPrefix(str, prefix) {
|
|
1441
|
+
return String.prototype.startsWith ? str.startsWith(prefix) : str.indexOf(prefix) === 0;
|
|
1442
|
+
}
|
|
1443
|
+
var dataURIPrefix = "data:application/octet-stream;base64,";
|
|
1444
|
+
function isDataURI(filename) {
|
|
1445
|
+
return hasPrefix(filename, dataURIPrefix);
|
|
1446
|
+
}
|
|
1447
|
+
var fileURIPrefix = "file://";
|
|
1448
|
+
function isFileURI(filename) {
|
|
1449
|
+
return hasPrefix(filename, fileURIPrefix);
|
|
1450
|
+
}
|
|
1451
|
+
var wasmBinaryFile = "tflite-simd.wasm";
|
|
1452
|
+
if (!isDataURI(wasmBinaryFile)) {
|
|
1453
|
+
wasmBinaryFile = locateFile(wasmBinaryFile);
|
|
1454
|
+
}
|
|
1455
|
+
function getBinary(file) {
|
|
1456
|
+
try {
|
|
1457
|
+
if (file == wasmBinaryFile && wasmBinary) {
|
|
1458
|
+
return new Uint8Array(wasmBinary);
|
|
1459
|
+
}
|
|
1460
|
+
if (readBinary) {
|
|
1461
|
+
return readBinary(file);
|
|
1462
|
+
}
|
|
1463
|
+
else {
|
|
1464
|
+
throw "both async and sync fetching of the wasm failed";
|
|
1465
|
+
}
|
|
1466
|
+
}
|
|
1467
|
+
catch (err) {
|
|
1468
|
+
abort(err);
|
|
1469
|
+
}
|
|
1470
|
+
}
|
|
1471
|
+
function getBinaryPromise() {
|
|
1472
|
+
if (!wasmBinary && (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER)) {
|
|
1473
|
+
if (typeof fetch === "function" && !isFileURI(wasmBinaryFile)) {
|
|
1474
|
+
return fetch(wasmBinaryFile, { credentials: "same-origin" })
|
|
1475
|
+
.then(function (response) {
|
|
1476
|
+
if (!response["ok"]) {
|
|
1477
|
+
throw "failed to load wasm binary file at '" + wasmBinaryFile + "'";
|
|
1478
|
+
}
|
|
1479
|
+
return response["arrayBuffer"]();
|
|
1480
|
+
})
|
|
1481
|
+
.catch(function () {
|
|
1482
|
+
return getBinary(wasmBinaryFile);
|
|
1483
|
+
});
|
|
1484
|
+
}
|
|
1485
|
+
else {
|
|
1486
|
+
if (readAsync) {
|
|
1487
|
+
return new Promise(function (resolve, reject) {
|
|
1488
|
+
readAsync(wasmBinaryFile, function (response) {
|
|
1489
|
+
resolve(new Uint8Array(response));
|
|
1490
|
+
}, reject);
|
|
1491
|
+
});
|
|
1492
|
+
}
|
|
1493
|
+
}
|
|
1494
|
+
}
|
|
1495
|
+
return Promise.resolve().then(function () {
|
|
1496
|
+
return getBinary(wasmBinaryFile);
|
|
1497
|
+
});
|
|
1498
|
+
}
|
|
1499
|
+
function createWasm() {
|
|
1500
|
+
var info = { a: asmLibraryArg };
|
|
1501
|
+
function receiveInstance(instance, module) {
|
|
1502
|
+
var exports = instance.exports;
|
|
1503
|
+
Module["asm"] = exports;
|
|
1504
|
+
wasmMemory = Module["asm"]["q"];
|
|
1505
|
+
updateGlobalBufferAndViews(wasmMemory.buffer);
|
|
1506
|
+
wasmTable = Module["asm"]["D"];
|
|
1507
|
+
removeRunDependency();
|
|
1508
|
+
}
|
|
1509
|
+
addRunDependency();
|
|
1510
|
+
function receiveInstantiatedSource(output) {
|
|
1511
|
+
receiveInstance(output["instance"]);
|
|
1512
|
+
}
|
|
1513
|
+
function instantiateArrayBuffer(receiver) {
|
|
1514
|
+
return getBinaryPromise()
|
|
1515
|
+
.then(function (binary) {
|
|
1516
|
+
return WebAssembly.instantiate(binary, info);
|
|
1517
|
+
})
|
|
1518
|
+
.then(receiver, function (reason) {
|
|
1519
|
+
err("failed to asynchronously prepare wasm: " + reason);
|
|
1520
|
+
abort(reason);
|
|
1521
|
+
});
|
|
1522
|
+
}
|
|
1523
|
+
function instantiateAsync() {
|
|
1524
|
+
if (!wasmBinary &&
|
|
1525
|
+
typeof WebAssembly.instantiateStreaming === "function" &&
|
|
1526
|
+
!isDataURI(wasmBinaryFile) &&
|
|
1527
|
+
!isFileURI(wasmBinaryFile) &&
|
|
1528
|
+
typeof fetch === "function") {
|
|
1529
|
+
return fetch(wasmBinaryFile, { credentials: "same-origin" }).then(function (response) {
|
|
1530
|
+
var result = WebAssembly.instantiateStreaming(response, info);
|
|
1531
|
+
return result.then(receiveInstantiatedSource, function (reason) {
|
|
1532
|
+
err("wasm streaming compile failed: " + reason);
|
|
1533
|
+
err("falling back to ArrayBuffer instantiation");
|
|
1534
|
+
return instantiateArrayBuffer(receiveInstantiatedSource);
|
|
1535
|
+
});
|
|
1536
|
+
});
|
|
1537
|
+
}
|
|
1538
|
+
else {
|
|
1539
|
+
return instantiateArrayBuffer(receiveInstantiatedSource);
|
|
1540
|
+
}
|
|
1541
|
+
}
|
|
1542
|
+
if (Module["instantiateWasm"]) {
|
|
1543
|
+
try {
|
|
1544
|
+
var exports = Module["instantiateWasm"](info, receiveInstance);
|
|
1545
|
+
return exports;
|
|
1546
|
+
}
|
|
1547
|
+
catch (e) {
|
|
1548
|
+
err("Module.instantiateWasm callback failed with error: " + e);
|
|
1549
|
+
return false;
|
|
1550
|
+
}
|
|
1551
|
+
}
|
|
1552
|
+
instantiateAsync().catch(readyPromiseReject);
|
|
1553
|
+
return {};
|
|
1554
|
+
}
|
|
1555
|
+
function callRuntimeCallbacks(callbacks) {
|
|
1556
|
+
while (callbacks.length > 0) {
|
|
1557
|
+
var callback = callbacks.shift();
|
|
1558
|
+
if (typeof callback == "function") {
|
|
1559
|
+
callback(Module);
|
|
1560
|
+
continue;
|
|
1561
|
+
}
|
|
1562
|
+
var func = callback.func;
|
|
1563
|
+
if (typeof func === "number") {
|
|
1564
|
+
if (callback.arg === undefined) {
|
|
1565
|
+
wasmTable.get(func)();
|
|
1566
|
+
}
|
|
1567
|
+
else {
|
|
1568
|
+
wasmTable.get(func)(callback.arg);
|
|
1569
|
+
}
|
|
1570
|
+
}
|
|
1571
|
+
else {
|
|
1572
|
+
func(callback.arg === undefined ? null : callback.arg);
|
|
1573
|
+
}
|
|
1574
|
+
}
|
|
1575
|
+
}
|
|
1576
|
+
function _abort() {
|
|
1577
|
+
abort();
|
|
1578
|
+
}
|
|
1579
|
+
var _emscripten_get_now;
|
|
1580
|
+
if (ENVIRONMENT_IS_NODE) {
|
|
1581
|
+
_emscripten_get_now = function () {
|
|
1582
|
+
var t = process["hrtime"]();
|
|
1583
|
+
return t[0] * 1e3 + t[1] / 1e6;
|
|
1584
|
+
};
|
|
1585
|
+
}
|
|
1586
|
+
else if (typeof dateNow !== "undefined") {
|
|
1587
|
+
_emscripten_get_now = dateNow;
|
|
1588
|
+
}
|
|
1589
|
+
else
|
|
1590
|
+
_emscripten_get_now = function () {
|
|
1591
|
+
return performance.now();
|
|
1592
|
+
};
|
|
1593
|
+
var _emscripten_get_now_is_monotonic = true;
|
|
1594
|
+
function setErrNo(value) {
|
|
1595
|
+
HEAP32[___errno_location() >> 2] = value;
|
|
1596
|
+
return value;
|
|
1597
|
+
}
|
|
1598
|
+
function _clock_gettime(clk_id, tp) {
|
|
1599
|
+
var now;
|
|
1600
|
+
if (clk_id === 0) {
|
|
1601
|
+
now = Date.now();
|
|
1602
|
+
}
|
|
1603
|
+
else if ((clk_id === 1 || clk_id === 4) && _emscripten_get_now_is_monotonic) {
|
|
1604
|
+
now = _emscripten_get_now();
|
|
1605
|
+
}
|
|
1606
|
+
else {
|
|
1607
|
+
setErrNo(28);
|
|
1608
|
+
return -1;
|
|
1609
|
+
}
|
|
1610
|
+
HEAP32[tp >> 2] = (now / 1e3) | 0;
|
|
1611
|
+
HEAP32[(tp + 4) >> 2] = ((now % 1e3) * 1e3 * 1e3) | 0;
|
|
1612
|
+
return 0;
|
|
1613
|
+
}
|
|
1614
|
+
function _dlopen(filename, flag) {
|
|
1615
|
+
abort("To use dlopen, you need to use Emscripten's linking support, see https://github.com/emscripten-core/emscripten/wiki/Linking");
|
|
1616
|
+
}
|
|
1617
|
+
function _dlsym(handle, symbol) {
|
|
1618
|
+
abort("To use dlopen, you need to use Emscripten's linking support, see https://github.com/emscripten-core/emscripten/wiki/Linking");
|
|
1619
|
+
}
|
|
1620
|
+
function _emscripten_memcpy_big(dest, src, num) {
|
|
1621
|
+
HEAPU8.copyWithin(dest, src, src + num);
|
|
1622
|
+
}
|
|
1623
|
+
function _emscripten_get_heap_size() {
|
|
1624
|
+
return HEAPU8.length;
|
|
1625
|
+
}
|
|
1626
|
+
function emscripten_realloc_buffer(size) {
|
|
1627
|
+
try {
|
|
1628
|
+
wasmMemory.grow((size - buffer.byteLength + 65535) >>> 16);
|
|
1629
|
+
updateGlobalBufferAndViews(wasmMemory.buffer);
|
|
1630
|
+
return 1;
|
|
1631
|
+
}
|
|
1632
|
+
catch (e) { }
|
|
1633
|
+
}
|
|
1634
|
+
function _emscripten_resize_heap(requestedSize) {
|
|
1635
|
+
var oldSize = _emscripten_get_heap_size();
|
|
1636
|
+
var maxHeapSize = 2147483648;
|
|
1637
|
+
if (requestedSize > maxHeapSize) {
|
|
1638
|
+
return false;
|
|
1639
|
+
}
|
|
1640
|
+
for (var cutDown = 1; cutDown <= 4; cutDown *= 2) {
|
|
1641
|
+
var overGrownHeapSize = oldSize * (1 + 0.2 / cutDown);
|
|
1642
|
+
overGrownHeapSize = Math.min(overGrownHeapSize, requestedSize + 100663296);
|
|
1643
|
+
var newSize = Math.min(maxHeapSize, alignUp(Math.max(requestedSize, overGrownHeapSize), 65536));
|
|
1644
|
+
var replacement = emscripten_realloc_buffer(newSize);
|
|
1645
|
+
if (replacement) {
|
|
1646
|
+
return true;
|
|
1647
|
+
}
|
|
1648
|
+
}
|
|
1649
|
+
return false;
|
|
1650
|
+
}
|
|
1651
|
+
function _emscripten_thread_sleep(msecs) {
|
|
1652
|
+
var start = _emscripten_get_now();
|
|
1653
|
+
while (_emscripten_get_now() - start < msecs) { }
|
|
1654
|
+
}
|
|
1655
|
+
var ENV = {};
|
|
1656
|
+
function getExecutableName() {
|
|
1657
|
+
return thisProgram || "./this.program";
|
|
1658
|
+
}
|
|
1659
|
+
function getEnvStrings() {
|
|
1660
|
+
if (!getEnvStrings.strings) {
|
|
1661
|
+
var lang = ((typeof navigator === "object" && navigator.languages && navigator.languages[0]) || "C").replace("-", "_") + ".UTF-8";
|
|
1662
|
+
var env = {
|
|
1663
|
+
USER: "web_user",
|
|
1664
|
+
LOGNAME: "web_user",
|
|
1665
|
+
PATH: "/",
|
|
1666
|
+
PWD: "/",
|
|
1667
|
+
HOME: "/home/web_user",
|
|
1668
|
+
LANG: lang,
|
|
1669
|
+
_: getExecutableName(),
|
|
1670
|
+
};
|
|
1671
|
+
for (var x in ENV) {
|
|
1672
|
+
env[x] = ENV[x];
|
|
1673
|
+
}
|
|
1674
|
+
var strings = [];
|
|
1675
|
+
for (var x in env) {
|
|
1676
|
+
strings.push(x + "=" + env[x]);
|
|
1677
|
+
}
|
|
1678
|
+
getEnvStrings.strings = strings;
|
|
1679
|
+
}
|
|
1680
|
+
return getEnvStrings.strings;
|
|
1681
|
+
}
|
|
1682
|
+
var SYSCALLS = {
|
|
1683
|
+
mappings: {},
|
|
1684
|
+
buffers: [null, [], []],
|
|
1685
|
+
printChar: function (stream, curr) {
|
|
1686
|
+
var buffer = SYSCALLS.buffers[stream];
|
|
1687
|
+
if (curr === 0 || curr === 10) {
|
|
1688
|
+
(stream === 1 ? out : err)(UTF8ArrayToString(buffer, 0));
|
|
1689
|
+
buffer.length = 0;
|
|
1690
|
+
}
|
|
1691
|
+
else {
|
|
1692
|
+
buffer.push(curr);
|
|
1693
|
+
}
|
|
1694
|
+
},
|
|
1695
|
+
varargs: undefined,
|
|
1696
|
+
get: function () {
|
|
1697
|
+
SYSCALLS.varargs += 4;
|
|
1698
|
+
var ret = HEAP32[(SYSCALLS.varargs - 4) >> 2];
|
|
1699
|
+
return ret;
|
|
1700
|
+
},
|
|
1701
|
+
getStr: function (ptr) {
|
|
1702
|
+
var ret = UTF8ToString(ptr);
|
|
1703
|
+
return ret;
|
|
1704
|
+
},
|
|
1705
|
+
get64: function (low, high) {
|
|
1706
|
+
return low;
|
|
1707
|
+
},
|
|
1708
|
+
};
|
|
1709
|
+
function _environ_get(__environ, environ_buf) {
|
|
1710
|
+
var bufSize = 0;
|
|
1711
|
+
getEnvStrings().forEach(function (string, i) {
|
|
1712
|
+
var ptr = environ_buf + bufSize;
|
|
1713
|
+
HEAP32[(__environ + i * 4) >> 2] = ptr;
|
|
1714
|
+
writeAsciiToMemory(string, ptr);
|
|
1715
|
+
bufSize += string.length + 1;
|
|
1716
|
+
});
|
|
1717
|
+
return 0;
|
|
1718
|
+
}
|
|
1719
|
+
function _environ_sizes_get(penviron_count, penviron_buf_size) {
|
|
1720
|
+
var strings = getEnvStrings();
|
|
1721
|
+
HEAP32[penviron_count >> 2] = strings.length;
|
|
1722
|
+
var bufSize = 0;
|
|
1723
|
+
strings.forEach(function (string) {
|
|
1724
|
+
bufSize += string.length + 1;
|
|
1725
|
+
});
|
|
1726
|
+
HEAP32[penviron_buf_size >> 2] = bufSize;
|
|
1727
|
+
return 0;
|
|
1728
|
+
}
|
|
1729
|
+
function _exit(status) {
|
|
1730
|
+
exit(status);
|
|
1731
|
+
}
|
|
1732
|
+
function _fd_close(fd) {
|
|
1733
|
+
return 0;
|
|
1734
|
+
}
|
|
1735
|
+
function _fd_seek(fd, offset_low, offset_high, whence, newOffset) { }
|
|
1736
|
+
function _fd_write(fd, iov, iovcnt, pnum) {
|
|
1737
|
+
var num = 0;
|
|
1738
|
+
for (var i = 0; i < iovcnt; i++) {
|
|
1739
|
+
var ptr = HEAP32[(iov + i * 8) >> 2];
|
|
1740
|
+
var len = HEAP32[(iov + (i * 8 + 4)) >> 2];
|
|
1741
|
+
for (var j = 0; j < len; j++) {
|
|
1742
|
+
SYSCALLS.printChar(fd, HEAPU8[ptr + j]);
|
|
1743
|
+
}
|
|
1744
|
+
num += len;
|
|
1745
|
+
}
|
|
1746
|
+
HEAP32[pnum >> 2] = num;
|
|
1747
|
+
return 0;
|
|
1748
|
+
}
|
|
1749
|
+
function _pthread_create() {
|
|
1750
|
+
return 6;
|
|
1751
|
+
}
|
|
1752
|
+
function _pthread_join() {
|
|
1753
|
+
return 28;
|
|
1754
|
+
}
|
|
1755
|
+
function _sysconf(name) {
|
|
1756
|
+
switch (name) {
|
|
1757
|
+
case 30:
|
|
1758
|
+
return 16384;
|
|
1759
|
+
case 85:
|
|
1760
|
+
var maxHeapSize = 2147483648;
|
|
1761
|
+
return maxHeapSize / 16384;
|
|
1762
|
+
case 132:
|
|
1763
|
+
case 133:
|
|
1764
|
+
case 12:
|
|
1765
|
+
case 137:
|
|
1766
|
+
case 138:
|
|
1767
|
+
case 15:
|
|
1768
|
+
case 235:
|
|
1769
|
+
case 16:
|
|
1770
|
+
case 17:
|
|
1771
|
+
case 18:
|
|
1772
|
+
case 19:
|
|
1773
|
+
case 20:
|
|
1774
|
+
case 149:
|
|
1775
|
+
case 13:
|
|
1776
|
+
case 10:
|
|
1777
|
+
case 236:
|
|
1778
|
+
case 153:
|
|
1779
|
+
case 9:
|
|
1780
|
+
case 21:
|
|
1781
|
+
case 22:
|
|
1782
|
+
case 159:
|
|
1783
|
+
case 154:
|
|
1784
|
+
case 14:
|
|
1785
|
+
case 77:
|
|
1786
|
+
case 78:
|
|
1787
|
+
case 139:
|
|
1788
|
+
case 82:
|
|
1789
|
+
case 68:
|
|
1790
|
+
case 67:
|
|
1791
|
+
case 164:
|
|
1792
|
+
case 11:
|
|
1793
|
+
case 29:
|
|
1794
|
+
case 47:
|
|
1795
|
+
case 48:
|
|
1796
|
+
case 95:
|
|
1797
|
+
case 52:
|
|
1798
|
+
case 51:
|
|
1799
|
+
case 46:
|
|
1800
|
+
return 200809;
|
|
1801
|
+
case 27:
|
|
1802
|
+
case 246:
|
|
1803
|
+
case 127:
|
|
1804
|
+
case 128:
|
|
1805
|
+
case 23:
|
|
1806
|
+
case 24:
|
|
1807
|
+
case 160:
|
|
1808
|
+
case 161:
|
|
1809
|
+
case 181:
|
|
1810
|
+
case 182:
|
|
1811
|
+
case 242:
|
|
1812
|
+
case 183:
|
|
1813
|
+
case 184:
|
|
1814
|
+
case 243:
|
|
1815
|
+
case 244:
|
|
1816
|
+
case 245:
|
|
1817
|
+
case 165:
|
|
1818
|
+
case 178:
|
|
1819
|
+
case 179:
|
|
1820
|
+
case 49:
|
|
1821
|
+
case 50:
|
|
1822
|
+
case 168:
|
|
1823
|
+
case 169:
|
|
1824
|
+
case 175:
|
|
1825
|
+
case 170:
|
|
1826
|
+
case 171:
|
|
1827
|
+
case 172:
|
|
1828
|
+
case 97:
|
|
1829
|
+
case 76:
|
|
1830
|
+
case 32:
|
|
1831
|
+
case 173:
|
|
1832
|
+
case 35:
|
|
1833
|
+
case 80:
|
|
1834
|
+
case 81:
|
|
1835
|
+
case 79:
|
|
1836
|
+
return -1;
|
|
1837
|
+
case 176:
|
|
1838
|
+
case 177:
|
|
1839
|
+
case 7:
|
|
1840
|
+
case 155:
|
|
1841
|
+
case 8:
|
|
1842
|
+
case 157:
|
|
1843
|
+
case 125:
|
|
1844
|
+
case 126:
|
|
1845
|
+
case 92:
|
|
1846
|
+
case 93:
|
|
1847
|
+
case 129:
|
|
1848
|
+
case 130:
|
|
1849
|
+
case 131:
|
|
1850
|
+
case 94:
|
|
1851
|
+
case 91:
|
|
1852
|
+
return 1;
|
|
1853
|
+
case 74:
|
|
1854
|
+
case 60:
|
|
1855
|
+
case 69:
|
|
1856
|
+
case 70:
|
|
1857
|
+
case 4:
|
|
1858
|
+
return 1024;
|
|
1859
|
+
case 31:
|
|
1860
|
+
case 42:
|
|
1861
|
+
case 72:
|
|
1862
|
+
return 32;
|
|
1863
|
+
case 87:
|
|
1864
|
+
case 26:
|
|
1865
|
+
case 33:
|
|
1866
|
+
return 2147483647;
|
|
1867
|
+
case 34:
|
|
1868
|
+
case 1:
|
|
1869
|
+
return 47839;
|
|
1870
|
+
case 38:
|
|
1871
|
+
case 36:
|
|
1872
|
+
return 99;
|
|
1873
|
+
case 43:
|
|
1874
|
+
case 37:
|
|
1875
|
+
return 2048;
|
|
1876
|
+
case 0:
|
|
1877
|
+
return 2097152;
|
|
1878
|
+
case 3:
|
|
1879
|
+
return 65536;
|
|
1880
|
+
case 28:
|
|
1881
|
+
return 32768;
|
|
1882
|
+
case 44:
|
|
1883
|
+
return 32767;
|
|
1884
|
+
case 75:
|
|
1885
|
+
return 16384;
|
|
1886
|
+
case 39:
|
|
1887
|
+
return 1e3;
|
|
1888
|
+
case 89:
|
|
1889
|
+
return 700;
|
|
1890
|
+
case 71:
|
|
1891
|
+
return 256;
|
|
1892
|
+
case 40:
|
|
1893
|
+
return 255;
|
|
1894
|
+
case 2:
|
|
1895
|
+
return 100;
|
|
1896
|
+
case 180:
|
|
1897
|
+
return 64;
|
|
1898
|
+
case 25:
|
|
1899
|
+
return 20;
|
|
1900
|
+
case 5:
|
|
1901
|
+
return 16;
|
|
1902
|
+
case 6:
|
|
1903
|
+
return 6;
|
|
1904
|
+
case 73:
|
|
1905
|
+
return 4;
|
|
1906
|
+
case 84: {
|
|
1907
|
+
if (typeof navigator === "object")
|
|
1908
|
+
return navigator["hardwareConcurrency"] || 1;
|
|
1909
|
+
return 1;
|
|
1910
|
+
}
|
|
1911
|
+
}
|
|
1912
|
+
setErrNo(28);
|
|
1913
|
+
return -1;
|
|
1914
|
+
}
|
|
1915
|
+
var asmLibraryArg = {
|
|
1916
|
+
a: _abort,
|
|
1917
|
+
n: _clock_gettime,
|
|
1918
|
+
i: _dlopen,
|
|
1919
|
+
e: _dlsym,
|
|
1920
|
+
l: _emscripten_memcpy_big,
|
|
1921
|
+
m: _emscripten_resize_heap,
|
|
1922
|
+
o: _emscripten_thread_sleep,
|
|
1923
|
+
p: _environ_get,
|
|
1924
|
+
g: _environ_sizes_get,
|
|
1925
|
+
j: _exit,
|
|
1926
|
+
h: _fd_close,
|
|
1927
|
+
k: _fd_seek,
|
|
1928
|
+
c: _fd_write,
|
|
1929
|
+
d: _pthread_create,
|
|
1930
|
+
f: _pthread_join,
|
|
1931
|
+
b: _sysconf,
|
|
1932
|
+
};
|
|
1933
|
+
createWasm();
|
|
1934
|
+
var ___wasm_call_ctors = (Module["___wasm_call_ctors"] = function () {
|
|
1935
|
+
return (___wasm_call_ctors = Module["___wasm_call_ctors"] = Module["asm"]["r"]).apply(null, arguments);
|
|
1936
|
+
});
|
|
1937
|
+
(Module["_getModelBufferMemoryOffset"] = function () {
|
|
1938
|
+
return (Module["_getModelBufferMemoryOffset"] = Module["asm"]["s"]).apply(null, arguments);
|
|
1939
|
+
});
|
|
1940
|
+
(Module["_getInputMemoryOffset"] = function () {
|
|
1941
|
+
return (Module["_getInputMemoryOffset"] = Module["asm"]["t"]).apply(null, arguments);
|
|
1942
|
+
});
|
|
1943
|
+
(Module["_getInputHeight"] = function () {
|
|
1944
|
+
return (Module["_getInputHeight"] = Module["asm"]["u"]).apply(null, arguments);
|
|
1945
|
+
});
|
|
1946
|
+
(Module["_getInputWidth"] = function () {
|
|
1947
|
+
return (Module["_getInputWidth"] = Module["asm"]["v"]).apply(null, arguments);
|
|
1948
|
+
});
|
|
1949
|
+
(Module["_getInputChannelCount"] = function () {
|
|
1950
|
+
return (Module["_getInputChannelCount"] = Module["asm"]["w"]).apply(null, arguments);
|
|
1951
|
+
});
|
|
1952
|
+
(Module["_getOutputMemoryOffset"] = function () {
|
|
1953
|
+
return (Module["_getOutputMemoryOffset"] = Module["asm"]["x"]).apply(null, arguments);
|
|
1954
|
+
});
|
|
1955
|
+
(Module["_getOutputHeight"] = function () {
|
|
1956
|
+
return (Module["_getOutputHeight"] = Module["asm"]["y"]).apply(null, arguments);
|
|
1957
|
+
});
|
|
1958
|
+
(Module["_getOutputWidth"] = function () {
|
|
1959
|
+
return (Module["_getOutputWidth"] = Module["asm"]["z"]).apply(null, arguments);
|
|
1960
|
+
});
|
|
1961
|
+
(Module["_getOutputChannelCount"] = function () {
|
|
1962
|
+
return (Module["_getOutputChannelCount"] = Module["asm"]["A"]).apply(null, arguments);
|
|
1963
|
+
});
|
|
1964
|
+
(Module["_loadModel"] = function () {
|
|
1965
|
+
return (Module["_loadModel"] = Module["asm"]["B"]).apply(null, arguments);
|
|
1966
|
+
});
|
|
1967
|
+
(Module["_runInference"] = function () {
|
|
1968
|
+
return (Module["_runInference"] = Module["asm"]["C"]).apply(null, arguments);
|
|
1969
|
+
});
|
|
1970
|
+
var ___errno_location = (Module["___errno_location"] = function () {
|
|
1971
|
+
return (___errno_location = Module["___errno_location"] = Module["asm"]["E"]).apply(null, arguments);
|
|
1972
|
+
});
|
|
1973
|
+
var calledRun;
|
|
1974
|
+
function ExitStatus(status) {
|
|
1975
|
+
this.name = "ExitStatus";
|
|
1976
|
+
this.message = "Program terminated with exit(" + status + ")";
|
|
1977
|
+
this.status = status;
|
|
1978
|
+
}
|
|
1979
|
+
dependenciesFulfilled = function runCaller() {
|
|
1980
|
+
if (!calledRun)
|
|
1981
|
+
run();
|
|
1982
|
+
if (!calledRun)
|
|
1983
|
+
dependenciesFulfilled = runCaller;
|
|
1984
|
+
};
|
|
1985
|
+
function run(args) {
|
|
1986
|
+
if (runDependencies > 0) {
|
|
1987
|
+
return;
|
|
1988
|
+
}
|
|
1989
|
+
preRun();
|
|
1990
|
+
if (runDependencies > 0) {
|
|
1991
|
+
return;
|
|
1992
|
+
}
|
|
1993
|
+
function doRun() {
|
|
1994
|
+
if (calledRun)
|
|
1995
|
+
return;
|
|
1996
|
+
calledRun = true;
|
|
1997
|
+
Module["calledRun"] = true;
|
|
1998
|
+
if (ABORT)
|
|
1999
|
+
return;
|
|
2000
|
+
initRuntime();
|
|
2001
|
+
preMain();
|
|
2002
|
+
readyPromiseResolve(Module);
|
|
2003
|
+
if (Module["onRuntimeInitialized"])
|
|
2004
|
+
Module["onRuntimeInitialized"]();
|
|
2005
|
+
postRun();
|
|
2006
|
+
}
|
|
2007
|
+
if (Module["setStatus"]) {
|
|
2008
|
+
Module["setStatus"]("Running...");
|
|
2009
|
+
setTimeout(function () {
|
|
2010
|
+
setTimeout(function () {
|
|
2011
|
+
Module["setStatus"]("");
|
|
2012
|
+
}, 1);
|
|
2013
|
+
doRun();
|
|
2014
|
+
}, 1);
|
|
2015
|
+
}
|
|
2016
|
+
else {
|
|
2017
|
+
doRun();
|
|
2018
|
+
}
|
|
2019
|
+
}
|
|
2020
|
+
Module["run"] = run;
|
|
2021
|
+
function exit(status, implicit) {
|
|
2022
|
+
quit_(status, new ExitStatus(status));
|
|
2023
|
+
}
|
|
2024
|
+
if (Module["preInit"]) {
|
|
2025
|
+
if (typeof Module["preInit"] == "function")
|
|
2026
|
+
Module["preInit"] = [Module["preInit"]];
|
|
2027
|
+
while (Module["preInit"].length > 0) {
|
|
2028
|
+
Module["preInit"].pop()();
|
|
2029
|
+
}
|
|
2030
|
+
}
|
|
2031
|
+
run();
|
|
2032
|
+
return createTFLiteSIMDModule.ready;
|
|
2033
|
+
};
|
|
2034
|
+
})();
|
|
2035
|
+
|
|
2036
|
+
var tfliteWasmUrl = "./assets/tflite/tflite.wasm";
|
|
2037
|
+
|
|
2038
|
+
var tfliteSimdWasmUrl = "./assets/tflite/tflite-simd.wasm";
|
|
2039
|
+
|
|
2040
|
+
var modelMeetLiteUrl = "./assets/models/segm_lite_v681.tflite";
|
|
2041
|
+
|
|
2042
|
+
var modelMeetFullUrl = "./assets/models/segm_full_v679.tflite";
|
|
2043
|
+
|
|
2044
|
+
var modelMLKitUrl = "./assets/models/selfiesegmentation_mlkit-256x256-2021_01_19-v1215.f16.tflite";
|
|
2045
|
+
|
|
2046
|
+
const SEGMENTATIONMODEL_TYPE_BACKGROUND_PERSON = 1;
|
|
2047
|
+
const SEGMENTATIONMODEL_TYPE_PERSON = 2;
|
|
2048
|
+
const fixWebPackGeneratedFileUrl = (url) => {
|
|
2049
|
+
return new URL(url, (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href))).href;
|
|
2050
|
+
};
|
|
2051
|
+
const models = {
|
|
2052
|
+
meetlite: {
|
|
2053
|
+
url: fixWebPackGeneratedFileUrl(modelMeetLiteUrl),
|
|
2054
|
+
type: SEGMENTATIONMODEL_TYPE_BACKGROUND_PERSON,
|
|
2055
|
+
},
|
|
2056
|
+
meetfull: {
|
|
2057
|
+
url: fixWebPackGeneratedFileUrl(modelMeetFullUrl),
|
|
2058
|
+
type: SEGMENTATIONMODEL_TYPE_BACKGROUND_PERSON,
|
|
2059
|
+
},
|
|
2060
|
+
mlkit: {
|
|
2061
|
+
url: fixWebPackGeneratedFileUrl(modelMLKitUrl),
|
|
2062
|
+
type: SEGMENTATIONMODEL_TYPE_PERSON,
|
|
2063
|
+
},
|
|
2064
|
+
};
|
|
2065
|
+
let _tflite = null;
|
|
2066
|
+
const loadTFLite = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
2067
|
+
if (_tflite)
|
|
2068
|
+
return _tflite;
|
|
2069
|
+
const simdIsSupported = yield wasmFeatureDetect.simd();
|
|
2070
|
+
_tflite = yield (simdIsSupported ? createTFLiteSIMDModule : createTFLiteModule)({
|
|
2071
|
+
locateFile(path) {
|
|
2072
|
+
if (path.endsWith(".wasm")) {
|
|
2073
|
+
return fixWebPackGeneratedFileUrl(simdIsSupported ? tfliteSimdWasmUrl : tfliteWasmUrl);
|
|
2074
|
+
}
|
|
2075
|
+
return path;
|
|
2076
|
+
},
|
|
2077
|
+
});
|
|
2078
|
+
return _tflite;
|
|
2079
|
+
});
|
|
2080
|
+
let _currentModelAndInfo;
|
|
2081
|
+
const loadSegmentationModel = (segmentationModelId) => __awaiter(void 0, void 0, void 0, function* () {
|
|
2082
|
+
const model = models[segmentationModelId];
|
|
2083
|
+
const tflite = yield loadTFLite();
|
|
2084
|
+
if ((_currentModelAndInfo === null || _currentModelAndInfo === void 0 ? void 0 : _currentModelAndInfo.url) === model.url)
|
|
2085
|
+
return _currentModelAndInfo;
|
|
2086
|
+
const modelResponse = yield fetch(model.url);
|
|
2087
|
+
const modelBuffer = yield modelResponse.arrayBuffer();
|
|
2088
|
+
const modelBufferOffset = tflite._getModelBufferMemoryOffset();
|
|
2089
|
+
tflite.HEAPU8.set(new Uint8Array(modelBuffer), modelBufferOffset);
|
|
2090
|
+
tflite._loadModel(modelBuffer.byteLength);
|
|
2091
|
+
const inputHeight = tflite._getInputHeight();
|
|
2092
|
+
const inputWidth = tflite._getInputWidth();
|
|
2093
|
+
const inputMemoryOffset = tflite._getInputMemoryOffset() / 4;
|
|
2094
|
+
const outputMemoryOffset = tflite._getOutputMemoryOffset() / 4;
|
|
2095
|
+
const segmentationPixelCount = inputWidth * inputHeight;
|
|
2096
|
+
tflite._runInference();
|
|
2097
|
+
_currentModelAndInfo = {
|
|
2098
|
+
tflite,
|
|
2099
|
+
model,
|
|
2100
|
+
url: model.url,
|
|
2101
|
+
inputHeight,
|
|
2102
|
+
inputWidth,
|
|
2103
|
+
inputMemoryOffset,
|
|
2104
|
+
outputMemoryOffset,
|
|
2105
|
+
segmentationPixelCount,
|
|
2106
|
+
};
|
|
2107
|
+
return _currentModelAndInfo;
|
|
2108
|
+
});
|
|
2109
|
+
|
|
2110
|
+
const baseCanvasParams = {
|
|
2111
|
+
maskOperation: "copy",
|
|
2112
|
+
maskFilter: "blur(4px)",
|
|
2113
|
+
personOperation: "source-in",
|
|
2114
|
+
personFilter: "none",
|
|
2115
|
+
backgroundOperation: "destination-over",
|
|
2116
|
+
backgroundFilter: "none",
|
|
2117
|
+
};
|
|
2118
|
+
function getBlurCanvasParams(amount) {
|
|
2119
|
+
switch (amount) {
|
|
2120
|
+
case "slight": {
|
|
2121
|
+
return Object.assign(Object.assign({}, baseCanvasParams), { backgroundFilter: "blur(4px)", crop: 4 });
|
|
2122
|
+
}
|
|
2123
|
+
case "heavy": {
|
|
2124
|
+
return Object.assign(Object.assign({}, baseCanvasParams), { maskFilter: "blur(8px)", backgroundFilter: "blur(16px)", crop: 16 });
|
|
2125
|
+
}
|
|
2126
|
+
}
|
|
2127
|
+
return Object.assign(Object.assign({}, baseCanvasParams), { maskFilter: "blur(8px)", backgroundFilter: "blur(8px)", crop: 8 });
|
|
2128
|
+
}
|
|
2129
|
+
function getCanvasParams(params) {
|
|
2130
|
+
if (params.backgroundBlur)
|
|
2131
|
+
return getBlurCanvasParams(params.backgroundBlur.amount);
|
|
2132
|
+
return baseCanvasParams;
|
|
2133
|
+
}
|
|
2134
|
+
function createCanvasEngine(videoWidth, videoHeight, setup, effectCanvas, params) {
|
|
2135
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
2136
|
+
const { tflite, model: segmentationModel, inputHeight, inputWidth, inputMemoryOffset, outputMemoryOffset, segmentationPixelCount, } = yield loadSegmentationModel(setup.segmentationModelId);
|
|
2137
|
+
let canvasParams = getCanvasParams(params);
|
|
2138
|
+
const segmentationCanvas = createCanvas(inputWidth, inputHeight);
|
|
2139
|
+
const segmentationCtx = segmentationCanvas.getContext("2d", {
|
|
2140
|
+
willReadFrequently: true,
|
|
2141
|
+
});
|
|
2142
|
+
const segmentationMask = new ImageData(inputWidth, inputHeight);
|
|
2143
|
+
let currentBackgroundFrame = null;
|
|
2144
|
+
let backgroundDimensions = { x: 0, y: 0, width: 0, height: 0 };
|
|
2145
|
+
const videoAspectRatio = videoWidth / videoHeight;
|
|
2146
|
+
let cropX = 0;
|
|
2147
|
+
let cropY = 0;
|
|
2148
|
+
const updateCrop = () => {
|
|
2149
|
+
const crop = canvasParams.crop || 0;
|
|
2150
|
+
if (crop) {
|
|
2151
|
+
cropX = videoAspectRatio > 1 ? Math.round(crop * videoAspectRatio) : crop;
|
|
2152
|
+
cropY = videoAspectRatio < 1 ? Math.round(crop / videoAspectRatio) : crop;
|
|
2153
|
+
}
|
|
2154
|
+
else {
|
|
2155
|
+
cropX = 0;
|
|
2156
|
+
cropY = 0;
|
|
2157
|
+
}
|
|
2158
|
+
};
|
|
2159
|
+
updateCrop();
|
|
2160
|
+
const effectCtx = effectCanvas.getContext("2d");
|
|
2161
|
+
return {
|
|
2162
|
+
effectCtx,
|
|
2163
|
+
updateBackgroundFrame(frame, dimensions) {
|
|
2164
|
+
currentBackgroundFrame = frame;
|
|
2165
|
+
if (dimensions)
|
|
2166
|
+
backgroundDimensions = dimensions;
|
|
2167
|
+
},
|
|
2168
|
+
updateParams(updatedParams) {
|
|
2169
|
+
canvasParams = getCanvasParams(updatedParams);
|
|
2170
|
+
updateCrop();
|
|
2171
|
+
},
|
|
2172
|
+
processFrame(frame) {
|
|
2173
|
+
segmentationCtx.drawImage(frame, 0, 0, videoWidth, videoHeight, 0, 0, inputWidth, inputHeight);
|
|
2174
|
+
const imageData = segmentationCtx.getImageData(0, 0, inputWidth, inputHeight);
|
|
2175
|
+
for (let i = 0; i < segmentationPixelCount; i++) {
|
|
2176
|
+
tflite.HEAPF32[inputMemoryOffset + i * 3] = imageData.data[i * 4] / 255;
|
|
2177
|
+
tflite.HEAPF32[inputMemoryOffset + i * 3 + 1] = imageData.data[i * 4 + 1] / 255;
|
|
2178
|
+
tflite.HEAPF32[inputMemoryOffset + i * 3 + 2] = imageData.data[i * 4 + 2] / 255;
|
|
2179
|
+
}
|
|
2180
|
+
tflite._runInference();
|
|
2181
|
+
for (let i = 0; i < segmentationPixelCount; i++) {
|
|
2182
|
+
if (segmentationModel.type === SEGMENTATIONMODEL_TYPE_BACKGROUND_PERSON) {
|
|
2183
|
+
const background = tflite.HEAPF32[outputMemoryOffset + i * 2];
|
|
2184
|
+
const person = tflite.HEAPF32[outputMemoryOffset + i * 2 + 1];
|
|
2185
|
+
const shift = Math.max(background, person);
|
|
2186
|
+
const backgroundExp = Math.exp(background - shift);
|
|
2187
|
+
const personExp = Math.exp(person - shift);
|
|
2188
|
+
segmentationMask.data[i * 4 + 3] = (255 * personExp) / (backgroundExp + personExp);
|
|
2189
|
+
}
|
|
2190
|
+
else if (segmentationModel.type === SEGMENTATIONMODEL_TYPE_PERSON) {
|
|
2191
|
+
const person = tflite.HEAPF32[outputMemoryOffset + i];
|
|
2192
|
+
segmentationMask.data[i * 4 + 3] = 255 * person;
|
|
2193
|
+
}
|
|
2194
|
+
}
|
|
2195
|
+
segmentationCtx.putImageData(segmentationMask, 0, 0);
|
|
2196
|
+
effectCtx.globalCompositeOperation = canvasParams.maskOperation;
|
|
2197
|
+
effectCtx.filter = canvasParams.maskFilter;
|
|
2198
|
+
effectCtx.drawImage(segmentationCanvas, 0, 0, inputWidth, inputHeight, 0, 0, videoWidth, videoHeight);
|
|
2199
|
+
effectCtx.globalCompositeOperation = canvasParams.personOperation;
|
|
2200
|
+
effectCtx.filter = canvasParams.personFilter;
|
|
2201
|
+
effectCtx.drawImage(frame, 0, 0);
|
|
2202
|
+
effectCtx.globalCompositeOperation = canvasParams.backgroundOperation;
|
|
2203
|
+
effectCtx.filter = canvasParams.backgroundFilter;
|
|
2204
|
+
if (currentBackgroundFrame) {
|
|
2205
|
+
effectCtx.drawImage(currentBackgroundFrame, backgroundDimensions.x, backgroundDimensions.y, backgroundDimensions.width, backgroundDimensions.height, 0, 0, videoWidth, videoHeight, 0);
|
|
2206
|
+
}
|
|
2207
|
+
else {
|
|
2208
|
+
effectCtx.drawImage(frame, 0, 0);
|
|
2209
|
+
}
|
|
2210
|
+
if (cropX || cropY) {
|
|
2211
|
+
effectCtx.globalCompositeOperation = "source-over";
|
|
2212
|
+
effectCtx.filter = "none";
|
|
2213
|
+
effectCtx.drawImage(effectCanvas, cropX, cropY, videoWidth - cropX * 2, videoHeight - cropY * 2, 0, 0, videoWidth, videoHeight);
|
|
2214
|
+
}
|
|
2215
|
+
},
|
|
2216
|
+
dispose() { },
|
|
2217
|
+
};
|
|
2218
|
+
});
|
|
2219
|
+
}
|
|
2220
|
+
|
|
2221
|
+
const glsl = String.raw;
|
|
2222
|
+
function createProgram(gl, vertexShader, fragmentShader) {
|
|
2223
|
+
const program = gl.createProgram();
|
|
2224
|
+
gl.attachShader(program, vertexShader);
|
|
2225
|
+
gl.attachShader(program, fragmentShader);
|
|
2226
|
+
gl.linkProgram(program);
|
|
2227
|
+
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
|
|
2228
|
+
throw new Error(`Could not link WebGL program: ${gl.getProgramInfoLog(program)}`);
|
|
2229
|
+
}
|
|
2230
|
+
return program;
|
|
2231
|
+
}
|
|
2232
|
+
function createPiplelineStageProgram(gl, vertexShader, fragmentShader, positionBuffer, texCoordBuffer) {
|
|
2233
|
+
const program = createProgram(gl, vertexShader, fragmentShader);
|
|
2234
|
+
const positionAttributeLocation = gl.getAttribLocation(program, "a_position");
|
|
2235
|
+
gl.enableVertexAttribArray(positionAttributeLocation);
|
|
2236
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
|
|
2237
|
+
gl.vertexAttribPointer(positionAttributeLocation, 2, gl.FLOAT, false, 0, 0);
|
|
2238
|
+
const texCoordAttributeLocation = gl.getAttribLocation(program, "a_texCoord");
|
|
2239
|
+
gl.enableVertexAttribArray(texCoordAttributeLocation);
|
|
2240
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, texCoordBuffer);
|
|
2241
|
+
gl.vertexAttribPointer(texCoordAttributeLocation, 2, gl.FLOAT, false, 0, 0);
|
|
2242
|
+
return program;
|
|
2243
|
+
}
|
|
2244
|
+
function compileShader(gl, shaderType, shaderSource) {
|
|
2245
|
+
const shader = gl.createShader(shaderType);
|
|
2246
|
+
gl.shaderSource(shader, shaderSource);
|
|
2247
|
+
gl.compileShader(shader);
|
|
2248
|
+
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
|
|
2249
|
+
throw new Error(`Could not compile shader: ${gl.getShaderInfoLog(shader)}`);
|
|
2250
|
+
}
|
|
2251
|
+
return shader;
|
|
2252
|
+
}
|
|
2253
|
+
function createTexture(gl, internalformat, width, height, minFilter = gl.NEAREST, magFilter = gl.NEAREST) {
|
|
2254
|
+
const texture = gl.createTexture();
|
|
2255
|
+
gl.bindTexture(gl.TEXTURE_2D, texture);
|
|
2256
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
|
2257
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
|
2258
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, minFilter);
|
|
2259
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, magFilter);
|
|
2260
|
+
gl.texStorage2D(gl.TEXTURE_2D, 1, internalformat, width, height);
|
|
2261
|
+
return texture;
|
|
2262
|
+
}
|
|
2263
|
+
|
|
2264
|
+
function generateOptimizedGaussianKernel(sigma) {
|
|
2265
|
+
const g = (x) => Math.exp(-(x * x) / (2 * sigma * sigma));
|
|
2266
|
+
const numbers = [];
|
|
2267
|
+
let x = 0;
|
|
2268
|
+
let currentSum = 0;
|
|
2269
|
+
while (true) {
|
|
2270
|
+
const n = g(x);
|
|
2271
|
+
const nextSum = currentSum + (currentSum ? n * 2 : n);
|
|
2272
|
+
if (currentSum && n / nextSum < 0.01 && numbers.length % 2)
|
|
2273
|
+
break;
|
|
2274
|
+
currentSum = nextSum;
|
|
2275
|
+
numbers.push(n);
|
|
2276
|
+
x++;
|
|
2277
|
+
}
|
|
2278
|
+
const weights = numbers.map((n) => n / currentSum);
|
|
2279
|
+
const optimizedWeights = [weights[0]];
|
|
2280
|
+
const optimizedPositions = [0];
|
|
2281
|
+
if (weights.length >= 3 && weights.length % 2 === 1) {
|
|
2282
|
+
for (let i = 1; i < weights.length; i += 2) {
|
|
2283
|
+
const w1 = weights[i];
|
|
2284
|
+
const w2 = weights[i + 1];
|
|
2285
|
+
const w = w1 + w2;
|
|
2286
|
+
const p = w2 / (w1 + w2);
|
|
2287
|
+
optimizedWeights.push(w);
|
|
2288
|
+
optimizedPositions.push(i + p);
|
|
2289
|
+
}
|
|
2290
|
+
}
|
|
2291
|
+
return {
|
|
2292
|
+
weights: optimizedWeights,
|
|
2293
|
+
positions: optimizedPositions,
|
|
2294
|
+
};
|
|
2295
|
+
}
|
|
2296
|
+
function buildBlurPass(gl, vertexShader, positionBuffer, texCoordBuffer, personMaskTexture, outputWidth, outputHeight, params) {
|
|
2297
|
+
const fragmentShaderSource = glsl `#version 300 es
|
|
2298
|
+
precision highp float;
|
|
2299
|
+
uniform sampler2D u_inputFrame;
|
|
2300
|
+
uniform sampler2D u_personMask;
|
|
2301
|
+
uniform float u_weight[20];
|
|
2302
|
+
uniform float u_offset[20];
|
|
2303
|
+
uniform int u_ksize;
|
|
2304
|
+
uniform vec2 u_texelSize;
|
|
2305
|
+
in vec2 v_texCoord;
|
|
2306
|
+
out vec4 outColor;
|
|
2307
|
+
void main() {
|
|
2308
|
+
vec4 centerColor = texture(u_inputFrame, v_texCoord);
|
|
2309
|
+
float personMask = texture(u_personMask, v_texCoord).a;
|
|
2310
|
+
vec4 frameColor = centerColor * u_weight[0] * (1.0 - personMask);
|
|
2311
|
+
for (int i = 1; i < u_ksize; i++) {
|
|
2312
|
+
vec2 offset = vec2(u_offset[i]) * u_texelSize;
|
|
2313
|
+
vec2 texCoord = v_texCoord + offset;
|
|
2314
|
+
frameColor += texture(u_inputFrame, texCoord) * u_weight[i] *
|
|
2315
|
+
(1.0 - texture(u_personMask, texCoord).a);
|
|
2316
|
+
texCoord = v_texCoord - offset;
|
|
2317
|
+
frameColor += texture(u_inputFrame, texCoord) * u_weight[i] *
|
|
2318
|
+
(1.0 - texture(u_personMask, texCoord).a);
|
|
2319
|
+
}
|
|
2320
|
+
outColor = vec4(frameColor.rgb + (1.0 - frameColor.a) * centerColor.rgb, 1.0);
|
|
2321
|
+
}`;
|
|
2322
|
+
const scale = params.backgroundResampleScale || 0.2;
|
|
2323
|
+
const blurredBackgroundWidth = Math.round(outputWidth * scale);
|
|
2324
|
+
const blurredBackgroundHeight = Math.round(outputHeight * scale);
|
|
2325
|
+
const texelWidth = 1 / blurredBackgroundWidth;
|
|
2326
|
+
const texelHeight = 1 / blurredBackgroundHeight;
|
|
2327
|
+
const fragmentShader = compileShader(gl, gl.FRAGMENT_SHADER, fragmentShaderSource);
|
|
2328
|
+
const program = createPiplelineStageProgram(gl, vertexShader, fragmentShader, positionBuffer, texCoordBuffer);
|
|
2329
|
+
const inputFrameLocation = gl.getUniformLocation(program, "u_inputFrame");
|
|
2330
|
+
const personMaskLocation = gl.getUniformLocation(program, "u_personMask");
|
|
2331
|
+
const texelSizeLocation = gl.getUniformLocation(program, "u_texelSize");
|
|
2332
|
+
const weightLocation = gl.getUniformLocation(program, "u_weight");
|
|
2333
|
+
const offsetLocation = gl.getUniformLocation(program, "u_offset");
|
|
2334
|
+
const kernelsizeLocation = gl.getUniformLocation(program, "u_ksize");
|
|
2335
|
+
const texture1 = createTexture(gl, gl.RGBA8, blurredBackgroundWidth, blurredBackgroundHeight, gl.NEAREST, gl.LINEAR);
|
|
2336
|
+
const texture2 = createTexture(gl, gl.RGBA8, blurredBackgroundWidth, blurredBackgroundHeight, gl.NEAREST, gl.LINEAR);
|
|
2337
|
+
const frameBuffer1 = gl.createFramebuffer();
|
|
2338
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, frameBuffer1);
|
|
2339
|
+
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture1, 0);
|
|
2340
|
+
const frameBuffer2 = gl.createFramebuffer();
|
|
2341
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, frameBuffer2);
|
|
2342
|
+
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture2, 0);
|
|
2343
|
+
gl.useProgram(program);
|
|
2344
|
+
gl.uniform1i(personMaskLocation, 1);
|
|
2345
|
+
function updateKernel(kernel = "5x5") {
|
|
2346
|
+
gl.useProgram(program);
|
|
2347
|
+
if (kernel === "9x9") {
|
|
2348
|
+
gl.uniform1i(kernelsizeLocation, 5);
|
|
2349
|
+
gl.uniform1fv(weightLocation, [0.227027027, 0.1945945946, 0.1216216216, 0.0540540541, 0.0162162162]);
|
|
2350
|
+
gl.uniform1fv(offsetLocation, [0.0, 1.0, 2.0, 3.0, 4.0]);
|
|
2351
|
+
}
|
|
2352
|
+
else if (kernel === "9x9o") {
|
|
2353
|
+
gl.uniform1i(kernelsizeLocation, 3);
|
|
2354
|
+
gl.uniform1fv(weightLocation, [0.2734375, 0.328125, 0.03515625]);
|
|
2355
|
+
gl.uniform1fv(offsetLocation, [0.0, 1.3333333333333333, 3.111111111111111]);
|
|
2356
|
+
}
|
|
2357
|
+
else if (kernel === "7x7") {
|
|
2358
|
+
gl.uniform1i(kernelsizeLocation, 4);
|
|
2359
|
+
gl.uniform1fv(weightLocation, [0.3125, 0.234375, 0.09375, 0.015625]);
|
|
2360
|
+
gl.uniform1fv(offsetLocation, [0.0, 1.0, 2.0, 3.0]);
|
|
2361
|
+
}
|
|
2362
|
+
else if (kernel === "5x5") {
|
|
2363
|
+
gl.uniform1i(kernelsizeLocation, 3);
|
|
2364
|
+
gl.uniform1fv(weightLocation, [0.3715, 0.25, 0.0625]);
|
|
2365
|
+
gl.uniform1fv(offsetLocation, [0.0, 1.0, 2.0]);
|
|
2366
|
+
}
|
|
2367
|
+
else if (kernel === "3x3") {
|
|
2368
|
+
gl.uniform1i(kernelsizeLocation, 2);
|
|
2369
|
+
gl.uniform1fv(weightLocation, [0.5, 0.25]);
|
|
2370
|
+
gl.uniform1fv(offsetLocation, [0.0, 1.0]);
|
|
2371
|
+
}
|
|
2372
|
+
else if (kernel === "101o") {
|
|
2373
|
+
gl.uniform1i(kernelsizeLocation, 6);
|
|
2374
|
+
gl.uniform1fv(weightLocation, [
|
|
2375
|
+
0.0824930077775077, 0.1570851015371093, 0.12909299748961606, 0.09061657810797075,
|
|
2376
|
+
0.05427127182510044, 0.027687547151449544,
|
|
2377
|
+
]);
|
|
2378
|
+
gl.uniform1fv(offsetLocation, [0, 1.4851485148514851, 3.4653465346534653, 5.445544554455446, 7.425742574257426, 9.405940594059405]);
|
|
2379
|
+
}
|
|
2380
|
+
else if (kernel === "171o") {
|
|
2381
|
+
gl.uniform1i(kernelsizeLocation, 7);
|
|
2382
|
+
gl.uniform1fv(weightLocation, [
|
|
2383
|
+
0.06465218005811185, 0.1255973586132927, 0.11180602454697099, 0.09067345726996107,
|
|
2384
|
+
0.06697712131207971, 0.04504642671896425, 0.027573521509675278,
|
|
2385
|
+
]);
|
|
2386
|
+
gl.uniform1fv(offsetLocation, [
|
|
2387
|
+
0, 1.4912280701754388, 3.4795321637426904, 5.4678362573099415, 7.456140350877193, 9.444444444444445,
|
|
2388
|
+
11.432748538011696,
|
|
2389
|
+
]);
|
|
2390
|
+
}
|
|
2391
|
+
else if (kernel.startsWith("og")) {
|
|
2392
|
+
const s = parseFloat(kernel.substring(2));
|
|
2393
|
+
const { weights, positions } = generateOptimizedGaussianKernel(s);
|
|
2394
|
+
gl.uniform1i(kernelsizeLocation, weights.length);
|
|
2395
|
+
gl.uniform1fv(weightLocation, weights);
|
|
2396
|
+
gl.uniform1fv(offsetLocation, positions);
|
|
2397
|
+
}
|
|
2398
|
+
}
|
|
2399
|
+
let numPasses = 1;
|
|
2400
|
+
function updateParams(params) {
|
|
2401
|
+
updateKernel(params.kernel);
|
|
2402
|
+
numPasses = params.numPasses || 1;
|
|
2403
|
+
}
|
|
2404
|
+
updateParams(params);
|
|
2405
|
+
function render() {
|
|
2406
|
+
gl.viewport(0, 0, blurredBackgroundWidth, blurredBackgroundHeight);
|
|
2407
|
+
gl.useProgram(program);
|
|
2408
|
+
gl.uniform1i(inputFrameLocation, 0);
|
|
2409
|
+
gl.activeTexture(gl.TEXTURE1);
|
|
2410
|
+
gl.bindTexture(gl.TEXTURE_2D, personMaskTexture);
|
|
2411
|
+
for (let i = 0; i < numPasses; i++) {
|
|
2412
|
+
gl.uniform2f(texelSizeLocation, 0, texelHeight);
|
|
2413
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, frameBuffer1);
|
|
2414
|
+
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
|
|
2415
|
+
gl.activeTexture(gl.TEXTURE2);
|
|
2416
|
+
gl.bindTexture(gl.TEXTURE_2D, texture1);
|
|
2417
|
+
gl.uniform1i(inputFrameLocation, 2);
|
|
2418
|
+
gl.uniform2f(texelSizeLocation, texelWidth, 0);
|
|
2419
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, frameBuffer2);
|
|
2420
|
+
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
|
|
2421
|
+
gl.bindTexture(gl.TEXTURE_2D, texture2);
|
|
2422
|
+
}
|
|
2423
|
+
}
|
|
2424
|
+
function cleanUp() {
|
|
2425
|
+
gl.deleteFramebuffer(frameBuffer2);
|
|
2426
|
+
gl.deleteFramebuffer(frameBuffer1);
|
|
2427
|
+
gl.deleteTexture(texture2);
|
|
2428
|
+
gl.deleteTexture(texture1);
|
|
2429
|
+
gl.deleteProgram(program);
|
|
2430
|
+
gl.deleteShader(fragmentShader);
|
|
2431
|
+
}
|
|
2432
|
+
return {
|
|
2433
|
+
render,
|
|
2434
|
+
updateParams,
|
|
2435
|
+
cleanUp,
|
|
2436
|
+
};
|
|
2437
|
+
}
|
|
2438
|
+
function buildBlendPass(gl, positionBuffer, texCoordBuffer, outputWidth, outputHeight, params) {
|
|
2439
|
+
const vertexShaderSource = glsl `#version 300 es
|
|
2440
|
+
in vec2 a_position;
|
|
2441
|
+
in vec2 a_texCoord;
|
|
2442
|
+
out vec2 v_texCoord;
|
|
2443
|
+
void main() {
|
|
2444
|
+
// Flipping Y is required when rendering to canvas
|
|
2445
|
+
gl_Position = vec4(a_position * vec2(1.0, -1.0), 0.0, 1.0);
|
|
2446
|
+
v_texCoord = a_texCoord;
|
|
2447
|
+
}`;
|
|
2448
|
+
const fragmentShaderSource = glsl `#version 300 es
|
|
2449
|
+
precision highp float;
|
|
2450
|
+
uniform sampler2D u_inputFrame;
|
|
2451
|
+
uniform sampler2D u_personMask;
|
|
2452
|
+
uniform sampler2D u_blurredInputFrame;
|
|
2453
|
+
uniform vec2 u_coverage;
|
|
2454
|
+
in vec2 v_texCoord;
|
|
2455
|
+
out vec4 outColor;
|
|
2456
|
+
void main() {
|
|
2457
|
+
vec3 color = texture(u_inputFrame, v_texCoord).rgb;
|
|
2458
|
+
vec3 blurredColor = texture(u_blurredInputFrame, v_texCoord).rgb;
|
|
2459
|
+
float personMask = texture(u_personMask, v_texCoord).a;
|
|
2460
|
+
personMask = smoothstep(u_coverage.x, u_coverage.y, personMask);
|
|
2461
|
+
outColor = vec4(mix(blurredColor, color, personMask), 1.0);
|
|
2462
|
+
}`;
|
|
2463
|
+
const vertexShader = compileShader(gl, gl.VERTEX_SHADER, vertexShaderSource);
|
|
2464
|
+
const fragmentShader = compileShader(gl, gl.FRAGMENT_SHADER, fragmentShaderSource);
|
|
2465
|
+
const program = createPiplelineStageProgram(gl, vertexShader, fragmentShader, positionBuffer, texCoordBuffer);
|
|
2466
|
+
const inputFrameLocation = gl.getUniformLocation(program, "u_inputFrame");
|
|
2467
|
+
const personMaskLocation = gl.getUniformLocation(program, "u_personMask");
|
|
2468
|
+
const blurredInputFrame = gl.getUniformLocation(program, "u_blurredInputFrame");
|
|
2469
|
+
const coverageLocation = gl.getUniformLocation(program, "u_coverage");
|
|
2470
|
+
gl.useProgram(program);
|
|
2471
|
+
gl.uniform1i(inputFrameLocation, 0);
|
|
2472
|
+
gl.uniform1i(personMaskLocation, 1);
|
|
2473
|
+
gl.uniform1i(blurredInputFrame, 2);
|
|
2474
|
+
function updateParams(params) {
|
|
2475
|
+
gl.useProgram(program);
|
|
2476
|
+
const coverage = params.coverage || [0, 1];
|
|
2477
|
+
gl.uniform2f(coverageLocation, coverage[0], coverage[1]);
|
|
2478
|
+
}
|
|
2479
|
+
updateParams(params);
|
|
2480
|
+
function render() {
|
|
2481
|
+
gl.viewport(0, 0, outputWidth, outputHeight);
|
|
2482
|
+
gl.useProgram(program);
|
|
2483
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
|
|
2484
|
+
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
|
|
2485
|
+
}
|
|
2486
|
+
function cleanUp() {
|
|
2487
|
+
gl.deleteProgram(program);
|
|
2488
|
+
gl.deleteShader(fragmentShader);
|
|
2489
|
+
gl.deleteShader(vertexShader);
|
|
2490
|
+
}
|
|
2491
|
+
return {
|
|
2492
|
+
render,
|
|
2493
|
+
updateParams,
|
|
2494
|
+
cleanUp,
|
|
2495
|
+
};
|
|
2496
|
+
}
|
|
2497
|
+
function buildBackgroundBlurStage(gl, vertexShader, positionBuffer, texCoordBuffer, personMaskTexture, outputWidth, outputHeight, params) {
|
|
2498
|
+
let blurPass = buildBlurPass(gl, vertexShader, positionBuffer, texCoordBuffer, personMaskTexture, outputWidth, outputHeight, params);
|
|
2499
|
+
const blendPass = buildBlendPass(gl, positionBuffer, texCoordBuffer, outputWidth, outputHeight, params);
|
|
2500
|
+
function render() {
|
|
2501
|
+
blurPass.render();
|
|
2502
|
+
blendPass.render();
|
|
2503
|
+
}
|
|
2504
|
+
let currentResampleScale = params.backgroundResampleScale;
|
|
2505
|
+
function updateParams(params) {
|
|
2506
|
+
if (currentResampleScale !== params.backgroundResampleScale) {
|
|
2507
|
+
blurPass.cleanUp();
|
|
2508
|
+
blurPass = buildBlurPass(gl, vertexShader, positionBuffer, texCoordBuffer, personMaskTexture, outputWidth, outputHeight, params);
|
|
2509
|
+
currentResampleScale = params.backgroundResampleScale;
|
|
2510
|
+
}
|
|
2511
|
+
blurPass.updateParams(params);
|
|
2512
|
+
blendPass.updateParams(params);
|
|
2513
|
+
}
|
|
2514
|
+
function cleanUp() {
|
|
2515
|
+
blendPass.cleanUp();
|
|
2516
|
+
blurPass.cleanUp();
|
|
2517
|
+
}
|
|
2518
|
+
return {
|
|
2519
|
+
render,
|
|
2520
|
+
updateParams,
|
|
2521
|
+
cleanUp,
|
|
2522
|
+
};
|
|
2523
|
+
}
|
|
2524
|
+
|
|
2525
|
+
function buildBackgroundImageStage(gl, positionBuffer, texCoordBuffer, personMaskTexture, backgroundImage, outputWidth, outputHeight, params) {
|
|
2526
|
+
const vertexShaderSource = glsl `#version 300 es
|
|
2527
|
+
uniform vec2 u_backgroundScale;
|
|
2528
|
+
uniform vec2 u_backgroundOffset;
|
|
2529
|
+
in vec2 a_position;
|
|
2530
|
+
in vec2 a_texCoord;
|
|
2531
|
+
out vec2 v_texCoord;
|
|
2532
|
+
out vec2 v_backgroundCoord;
|
|
2533
|
+
void main() {
|
|
2534
|
+
// Flipping Y for canvas
|
|
2535
|
+
gl_Position = vec4(a_position * vec2(1.0, -1.0), 0.0, 1.0);
|
|
2536
|
+
v_texCoord = a_texCoord;
|
|
2537
|
+
v_backgroundCoord = a_texCoord * u_backgroundScale + u_backgroundOffset;
|
|
2538
|
+
}`;
|
|
2539
|
+
const fragmentShaderSource = glsl `#version 300 es
|
|
2540
|
+
precision highp float;
|
|
2541
|
+
uniform sampler2D u_inputFrame;
|
|
2542
|
+
uniform sampler2D u_personMask;
|
|
2543
|
+
uniform sampler2D u_background;
|
|
2544
|
+
uniform vec2 u_coverage;
|
|
2545
|
+
in vec2 v_texCoord;
|
|
2546
|
+
in vec2 v_backgroundCoord;
|
|
2547
|
+
out vec4 outColor;
|
|
2548
|
+
|
|
2549
|
+
void main() {
|
|
2550
|
+
vec3 frameColor = texture(u_inputFrame, v_texCoord).rgb;
|
|
2551
|
+
vec3 backgroundColor = texture(u_background, v_backgroundCoord).rgb;
|
|
2552
|
+
float personMask = texture(u_personMask, v_texCoord).a;
|
|
2553
|
+
personMask = smoothstep(u_coverage.x, u_coverage.y, personMask);
|
|
2554
|
+
outColor = vec4(frameColor * personMask + backgroundColor * (1.0 - personMask), 1.0);
|
|
2555
|
+
}`;
|
|
2556
|
+
const outputRatio = outputWidth / outputHeight;
|
|
2557
|
+
const vertexShader = compileShader(gl, gl.VERTEX_SHADER, vertexShaderSource);
|
|
2558
|
+
const fragmentShader = compileShader(gl, gl.FRAGMENT_SHADER, fragmentShaderSource);
|
|
2559
|
+
const program = createPiplelineStageProgram(gl, vertexShader, fragmentShader, positionBuffer, texCoordBuffer);
|
|
2560
|
+
const backgroundScaleLocation = gl.getUniformLocation(program, "u_backgroundScale");
|
|
2561
|
+
const backgroundOffsetLocation = gl.getUniformLocation(program, "u_backgroundOffset");
|
|
2562
|
+
const inputFrameLocation = gl.getUniformLocation(program, "u_inputFrame");
|
|
2563
|
+
const personMaskLocation = gl.getUniformLocation(program, "u_personMask");
|
|
2564
|
+
const backgroundLocation = gl.getUniformLocation(program, "u_background");
|
|
2565
|
+
const coverageLocation = gl.getUniformLocation(program, "u_coverage");
|
|
2566
|
+
gl.useProgram(program);
|
|
2567
|
+
gl.uniform2f(backgroundScaleLocation, 1, 1);
|
|
2568
|
+
gl.uniform2f(backgroundOffsetLocation, 0, 0);
|
|
2569
|
+
gl.uniform1i(inputFrameLocation, 0);
|
|
2570
|
+
gl.uniform1i(personMaskLocation, 1);
|
|
2571
|
+
let backgroundTexture = null;
|
|
2572
|
+
function render() {
|
|
2573
|
+
gl.viewport(0, 0, outputWidth, outputHeight);
|
|
2574
|
+
gl.useProgram(program);
|
|
2575
|
+
gl.activeTexture(gl.TEXTURE1);
|
|
2576
|
+
gl.bindTexture(gl.TEXTURE_2D, personMaskTexture);
|
|
2577
|
+
if (backgroundTexture !== null) {
|
|
2578
|
+
gl.activeTexture(gl.TEXTURE2);
|
|
2579
|
+
gl.bindTexture(gl.TEXTURE_2D, backgroundTexture);
|
|
2580
|
+
gl.uniform1i(backgroundLocation, 2);
|
|
2581
|
+
}
|
|
2582
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
|
|
2583
|
+
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
|
|
2584
|
+
}
|
|
2585
|
+
function initBackgroundImage(backgroundImage) {
|
|
2586
|
+
const sourceWidth = backgroundImage.naturalWidth || backgroundImage.videoWidth || backgroundImage.width;
|
|
2587
|
+
const sourceHeight = backgroundImage.naturalHeight || backgroundImage.videoHeight || backgroundImage.height;
|
|
2588
|
+
backgroundTexture = createTexture(gl, gl.RGBA8, sourceWidth, sourceHeight, gl.LINEAR, gl.LINEAR);
|
|
2589
|
+
gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, sourceWidth, sourceHeight, gl.RGBA, gl.UNSIGNED_BYTE, backgroundImage);
|
|
2590
|
+
let xOffset = 0;
|
|
2591
|
+
let yOffset = 0;
|
|
2592
|
+
let backgroundWidth = sourceWidth;
|
|
2593
|
+
let backgroundHeight = sourceHeight;
|
|
2594
|
+
const backgroundRatio = backgroundWidth / backgroundHeight;
|
|
2595
|
+
if (backgroundRatio < outputRatio) {
|
|
2596
|
+
backgroundHeight = backgroundWidth / outputRatio;
|
|
2597
|
+
yOffset = (sourceHeight - backgroundHeight) / 2;
|
|
2598
|
+
}
|
|
2599
|
+
else {
|
|
2600
|
+
backgroundWidth = backgroundHeight * outputRatio;
|
|
2601
|
+
xOffset = (sourceWidth - backgroundWidth) / 2;
|
|
2602
|
+
}
|
|
2603
|
+
const xScale = backgroundWidth / sourceWidth;
|
|
2604
|
+
const yScale = backgroundHeight / sourceHeight;
|
|
2605
|
+
xOffset /= sourceWidth;
|
|
2606
|
+
yOffset /= sourceHeight;
|
|
2607
|
+
gl.uniform2f(backgroundScaleLocation, xScale, yScale);
|
|
2608
|
+
gl.uniform2f(backgroundOffsetLocation, xOffset, yOffset);
|
|
2609
|
+
}
|
|
2610
|
+
function updateBackgroundImage(backgroundImage, reInit) {
|
|
2611
|
+
if (reInit) {
|
|
2612
|
+
backgroundTexture = null;
|
|
2613
|
+
}
|
|
2614
|
+
if (!backgroundImage) {
|
|
2615
|
+
if (backgroundTexture)
|
|
2616
|
+
backgroundTexture = null;
|
|
2617
|
+
return;
|
|
2618
|
+
}
|
|
2619
|
+
if (backgroundTexture) {
|
|
2620
|
+
gl.bindTexture(gl.TEXTURE_2D, backgroundTexture);
|
|
2621
|
+
gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, backgroundImage.naturalWidth || backgroundImage.videoWidth || backgroundImage.width, backgroundImage.naturalHeight || backgroundImage.videoHeight || backgroundImage.height, gl.RGBA, gl.UNSIGNED_BYTE, backgroundImage);
|
|
2622
|
+
}
|
|
2623
|
+
else {
|
|
2624
|
+
initBackgroundImage(backgroundImage);
|
|
2625
|
+
}
|
|
2626
|
+
}
|
|
2627
|
+
function updateParams(params) {
|
|
2628
|
+
gl.useProgram(program);
|
|
2629
|
+
const coverage = params.coverage || [0, 1];
|
|
2630
|
+
gl.uniform2f(coverageLocation, coverage[0], coverage[1]);
|
|
2631
|
+
}
|
|
2632
|
+
updateParams(params);
|
|
2633
|
+
if (backgroundImage)
|
|
2634
|
+
initBackgroundImage(backgroundImage);
|
|
2635
|
+
function cleanUp() {
|
|
2636
|
+
gl.deleteTexture(backgroundTexture);
|
|
2637
|
+
gl.deleteProgram(program);
|
|
2638
|
+
gl.deleteShader(fragmentShader);
|
|
2639
|
+
gl.deleteShader(vertexShader);
|
|
2640
|
+
}
|
|
2641
|
+
return {
|
|
2642
|
+
render,
|
|
2643
|
+
updateParams,
|
|
2644
|
+
cleanUp,
|
|
2645
|
+
updateBackgroundImage,
|
|
2646
|
+
};
|
|
2647
|
+
}
|
|
2648
|
+
|
|
2649
|
+
function buildImproveMaskStage(gl, vertexShader, positionBuffer, texCoordBuffer, personMaskTexture, improvedMaskTexture, outputWidth, outputHeight) {
|
|
2650
|
+
const fragmentShaderSource = glsl `#version 300 es
|
|
2651
|
+
precision highp float;
|
|
2652
|
+
uniform sampler2D u_personMask;
|
|
2653
|
+
uniform vec2 u_texelSize;
|
|
2654
|
+
in vec2 v_texCoord;
|
|
2655
|
+
out vec4 outColor;
|
|
2656
|
+
const float offset[3] = float[](0.0, 1.0, 2.0);
|
|
2657
|
+
const float weight[3] = float[](0.38774, 0.24477, 0.06136);
|
|
2658
|
+
void main() {
|
|
2659
|
+
float blurredAlpha = texture(u_personMask, v_texCoord).a * weight[0];
|
|
2660
|
+
for (int i = 1; i < 3; i++) {
|
|
2661
|
+
vec2 offset = vec2(offset[i]) * u_texelSize;
|
|
2662
|
+
vec2 texCoord = v_texCoord + offset;
|
|
2663
|
+
blurredAlpha += texture(u_personMask, texCoord).a * weight[i];
|
|
2664
|
+
texCoord = v_texCoord - offset;
|
|
2665
|
+
blurredAlpha += texture(u_personMask, texCoord).a * weight[i];
|
|
2666
|
+
}
|
|
2667
|
+
outColor = vec4(0,0,0,blurredAlpha);
|
|
2668
|
+
}`;
|
|
2669
|
+
const texelWidth = 1 / outputWidth;
|
|
2670
|
+
const texelHeight = 1 / outputHeight;
|
|
2671
|
+
const fragmentShader = compileShader(gl, gl.FRAGMENT_SHADER, fragmentShaderSource);
|
|
2672
|
+
const program = createPiplelineStageProgram(gl, vertexShader, fragmentShader, positionBuffer, texCoordBuffer);
|
|
2673
|
+
const personMaskLocation = gl.getUniformLocation(program, "u_personMask");
|
|
2674
|
+
const texelSizeLocation = gl.getUniformLocation(program, "u_texelSize");
|
|
2675
|
+
const texture1 = createTexture(gl, gl.RGBA8, outputWidth, outputHeight, gl.NEAREST, gl.NEAREST);
|
|
2676
|
+
const frameBuffer1 = gl.createFramebuffer();
|
|
2677
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, frameBuffer1);
|
|
2678
|
+
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture1, 0);
|
|
2679
|
+
const frameBuffer2 = gl.createFramebuffer();
|
|
2680
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, frameBuffer2);
|
|
2681
|
+
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, improvedMaskTexture, 0);
|
|
2682
|
+
gl.useProgram(program);
|
|
2683
|
+
gl.uniform1i(personMaskLocation, 1);
|
|
2684
|
+
function render() {
|
|
2685
|
+
gl.viewport(0, 0, outputWidth, outputHeight);
|
|
2686
|
+
gl.useProgram(program);
|
|
2687
|
+
gl.activeTexture(gl.TEXTURE1);
|
|
2688
|
+
gl.bindTexture(gl.TEXTURE_2D, personMaskTexture);
|
|
2689
|
+
gl.uniform2f(texelSizeLocation, 0, texelHeight);
|
|
2690
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, frameBuffer1);
|
|
2691
|
+
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
|
|
2692
|
+
gl.bindTexture(gl.TEXTURE_2D, texture1);
|
|
2693
|
+
gl.uniform2f(texelSizeLocation, texelWidth, 0);
|
|
2694
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, frameBuffer2);
|
|
2695
|
+
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
|
|
2696
|
+
}
|
|
2697
|
+
function cleanUp() {
|
|
2698
|
+
gl.deleteFramebuffer(frameBuffer2);
|
|
2699
|
+
gl.deleteFramebuffer(frameBuffer1);
|
|
2700
|
+
gl.deleteTexture(texture1);
|
|
2701
|
+
gl.deleteProgram(program);
|
|
2702
|
+
gl.deleteShader(fragmentShader);
|
|
2703
|
+
}
|
|
2704
|
+
return { render, cleanUp };
|
|
2705
|
+
}
|
|
2706
|
+
|
|
2707
|
+
function buildTFLiteOutputToMaskStage(gl, vertexShader, positionBuffer, texCoordBuffer, outputTexture, tflite, outputWidth, outputHeight, outputMemoryOffset) {
|
|
2708
|
+
const fragmentShaderSource = glsl `#version 300 es
|
|
2709
|
+
precision highp float;
|
|
2710
|
+
uniform sampler2D u_inputSegmentation;
|
|
2711
|
+
|
|
2712
|
+
in vec2 v_texCoord;
|
|
2713
|
+
out vec4 outColor;
|
|
2714
|
+
|
|
2715
|
+
float getMask(vec2 segmentation) {
|
|
2716
|
+
float shift = max(segmentation.r, segmentation.g);
|
|
2717
|
+
float backgroundExp = exp(segmentation.r - shift);
|
|
2718
|
+
float personExp = exp(segmentation.g - shift);
|
|
2719
|
+
return personExp / (backgroundExp + personExp);
|
|
2720
|
+
}
|
|
2721
|
+
|
|
2722
|
+
void main() {
|
|
2723
|
+
float mask = getMask(texture(u_inputSegmentation, v_texCoord).rg);
|
|
2724
|
+
outColor = vec4(vec3(0.0), mask);
|
|
2725
|
+
}`;
|
|
2726
|
+
const fragmentShader = compileShader(gl, gl.FRAGMENT_SHADER, fragmentShaderSource);
|
|
2727
|
+
const program = createPiplelineStageProgram(gl, vertexShader, fragmentShader, positionBuffer, texCoordBuffer);
|
|
2728
|
+
const inputLocation = gl.getUniformLocation(program, "u_inputSegmentation");
|
|
2729
|
+
const inputTexture = createTexture(gl, gl.RG32F, outputWidth, outputHeight, gl.NEAREST, gl.NEAREST);
|
|
2730
|
+
const frameBuffer = gl.createFramebuffer();
|
|
2731
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, frameBuffer);
|
|
2732
|
+
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, outputTexture, 0);
|
|
2733
|
+
gl.useProgram(program);
|
|
2734
|
+
gl.uniform1i(inputLocation, 1);
|
|
2735
|
+
function render() {
|
|
2736
|
+
gl.viewport(0, 0, outputWidth, outputHeight);
|
|
2737
|
+
gl.useProgram(program);
|
|
2738
|
+
gl.activeTexture(gl.TEXTURE1);
|
|
2739
|
+
gl.bindTexture(gl.TEXTURE_2D, inputTexture);
|
|
2740
|
+
gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, outputWidth, outputHeight, gl.RG, gl.FLOAT, tflite.HEAPF32, outputMemoryOffset);
|
|
2741
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, frameBuffer);
|
|
2742
|
+
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
|
|
2743
|
+
}
|
|
2744
|
+
function cleanUp() {
|
|
2745
|
+
gl.deleteFramebuffer(frameBuffer);
|
|
2746
|
+
gl.deleteTexture(inputTexture);
|
|
2747
|
+
gl.deleteProgram(program);
|
|
2748
|
+
gl.deleteShader(fragmentShader);
|
|
2749
|
+
}
|
|
2750
|
+
return { render, cleanUp };
|
|
2751
|
+
}
|
|
2752
|
+
|
|
2753
|
+
function buildTFLiteInputStage(gl, vertexShader, positionBuffer, texCoordBuffer, tflite, inputWidth, inputHeight, segmentationPixelCount, inputMemoryOffset) {
|
|
2754
|
+
const fragmentShaderSource = glsl `#version 300 es
|
|
2755
|
+
precision highp float;
|
|
2756
|
+
uniform sampler2D u_inputFrame;
|
|
2757
|
+
uniform sampler2D u_floatlut;
|
|
2758
|
+
in vec2 v_texCoord;
|
|
2759
|
+
uniform float u_pixelSize;
|
|
2760
|
+
out vec4 outColor;
|
|
2761
|
+
|
|
2762
|
+
void main() {
|
|
2763
|
+
float x = v_texCoord.x;
|
|
2764
|
+
vec4 color = texture(u_inputFrame, v_texCoord);
|
|
2765
|
+
float r = mod(x * u_pixelSize, 3.0);
|
|
2766
|
+
float c = 0.0;
|
|
2767
|
+
if (r >= 2.0 ) c = color.b;
|
|
2768
|
+
else if (r >= 1.0) c = color.g;
|
|
2769
|
+
else c = color.r;
|
|
2770
|
+
outColor = texture(u_floatlut, vec2(c, 0.5));
|
|
2771
|
+
}`;
|
|
2772
|
+
const fragmentShader = compileShader(gl, gl.FRAGMENT_SHADER, fragmentShaderSource);
|
|
2773
|
+
const program = createPiplelineStageProgram(gl, vertexShader, fragmentShader, positionBuffer, texCoordBuffer);
|
|
2774
|
+
const inputFrameLocation = gl.getUniformLocation(program, "u_inputFrame");
|
|
2775
|
+
const pixelSizeLocation = gl.getUniformLocation(program, "u_pixelSize");
|
|
2776
|
+
const floatlutLocation = gl.getUniformLocation(program, "u_floatlut");
|
|
2777
|
+
const outputTexture = createTexture(gl, gl.RGBA8, inputWidth * 3, inputHeight);
|
|
2778
|
+
const frameBuffer = gl.createFramebuffer();
|
|
2779
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, frameBuffer);
|
|
2780
|
+
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, outputTexture, 0);
|
|
2781
|
+
const outputPixels = new Uint8Array(tflite.HEAPF32.buffer, inputMemoryOffset * 4, segmentationPixelCount * 4 * 3);
|
|
2782
|
+
gl.useProgram(program);
|
|
2783
|
+
gl.uniform1i(inputFrameLocation, 0);
|
|
2784
|
+
gl.uniform1f(pixelSizeLocation, inputWidth * 3);
|
|
2785
|
+
gl.uniform1i(floatlutLocation, 3);
|
|
2786
|
+
const floatLutTexture = gl.createTexture();
|
|
2787
|
+
gl.bindTexture(gl.TEXTURE_2D, floatLutTexture);
|
|
2788
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
|
2789
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
|
2790
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
|
|
2791
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
|
|
2792
|
+
const lut = new Float32Array(256);
|
|
2793
|
+
for (let i = 0; i < 256; i++)
|
|
2794
|
+
lut[i] = i / 256;
|
|
2795
|
+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 256, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, new Uint8Array(lut.buffer, 0, 256 * 4));
|
|
2796
|
+
function render() {
|
|
2797
|
+
gl.viewport(0, 0, inputWidth * 3, inputHeight);
|
|
2798
|
+
gl.useProgram(program);
|
|
2799
|
+
gl.activeTexture(gl.TEXTURE3);
|
|
2800
|
+
gl.bindTexture(gl.TEXTURE_2D, floatLutTexture);
|
|
2801
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, frameBuffer);
|
|
2802
|
+
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
|
|
2803
|
+
gl.readPixels(0, 0, inputWidth * 3, inputHeight, gl.RGBA, gl.UNSIGNED_BYTE, outputPixels);
|
|
2804
|
+
}
|
|
2805
|
+
function cleanUp() {
|
|
2806
|
+
gl.deleteFramebuffer(frameBuffer);
|
|
2807
|
+
gl.deleteTexture(outputTexture);
|
|
2808
|
+
gl.deleteTexture(floatLutTexture);
|
|
2809
|
+
gl.deleteProgram(program);
|
|
2810
|
+
gl.deleteShader(fragmentShader);
|
|
2811
|
+
}
|
|
2812
|
+
return { render, cleanUp };
|
|
2813
|
+
}
|
|
2814
|
+
|
|
2815
|
+
function getBlurEngineParams(params) {
|
|
2816
|
+
switch (params.amount) {
|
|
2817
|
+
case "slight": {
|
|
2818
|
+
return Object.assign({ kernel: "og5", numPasses: 1, backgroundResampleScale: 1 }, params);
|
|
2819
|
+
}
|
|
2820
|
+
case "heavy": {
|
|
2821
|
+
return Object.assign({ kernel: "og10", numPasses: 1, backgroundResampleScale: 0.5 }, params);
|
|
2822
|
+
}
|
|
2823
|
+
default: {
|
|
2824
|
+
return Object.assign({ kernel: "og5", numPasses: 1, backgroundResampleScale: 0.5 }, params);
|
|
2825
|
+
}
|
|
2826
|
+
}
|
|
2827
|
+
}
|
|
2828
|
+
function getEngineParams(params) {
|
|
2829
|
+
const baseEngineParams = {
|
|
2830
|
+
coverage: params.coverage,
|
|
2831
|
+
};
|
|
2832
|
+
if (params.backgroundBlur) {
|
|
2833
|
+
return Object.assign({ baseEngineParams }, getBlurEngineParams(params.backgroundBlur));
|
|
2834
|
+
}
|
|
2835
|
+
return baseEngineParams;
|
|
2836
|
+
}
|
|
2837
|
+
function createWebGLEngine(videoWidth, videoHeight, setup, effectCanvas, params, initialBackgroundFrame) {
|
|
2838
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
2839
|
+
const { tflite, inputHeight, inputWidth, inputMemoryOffset, outputMemoryOffset, segmentationPixelCount } = yield loadSegmentationModel(setup.segmentationModelId);
|
|
2840
|
+
let engineParams = getEngineParams(params);
|
|
2841
|
+
const vertexShaderSource = glsl `#version 300 es
|
|
2842
|
+
in vec2 a_position;
|
|
2843
|
+
in vec2 a_texCoord;
|
|
2844
|
+
out vec2 v_texCoord;
|
|
2845
|
+
void main() {
|
|
2846
|
+
gl_Position = vec4(a_position, 0.0, 1.0);
|
|
2847
|
+
v_texCoord = a_texCoord;
|
|
2848
|
+
}`;
|
|
2849
|
+
const gl = effectCanvas.getContext("webgl2", setup.webglContextSettings);
|
|
2850
|
+
const vertexShader = compileShader(gl, gl.VERTEX_SHADER, vertexShaderSource);
|
|
2851
|
+
const vertexArray = gl.createVertexArray();
|
|
2852
|
+
gl.bindVertexArray(vertexArray);
|
|
2853
|
+
const positionBuffer = gl.createBuffer();
|
|
2854
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
|
|
2855
|
+
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 1.0, -1, -1, 1.0, 1.0, 1.0]), gl.STATIC_DRAW);
|
|
2856
|
+
const texCoordBuffer = gl.createBuffer();
|
|
2857
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, texCoordBuffer);
|
|
2858
|
+
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0]), gl.STATIC_DRAW);
|
|
2859
|
+
const inputFrameTexture = gl.createTexture();
|
|
2860
|
+
gl.bindTexture(gl.TEXTURE_2D, inputFrameTexture);
|
|
2861
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
|
2862
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
|
2863
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
|
|
2864
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
|
|
2865
|
+
const maskTexture = createTexture(gl, gl.RGBA8, inputWidth, inputHeight);
|
|
2866
|
+
const improvedMaskWidth = inputWidth;
|
|
2867
|
+
const improvedMaskHeight = inputHeight;
|
|
2868
|
+
const improvedMaskTexture = createTexture(gl, gl.RGBA8, improvedMaskWidth, improvedMaskHeight, gl.LINEAR, gl.LINEAR);
|
|
2869
|
+
const tfliteInputStage = buildTFLiteInputStage(gl, vertexShader, positionBuffer, texCoordBuffer, tflite, inputWidth, inputHeight, segmentationPixelCount, inputMemoryOffset);
|
|
2870
|
+
const tfliteOutputToMaskStage = buildTFLiteOutputToMaskStage(gl, vertexShader, positionBuffer, texCoordBuffer, maskTexture, tflite, inputWidth, inputHeight, outputMemoryOffset);
|
|
2871
|
+
const improveMaskStage = buildImproveMaskStage(gl, vertexShader, positionBuffer, texCoordBuffer, maskTexture, improvedMaskTexture, improvedMaskWidth, improvedMaskHeight);
|
|
2872
|
+
const backgroundBlurStage = buildBackgroundBlurStage(gl, vertexShader, positionBuffer, texCoordBuffer, improvedMaskTexture, videoWidth, videoHeight, engineParams);
|
|
2873
|
+
const backgroundImageStage = buildBackgroundImageStage(gl, positionBuffer, texCoordBuffer, improvedMaskTexture, initialBackgroundFrame, videoWidth, videoHeight, engineParams);
|
|
2874
|
+
return {
|
|
2875
|
+
effectCtx: gl,
|
|
2876
|
+
updateBackgroundFrame(frame, _, reInit) {
|
|
2877
|
+
backgroundImageStage.updateBackgroundImage(frame, reInit);
|
|
2878
|
+
},
|
|
2879
|
+
updateParams(updatedParams) {
|
|
2880
|
+
params = updatedParams;
|
|
2881
|
+
engineParams = getEngineParams(params);
|
|
2882
|
+
backgroundBlurStage.updateParams(engineParams);
|
|
2883
|
+
backgroundImageStage.updateParams(engineParams);
|
|
2884
|
+
},
|
|
2885
|
+
processFrame(frame) {
|
|
2886
|
+
gl.clearColor(0, 0, 0, 0);
|
|
2887
|
+
gl.clear(gl.COLOR_BUFFER_BIT);
|
|
2888
|
+
gl.activeTexture(gl.TEXTURE0);
|
|
2889
|
+
gl.bindTexture(gl.TEXTURE_2D, inputFrameTexture);
|
|
2890
|
+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, frame);
|
|
2891
|
+
gl.bindVertexArray(vertexArray);
|
|
2892
|
+
tfliteInputStage.render();
|
|
2893
|
+
tflite._runInference();
|
|
2894
|
+
tfliteOutputToMaskStage.render();
|
|
2895
|
+
improveMaskStage.render();
|
|
2896
|
+
params.backgroundUrl ? backgroundImageStage.render() : backgroundBlurStage.render();
|
|
2897
|
+
},
|
|
2898
|
+
dispose() {
|
|
2899
|
+
backgroundImageStage.cleanUp();
|
|
2900
|
+
backgroundBlurStage.cleanUp();
|
|
2901
|
+
improveMaskStage.cleanUp();
|
|
2902
|
+
tfliteOutputToMaskStage.cleanUp();
|
|
2903
|
+
tfliteInputStage.cleanUp();
|
|
2904
|
+
gl.deleteTexture(improvedMaskTexture);
|
|
2905
|
+
gl.deleteTexture(maskTexture);
|
|
2906
|
+
gl.deleteTexture(inputFrameTexture);
|
|
2907
|
+
gl.deleteBuffer(texCoordBuffer);
|
|
2908
|
+
gl.deleteBuffer(positionBuffer);
|
|
2909
|
+
gl.deleteVertexArray(vertexArray);
|
|
2910
|
+
gl.deleteShader(vertexShader);
|
|
2911
|
+
},
|
|
2912
|
+
};
|
|
2913
|
+
});
|
|
2914
|
+
}
|
|
2915
|
+
|
|
2916
|
+
class Processor extends events.EventEmitter {
|
|
2917
|
+
constructor({ setup, params, videoWidth, videoHeight, emit }) {
|
|
2918
|
+
super();
|
|
2919
|
+
this.setup = setup;
|
|
2920
|
+
this.params = params;
|
|
2921
|
+
this.videoWidth = videoWidth;
|
|
2922
|
+
this.videoHeight = videoHeight;
|
|
2923
|
+
if (emit)
|
|
2924
|
+
this.emit = emit;
|
|
2925
|
+
}
|
|
2926
|
+
setupEngine(videoWidth, videoHeight, effectCanvas, initialBackgroundFrame) {
|
|
2927
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
2928
|
+
this.videoWidth = videoWidth;
|
|
2929
|
+
this.videoHeight = videoHeight;
|
|
2930
|
+
this.videoAspectRatio = this.videoWidth / this.videoHeight;
|
|
2931
|
+
this.effectCanvas = effectCanvas || createCanvas(this.videoWidth, this.videoHeight);
|
|
2932
|
+
const createEngine = this.setup.useWebGL ? createWebGLEngine : createCanvasEngine;
|
|
2933
|
+
this.engine = yield createEngine(this.videoWidth, this.videoHeight, this.setup, this.effectCanvas, this.params, initialBackgroundFrame);
|
|
2934
|
+
this.effectCtx = this.engine.effectCtx;
|
|
2935
|
+
if (initialBackgroundFrame) {
|
|
2936
|
+
this.updateBackgroundFrame({ frame: initialBackgroundFrame });
|
|
2937
|
+
}
|
|
2938
|
+
});
|
|
2939
|
+
}
|
|
2940
|
+
updateVideoSize(_a) {
|
|
2941
|
+
return __awaiter(this, arguments, void 0, function* ({ videoWidth, videoHeight }) {
|
|
2942
|
+
const oldBackgroundFrame = this.currentBackgroundFrame;
|
|
2943
|
+
this.currentBackgroundFrame = null;
|
|
2944
|
+
const oldEngine = this.engine;
|
|
2945
|
+
yield this.setupEngine(videoWidth, videoHeight, this.effectCanvas, oldBackgroundFrame);
|
|
2946
|
+
oldEngine.dispose();
|
|
2947
|
+
});
|
|
2948
|
+
}
|
|
2949
|
+
init(_a) {
|
|
2950
|
+
return __awaiter(this, arguments, void 0, function* ({ initialBackgroundFrame, useInsertableStreams, effectCanvas }) {
|
|
2951
|
+
yield this.setupEngine(this.videoWidth, this.videoHeight, effectCanvas, initialBackgroundFrame);
|
|
2952
|
+
if (useInsertableStreams) {
|
|
2953
|
+
let dropFirst = 5;
|
|
2954
|
+
let localStartTime = 0;
|
|
2955
|
+
let frameStartTime = 0;
|
|
2956
|
+
let firstFrameRendered = false;
|
|
2957
|
+
this.transformer = new TransformStream({
|
|
2958
|
+
transform: (frame, controller) => __awaiter(this, void 0, void 0, function* () {
|
|
2959
|
+
this.transformController = controller;
|
|
2960
|
+
const frameTimestamp = frame.timestamp;
|
|
2961
|
+
const now = performance.now();
|
|
2962
|
+
if (!localStartTime) {
|
|
2963
|
+
localStartTime = now;
|
|
2964
|
+
frameStartTime = frameTimestamp / 1000;
|
|
2965
|
+
}
|
|
2966
|
+
const elapsedFrameTime = frameTimestamp / 1000 - frameStartTime;
|
|
2967
|
+
const elapsedLocalTime = now - localStartTime;
|
|
2968
|
+
if (dropFirst > 0 || elapsedLocalTime < elapsedFrameTime + 200) {
|
|
2969
|
+
this.processAndRenderFrame(frame);
|
|
2970
|
+
frame.close();
|
|
2971
|
+
if (this.hasVideoBackground && this.setup.transferType !== "canvas") {
|
|
2972
|
+
this.emit("getbackgroundframe");
|
|
2973
|
+
}
|
|
2974
|
+
}
|
|
2975
|
+
else {
|
|
2976
|
+
frame.close();
|
|
2977
|
+
return;
|
|
2978
|
+
}
|
|
2979
|
+
if (dropFirst > 0) {
|
|
2980
|
+
dropFirst--;
|
|
2981
|
+
localStartTime = now;
|
|
2982
|
+
frameStartTime = frameTimestamp / 1000;
|
|
2983
|
+
}
|
|
2984
|
+
controller.enqueue(new VideoFrame(this.effectCanvas, { timestamp: frameTimestamp }));
|
|
2985
|
+
if (!firstFrameRendered) {
|
|
2986
|
+
firstFrameRendered = true;
|
|
2987
|
+
this.emit("firstframerendered");
|
|
2988
|
+
}
|
|
2989
|
+
}),
|
|
2990
|
+
});
|
|
2991
|
+
}
|
|
2992
|
+
else if (!this.setup.useBackgroundWorker) {
|
|
2993
|
+
const timerWorkerUrl = new URL("./workers/timer.worker.js", (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href)));
|
|
2994
|
+
this.timerWorker = new Worker(timerWorkerUrl);
|
|
2995
|
+
this.timerWorker.addEventListener("message", () => {
|
|
2996
|
+
this.emit("output", { frame: this.currentOutputFrame }, [this.currentOutputFrame]);
|
|
2997
|
+
});
|
|
2998
|
+
}
|
|
2999
|
+
this.emit("ready");
|
|
3000
|
+
});
|
|
3001
|
+
}
|
|
3002
|
+
updateParams({ params, initialBackgroundFrame }) {
|
|
3003
|
+
this.params = params;
|
|
3004
|
+
this.currentBackgroundFrame = null;
|
|
3005
|
+
this.currentBackgroundFrameCtx = null;
|
|
3006
|
+
if (initialBackgroundFrame) {
|
|
3007
|
+
this.updateBackgroundFrame({ frame: initialBackgroundFrame, reInit: true });
|
|
3008
|
+
}
|
|
3009
|
+
else {
|
|
3010
|
+
this.engine.updateBackgroundFrame(null);
|
|
3011
|
+
}
|
|
3012
|
+
this.engine.updateParams(params);
|
|
3013
|
+
}
|
|
3014
|
+
updateBackgroundFrame({ frame, reInit }) {
|
|
3015
|
+
let updatedDimensions;
|
|
3016
|
+
if (!this.currentBackgroundFrame) {
|
|
3017
|
+
this.hasVideoBackground = this.params.backgroundUrl && this.params.backgroundType === "video";
|
|
3018
|
+
updatedDimensions = { x: 0, y: 0, width: this.videoWidth, height: this.videoHeight };
|
|
3019
|
+
}
|
|
3020
|
+
switch (this.setup.transferType) {
|
|
3021
|
+
case "imagebitmap": {
|
|
3022
|
+
this.currentBackgroundFrame = frame;
|
|
3023
|
+
break;
|
|
3024
|
+
}
|
|
3025
|
+
case "imagedata": {
|
|
3026
|
+
if (!this.currentBackgroundFrame) {
|
|
3027
|
+
this.currentBackgroundFrame = createCanvas(this.videoWidth, this.videoHeight);
|
|
3028
|
+
this.currentBackgroundFrameCtx = this.currentBackgroundFrame.getContext("2d");
|
|
3029
|
+
}
|
|
3030
|
+
frame = new ImageData(new Uint8ClampedArray(frame), this.videoWidth, this.videoHeight);
|
|
3031
|
+
this.currentBackgroundFrameCtx.putImageData(frame, 0, 0);
|
|
3032
|
+
break;
|
|
3033
|
+
}
|
|
3034
|
+
case "canvas": {
|
|
3035
|
+
if (!this.currentBackgroundFrame) {
|
|
3036
|
+
updatedDimensions.width = frame.videoWidth || frame.naturalWidth || frame.width;
|
|
3037
|
+
updatedDimensions.height = frame.videoHeight || frame.naturalHeight || frame.height;
|
|
3038
|
+
const backgroundAspectRatio = updatedDimensions.width / updatedDimensions.height;
|
|
3039
|
+
if (backgroundAspectRatio > this.videoAspectRatio) {
|
|
3040
|
+
const outsideX = updatedDimensions.width - updatedDimensions.height * this.videoAspectRatio;
|
|
3041
|
+
updatedDimensions.width -= outsideX;
|
|
3042
|
+
updatedDimensions.x = Math.floor(outsideX / 2);
|
|
3043
|
+
}
|
|
3044
|
+
else if (backgroundAspectRatio < this.videoAspectRatio) {
|
|
3045
|
+
const outsideY = updatedDimensions.height - updatedDimensions.width / this.videoAspectRatio;
|
|
3046
|
+
updatedDimensions.height -= outsideY;
|
|
3047
|
+
updatedDimensions.y = Math.floor(outsideY / 2);
|
|
3048
|
+
}
|
|
3049
|
+
}
|
|
3050
|
+
this.currentBackgroundFrame = frame;
|
|
3051
|
+
break;
|
|
3052
|
+
}
|
|
3053
|
+
}
|
|
3054
|
+
this.engine.updateBackgroundFrame(this.currentBackgroundFrame, updatedDimensions, reInit);
|
|
3055
|
+
}
|
|
3056
|
+
terminate() {
|
|
3057
|
+
if (this.transformController)
|
|
3058
|
+
this.transformController.terminate();
|
|
3059
|
+
if (this.timerWorker)
|
|
3060
|
+
this.timerWorker.terminate();
|
|
3061
|
+
if (this.engine) {
|
|
3062
|
+
this.engine.dispose();
|
|
3063
|
+
this.engine = null;
|
|
3064
|
+
}
|
|
3065
|
+
this._terminated = true;
|
|
3066
|
+
this.emit("terminated");
|
|
3067
|
+
}
|
|
3068
|
+
start({ inputStream, outputStream }) {
|
|
3069
|
+
if (inputStream)
|
|
3070
|
+
inputStream.pipeThrough(this.transformer).pipeTo(outputStream);
|
|
3071
|
+
this.frameCounter = 0;
|
|
3072
|
+
this.frameCounterTime = performance.now();
|
|
3073
|
+
}
|
|
3074
|
+
input(_a) {
|
|
3075
|
+
return __awaiter(this, arguments, void 0, function* ({ videoFrame, backgroundFrame, requestTime, lastRenderTime, delay }) {
|
|
3076
|
+
var _b;
|
|
3077
|
+
if (this._terminated)
|
|
3078
|
+
return;
|
|
3079
|
+
const rtt = (Date.now() - requestTime) * 2;
|
|
3080
|
+
const returnTime = lastRenderTime + delay - rtt;
|
|
3081
|
+
if (backgroundFrame)
|
|
3082
|
+
this.updateBackgroundFrame({ frame: backgroundFrame });
|
|
3083
|
+
let frame;
|
|
3084
|
+
switch (this.setup.transferType) {
|
|
3085
|
+
case "imagebitmap": {
|
|
3086
|
+
(_b = this.prevVideoFrame) === null || _b === void 0 ? void 0 : _b.close();
|
|
3087
|
+
this.prevVideoFrame = videoFrame;
|
|
3088
|
+
this.processAndRenderFrame(videoFrame);
|
|
3089
|
+
frame = yield createImageBitmap(this.effectCanvas);
|
|
3090
|
+
break;
|
|
3091
|
+
}
|
|
3092
|
+
case "imagedata": {
|
|
3093
|
+
videoFrame = new ImageData(new Uint8ClampedArray(videoFrame), this.videoWidth, this.videoHeight);
|
|
3094
|
+
if (!this.inputCanvas) {
|
|
3095
|
+
this.inputCanvas = createCanvas(this.videoWidth, this.videoHeight);
|
|
3096
|
+
this.inputCanvasCtx = this.inputCanvas.getContext("2d");
|
|
3097
|
+
}
|
|
3098
|
+
this.inputCanvasCtx.putImageData(videoFrame, 0, 0);
|
|
3099
|
+
this.processAndRenderFrame(this.inputCanvas);
|
|
3100
|
+
frame = this.effectCtx.getImageData(0, 0, this.videoWidth, this.videoHeight).data.buffer;
|
|
3101
|
+
break;
|
|
3102
|
+
}
|
|
3103
|
+
case "canvas": {
|
|
3104
|
+
this.processAndRenderFrame(videoFrame);
|
|
3105
|
+
frame = this.effectCanvas;
|
|
3106
|
+
}
|
|
3107
|
+
}
|
|
3108
|
+
const returnDelay = Math.max(0, returnTime - Date.now());
|
|
3109
|
+
if (this.timerWorker) {
|
|
3110
|
+
this.currentOutputFrame = frame;
|
|
3111
|
+
this.timerWorker.postMessage(returnDelay);
|
|
3112
|
+
}
|
|
3113
|
+
else {
|
|
3114
|
+
setTimeout(() => {
|
|
3115
|
+
this.emit("output", { frame }, [frame]);
|
|
3116
|
+
}, returnDelay);
|
|
3117
|
+
}
|
|
3118
|
+
});
|
|
3119
|
+
}
|
|
3120
|
+
processAndRenderFrame(frame) {
|
|
3121
|
+
this.engine.processFrame(frame);
|
|
3122
|
+
if (this.setup.logToConsole) {
|
|
3123
|
+
this.frameCounter++;
|
|
3124
|
+
const now = performance.now();
|
|
3125
|
+
if (now - this.frameCounterTime > 1000) {
|
|
3126
|
+
console.log(`Running camera effects, ${Math.round((this.frameCounter * 1000) / (now - this.frameCounterTime))} fps`);
|
|
3127
|
+
this.frameCounter = 0;
|
|
3128
|
+
this.frameCounterTime = now;
|
|
3129
|
+
}
|
|
3130
|
+
}
|
|
3131
|
+
}
|
|
3132
|
+
}
|
|
3133
|
+
|
|
3134
|
+
let nextProcessorId = 0;
|
|
3135
|
+
class ProcessorProxy extends events.EventEmitter {
|
|
3136
|
+
constructor(worker, config) {
|
|
3137
|
+
super();
|
|
3138
|
+
const processorId = nextProcessorId++;
|
|
3139
|
+
const proxyMethod = (name, getTransferable) => {
|
|
3140
|
+
this[name] = (args) => {
|
|
3141
|
+
worker.postMessage(Object.assign({ type: name, processorId }, args), getTransferable && getTransferable(args).filter((e) => e));
|
|
3142
|
+
};
|
|
3143
|
+
};
|
|
3144
|
+
proxyMethod("init", (args) => [args.initialBackgroundFrame]);
|
|
3145
|
+
proxyMethod("start", (args) => [args.inputStream, args.outputStream]);
|
|
3146
|
+
proxyMethod("input", (args) => [args.videoFrame, args.backgroundFrame]);
|
|
3147
|
+
proxyMethod("updateBackgroundFrame", (args) => [args.frame]);
|
|
3148
|
+
proxyMethod("updateParams", (args) => [args.initialBackgroundFrame]);
|
|
3149
|
+
proxyMethod("updateVideoSize");
|
|
3150
|
+
proxyMethod("terminate");
|
|
3151
|
+
const onMessage = (request) => __awaiter(this, void 0, void 0, function* () {
|
|
3152
|
+
if (request.data.processorId !== processorId)
|
|
3153
|
+
return;
|
|
3154
|
+
const _a = request.data, { type } = _a, data = __rest(_a, ["type"]);
|
|
3155
|
+
this.emit(type, data);
|
|
3156
|
+
if (type === "terminated") {
|
|
3157
|
+
worker.removeEventListener("message", onMessage);
|
|
3158
|
+
}
|
|
3159
|
+
});
|
|
3160
|
+
worker.postMessage(Object.assign({ type: "create", processorId }, config));
|
|
3161
|
+
worker.addEventListener("message", onMessage);
|
|
3162
|
+
}
|
|
3163
|
+
}
|
|
3164
|
+
|
|
3165
|
+
let sharedWorker;
|
|
3166
|
+
function createProcessor(useBackgroundWorker, config) {
|
|
3167
|
+
if (!useBackgroundWorker)
|
|
3168
|
+
return new Processor(config);
|
|
3169
|
+
if (!sharedWorker) {
|
|
3170
|
+
const workerUrl = new URL("./workers/ProcessorProxy.worker.js", (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href)));
|
|
3171
|
+
sharedWorker = new Worker(workerUrl);
|
|
3172
|
+
}
|
|
3173
|
+
return new ProcessorProxy(sharedWorker, config);
|
|
3174
|
+
}
|
|
3175
|
+
|
|
3176
|
+
const grayImage = new Image(1, 1);
|
|
3177
|
+
grayImage.src = "data:image/gif;base64,R0lGODlhAQABAIAAAMLCwgAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==";
|
|
3178
|
+
function _createImageBitmap(source, options) {
|
|
3179
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
3180
|
+
try {
|
|
3181
|
+
return yield createImageBitmap(source, options);
|
|
3182
|
+
}
|
|
3183
|
+
catch (_a) {
|
|
3184
|
+
return yield createImageBitmap(grayImage, options);
|
|
3185
|
+
}
|
|
3186
|
+
});
|
|
3187
|
+
}
|
|
3188
|
+
const createEffectStream$1 = (inputStream, setup, params) => __awaiter(void 0, void 0, void 0, function* () {
|
|
3189
|
+
yield fixBackgroundUrlPromise(params);
|
|
3190
|
+
const cleanup = [];
|
|
3191
|
+
const inputVideoTrack = inputStream.getVideoTracks()[0];
|
|
3192
|
+
const inputAudioTrack = inputStream.getAudioTracks()[0];
|
|
3193
|
+
let { width: videoWidth, height: videoHeight } = inputVideoTrack.getSettings();
|
|
3194
|
+
const processor = createProcessor(setup.useBackgroundWorker, {
|
|
3195
|
+
setup,
|
|
3196
|
+
params,
|
|
3197
|
+
videoWidth,
|
|
3198
|
+
videoHeight,
|
|
3199
|
+
});
|
|
3200
|
+
let keepAliveVideoBackground;
|
|
3201
|
+
const updateBackgroundProvider = (params) => __awaiter(void 0, void 0, void 0, function* () {
|
|
3202
|
+
const backgroundElement = yield createBackgroundElement(params.backgroundUrl, params.backgroundType);
|
|
3203
|
+
keepAliveVideoBackground =
|
|
3204
|
+
backgroundElement &&
|
|
3205
|
+
backgroundElement.play &&
|
|
3206
|
+
(() => {
|
|
3207
|
+
if (backgroundElement.paused)
|
|
3208
|
+
backgroundElement.play();
|
|
3209
|
+
});
|
|
3210
|
+
return createBackgroundProvider(backgroundElement, videoWidth, videoHeight, setup.transferType);
|
|
3211
|
+
});
|
|
3212
|
+
let getBackgroundFrame = yield updateBackgroundProvider(params);
|
|
3213
|
+
let firstFrameRenderedResolve = null;
|
|
3214
|
+
const firstFrameRendered = new Promise((resolve) => (firstFrameRenderedResolve = resolve));
|
|
3215
|
+
let resultCanvas;
|
|
3216
|
+
let outputTrack;
|
|
3217
|
+
if (setup.useInsertableStreams) {
|
|
3218
|
+
const trackProcessor = new window.MediaStreamTrackProcessor({ track: inputVideoTrack, maxBufferSize: 2 });
|
|
3219
|
+
const trackGenerator = new window.MediaStreamTrackGenerator({ kind: "video", signalTarget: inputVideoTrack });
|
|
3220
|
+
outputTrack = trackGenerator;
|
|
3221
|
+
processor.on("ready", () => {
|
|
3222
|
+
processor.start({
|
|
3223
|
+
inputStream: trackProcessor === null || trackProcessor === void 0 ? void 0 : trackProcessor.readable,
|
|
3224
|
+
outputStream: trackGenerator === null || trackGenerator === void 0 ? void 0 : trackGenerator.writable,
|
|
3225
|
+
});
|
|
3226
|
+
});
|
|
3227
|
+
processor.on("firstframerendered", () => firstFrameRenderedResolve());
|
|
3228
|
+
processor.on("getbackgroundframe", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
3229
|
+
if (!keepAliveVideoBackground)
|
|
3230
|
+
return;
|
|
3231
|
+
keepAliveVideoBackground();
|
|
3232
|
+
processor.updateBackgroundFrame({ frame: yield getBackgroundFrame() });
|
|
3233
|
+
}));
|
|
3234
|
+
}
|
|
3235
|
+
else {
|
|
3236
|
+
const videoElement = document.createElement("video");
|
|
3237
|
+
const onResizeInputStream = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
3238
|
+
const { width, height } = inputVideoTrack.getSettings();
|
|
3239
|
+
if (videoWidth === width && videoHeight === height)
|
|
3240
|
+
return;
|
|
3241
|
+
videoWidth = width;
|
|
3242
|
+
videoHeight = height;
|
|
3243
|
+
if (typeof resultCanvas === "object") {
|
|
3244
|
+
resultCanvas.width = videoWidth;
|
|
3245
|
+
resultCanvas.height = videoHeight;
|
|
3246
|
+
}
|
|
3247
|
+
yield processor.updateVideoSize({ videoWidth, videoHeight });
|
|
3248
|
+
yield updateBackgroundProvider(params);
|
|
3249
|
+
});
|
|
3250
|
+
videoElement.addEventListener("resize", onResizeInputStream);
|
|
3251
|
+
cleanup.push(() => {
|
|
3252
|
+
videoElement.removeEventListener("resize", onResizeInputStream);
|
|
3253
|
+
});
|
|
3254
|
+
if (setup.keepVideoElementInDom) {
|
|
3255
|
+
videoElement.playsInline = true;
|
|
3256
|
+
videoElement.style.position = "absolute";
|
|
3257
|
+
videoElement.style.top = "0px";
|
|
3258
|
+
videoElement.style.left = "0px";
|
|
3259
|
+
videoElement.style.width = "1px";
|
|
3260
|
+
videoElement.style.opacity = 0;
|
|
3261
|
+
document.body.appendChild(videoElement);
|
|
3262
|
+
cleanup.push(() => {
|
|
3263
|
+
try {
|
|
3264
|
+
document.body.removeChild(videoElement);
|
|
3265
|
+
}
|
|
3266
|
+
catch (_a) {
|
|
3267
|
+
console.warn("video element already removed from DOM");
|
|
3268
|
+
}
|
|
3269
|
+
videoElement.srcObject = null;
|
|
3270
|
+
});
|
|
3271
|
+
}
|
|
3272
|
+
yield new Promise((resolve) => {
|
|
3273
|
+
videoElement.addEventListener("loadedmetadata", resolve);
|
|
3274
|
+
videoElement.srcObject = new MediaStream([inputVideoTrack]);
|
|
3275
|
+
videoElement.play();
|
|
3276
|
+
});
|
|
3277
|
+
const onVisibilityChange = () => {
|
|
3278
|
+
if (document.visibilityState !== "visible") {
|
|
3279
|
+
videoElement.play();
|
|
3280
|
+
}
|
|
3281
|
+
};
|
|
3282
|
+
document.addEventListener("visibilitychange", onVisibilityChange);
|
|
3283
|
+
cleanup.push(() => {
|
|
3284
|
+
document.removeEventListener("visibilitychange", onVisibilityChange);
|
|
3285
|
+
});
|
|
3286
|
+
resultCanvas = createCanvas(videoWidth, videoHeight);
|
|
3287
|
+
let resultCanvasCtx;
|
|
3288
|
+
let getInputFrame;
|
|
3289
|
+
let renderOutputFrame;
|
|
3290
|
+
if (setup.transferType === "imagebitmap") {
|
|
3291
|
+
resultCanvasCtx = resultCanvas.getContext("bitmaprenderer");
|
|
3292
|
+
getInputFrame = () => _createImageBitmap(videoElement);
|
|
3293
|
+
renderOutputFrame = (frame) => {
|
|
3294
|
+
resultCanvasCtx.transferFromImageBitmap(frame);
|
|
3295
|
+
frame.close();
|
|
3296
|
+
};
|
|
3297
|
+
}
|
|
3298
|
+
else if (setup.transferType === "imagedata") {
|
|
3299
|
+
resultCanvasCtx = resultCanvas.getContext("2d");
|
|
3300
|
+
const videoCanvas = createCanvas(videoWidth, videoHeight);
|
|
3301
|
+
const videoCanvasCtx = videoCanvas.getContext("2d");
|
|
3302
|
+
getInputFrame = () => {
|
|
3303
|
+
videoCanvasCtx.drawImage(videoElement, 0, 0);
|
|
3304
|
+
return videoCanvasCtx.getImageData(0, 0, videoWidth, videoHeight).data.buffer;
|
|
3305
|
+
};
|
|
3306
|
+
renderOutputFrame = (frame) => {
|
|
3307
|
+
frame = new ImageData(new Uint8ClampedArray(frame), videoWidth, videoHeight);
|
|
3308
|
+
resultCanvasCtx.putImageData(frame, 0, 0);
|
|
3309
|
+
};
|
|
3310
|
+
}
|
|
3311
|
+
else if (setup.transferType === "canvas") {
|
|
3312
|
+
resultCanvasCtx = resultCanvas.getContext(setup.useWebGL ? "webgl2" : "2d", setup.useWebGL ? setup.webglContextSettings : undefined);
|
|
3313
|
+
getInputFrame = () => videoElement;
|
|
3314
|
+
if (setup.useWebGL && setup.canvasMemoryLeakWorkaround)
|
|
3315
|
+
getInputFrame = () => _createImageBitmap(videoElement);
|
|
3316
|
+
renderOutputFrame = () => {
|
|
3317
|
+
};
|
|
3318
|
+
}
|
|
3319
|
+
outputTrack = resultCanvas.captureStream().getVideoTracks()[0];
|
|
3320
|
+
let lastRenderTime = Date.now();
|
|
3321
|
+
const onInputFrame = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
3322
|
+
keepAliveVideoBackground === null || keepAliveVideoBackground === void 0 ? void 0 : keepAliveVideoBackground();
|
|
3323
|
+
const [videoFrame, backgroundFrame] = yield Promise.all([
|
|
3324
|
+
getInputFrame(),
|
|
3325
|
+
keepAliveVideoBackground && getBackgroundFrame(),
|
|
3326
|
+
]);
|
|
3327
|
+
processor.input({
|
|
3328
|
+
videoFrame,
|
|
3329
|
+
backgroundFrame,
|
|
3330
|
+
lastRenderTime,
|
|
3331
|
+
requestTime: Date.now(),
|
|
3332
|
+
delay: setup.framerate ? 1000 / setup.framerate : 0,
|
|
3333
|
+
});
|
|
3334
|
+
});
|
|
3335
|
+
const requestInputFrame = () => {
|
|
3336
|
+
onInputFrame();
|
|
3337
|
+
};
|
|
3338
|
+
processor.on("ready", () => {
|
|
3339
|
+
requestInputFrame();
|
|
3340
|
+
processor.start({});
|
|
3341
|
+
});
|
|
3342
|
+
processor.on("output", ({ frame }) => {
|
|
3343
|
+
lastRenderTime = Date.now();
|
|
3344
|
+
renderOutputFrame(frame);
|
|
3345
|
+
requestInputFrame();
|
|
3346
|
+
if (firstFrameRenderedResolve) {
|
|
3347
|
+
firstFrameRenderedResolve();
|
|
3348
|
+
firstFrameRenderedResolve = null;
|
|
3349
|
+
}
|
|
3350
|
+
});
|
|
3351
|
+
}
|
|
3352
|
+
processor.on("terminated", () => {
|
|
3353
|
+
cleanup.forEach((t) => t());
|
|
3354
|
+
});
|
|
3355
|
+
processor.init({
|
|
3356
|
+
useInsertableStreams: setup.useInsertableStreams,
|
|
3357
|
+
initialBackgroundFrame: yield getBackgroundFrame(),
|
|
3358
|
+
effectCanvas: setup.transferType === "canvas" ? resultCanvas : undefined,
|
|
3359
|
+
});
|
|
3360
|
+
const stop = () => {
|
|
3361
|
+
processor.terminate();
|
|
3362
|
+
};
|
|
3363
|
+
const updateParams = (params) => __awaiter(void 0, void 0, void 0, function* () {
|
|
3364
|
+
yield fixBackgroundUrlPromise(params);
|
|
3365
|
+
getBackgroundFrame = yield updateBackgroundProvider(params);
|
|
3366
|
+
processor.updateParams({
|
|
3367
|
+
params,
|
|
3368
|
+
initialBackgroundFrame: yield getBackgroundFrame(),
|
|
3369
|
+
});
|
|
3370
|
+
return true;
|
|
3371
|
+
});
|
|
3372
|
+
yield firstFrameRendered;
|
|
3373
|
+
Object.defineProperty(outputTrack, "label", {
|
|
3374
|
+
get: () => inputVideoTrack.label,
|
|
3375
|
+
});
|
|
3376
|
+
Object.defineProperty(outputTrack, "readyState", {
|
|
3377
|
+
get: () => inputVideoTrack.readyState,
|
|
3378
|
+
});
|
|
3379
|
+
Object.defineProperty(outputTrack, "enabled", {
|
|
3380
|
+
get: () => inputVideoTrack.enabled,
|
|
3381
|
+
set: (value) => {
|
|
3382
|
+
inputVideoTrack.enabled = value;
|
|
3383
|
+
},
|
|
3384
|
+
});
|
|
3385
|
+
Object.defineProperty(outputTrack, "getSettings", {
|
|
3386
|
+
get: () => () => inputVideoTrack.getSettings(),
|
|
3387
|
+
});
|
|
3388
|
+
const outputStream = new MediaStream([outputTrack, inputAudioTrack].filter((e) => e));
|
|
3389
|
+
return { stream: outputStream, stop, updateParams };
|
|
3390
|
+
});
|
|
3391
|
+
|
|
3392
|
+
var tfliteSegmentCanvasEffectsModule = /*#__PURE__*/Object.freeze({
|
|
3393
|
+
__proto__: null,
|
|
3394
|
+
createEffectStream: createEffectStream$1
|
|
3395
|
+
});
|
|
3396
|
+
|
|
3397
|
+
const canUseWebGL = (function () {
|
|
3398
|
+
const canvas = document.createElement("canvas");
|
|
3399
|
+
return !!canvas.getContext("webgl2");
|
|
3400
|
+
})();
|
|
3401
|
+
const pipelines = {
|
|
3402
|
+
tfliteSegmentCanvasEffects: {
|
|
3403
|
+
module: tfliteSegmentCanvasEffectsModule,
|
|
3404
|
+
getDefaultSetup: () => {
|
|
3405
|
+
const shared = {
|
|
3406
|
+
useWebGL: canUseWebGL,
|
|
3407
|
+
segmentationModelId: "meetlite",
|
|
3408
|
+
logToConsole: false,
|
|
3409
|
+
framerate: 30,
|
|
3410
|
+
useInsertableStreams: false,
|
|
3411
|
+
canvasMemoryLeakWorkaround: true,
|
|
3412
|
+
transferType: "canvas",
|
|
3413
|
+
useBackgroundWorker: false,
|
|
3414
|
+
};
|
|
3415
|
+
if (adapter.browserDetails.browser === "chrome") {
|
|
3416
|
+
return Object.assign(Object.assign({}, shared), { useWebGL: false });
|
|
3417
|
+
}
|
|
3418
|
+
if (adapter.browserDetails.browser === "firefox") {
|
|
3419
|
+
return Object.assign(Object.assign({}, shared), { transferType: canUseWebGL ? "imagebitmap" : "canvas" });
|
|
3420
|
+
}
|
|
3421
|
+
if (adapter.browserDetails.browser === "safari") {
|
|
3422
|
+
return Object.assign(Object.assign({}, shared), { canvasMemoryLeakWorkaround: false, keepVideoElementInDom: true });
|
|
3423
|
+
}
|
|
3424
|
+
return Object.assign({}, shared);
|
|
3425
|
+
},
|
|
3426
|
+
canUse: (options = {}) => {
|
|
3427
|
+
var _a, _b, _c;
|
|
3428
|
+
return (adapter.browserDetails.browser === "safari" && canUseWebGL && options.allowSafari) ||
|
|
3429
|
+
(adapter.browserDetails.browser === "chrome" && ((_a = adapter.browserDetails.version) !== null && _a !== void 0 ? _a : 0) >= 90) ||
|
|
3430
|
+
(adapter.browserDetails.browser === "firefox" && ((_c = (_b = adapter.browserDetails) === null || _b === void 0 ? void 0 : _b.version) !== null && _c !== void 0 ? _c : 0) >= 89);
|
|
3431
|
+
},
|
|
3432
|
+
},
|
|
3433
|
+
};
|
|
3434
|
+
const presets = [
|
|
3435
|
+
{
|
|
3436
|
+
id: "slight-blur",
|
|
3437
|
+
pipelineConfigs: {
|
|
3438
|
+
tfliteSegmentCanvasEffects: {
|
|
3439
|
+
params: { backgroundBlur: { amount: "slight" } },
|
|
3440
|
+
},
|
|
3441
|
+
},
|
|
3442
|
+
},
|
|
3443
|
+
{
|
|
3444
|
+
id: "blur",
|
|
3445
|
+
pipelineConfigs: {
|
|
3446
|
+
tfliteSegmentCanvasEffects: {
|
|
3447
|
+
params: { backgroundBlur: { amount: "normal" } },
|
|
3448
|
+
},
|
|
3449
|
+
},
|
|
3450
|
+
},
|
|
3451
|
+
{
|
|
3452
|
+
id: "heavy-blur",
|
|
3453
|
+
pipelineConfigs: {
|
|
3454
|
+
tfliteSegmentCanvasEffects: {
|
|
3455
|
+
params: { backgroundBlur: { amount: "heavy" } },
|
|
3456
|
+
},
|
|
3457
|
+
},
|
|
3458
|
+
},
|
|
3459
|
+
];
|
|
3460
|
+
presets.push(...[
|
|
3461
|
+
{ id: "cabin", backgroundUrl: Promise.resolve().then(function () { return cabin720p$1; }) },
|
|
3462
|
+
{ id: "concrete", backgroundUrl: Promise.resolve().then(function () { return concrete720p$1; }) },
|
|
3463
|
+
{ id: "brick", backgroundUrl: Promise.resolve().then(function () { return brick720p$1; }) },
|
|
3464
|
+
{ id: "sunrise", backgroundUrl: Promise.resolve().then(function () { return sunrise720p$1; }) },
|
|
3465
|
+
{ id: "day", backgroundUrl: Promise.resolve().then(function () { return day720p$1; }) },
|
|
3466
|
+
{ id: "night", backgroundUrl: Promise.resolve().then(function () { return night720p$1; }) },
|
|
3467
|
+
{ id: "clay", backgroundUrl: Promise.resolve().then(function () { return clay$1; }) },
|
|
3468
|
+
{ id: "focus", backgroundUrl: Promise.resolve().then(function () { return focus$1; }) },
|
|
3469
|
+
{ id: "glow", backgroundUrl: Promise.resolve().then(function () { return glow$1; }) },
|
|
3470
|
+
{ id: "haven", backgroundUrl: Promise.resolve().then(function () { return haven$1; }) },
|
|
3471
|
+
{ id: "pulse", backgroundUrl: Promise.resolve().then(function () { return pulse$1; }) },
|
|
3472
|
+
{ id: "studio", backgroundUrl: Promise.resolve().then(function () { return studio$1; }) },
|
|
3473
|
+
{ id: "custom" },
|
|
3474
|
+
].map(({ id, backgroundUrl }) => ({
|
|
3475
|
+
id: `image-${id}`,
|
|
3476
|
+
pipelineConfigs: {
|
|
3477
|
+
tfliteSegmentCanvasEffects: {
|
|
3478
|
+
params: {
|
|
3479
|
+
backgroundUrl,
|
|
3480
|
+
},
|
|
3481
|
+
},
|
|
3482
|
+
},
|
|
3483
|
+
})));
|
|
3484
|
+
presets.push(...[Promise.resolve().then(function () { return neon$1; }), Promise.resolve().then(function () { return bubbles$1; })].map((urlModule, index) => ({
|
|
3485
|
+
id: `video-${index + 1}`,
|
|
3486
|
+
pipelineConfigs: {
|
|
3487
|
+
tfliteSegmentCanvasEffects: {
|
|
3488
|
+
params: {
|
|
3489
|
+
backgroundType: "video",
|
|
3490
|
+
backgroundUrl: urlModule,
|
|
3491
|
+
},
|
|
3492
|
+
},
|
|
3493
|
+
},
|
|
3494
|
+
})));
|
|
3495
|
+
const getPresetAndMergedParams = (presetId, setup, params) => {
|
|
3496
|
+
var _a, _b, _c;
|
|
3497
|
+
const preset = presets.find((p) => p.id === presetId);
|
|
3498
|
+
if (!preset)
|
|
3499
|
+
return;
|
|
3500
|
+
let pipelineId = Object.keys((_a = preset.pipelineConfigs) !== null && _a !== void 0 ? _a : []).find((pipelineId) => pipelines[pipelineId].canUse());
|
|
3501
|
+
if (!pipelineId)
|
|
3502
|
+
pipelineId = Object.keys((_b = preset.pipelineConfigs) !== null && _b !== void 0 ? _b : [])[0];
|
|
3503
|
+
if (!pipelineId)
|
|
3504
|
+
return;
|
|
3505
|
+
const pipelineConfig = (_c = preset.pipelineConfigs) === null || _c === void 0 ? void 0 : _c[pipelineId];
|
|
3506
|
+
const pipeline = pipelines[pipelineId];
|
|
3507
|
+
const mergedSetup = Object.assign(Object.assign(Object.assign({}, pipeline.getDefaultSetup()), pipelineConfig === null || pipelineConfig === void 0 ? void 0 : pipelineConfig.setup), setup);
|
|
3508
|
+
mergedSetup.useWebGL = mergedSetup.useWebGL && canUseWebGL;
|
|
3509
|
+
const mergedParams = Object.assign(Object.assign({}, pipelineConfig === null || pipelineConfig === void 0 ? void 0 : pipelineConfig.params), params);
|
|
3510
|
+
return { pipeline, mergedSetup, mergedParams };
|
|
3511
|
+
};
|
|
3512
|
+
const safariKeepAliveVideoSrcObjectHack = () => {
|
|
3513
|
+
let stop = false;
|
|
3514
|
+
const keepAlive = () => requestAnimationFrame(() => {
|
|
3515
|
+
if (!stop)
|
|
3516
|
+
keepAlive();
|
|
3517
|
+
});
|
|
3518
|
+
keepAlive();
|
|
3519
|
+
return () => (stop = true);
|
|
3520
|
+
};
|
|
3521
|
+
const createEffectStream = (inputStream, presetId, setup, params) => __awaiter(void 0, void 0, void 0, function* () {
|
|
3522
|
+
const { pipeline, mergedSetup, mergedParams } = getPresetAndMergedParams(presetId, setup, params);
|
|
3523
|
+
if (mergedSetup.logToConsole) {
|
|
3524
|
+
console.log(`Setting effect preset ${presetId}`, { setup: mergedSetup, params: mergedParams });
|
|
3525
|
+
}
|
|
3526
|
+
const { stream, stop: stopEffectStream, updateParams, } = yield pipeline.module.createEffectStream(inputStream, mergedSetup, mergedParams);
|
|
3527
|
+
const tryUpdate = (updatedPresetId, updatedSetup, updatedParams) => __awaiter(void 0, void 0, void 0, function* () {
|
|
3528
|
+
const { pipeline: updatedPipeline, mergedSetup: updatedMergedSetup, mergedParams: updatedMergedParams, } = getPresetAndMergedParams(updatedPresetId, updatedSetup, updatedParams);
|
|
3529
|
+
if (!updateParams ||
|
|
3530
|
+
updatedPipeline !== pipeline ||
|
|
3531
|
+
JSON.stringify(updatedMergedSetup) !== JSON.stringify(mergedSetup)) {
|
|
3532
|
+
if (mergedSetup.logToConsole) {
|
|
3533
|
+
console.log("Cannot update effect, needs to create new pipeline");
|
|
3534
|
+
}
|
|
3535
|
+
return false;
|
|
3536
|
+
}
|
|
3537
|
+
if (mergedSetup.logToConsole) {
|
|
3538
|
+
console.log(`Updating to effect preset ${updatedPresetId}`, { params: updatedMergedParams });
|
|
3539
|
+
}
|
|
3540
|
+
return yield updateParams(updatedMergedParams);
|
|
3541
|
+
});
|
|
3542
|
+
const stopSafariHack = adapter.browserDetails.browser === "safari" ? safariKeepAliveVideoSrcObjectHack() : () => undefined;
|
|
3543
|
+
const stop = () => {
|
|
3544
|
+
if (mergedSetup.logToConsole) {
|
|
3545
|
+
console.log("Stopping effects");
|
|
3546
|
+
}
|
|
3547
|
+
stopSafariHack();
|
|
3548
|
+
stopEffectStream();
|
|
3549
|
+
};
|
|
3550
|
+
return { stream, stop, tryUpdate };
|
|
3551
|
+
});
|
|
3552
|
+
const getUsablePresets = (filter = (pipeline, options) => pipeline.canUse(options), options) => presets
|
|
3553
|
+
.filter((preset) => {
|
|
3554
|
+
var _a;
|
|
3555
|
+
return Object.keys((_a = preset.pipelineConfigs) !== null && _a !== void 0 ? _a : []).find((pipelineId) => filter(pipelines[pipelineId], options));
|
|
3556
|
+
})
|
|
3557
|
+
.map((preset) => preset.id);
|
|
3558
|
+
const getPresets = (options) => {
|
|
3559
|
+
const kindOf = (id) => (id.startsWith("image-") ? "image" : id.startsWith("video-") ? "video" : "blur");
|
|
3560
|
+
return presets.map((preset) => {
|
|
3561
|
+
var _a;
|
|
3562
|
+
const supported = !!Object.keys((_a = preset.pipelineConfigs) !== null && _a !== void 0 ? _a : []).find((pipelineId) => pipelines[pipelineId].canUse((options !== null && options !== void 0 ? options : {})));
|
|
3563
|
+
return { id: preset.id, kind: kindOf(preset.id), supported };
|
|
3564
|
+
});
|
|
3565
|
+
};
|
|
3566
|
+
const warmup = (presetId_1, ...args_1) => __awaiter(void 0, [presetId_1, ...args_1], void 0, function* (presetId, setup = () => ({}), params = {}) {
|
|
3567
|
+
var _a;
|
|
3568
|
+
const chosenPresetId = presetId || getUsablePresets((p) => p.canUse({}), {}).at(0) || ((_a = presets.at(0)) === null || _a === void 0 ? void 0 : _a.id);
|
|
3569
|
+
if (!chosenPresetId)
|
|
3570
|
+
return;
|
|
3571
|
+
const merged = getPresetAndMergedParams(chosenPresetId, setup, params);
|
|
3572
|
+
if (!merged)
|
|
3573
|
+
return;
|
|
3574
|
+
const { mergedSetup, mergedParams } = merged;
|
|
3575
|
+
yield loadSegmentationModel(mergedSetup.segmentationModelId);
|
|
3576
|
+
if (mergedParams === null || mergedParams === void 0 ? void 0 : mergedParams.backgroundUrl) {
|
|
3577
|
+
yield fixBackgroundUrlPromise(mergedParams);
|
|
3578
|
+
try {
|
|
3579
|
+
yield createBackgroundElement(mergedParams.backgroundUrl, mergedParams.backgroundType);
|
|
3580
|
+
}
|
|
3581
|
+
catch (_b) {
|
|
3582
|
+
}
|
|
3583
|
+
}
|
|
3584
|
+
});
|
|
3585
|
+
const setDefaultSetup = (pipelineId, setup) => {
|
|
3586
|
+
const oldSetup = pipelines[pipelineId].getDefaultSetup();
|
|
3587
|
+
const newSetup = Object.assign(Object.assign({}, oldSetup), setup);
|
|
3588
|
+
pipelines[pipelineId].getDefaultSetup = () => newSetup;
|
|
3589
|
+
};
|
|
3590
|
+
|
|
3591
|
+
var cabin720p = "./assets/backgrounds/cabin-720p.jpg";
|
|
3592
|
+
|
|
3593
|
+
var cabin720p$1 = /*#__PURE__*/Object.freeze({
|
|
3594
|
+
__proto__: null,
|
|
3595
|
+
default: cabin720p
|
|
3596
|
+
});
|
|
3597
|
+
|
|
3598
|
+
var concrete720p = "./assets/backgrounds/concrete-720p.jpg";
|
|
3599
|
+
|
|
3600
|
+
var concrete720p$1 = /*#__PURE__*/Object.freeze({
|
|
3601
|
+
__proto__: null,
|
|
3602
|
+
default: concrete720p
|
|
3603
|
+
});
|
|
3604
|
+
|
|
3605
|
+
var brick720p = "./assets/backgrounds/brick-720p.jpg";
|
|
3606
|
+
|
|
3607
|
+
var brick720p$1 = /*#__PURE__*/Object.freeze({
|
|
3608
|
+
__proto__: null,
|
|
3609
|
+
default: brick720p
|
|
3610
|
+
});
|
|
3611
|
+
|
|
3612
|
+
var sunrise720p = "./assets/backgrounds/sunrise-720p.png";
|
|
3613
|
+
|
|
3614
|
+
var sunrise720p$1 = /*#__PURE__*/Object.freeze({
|
|
3615
|
+
__proto__: null,
|
|
3616
|
+
default: sunrise720p
|
|
3617
|
+
});
|
|
3618
|
+
|
|
3619
|
+
var day720p = "./assets/backgrounds/day-720p.png";
|
|
3620
|
+
|
|
3621
|
+
var day720p$1 = /*#__PURE__*/Object.freeze({
|
|
3622
|
+
__proto__: null,
|
|
3623
|
+
default: day720p
|
|
3624
|
+
});
|
|
3625
|
+
|
|
3626
|
+
var night720p = "./assets/backgrounds/night-720p.png";
|
|
3627
|
+
|
|
3628
|
+
var night720p$1 = /*#__PURE__*/Object.freeze({
|
|
3629
|
+
__proto__: null,
|
|
3630
|
+
default: night720p
|
|
3631
|
+
});
|
|
3632
|
+
|
|
3633
|
+
var clay = "./assets/backgrounds/clay.png";
|
|
3634
|
+
|
|
3635
|
+
var clay$1 = /*#__PURE__*/Object.freeze({
|
|
3636
|
+
__proto__: null,
|
|
3637
|
+
default: clay
|
|
3638
|
+
});
|
|
3639
|
+
|
|
3640
|
+
var focus = "./assets/backgrounds/focus.png";
|
|
3641
|
+
|
|
3642
|
+
var focus$1 = /*#__PURE__*/Object.freeze({
|
|
3643
|
+
__proto__: null,
|
|
3644
|
+
default: focus
|
|
3645
|
+
});
|
|
3646
|
+
|
|
3647
|
+
var glow = "./assets/backgrounds/glow.png";
|
|
3648
|
+
|
|
3649
|
+
var glow$1 = /*#__PURE__*/Object.freeze({
|
|
3650
|
+
__proto__: null,
|
|
3651
|
+
default: glow
|
|
3652
|
+
});
|
|
3653
|
+
|
|
3654
|
+
var haven = "./assets/backgrounds/haven.png";
|
|
3655
|
+
|
|
3656
|
+
var haven$1 = /*#__PURE__*/Object.freeze({
|
|
3657
|
+
__proto__: null,
|
|
3658
|
+
default: haven
|
|
3659
|
+
});
|
|
3660
|
+
|
|
3661
|
+
var pulse = "./assets/backgrounds/pulse.png";
|
|
3662
|
+
|
|
3663
|
+
var pulse$1 = /*#__PURE__*/Object.freeze({
|
|
3664
|
+
__proto__: null,
|
|
3665
|
+
default: pulse
|
|
3666
|
+
});
|
|
3667
|
+
|
|
3668
|
+
var studio = "./assets/backgrounds/studio.png";
|
|
3669
|
+
|
|
3670
|
+
var studio$1 = /*#__PURE__*/Object.freeze({
|
|
3671
|
+
__proto__: null,
|
|
3672
|
+
default: studio
|
|
3673
|
+
});
|
|
3674
|
+
|
|
3675
|
+
var neon = "./assets/backgrounds/neon.mp4";
|
|
3676
|
+
|
|
3677
|
+
var neon$1 = /*#__PURE__*/Object.freeze({
|
|
3678
|
+
__proto__: null,
|
|
3679
|
+
default: neon
|
|
3680
|
+
});
|
|
3681
|
+
|
|
3682
|
+
var bubbles = "./assets/backgrounds/bubbles.mp4";
|
|
3683
|
+
|
|
3684
|
+
var bubbles$1 = /*#__PURE__*/Object.freeze({
|
|
3685
|
+
__proto__: null,
|
|
3686
|
+
default: bubbles
|
|
3687
|
+
});
|
|
3688
|
+
|
|
3689
|
+
exports.createEffectStream = createEffectStream;
|
|
3690
|
+
exports.getPresets = getPresets;
|
|
3691
|
+
exports.getUsablePresets = getUsablePresets;
|
|
3692
|
+
exports.setDefaultSetup = setDefaultSetup;
|
|
3693
|
+
exports.warmup = warmup;
|