index.html 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <!-- Required meta tags -->
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  7. <script src="https://cdn.jsdelivr.net/npm/@vladmandic/human/dist/human.js"></script>
  8. <script src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
  9. </head>
  10. <body>
  11. <div id="data-container"></div>
  12. {# <form id="data-form" action="/send_data" method="post"> #}
  13. {# <input type="hidden" name="data" id="data-input"> #}
  14. {# <input type="submit" value="Отправить данные"> #}
  15. <canvas id="canvas" style="margin: 0 auto; width: 100%"></canvas>
  16. <pre id="log" style="padding: 8px; position: fixed; bottom: 0"></pre>
  17. <script>
  18. console.log("start", Human);
  19. const humanConfig = { // user configuration for human, used to fine-tune behavior
  20. modelBasePath: 'https://cdn.jsdelivr.net/npm/@vladmandic/human/models/', // models can be loaded directly from cdn as well
  21. filter: { enabled: true, equalization: true, flip: false },
  22. face: { enabled: true, detector: { rotation: false }, mesh: { enabled: false }, attention: { enabled: true }, iris: { enabled: true }, description: { enabled: true }, emotion: { enabled: true } },
  23. body: { enabled: false },
  24. hand: { enabled: false },
  25. gesture: { enabled: false },
  26. object: { enabled: false },
  27. segmentation: { enabled: false },
  28. };
  29. const human = new Human.Human(humanConfig);
  30. //console.log("continue", human);
  31. const canvas = document.getElementById('canvas');
  32. //const dataForm = document.getElementById('data-form');
  33. //const dataInput = document.getElementById('data-input');
  34. //const canvas = $('#canvas').get(0)
  35. var interpolated
  36. var need_generation = true
  37. var need_playing = true
  38. var text
  39. function splitTextIntoLines(text, wordsPerLine) {
  40. const words = text.split(' ');
  41. let line = '';
  42. let j = 0;
  43. for (let i = 0; i < words.length; i++) {
  44. if (j < wordsPerLine) {
  45. line += words[i] + ' ';
  46. j += 1;
  47. } else {
  48. line += '\n';
  49. j = 0;
  50. }
  51. }
  52. return line;
  53. }
  54. async function drawLoop() { // main screen refresh loop
  55. interpolated = human.next(); // get smoothened result using last-known results which are continously updated based on input webcam video
  56. human.draw.canvas(human.webcam.element, canvas); // draw webcam video to screen canvas // better than using procesed image as this loop happens faster than processing loop
  57. await human.draw.all(canvas, interpolated);
  58. document.getElementById('log').innerHTML =
  59. `human version: ${human.version} | ` +
  60. `tfjs version: ${human.tf.version['tfjs-core']}<br>` +
  61. `platform: ${human.env.platform} | ` +
  62. `agent ${human.env.agent}<br>` +
  63. `need_generation ${need_generation}<br>` + // draw labels, boxes, lines, etc.
  64. `text: ${text}`;
  65. }
  66. async function playAudio(audioSrc) {
  67. console.log('playing audio')
  68. const audioPlayer = new Audio(audioSrc);
  69. audioPlayer.addEventListener('ended', function () {
  70. need_generation = true;
  71. need_playing = true;
  72. console.log('playing done');
  73. });
  74. audioPlayer.play();
  75. }
  76. async function checkForNewAudio() {
  77. $.ajax({
  78. url: '/generate_audio',
  79. method: 'GET',
  80. success: function (response) {
  81. need_generation = response.need_generation;
  82. if (response.newAudio && need_playing) {
  83. console.log(response.newAudio)
  84. // Если есть новый аудиофайл, проигрывайте его на странице
  85. text = splitTextIntoLines(response.text, 20);
  86. need_generation = false;
  87. need_playing = false;
  88. playAudio(response.filename);
  89. }
  90. },
  91. error: function (error) {
  92. console.error('Ошибка при проверке наличия нового аудиофайла:', error);
  93. }
  94. });
  95. }
  96. async function send_data() {
  97. $.ajax({
  98. url: '/send_data',
  99. type: 'POST',
  100. data: { data: JSON.stringify(interpolated), state: need_generation },
  101. success: function (response) {
  102. console.log('face data sent!');
  103. }
  104. });
  105. };
  106. async function main() { // main entry point
  107. document.getElementById('log').innerHTML =
  108. `human version: ${human.version} | ` +
  109. `tfjs version: ${human.tf.version['tfjs-core']} <br>` +
  110. `platform: ${human.env.platform} | ` +
  111. `agent ${human.env.agent}<br>` +
  112. `need_generation ${need_generation}<br>` +
  113. `text: ${text}`;
  114. await human.webcam.start({ crop: true }); // find webcam and start it
  115. human.video(human.webcam.element); // instruct human to continously detect video frames
  116. canvas.width = human.webcam.width; // set canvas resolution to input webcam native resolution
  117. canvas.height = human.webcam.height;
  118. canvas.onclick = async () => { // pause when clicked on screen and resume on next click
  119. if (human.webcam.paused) await human.webcam.play();
  120. else human.webcam.pause();
  121. };
  122. await setInterval(drawLoop, 30); // start draw loop
  123. await setInterval(send_data, 2000);
  124. await setInterval(checkForNewAudio, 5000)
  125. };
  126. window.onload = main;
  127. </script>
  128. </body>
  129. </html>