FaceExpressionNet.js 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var tslib_1 = require("tslib");
  4. var tf = require("@tensorflow/tfjs-core");
  5. var dom_1 = require("../dom");
  6. var FaceFeatureExtractor_1 = require("../faceFeatureExtractor/FaceFeatureExtractor");
  7. var FaceProcessor_1 = require("../faceProcessor/FaceProcessor");
  8. var FaceExpressions_1 = require("./FaceExpressions");
  9. var FaceExpressionNet = /** @class */ (function (_super) {
  10. tslib_1.__extends(FaceExpressionNet, _super);
  11. function FaceExpressionNet(faceFeatureExtractor) {
  12. if (faceFeatureExtractor === void 0) { faceFeatureExtractor = new FaceFeatureExtractor_1.FaceFeatureExtractor(); }
  13. return _super.call(this, 'FaceExpressionNet', faceFeatureExtractor) || this;
  14. }
  15. FaceExpressionNet.prototype.forwardInput = function (input) {
  16. var _this = this;
  17. return tf.tidy(function () { return tf.softmax(_this.runNet(input)); });
  18. };
  19. FaceExpressionNet.prototype.forward = function (input) {
  20. return tslib_1.__awaiter(this, void 0, void 0, function () {
  21. var _a;
  22. return tslib_1.__generator(this, function (_b) {
  23. switch (_b.label) {
  24. case 0:
  25. _a = this.forwardInput;
  26. return [4 /*yield*/, dom_1.toNetInput(input)];
  27. case 1: return [2 /*return*/, _a.apply(this, [_b.sent()])];
  28. }
  29. });
  30. });
  31. };
  32. FaceExpressionNet.prototype.predictExpressions = function (input) {
  33. return tslib_1.__awaiter(this, void 0, void 0, function () {
  34. var netInput, out, probabilitesByBatch, predictionsByBatch;
  35. var _this = this;
  36. return tslib_1.__generator(this, function (_a) {
  37. switch (_a.label) {
  38. case 0: return [4 /*yield*/, dom_1.toNetInput(input)];
  39. case 1:
  40. netInput = _a.sent();
  41. return [4 /*yield*/, this.forwardInput(netInput)];
  42. case 2:
  43. out = _a.sent();
  44. return [4 /*yield*/, Promise.all(tf.unstack(out).map(function (t) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
  45. var data;
  46. return tslib_1.__generator(this, function (_a) {
  47. switch (_a.label) {
  48. case 0: return [4 /*yield*/, t.data()];
  49. case 1:
  50. data = _a.sent();
  51. t.dispose();
  52. return [2 /*return*/, data];
  53. }
  54. });
  55. }); }))];
  56. case 3:
  57. probabilitesByBatch = _a.sent();
  58. out.dispose();
  59. predictionsByBatch = probabilitesByBatch
  60. .map(function (probabilites) { return new FaceExpressions_1.FaceExpressions(probabilites); });
  61. return [2 /*return*/, netInput.isBatchInput
  62. ? predictionsByBatch
  63. : predictionsByBatch[0]];
  64. }
  65. });
  66. });
  67. };
  68. FaceExpressionNet.prototype.getDefaultModelName = function () {
  69. return 'face_expression_model';
  70. };
  71. FaceExpressionNet.prototype.getClassifierChannelsIn = function () {
  72. return 256;
  73. };
  74. FaceExpressionNet.prototype.getClassifierChannelsOut = function () {
  75. return 7;
  76. };
  77. return FaceExpressionNet;
  78. }(FaceProcessor_1.FaceProcessor));
  79. exports.FaceExpressionNet = FaceExpressionNet;
  80. //# sourceMappingURL=FaceExpressionNet.js.map