You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

facialExpressionsWorker.js 2.8KB

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