FaceExpressionNet.js 3.6 KB

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