minor optimization to imagefx

pull/293/head
Vladimir Mandic 2020-11-02 12:21:30 -05:00
parent 8b8a01afe0
commit 3072e3a5d2
3 changed files with 21 additions and 22 deletions

View File

@ -407,8 +407,8 @@ result = {
} }
], ],
performance = { // performance data of last execution for each module measuredin miliseconds performance = { // performance data of last execution for each module measuredin miliseconds
backend, // time to initialize tf backend backend, // time to initialize tf backend, valid only during backend startup
load, // time to load models load, // time to load models, valid only during model load
image, // time for image processing image, // time for image processing
body, // model time body, // model time
hand, // model time hand, // model time

View File

@ -172,8 +172,8 @@ class Human {
if (this.config.filter.height > 0) targetHeight = this.config.filter.height; if (this.config.filter.height > 0) targetHeight = this.config.filter.height;
else if (this.config.filter.width > 0) targetHeight = originalHeight * (this.config.filter.width / originalWidth); else if (this.config.filter.width > 0) targetHeight = originalHeight * (this.config.filter.width / originalWidth);
const offscreenCanvas = (typeof OffscreenCanvas !== 'undefined') ? new OffscreenCanvas(targetWidth, targetHeight) : document.createElement('canvas'); const offscreenCanvas = (typeof OffscreenCanvas !== 'undefined') ? new OffscreenCanvas(targetWidth, targetHeight) : document.createElement('canvas');
offscreenCanvas.width = targetWidth; if (offscreenCanvas.width !== targetWidth) offscreenCanvas.width = targetWidth;
offscreenCanvas.height = targetHeight; if (offscreenCanvas.height !== targetHeight) offscreenCanvas.height = targetHeight;
const ctx = offscreenCanvas.getContext('2d'); const ctx = offscreenCanvas.getContext('2d');
if (input instanceof ImageData) ctx.putImageData(input, 0, 0); if (input instanceof ImageData) ctx.putImageData(input, 0, 0);
else ctx.drawImage(input, 0, 0, originalWidth, originalHeight, 0, 0, offscreenCanvas.width, offscreenCanvas.height); else ctx.drawImage(input, 0, 0, originalWidth, originalHeight, 0, 0, offscreenCanvas.width, offscreenCanvas.height);
@ -200,10 +200,11 @@ class Human {
} else { } else {
const canvas = filtered || input; const canvas = filtered || input;
let pixels; let pixels;
// tf kernel-optimized method to get imagedata, also if input is imagedata, just use it if ((this.config.backend === 'webgl') || (canvas instanceof ImageData)) {
if ((this.config.backend === 'webgl') || (canvas instanceof ImageData)) pixels = tf.browser.fromPixels(canvas); // tf kernel-optimized method to get imagedata, also if input is imagedata, just use it
// cpu and wasm kernel does not implement efficient fromPixels method nor we can use canvas as-is, so we do a silly one more canvas pixels = tf.browser.fromPixels(canvas);
else { } else {
// cpu and wasm kernel does not implement efficient fromPixels method nor we can use canvas as-is, so we do a silly one more canvas
const tempCanvas = (typeof OffscreenCanvas !== 'undefined') ? new OffscreenCanvas(targetWidth, targetHeight) : document.createElement('canvas'); const tempCanvas = (typeof OffscreenCanvas !== 'undefined') ? new OffscreenCanvas(targetWidth, targetHeight) : document.createElement('canvas');
tempCanvas.width = targetWidth; tempCanvas.width = targetWidth;
tempCanvas.height = targetHeight; tempCanvas.height = targetHeight;

View File

@ -1,13 +1,8 @@
/* eslint-disable no-shadow */
/* eslint-disable prefer-rest-params */
/* eslint-disable no-sequences */
/* eslint-disable no-unused-vars */
/* eslint-disable no-unused-expressions */
/* eslint-disable no-multi-assign */
/* eslint-disable no-use-before-define */ /* eslint-disable no-use-before-define */
/* /*
WebGLImageFilter - MIT Licensed WebGLImageFilter - MIT Licensed
2013, Dominic Szablewski - phoboslab.org 2013, Dominic Szablewski - phoboslab.org
<https://github.com/phoboslab/WebGLImageFilter>
*/ */
const WebGLProgram = function (gl, vertexSource, fragmentSource) { const WebGLProgram = function (gl, vertexSource, fragmentSource) {
@ -19,7 +14,7 @@ const WebGLProgram = function (gl, vertexSource, fragmentSource) {
}); });
}; };
const _compile = function (gl, source, type) { const _compile = function (source, type) {
const shader = gl.createShader(type); const shader = gl.createShader(type);
gl.shaderSource(shader, source); gl.shaderSource(shader, source);
gl.compileShader(shader); gl.compileShader(shader);
@ -33,8 +28,8 @@ const WebGLProgram = function (gl, vertexSource, fragmentSource) {
this.uniform = {}; this.uniform = {};
this.attribute = {}; this.attribute = {};
const _vsh = _compile(gl, vertexSource, gl.VERTEX_SHADER); const _vsh = _compile(vertexSource, gl.VERTEX_SHADER);
const _fsh = _compile(gl, fragmentSource, gl.FRAGMENT_SHADER); const _fsh = _compile(fragmentSource, gl.FRAGMENT_SHADER);
this.id = gl.createProgram(); this.id = gl.createProgram();
gl.attachShader(this.id, _vsh); gl.attachShader(this.id, _vsh);
@ -82,6 +77,7 @@ const WebGLImageFilter = function (params) {
if (!gl) throw new Error('Filter: getContext() failed'); if (!gl) throw new Error('Filter: getContext() failed');
this.addFilter = function (name) { this.addFilter = function (name) {
// eslint-disable-next-line prefer-rest-params
const args = Array.prototype.slice.call(arguments, 1); const args = Array.prototype.slice.call(arguments, 1);
const filter = _filter[name]; const filter = _filter[name];
@ -107,7 +103,7 @@ const WebGLImageFilter = function (params) {
// No filters? Just draw // No filters? Just draw
if (_filterChain.length === 0) { if (_filterChain.length === 0) {
const program = _compileShader(SHADER.FRAGMENT_IDENTITY); // const program = _compileShader(SHADER.FRAGMENT_IDENTITY);
_draw(); _draw();
return _canvas; return _canvas;
} }
@ -125,8 +121,10 @@ const WebGLImageFilter = function (params) {
// Same width/height? Nothing to do here // Same width/height? Nothing to do here
if (width === _width && height === _height) { return; } if (width === _width && height === _height) { return; }
_canvas.width = _width = width; _canvas.width = width;
_canvas.height = _height = height; _width = width;
_canvas.height = height;
_height = height;
// Create the context if we don't have it yet // Create the context if we don't have it yet
if (!_vertexBuffer) { if (!_vertexBuffer) {
@ -135,8 +133,8 @@ const WebGLImageFilter = function (params) {
-1, -1, 0, 1, 1, -1, 1, 1, -1, 1, 0, 0, -1, -1, 0, 1, 1, -1, 1, 1, -1, 1, 0, 0,
-1, 1, 0, 0, 1, -1, 1, 1, 1, 1, 1, 0, -1, 1, 0, 0, 1, -1, 1, 1, 1, 1, 1, 0,
]); ]);
_vertexBuffer = gl.createBuffer(), // eslint-disable-next-line no-unused-expressions
gl.bindBuffer(gl.ARRAY_BUFFER, _vertexBuffer); (_vertexBuffer = gl.createBuffer(), gl.bindBuffer(gl.ARRAY_BUFFER, _vertexBuffer));
gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW); gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
// Note sure if this is a good idea; at least it makes texture loading // Note sure if this is a good idea; at least it makes texture loading