commons.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. async function requestExternalImage(imageUrl) {
  2. const res = await fetch('fetch_external_image', {
  3. method: 'post',
  4. headers: {
  5. 'content-type': 'application/json'
  6. },
  7. body: JSON.stringify({ imageUrl })
  8. })
  9. if (!(res.status < 400)) {
  10. console.error(res.status + ' : ' + await res.text())
  11. throw new Error('failed to fetch image from url: ' + imageUrl)
  12. }
  13. let blob
  14. try {
  15. blob = await res.blob()
  16. return await faceapi.bufferToImage(blob)
  17. } catch (e) {
  18. console.error('received blob:', blob)
  19. console.error('error:', e)
  20. throw new Error('failed to load image from url: ' + imageUrl)
  21. }
  22. }
  23. function renderNavBar(navbarId, exampleUri) {
  24. const examples = [
  25. {
  26. uri: 'face_detection',
  27. name: 'Face Detection'
  28. },
  29. {
  30. uri: 'face_landmark_detection',
  31. name: 'Face Landmark Detection'
  32. },
  33. {
  34. uri: 'face_expression_recognition',
  35. name: 'Face Expression Recognition'
  36. },
  37. {
  38. uri: 'age_and_gender_recognition',
  39. name: 'Age and Gender Recognition'
  40. },
  41. {
  42. uri: 'face_recognition',
  43. name: 'Face Recognition'
  44. },
  45. {
  46. uri: 'face_extraction',
  47. name: 'Face Extraction'
  48. },
  49. {
  50. uri: 'video_face_tracking',
  51. name: 'Video Face Tracking'
  52. },
  53. {
  54. uri: 'webcam_face_detection',
  55. name: 'Webcam Face Detection'
  56. },
  57. {
  58. uri: 'webcam_face_landmark_detection',
  59. name: 'Webcam Face Landmark Detection'
  60. },
  61. {
  62. uri: 'webcam_face_expression_recognition',
  63. name: 'Webcam Face Expression Recognition'
  64. },
  65. {
  66. uri: 'webcam_age_and_gender_recognition',
  67. name: 'Webcam Age and Gender Recognition'
  68. },
  69. {
  70. uri: 'bbt_face_landmark_detection',
  71. name: 'BBT Face Landmark Detection'
  72. },
  73. {
  74. uri: 'bbt_face_similarity',
  75. name: 'BBT Face Similarity'
  76. },
  77. {
  78. uri: 'bbt_face_matching',
  79. name: 'BBT Face Matching'
  80. },
  81. {
  82. uri: 'bbt_face_recognition',
  83. name: 'BBT Face Recognition'
  84. },
  85. {
  86. uri: 'batch_face_landmarks',
  87. name: 'Batch Face Landmark Detection'
  88. },
  89. {
  90. uri: 'batch_face_recognition',
  91. name: 'Batch Face Recognition'
  92. }
  93. ]
  94. const navbar = $(navbarId).get(0)
  95. const pageContainer = $('.page-container').get(0)
  96. const header = document.createElement('h3')
  97. header.innerHTML = examples.find(ex => ex.uri === exampleUri).name
  98. pageContainer.insertBefore(header, pageContainer.children[0])
  99. const menuContent = document.createElement('ul')
  100. menuContent.id = 'slide-out'
  101. menuContent.classList.add('side-nav', 'fixed')
  102. navbar.appendChild(menuContent)
  103. const menuButton = document.createElement('a')
  104. menuButton.href='#'
  105. menuButton.classList.add('button-collapse', 'show-on-large')
  106. menuButton.setAttribute('data-activates', 'slide-out')
  107. const menuButtonIcon = document.createElement('img')
  108. menuButtonIcon.src = 'menu_icon.png'
  109. menuButton.appendChild(menuButtonIcon)
  110. navbar.appendChild(menuButton)
  111. const li = document.createElement('li')
  112. const githubLink = document.createElement('a')
  113. githubLink.classList.add('waves-effect', 'waves-light', 'side-by-side')
  114. githubLink.id = 'github-link'
  115. githubLink.href = 'https://github.com/justadudewhohacks/face-api.js'
  116. const h5 = document.createElement('h5')
  117. h5.innerHTML = 'face-api.js'
  118. githubLink.appendChild(h5)
  119. const githubLinkIcon = document.createElement('img')
  120. githubLinkIcon.src = 'github_link_icon.png'
  121. githubLink.appendChild(githubLinkIcon)
  122. li.appendChild(githubLink)
  123. menuContent.appendChild(li)
  124. examples
  125. .forEach(ex => {
  126. const li = document.createElement('li')
  127. if (ex.uri === exampleUri) {
  128. li.style.background='#b0b0b0'
  129. }
  130. const a = document.createElement('a')
  131. a.classList.add('waves-effect', 'waves-light', 'pad-sides-sm')
  132. a.href = ex.uri
  133. const span = document.createElement('span')
  134. span.innerHTML = ex.name
  135. span.style.whiteSpace = 'nowrap'
  136. a.appendChild(span)
  137. li.appendChild(a)
  138. menuContent.appendChild(li)
  139. })
  140. $('.button-collapse').sideNav({
  141. menuWidth: 260
  142. })
  143. }
  144. function renderSelectList(selectListId, onChange, initialValue, renderChildren) {
  145. const select = document.createElement('select')
  146. $(selectListId).get(0).appendChild(select)
  147. renderChildren(select)
  148. $(select).val(initialValue)
  149. $(select).on('change', (e) => onChange(e.target.value))
  150. $(select).material_select()
  151. }
  152. function renderOption(parent, text, value) {
  153. const option = document.createElement('option')
  154. option.innerHTML = text
  155. option.value = value
  156. parent.appendChild(option)
  157. }