js_face_detection.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Face Detection Example</title>
  6. <link href="js_example_style.css" rel="stylesheet" type="text/css" />
  7. </head>
  8. <body>
  9. <h2>Face Detection Example</h2>
  10. <p>
  11. &lt;canvas&gt; elements named <b>canvasInput</b> and <b>canvasOutput</b> have been prepared.<br>
  12. Click <b>Try it</b> button to see the result. You can choose another image.<br>
  13. You can change the code in the &lt;textarea&gt; to investigate more.
  14. </p>
  15. <div>
  16. <div class="control"><button id="tryIt" disabled>Try it</button></div>
  17. <textarea class="code" rows="9" cols="100" id="codeEditor" spellcheck="false">
  18. </textarea>
  19. <p class="err" id="errorMessage"></p>
  20. </div>
  21. <div>
  22. <table cellpadding="0" cellspacing="0" width="0" border="0">
  23. <tr>
  24. <td>
  25. <canvas id="canvasInput"></canvas>
  26. </td>
  27. <td>
  28. <canvas id="canvasOutput"></canvas>
  29. </td>
  30. </tr>
  31. <tr>
  32. <td>
  33. <div class="caption">canvasInput <input type="file" id="fileInput" name="file" accept="image/*" /></div>
  34. </td>
  35. <td>
  36. <div class="caption">canvasOutput</div>
  37. </td>
  38. </tr>
  39. </table>
  40. </div>
  41. <script src="utils.js" type="text/javascript"></script>
  42. <script id="codeSnippet" type="text/code-snippet">
  43. let src = cv.imread('canvasInput');
  44. let gray = new cv.Mat();
  45. cv.cvtColor(src, gray, cv.COLOR_RGBA2GRAY, 0);
  46. let faces = new cv.RectVector();
  47. let eyes = new cv.RectVector();
  48. let faceCascade = new cv.CascadeClassifier();
  49. let eyeCascade = new cv.CascadeClassifier();
  50. // load pre-trained classifiers
  51. faceCascade.load('haarcascade_frontalface_default.xml');
  52. eyeCascade.load('haarcascade_eye.xml');
  53. // detect faces
  54. let msize = new cv.Size(0, 0);
  55. faceCascade.detectMultiScale(gray, faces, 1.1, 3, 0, msize, msize);
  56. for (let i = 0; i < faces.size(); ++i) {
  57. let roiGray = gray.roi(faces.get(i));
  58. let roiSrc = src.roi(faces.get(i));
  59. let point1 = new cv.Point(faces.get(i).x, faces.get(i).y);
  60. let point2 = new cv.Point(faces.get(i).x + faces.get(i).width,
  61. faces.get(i).y + faces.get(i).height);
  62. cv.rectangle(src, point1, point2, [255, 0, 0, 255]);
  63. // detect eyes in face ROI
  64. eyeCascade.detectMultiScale(roiGray, eyes);
  65. for (let j = 0; j < eyes.size(); ++j) {
  66. let point1 = new cv.Point(eyes.get(j).x, eyes.get(j).y);
  67. let point2 = new cv.Point(eyes.get(j).x + eyes.get(j).width,
  68. eyes.get(j).y + eyes.get(j).height);
  69. cv.rectangle(roiSrc, point1, point2, [0, 0, 255, 255]);
  70. }
  71. roiGray.delete(); roiSrc.delete();
  72. }
  73. cv.imshow('canvasOutput', src);
  74. src.delete(); gray.delete(); faceCascade.delete();
  75. eyeCascade.delete(); faces.delete(); eyes.delete();
  76. </script>
  77. <script type="text/javascript">
  78. let utils = new Utils('errorMessage');
  79. utils.loadCode('codeSnippet', 'codeEditor');
  80. utils.loadImageToCanvas('lena.jpg', 'canvasInput');
  81. utils.addFileInputHandler('fileInput', 'canvasInput');
  82. let tryIt = document.getElementById('tryIt');
  83. tryIt.addEventListener('click', () => {
  84. utils.executeCode('codeEditor');
  85. });
  86. utils.loadOpenCv(() => {
  87. let eyeCascadeFile = 'haarcascade_eye.xml';
  88. utils.createFileFromUrl(eyeCascadeFile, eyeCascadeFile, () => {
  89. let faceCascadeFile = 'haarcascade_frontalface_default.xml';
  90. utils.createFileFromUrl(faceCascadeFile, faceCascadeFile, () => {
  91. tryIt.removeAttribute('disabled');
  92. });
  93. });
  94. });
  95. </script>
  96. </body>
  97. </html>