tfjs-model-facemesh
Version:
forked from @tensorflow-models/facemesh, used for local deployed tfjs models.
85 lines (84 loc) • 3.33 kB
JavaScript
;
/**
* @license
* Copyright 2020 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.enlargeBox = exports.cutBoxFromImageAndResize = exports.getBoxCenter = exports.getBoxSize = exports.scaleBoxCoordinates = exports.createBox = exports.disposeBox = void 0;
var tf = require("@tensorflow/tfjs-core");
function disposeBox(box) {
if (box != null && box.startPoint != null) {
box.startEndTensor.dispose();
box.startPoint.dispose();
box.endPoint.dispose();
}
}
exports.disposeBox = disposeBox;
function createBox(startEndTensor, startPoint, endPoint) {
return {
startEndTensor: startEndTensor,
startPoint: startPoint != null ? startPoint :
tf.slice(startEndTensor, [0, 0], [-1, 2]),
endPoint: endPoint != null ? endPoint :
tf.slice(startEndTensor, [0, 2], [-1, 2])
};
}
exports.createBox = createBox;
function scaleBoxCoordinates(box, factor) {
var newStart = tf.mul(box.startPoint, factor);
var newEnd = tf.mul(box.endPoint, factor);
return createBox(tf.concat2d([newStart, newEnd], 1));
}
exports.scaleBoxCoordinates = scaleBoxCoordinates;
function getBoxSize(box) {
return tf.tidy(function () {
var diff = tf.sub(box.endPoint, box.startPoint);
return tf.abs(diff);
});
}
exports.getBoxSize = getBoxSize;
function getBoxCenter(box) {
return tf.tidy(function () {
var halfSize = tf.div(tf.sub(box.endPoint, box.startPoint), 2);
return tf.add(box.startPoint, halfSize);
});
}
exports.getBoxCenter = getBoxCenter;
function cutBoxFromImageAndResize(box, image, cropSize) {
var height = image.shape[1];
var width = image.shape[2];
var xyxy = box.startEndTensor;
return tf.tidy(function () {
var yxyx = tf.concat2d([
xyxy.slice([0, 1], [-1, 1]), xyxy.slice([0, 0], [-1, 1]),
xyxy.slice([0, 3], [-1, 1]), xyxy.slice([0, 2], [-1, 1])
], 0);
var roundedCoords = tf.div(yxyx.transpose(), [height, width, height, width]);
return tf.image.cropAndResize(image, roundedCoords, [0], cropSize);
});
}
exports.cutBoxFromImageAndResize = cutBoxFromImageAndResize;
function enlargeBox(box, factor) {
if (factor === void 0) { factor = 1.5; }
return tf.tidy(function () {
var center = getBoxCenter(box);
var size = getBoxSize(box);
var newSize = tf.mul(tf.div(size, 2), factor);
var newStart = tf.sub(center, newSize);
var newEnd = tf.add(center, newSize);
return createBox(tf.concat2d([newStart, newEnd], 1), newStart, newEnd);
});
}
exports.enlargeBox = enlargeBox;