您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

facialExpressionsWorker.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // @flow
  2. import './faceApiPatch';
  3. import * as faceapi from 'face-api.js';
  4. import {
  5. CLEAR_TIMEOUT,
  6. CPU_TIME_INTERVAL,
  7. FACIAL_EXPRESSION_MESSAGE,
  8. INIT_WORKER,
  9. SET_TIMEOUT,
  10. INTERVAL_MESSAGE,
  11. WEBGL_TIME_INTERVAL
  12. } from './constants';
  13. /**
  14. * A flag that indicates whether the tensorflow models were loaded or not.
  15. */
  16. let modelsLoaded = false;
  17. /**
  18. * The url where the models for the facial detection of expressions are located.
  19. */
  20. let modelsURL;
  21. /**
  22. * A flag that indicates whether the tensorflow backend is set or not.
  23. */
  24. let backendSet = false;
  25. /**
  26. * A timer variable for set interval.
  27. */
  28. let timer;
  29. /**
  30. * The duration of the set timeout.
  31. */
  32. let timeoutDuration = -1;
  33. /**
  34. * A patch for having window object in the worker.
  35. */
  36. const window = {
  37. screen: {
  38. width: 1280,
  39. height: 720
  40. }
  41. };
  42. onmessage = async function(message) {
  43. switch (message.data.type) {
  44. case INIT_WORKER : {
  45. modelsURL = message.data.url;
  46. if (message.data.windowScreenSize) {
  47. window.screen = message.data.windowScreenSize;
  48. }
  49. break;
  50. }
  51. case SET_TIMEOUT : {
  52. if (!message.data.imageData || !modelsURL) {
  53. self.postMessage({
  54. type: FACIAL_EXPRESSION_MESSAGE,
  55. value: null
  56. });
  57. }
  58. // the models are loaded
  59. if (!modelsLoaded) {
  60. await faceapi.loadTinyFaceDetectorModel(modelsURL);
  61. await faceapi.loadFaceExpressionModel(modelsURL);
  62. modelsLoaded = true;
  63. }
  64. faceapi.tf.engine().startScope();
  65. const tensor = faceapi.tf.browser.fromPixels(message.data.imageData);
  66. const detections = await faceapi.detectSingleFace(
  67. tensor,
  68. new faceapi.TinyFaceDetectorOptions()
  69. ).withFaceExpressions();
  70. // The backend is set
  71. if (!backendSet) {
  72. const backend = faceapi.tf.getBackend();
  73. if (backend) {
  74. if (backend === 'webgl') {
  75. timeoutDuration = WEBGL_TIME_INTERVAL;
  76. } else if (backend === 'cpu') {
  77. timeoutDuration = CPU_TIME_INTERVAL;
  78. }
  79. self.postMessage({
  80. type: INTERVAL_MESSAGE,
  81. value: timeoutDuration
  82. });
  83. backendSet = true;
  84. }
  85. }
  86. faceapi.tf.engine().endScope();
  87. let facialExpression;
  88. if (detections) {
  89. facialExpression = detections.expressions.asSortedArray()[0].expression;
  90. }
  91. timer = setTimeout(() => {
  92. self.postMessage({
  93. type: FACIAL_EXPRESSION_MESSAGE,
  94. value: facialExpression
  95. });
  96. }, timeoutDuration);
  97. break;
  98. }
  99. case CLEAR_TIMEOUT: {
  100. if (timer) {
  101. clearTimeout(timer);
  102. timer = null;
  103. }
  104. break;
  105. }
  106. }
  107. };