face-api.js
Version:
JavaScript API for face detection and face recognition in the browser with tensorflow.js
66 lines • 3.26 kB
JavaScript
import { __extends } from "tslib";
import * as tf from '@tensorflow/tfjs-core';
import { fullyConnectedLayer } from '../common/fullyConnectedLayer';
import { NetInput } from '../dom';
import { NeuralNetwork } from '../NeuralNetwork';
import { extractParams } from './extractParams';
import { extractParamsFromWeigthMap } from './extractParamsFromWeigthMap';
import { seperateWeightMaps } from './util';
var FaceProcessor = /** @class */ (function (_super) {
__extends(FaceProcessor, _super);
function FaceProcessor(_name, faceFeatureExtractor) {
var _this = _super.call(this, _name) || this;
_this._faceFeatureExtractor = faceFeatureExtractor;
return _this;
}
Object.defineProperty(FaceProcessor.prototype, "faceFeatureExtractor", {
get: function () {
return this._faceFeatureExtractor;
},
enumerable: true,
configurable: true
});
FaceProcessor.prototype.runNet = function (input) {
var _this = this;
var params = this.params;
if (!params) {
throw new Error(this._name + " - load model before inference");
}
return tf.tidy(function () {
var bottleneckFeatures = input instanceof NetInput
? _this.faceFeatureExtractor.forwardInput(input)
: input;
return fullyConnectedLayer(bottleneckFeatures.as2D(bottleneckFeatures.shape[0], -1), params.fc);
});
};
FaceProcessor.prototype.dispose = function (throwOnRedispose) {
if (throwOnRedispose === void 0) { throwOnRedispose = true; }
this.faceFeatureExtractor.dispose(throwOnRedispose);
_super.prototype.dispose.call(this, throwOnRedispose);
};
FaceProcessor.prototype.loadClassifierParams = function (weights) {
var _a = this.extractClassifierParams(weights), params = _a.params, paramMappings = _a.paramMappings;
this._params = params;
this._paramMappings = paramMappings;
};
FaceProcessor.prototype.extractClassifierParams = function (weights) {
return extractParams(weights, this.getClassifierChannelsIn(), this.getClassifierChannelsOut());
};
FaceProcessor.prototype.extractParamsFromWeigthMap = function (weightMap) {
var _a = seperateWeightMaps(weightMap), featureExtractorMap = _a.featureExtractorMap, classifierMap = _a.classifierMap;
this.faceFeatureExtractor.loadFromWeightMap(featureExtractorMap);
return extractParamsFromWeigthMap(classifierMap);
};
FaceProcessor.prototype.extractParams = function (weights) {
var cIn = this.getClassifierChannelsIn();
var cOut = this.getClassifierChannelsOut();
var classifierWeightSize = (cOut * cIn) + cOut;
var featureExtractorWeights = weights.slice(0, weights.length - classifierWeightSize);
var classifierWeights = weights.slice(weights.length - classifierWeightSize);
this.faceFeatureExtractor.extractWeights(featureExtractorWeights);
return this.extractClassifierParams(classifierWeights);
};
return FaceProcessor;
}(NeuralNetwork));
export { FaceProcessor };
//# sourceMappingURL=FaceProcessor.js.map