index.html 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. var prompt;
  40. function splitTextIntoLines(text, wordsPerLine) {
  41. const words = text.split(' ');
  42. let line = '';
  43. let j = 0;
  44. for (let i = 0; i < words.length; i++) {
  45. if (j < wordsPerLine) {
  46. line += words[i] + ' ';
  47. j += 1;
  48. } else {
  49. line += '\n';
  50. j = 0;
  51. }
  52. }
  53. return line;
  54. }
  55. async function drawLoop() { // main screen refresh loop
  56. interpolated = human.next(); // get smoothened result using last-known results which are continously updated based on input webcam video
  57. 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
  58. await human.draw.all(canvas, interpolated);
  59. document.getElementById('log').innerHTML =
  60. `human version: ${human.version} | ` +
  61. `tfjs version: ${human.tf.version['tfjs-core']}<br>` +
  62. `platform: ${human.env.platform} | ` +
  63. `agent ${human.env.agent}<br>` +
  64. `need_generation ${need_generation}<br>` + // draw labels, boxes, lines, etc.
  65. `prompt ${prompt}<br>` +
  66. `text: ${text}`;
  67. }
  68. async function playAudio(audioSrc) {
  69. console.log('playing audio')
  70. const audioPlayer = new Audio(audioSrc);
  71. audioPlayer.addEventListener('ended', function () {
  72. need_generation = true;
  73. need_playing = true;
  74. console.log('playing done');
  75. });
  76. audioPlayer.play();
  77. }
  78. async function checkForNewAudio() {
  79. $.ajax({
  80. url: '/check_audio',
  81. method: 'GET',
  82. success: function (response) {
  83. // need_generation = response.need_generation;
  84. if (response.newAudio && need_playing) {
  85. console.log(response.newAudio)
  86. // Если есть новый аудиофайл, проигрывайте его на странице
  87. text = splitTextIntoLines(response.text, 20);
  88. prompt = splitTextIntoLines(response.prompt, 20);
  89. need_generation = false;
  90. need_playing = false;
  91. playAudio(response.filename);
  92. }
  93. },
  94. error: function (error) {
  95. console.error('Ошибка при проверке наличия нового аудиофайла:', error);
  96. }
  97. });
  98. }
  99. async function send_data() {
  100. $.ajax({
  101. url: '/send_data',
  102. type: 'POST',
  103. data: { data: JSON.stringify(interpolated), state: need_generation },
  104. success: function (response) {
  105. console.log('face data sent!');
  106. }
  107. });
  108. };
  109. async function main() { // main entry point
  110. document.getElementById('log').innerHTML =
  111. `human version: ${human.version} | ` +
  112. `tfjs version: ${human.tf.version['tfjs-core']} <br>` +
  113. `platform: ${human.env.platform} | ` +
  114. `agent ${human.env.agent}<br>` +
  115. `need_generation ${need_generation}<br>` +
  116. `text: ${text}`;
  117. await human.webcam.start({ crop: true }); // find webcam and start it
  118. human.video(human.webcam.element); // instruct human to continously detect video frames
  119. canvas.width = human.webcam.width; // set canvas resolution to input webcam native resolution
  120. canvas.height = human.webcam.height;
  121. canvas.onclick = async () => { // pause when clicked on screen and resume on next click
  122. if (human.webcam.paused) await human.webcam.play();
  123. else human.webcam.pause();
  124. };
  125. await setInterval(drawLoop, 30); // start draw loop
  126. await setInterval(send_data, 1000);
  127. await setInterval(checkForNewAudio, 5000)
  128. };
  129. window.onload = main;
  130. </script>
  131. </body>
  132. </html>