当前位置:   article > 正文

OpenCv.js(图像处理)学习历程

opencv.js

opencv.js官网

4.5.0文档

以下内容整理于opencv.js官网。

简介

OpenCV由Gary Bradski于1999年在英特尔创建。第一次发行是在2000年。OpenCV支持c++、Python、Java等多种编程语言,支持Windows、Linux、Os X、Android、iOS等平台。基于CUDA和OpenCL的高速GPU操作接口也在积极开发中。OpenCV.js将OpenCV带到开放的web平台,并使JavaScript程序员可以使用它。

图片处理 

读取图片

readImage.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Hello OpenCV.js</title>
  6. </head>
  7. <body>
  8. <h2>读取图片</h2>
  9. <p id="status">OpenCV.js is loading...</p>
  10. <div>
  11. <div class="inputoutput">
  12. <img id="imageSrc" alt="No Image" />
  13. <div class="caption">imageSrc <input type="file" id="fileInput" name="file" /></div>
  14. </div>
  15. <div class="inputoutput">
  16. <canvas id="canvasOutput" ></canvas>
  17. <div class="caption">canvasOutput</div>
  18. </div>
  19. </div>
  20. <script type="text/javascript">
  21. let imgElement = document.getElementById('imageSrc');
  22. let inputElement = document.getElementById('fileInput');
  23. inputElement.addEventListener('change', (e) => {
  24. imgElement.src = URL.createObjectURL(e.target.files[0]);
  25. }, false);
  26. imgElement.onload = function() {
  27. let mat = cv.imread(imgElement);
  28. cv.imshow('canvasOutput', mat);
  29. mat.delete();
  30. };
  31. var Module = {
  32. // https://emscripten.org/docs/api_reference/module.html#Module.onRuntimeInitialized
  33. onRuntimeInitialized() {
  34. document.getElementById('status').innerHTML = 'OpenCV.js is ready.';
  35. }
  36. };
  37. </script>
  38. <!-- <script async src="https://docs.opencv.org/4.5.0/opencv.js" type="text/javascript"></script> -->
  39. <script async src="opencvjs/opencv.js" type="text/javascript"></script>
  40. <script src="opencvjs/utils.js" type="text/javascript"></script>
  41. </body>
  42. </html>

浏览器运行。 

灰度图

blackAWhite.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Adaptive Threshold Example</title>
  6. <link href="opencvjs/style.css" rel="stylesheet" type="text/css" />
  7. </head>
  8. <body>
  9. <h2>IMAGE</h2>
  10. <p></p>
  11. <div>
  12. <div class="control"><button id="tryIt" disabled>Start</button></div>
  13. <textarea class="code" rows="9" cols="100" id="codeEditor" spellcheck="false"></textarea>
  14. <p class="err" id="errorMessage"></p>
  15. </div>
  16. <div>
  17. <table cellpadding="0" cellspacing="0" width="0" border="0">
  18. <tr>
  19. <td>
  20. <canvas id="canvasInput"></canvas>
  21. </td>
  22. <td>
  23. <canvas id="canvasOutput"></canvas>
  24. </td>
  25. </tr>
  26. <tr>
  27. <td>
  28. <div class="caption">canvasInput <input type="file" id="fileInput" name="file" accept="image/*" /></div>
  29. </td>
  30. <td>
  31. <div class="caption">canvasOutput</div>
  32. </td>
  33. </tr>
  34. </table>
  35. </div>
  36. <script type="text/javascript" src="opencvjs/opencv.js"></script>
  37. <script src="opencvjs/utils.js" type="text/javascript"></script>
  38. <script id="codeSnippet" type="text/code-snippet">
  39. let src = cv.imread('canvasInput');
  40. let dst = new cv.Mat();
  41. // To distinguish the input and output, we graying the image.
  42. // You can try different conversions.
  43. cv.cvtColor(src, dst, cv.COLOR_RGBA2GRAY);
  44. cv.imshow('canvasOutput', dst);
  45. src.delete();
  46. dst.delete();
  47. </script>
  48. <script type="text/javascript">
  49. let utils = new Utils('errorMessage');
  50. utils.loadCode('codeSnippet', 'codeEditor');
  51. utils.loadImageToCanvas('lena.jpg', 'canvasInput');
  52. utils.addFileInputHandler('fileInput', 'canvasInput');
  53. let tryIt = document.getElementById('tryIt');
  54. tryIt.addEventListener('click', () => {
  55. utils.executeCode('codeEditor');
  56. });
  57. utils.loadOpenCv(() => {
  58. tryIt.removeAttribute('disabled');
  59. });
  60. </script>
  61. </body>
  62. </html>

浏览器运行。 

 

 阈值

inRangeImage.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Hello OpenCV.js</title>
  6. </head>
  7. <body>
  8. <h2></h2>
  9. <p id="status">OpenCV.js is loading...</p>
  10. <div>
  11. <div class="inputoutput">
  12. <img id="imageSrc" alt="No Image" />
  13. <div class="caption">imageSrc <input type="file" id="fileInput" name="file" /></div>
  14. </div>
  15. <div class="inputoutput">
  16. <canvas id="canvasOutput" ></canvas>
  17. <div class="caption">canvasOutput</div>
  18. </div>
  19. </div>
  20. <script type="text/javascript">
  21. let imgElement = document.getElementById('imageSrc');
  22. let inputElement = document.getElementById('fileInput');
  23. inputElement.addEventListener('change', (e) => {
  24. imgElement.src = URL.createObjectURL(e.target.files[0]);
  25. }, false);
  26. imgElement.onload = function() {
  27. let src = cv.imread('imageSrc');
  28. let dst = new cv.Mat();
  29. let low = new cv.Mat(src.rows, src.cols, src.type(), [0, 0, 0, 0]);
  30. let high = new cv.Mat(src.rows, src.cols, src.type(), [150, 150, 150, 255]);
  31. // You can try more different parameters
  32. cv.inRange(src, low, high, dst);
  33. cv.imshow('canvasOutput', dst);
  34. src.delete(); dst.delete(); low.delete(); high.delete();
  35. };
  36. var Module = {
  37. // https://emscripten.org/docs/api_reference/module.html#Module.onRuntimeInitialized
  38. onRuntimeInitialized() {
  39. document.getElementById('status').innerHTML = 'OpenCV.js is ready.';
  40. }
  41. };
  42. </script>
  43. <script async src="opencvjs/opencv.js" type="text/javascript"></script>
  44. <script src="opencvjs/utils.js" type="text/javascript"></script>
  45. </body>
  46. </html>

 

其它 

imageTemplate.html

 修改框内的代码即可对图片进行不同的操作,此代码适用于大部分的官网中有关图片操作的代码。 

视频处理 

 均值漂移

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>CamShift Example</title>
  6. <link href="opencvjs/style.css" rel="stylesheet" type="text/css" />
  7. </head>
  8. <body>
  9. <div>
  10. <div class="control"><button id="startAndStop" disabled>Start</button></div>
  11. <textarea class="code" rows="29" cols="100" id="codeEditor" spellcheck="false"></textarea>
  12. </div>
  13. <p class="err" id="errorMessage"></p>
  14. <div>
  15. <table cellpadding="0" cellspacing="0" width="0" border="0">
  16. <tr>
  17. <td>
  18. <video id="videoInput" width="320" height="240" muted loop></video>
  19. </td>
  20. <td>
  21. <canvas id="canvasOutput" width="320" height="240"></canvas>
  22. </td>
  23. <td></td>
  24. <td></td>
  25. </tr>
  26. <tr>
  27. <td>
  28. <div class="caption">videoInput</div>
  29. </td>
  30. <td>
  31. <div class="caption">canvasOutput</div>
  32. </td>
  33. <td></td>
  34. <td></td>
  35. </tr>
  36. </table>
  37. </div>
  38. <script src="https://webrtc.github.io/adapter/adapter-5.0.4.js" type="text/javascript"></script>
  39. <script type="text/javascript" src="opencvjs/opencv.js"></script>
  40. <script src="opencvjs/utils.js" type="text/javascript"></script>
  41. <script id="codeSnippet" type="text/code-snippet">
  42. let video = document.getElementById('videoInput');
  43. let cap = new cv.VideoCapture(video);
  44. // take first frame of the video
  45. let frame = new cv.Mat(video.height, video.width, cv.CV_8UC4);
  46. cap.read(frame);
  47. // hardcode the initial location of window
  48. let trackWindow = new cv.Rect(150, 60, 63, 125);
  49. // set up the ROI for tracking
  50. let roi = frame.roi(trackWindow);
  51. let hsvRoi = new cv.Mat();
  52. cv.cvtColor(roi, hsvRoi, cv.COLOR_RGBA2RGB);
  53. cv.cvtColor(hsvRoi, hsvRoi, cv.COLOR_RGB2HSV);
  54. let mask = new cv.Mat();
  55. let lowScalar = new cv.Scalar(30, 30, 0);
  56. let highScalar = new cv.Scalar(180, 180, 180);
  57. let low = new cv.Mat(hsvRoi.rows, hsvRoi.cols, hsvRoi.type(), lowScalar);
  58. let high = new cv.Mat(hsvRoi.rows, hsvRoi.cols, hsvRoi.type(), highScalar);
  59. cv.inRange(hsvRoi, low, high, mask);
  60. let roiHist = new cv.Mat();
  61. let hsvRoiVec = new cv.MatVector();
  62. hsvRoiVec.push_back(hsvRoi);
  63. cv.calcHist(hsvRoiVec, [0], mask, roiHist, [180], [0, 180]);
  64. cv.normalize(roiHist, roiHist, 0, 255, cv.NORM_MINMAX);
  65. // delete useless mats.
  66. roi.delete(); hsvRoi.delete(); mask.delete(); low.delete(); high.delete(); hsvRoiVec.delete();
  67. // Setup the termination criteria, either 10 iteration or move by atleast 1 pt
  68. let termCrit = new cv.TermCriteria(cv.TERM_CRITERIA_EPS | cv.TERM_CRITERIA_COUNT, 10, 1);
  69. let hsv = new cv.Mat(video.height, video.width, cv.CV_8UC3);
  70. let hsvVec = new cv.MatVector();
  71. hsvVec.push_back(hsv);
  72. let dst = new cv.Mat();
  73. let trackBox = null;
  74. const FPS = 30;
  75. function processVideo() {
  76. try {
  77. if (!streaming) {
  78. // clean and stop.
  79. frame.delete(); dst.delete(); hsvVec.delete(); roiHist.delete(); hsv.delete();
  80. return;
  81. }
  82. let begin = Date.now();
  83. // start processing.
  84. cap.read(frame);
  85. cv.cvtColor(frame, hsv, cv.COLOR_RGBA2RGB);
  86. cv.cvtColor(hsv, hsv, cv.COLOR_RGB2HSV);
  87. cv.calcBackProject(hsvVec, [0], roiHist, dst, [0, 180], 1);
  88. // apply camshift to get the new location
  89. [trackBox, trackWindow] = cv.CamShift(dst, trackWindow, termCrit);
  90. // Draw it on image
  91. let pts = cv.rotatedRectPoints(trackBox);
  92. cv.line(frame, pts[0], pts[1], [255, 0, 0, 255], 3);
  93. cv.line(frame, pts[1], pts[2], [255, 0, 0, 255], 3);
  94. cv.line(frame, pts[2], pts[3], [255, 0, 0, 255], 3);
  95. cv.line(frame, pts[3], pts[0], [255, 0, 0, 255], 3);
  96. cv.imshow('canvasOutput', frame);
  97. // schedule the next one.
  98. let delay = 1000/FPS - (Date.now() - begin);
  99. setTimeout(processVideo, delay);
  100. } catch (err) {
  101. utils.printError(err);
  102. }
  103. };
  104. // schedule the first one.
  105. setTimeout(processVideo, 0);
  106. </script>
  107. <script type="text/javascript">
  108. let utils = new Utils('errorMessage');
  109. utils.loadCode('codeSnippet', 'codeEditor');
  110. let streaming = false;
  111. let videoInput = document.getElementById('videoInput');
  112. let startAndStop = document.getElementById('startAndStop');
  113. let canvasOutput = document.getElementById('canvasOutput');
  114. let canvasContext = canvasOutput.getContext('2d');
  115. startAndStop.addEventListener('click', () => {
  116. if (!streaming) {
  117. utils.clearError();
  118. videoInput.play().then(() => {
  119. onVideoStarted();
  120. });
  121. } else {
  122. videoInput.pause();
  123. videoInput.currentTime = 0;
  124. onVideoStopped();
  125. }
  126. });
  127. function onVideoStarted() {
  128. streaming = true;
  129. startAndStop.innerText = 'Stop';
  130. videoInput.height = videoInput.width * (videoInput.videoHeight / videoInput.videoWidth);
  131. utils.executeCode('codeEditor');
  132. }
  133. function onVideoStopped() {
  134. streaming = false;
  135. canvasContext.clearRect(0, 0, canvasOutput.width, canvasOutput.height);
  136. startAndStop.innerText = 'Start';
  137. }
  138. utils.loadOpenCv(() => {
  139. videoInput.addEventListener('canplay', () => {
  140. startAndStop.removeAttribute('disabled');
  141. });
  142. videoInput.src = 'video/cup.mp4';
  143. });
  144. </script>
  145. </body>
  146. </html>

背景差分 

backgroundSubtraction.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <link href="opencvjs/style.css" rel="stylesheet" type="text/css" />
  6. </head>
  7. <body>
  8. <div>
  9. <div class="control"><button id="startAndStop" disabled>Start</button></div>
  10. <textarea class="code" rows="29" cols="100" id="codeEditor" spellcheck="false"></textarea>
  11. </div>
  12. <p class="err" id="errorMessage"></p>
  13. <div>
  14. <table cellpadding="0" cellspacing="0" width="0" border="0">
  15. <tr>
  16. <td>
  17. <video id="videoInput" width="320" height="240" muted></video>
  18. </td>
  19. <td>
  20. <canvas id="canvasOutput" width="320" height="240"></canvas>
  21. </td>
  22. <td></td>
  23. <td></td>
  24. </tr>
  25. <tr>
  26. <td>
  27. <div class="caption">videoInput</div>
  28. </td>
  29. <td>
  30. <div class="caption">canvasOutput</div>
  31. </td>
  32. <td></td>
  33. <td></td>
  34. </tr>
  35. </table>
  36. </div>
  37. <script type="text/javascript" src="opencvjs/opencv.js"></script>
  38. <script src="opencvjs/utils.js" type="text/javascript"></script>
  39. <script id="codeSnippet" type="text/code-snippet">
  40. let video = document.getElementById('videoInput');
  41. let cap = new cv.VideoCapture(video);
  42. let frame = new cv.Mat(video.height, video.width, cv.CV_8UC4);
  43. let fgmask = new cv.Mat(video.height, video.width, cv.CV_8UC1);
  44. let fgbg = new cv.BackgroundSubtractorMOG2(500, 16, true);
  45. const FPS = 30;
  46. function processVideo() {
  47. try {
  48. if (!streaming) {
  49. // clean and stop.
  50. frame.delete(); fgmask.delete(); fgbg.delete();
  51. return;
  52. }
  53. let begin = Date.now();
  54. // start processing.
  55. cap.read(frame);
  56. fgbg.apply(frame, fgmask);
  57. cv.imshow('canvasOutput', fgmask);
  58. // schedule the next one.
  59. let delay = 1000/FPS - (Date.now() - begin);
  60. setTimeout(processVideo, delay);
  61. } catch (err) {
  62. utils.printError(err);
  63. }
  64. };
  65. // schedule the first one.
  66. setTimeout(processVideo, 0);
  67. </script>
  68. <script type="text/javascript">
  69. let utils = new Utils('errorMessage');
  70. utils.loadCode('codeSnippet', 'codeEditor');
  71. let streaming = false;
  72. let videoInput = document.getElementById('videoInput');
  73. let startAndStop = document.getElementById('startAndStop');
  74. startAndStop.addEventListener('click', () => {
  75. if (!streaming) {
  76. utils.clearError();
  77. videoInput.play().then(() => {
  78. onVideoStarted();
  79. });
  80. } else {
  81. videoInput.pause();
  82. videoInput.currentTime = 0;
  83. onVideoStopped();
  84. }
  85. });
  86. function onVideoStarted() {
  87. streaming = true;
  88. startAndStop.innerText = 'Stop';
  89. videoInput.height = videoInput.width * (videoInput.videoHeight / videoInput.videoWidth);
  90. utils.executeCode('codeEditor');
  91. }
  92. function onVideoStopped() {
  93. streaming = false;
  94. startAndStop.innerText = 'Start';
  95. }
  96. videoInput.addEventListener('ended', () => {
  97. onVideoStopped();
  98. });
  99. utils.loadOpenCv(() => {
  100. videoInput.addEventListener('canplay', () => {
  101. startAndStop.removeAttribute('disabled');
  102. });
  103. videoInput.src = 'video/box.mp4';
  104. });
  105. </script>
  106. </body>
  107. </html>

卡纳德方法

LucasKanade.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <link href="opencvjs/style.css" rel="stylesheet" type="text/css" />
  6. </head>
  7. <body>
  8. <div>
  9. <div class="control"><button id="startAndStop" disabled>Start</button></div>
  10. <textarea class="code" rows="29" cols="100" id="codeEditor" spellcheck="false"></textarea>
  11. </div>
  12. <p class="err" id="errorMessage"></p>
  13. <div>
  14. <table cellpadding="0" cellspacing="0" width="0" border="0">
  15. <tr>
  16. <td>
  17. <video id="videoInput" width="320" height="240" muted></video>
  18. </td>
  19. <td>
  20. <canvas id="canvasOutput" width="320" height="240"></canvas>
  21. </td>
  22. <td></td>
  23. <td></td>
  24. </tr>
  25. <tr>
  26. <td>
  27. <div class="caption">videoInput</div>
  28. </td>
  29. <td>
  30. <div class="caption">canvasOutput</div>
  31. </td>
  32. <td></td>
  33. <td></td>
  34. </tr>
  35. </table>
  36. </div>
  37. <script type="text/javascript" src="opencvjs/opencv.js"></script>
  38. <script src="opencvjs/utils.js" type="text/javascript"></script>
  39. <script id="codeSnippet" type="text/code-snippet">
  40. let video = document.getElementById('videoInput');
  41. let cap = new cv.VideoCapture(video);
  42. // take first frame of the video
  43. let frame1 = new cv.Mat(video.height, video.width, cv.CV_8UC4);
  44. cap.read(frame1);
  45. let prvs = new cv.Mat();
  46. cv.cvtColor(frame1, prvs, cv.COLOR_RGBA2GRAY);
  47. frame1.delete();
  48. let hsv = new cv.Mat();
  49. let hsv0 = new cv.Mat(video.height, video.width, cv.CV_8UC1);
  50. let hsv1 = new cv.Mat(video.height, video.width, cv.CV_8UC1, new cv.Scalar(255));
  51. let hsv2 = new cv.Mat(video.height, video.width, cv.CV_8UC1);
  52. let hsvVec = new cv.MatVector();
  53. hsvVec.push_back(hsv0); hsvVec.push_back(hsv1); hsvVec.push_back(hsv2);
  54. let frame2 = new cv.Mat(video.height, video.width, cv.CV_8UC4);
  55. let next = new cv.Mat(video.height, video.width, cv.CV_8UC1);
  56. let flow = new cv.Mat(video.height, video.width, cv.CV_32FC2);
  57. let flowVec = new cv.MatVector();
  58. let mag = new cv.Mat(video.height, video.width, cv.CV_32FC1);
  59. let ang = new cv.Mat(video.height, video.width, cv.CV_32FC1);
  60. let rgb = new cv.Mat(video.height, video.width, cv.CV_8UC3);
  61. const FPS = 30;
  62. function processVideo() {
  63. try {
  64. if (!streaming) {
  65. // clean and stop.
  66. prvs.delete(); hsv.delete(); hsv0.delete(); hsv1.delete(); hsv2.delete();
  67. hsvVec.delete(); frame2.delete(); flow.delete(); flowVec.delete(); next.delete();
  68. mag.delete(); ang.delete(); rgb.delete();
  69. return;
  70. }
  71. let begin = Date.now();
  72. // start processing.
  73. cap.read(frame2);
  74. cv.cvtColor(frame2, next, cv.COLOR_RGBA2GRAY);
  75. cv.calcOpticalFlowFarneback(prvs, next, flow, 0.5, 3, 15, 3, 5, 1.2, 0);
  76. cv.split(flow, flowVec);
  77. let u = flowVec.get(0);
  78. let v = flowVec.get(1);
  79. cv.cartToPolar(u, v, mag, ang);
  80. u.delete(); v.delete();
  81. ang.convertTo(hsv0, cv.CV_8UC1, 180/Math.PI/2);
  82. cv.normalize(mag, hsv2, 0, 255, cv.NORM_MINMAX, cv.CV_8UC1);
  83. cv.merge(hsvVec, hsv);
  84. cv.cvtColor(hsv, rgb, cv.COLOR_HSV2RGB);
  85. cv.imshow('canvasOutput', rgb);
  86. next.copyTo(prvs);
  87. // schedule the next one.
  88. let delay = 1000/FPS - (Date.now() - begin);
  89. setTimeout(processVideo, delay);
  90. } catch (err) {
  91. utils.printError(err);
  92. }
  93. };
  94. // schedule the first one.
  95. setTimeout(processVideo, 0);
  96. </script>
  97. <script type="text/javascript">
  98. let utils = new Utils('errorMessage');
  99. utils.loadCode('codeSnippet', 'codeEditor');
  100. let streaming = false;
  101. let videoInput = document.getElementById('videoInput');
  102. let startAndStop = document.getElementById('startAndStop');
  103. startAndStop.addEventListener('click', () => {
  104. if (!streaming) {
  105. utils.clearError();
  106. videoInput.play().then(() => {
  107. onVideoStarted();
  108. });
  109. } else {
  110. videoInput.pause();
  111. videoInput.currentTime = 0;
  112. onVideoStopped();
  113. }
  114. });
  115. function onVideoStarted() {
  116. streaming = true;
  117. startAndStop.innerText = 'Stop';
  118. videoInput.height = videoInput.width * (videoInput.videoHeight / videoInput.videoWidth);
  119. utils.executeCode('codeEditor');
  120. }
  121. function onVideoStopped() {
  122. streaming = false;
  123. startAndStop.innerText = 'Start';
  124. }
  125. videoInput.addEventListener('ended', () => {
  126. onVideoStopped();
  127. });
  128. utils.loadOpenCv(() => {
  129. videoInput.addEventListener('canplay', () => {
  130. startAndStop.removeAttribute('disabled');
  131. });
  132. videoInput.src = 'video/box.mp4';
  133. });
  134. </script>
  135. </body>
  136. </html>

人脸识别

人脸识别_照片 

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

 

 以上测试图片来自百度。

可以看到,有的地方不是耳朵也会被标记成耳朵,原因是官方的数据集数据比较微小,数据不够准确,但是眼睛的地方能标记出来就已经达到我们的学习目的了,以后可以用更精确的数据集替换效果就会好很多。

人脸识别_相机 

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Face Detection Camera Example</title>
  6. <link href="opencvjs/style.css" rel="stylesheet" type="text/css" />
  7. </head>
  8. <body>
  9. <!-- <h2>Face Detection Camera Example</h2> -->
  10. <div>
  11. <div class="control"><button id="startAndStop">Start</button></div>
  12. <textarea class="code" rows="29" cols="80" id="codeEditor" spellcheck="false">
  13. </textarea>
  14. </div>
  15. <p class="err" id="errorMessage"></p>
  16. <div>
  17. <table cellpadding="0" cellspacing="0" width="0" border="0">
  18. <tr>
  19. <td>
  20. <video id="videoInput" width=320 height=240></video>
  21. </td>
  22. <td>
  23. <canvas id="canvasOutput" width=320 height=240></canvas>
  24. </td>
  25. <td></td>
  26. <td></td>
  27. </tr>
  28. <tr>
  29. <td>
  30. <div class="caption">videoInput</div>
  31. </td>
  32. <td>
  33. <div class="caption">canvasOutput</div>
  34. </td>
  35. <td></td>
  36. <td></td>
  37. </tr>
  38. </table>
  39. </div>
  40. <script type="text/javascript" src="opencvjs/opencv.js"></script>
  41. <script src="opencvjs/utils.js" type="text/javascript"></script>
  42. <!-- <script src="opencvjs/adapter-5.0.4.js" type="text/javascript"></script> -->
  43. <!-- <script src="opencvjs/known.js" type="text/javascript"></script> -->
  44. <script id="codeSnippet" type="text/code-snippet">
  45. let video = document.getElementById('videoInput');
  46. let src = new cv.Mat(video.height, video.width, cv.CV_8UC4);
  47. let dst = new cv.Mat(video.height, video.width, cv.CV_8UC4);
  48. let gray = new cv.Mat();
  49. let cap = new cv.VideoCapture(video);
  50. let faces = new cv.RectVector();
  51. let classifier = new cv.CascadeClassifier();
  52. // load pre-trained classifiers
  53. classifier.load('haarcascade_frontalface_default.xml');
  54. const FPS = 30;
  55. function processVideo() {
  56. try {
  57. if (!streaming) {
  58. // clean and stop.
  59. src.delete();
  60. dst.delete();
  61. gray.delete();
  62. faces.delete();
  63. classifier.delete();
  64. return;
  65. }
  66. let begin = Date.now();
  67. // start processing.
  68. cap.read(src);
  69. src.copyTo(dst);
  70. cv.cvtColor(dst, gray, cv.COLOR_RGBA2GRAY, 0);
  71. // detect faces.
  72. classifier.detectMultiScale(gray, faces, 1.1, 3, 0);
  73. // draw faces.
  74. for (let i = 0; i < faces.size(); ++i) {
  75. let face = faces.get(i);
  76. let point1 = new cv.Point(face.x, face.y);
  77. let point2 = new cv.Point(face.x + face.width, face.y + face.height);
  78. cv.rectangle(dst, point1, point2, [255, 0, 0, 255]);
  79. }
  80. cv.imshow('canvasOutput', dst);
  81. // schedule the next one.
  82. let delay = 1000/FPS - (Date.now() - begin);
  83. setTimeout(processVideo, delay);
  84. } catch (err) {
  85. utils.printError(err);
  86. }
  87. };
  88. // schedule the first one.
  89. setTimeout(processVideo, 0);
  90. </script>
  91. <script type="text/javascript">
  92. let utils = new Utils('errorMessage');
  93. utils.loadCode('codeSnippet', 'codeEditor');
  94. let streaming = false;
  95. let videoInput = document.getElementById('videoInput');
  96. let startAndStop = document.getElementById('startAndStop');
  97. let canvasOutput = document.getElementById('canvasOutput');
  98. let canvasContext = canvasOutput.getContext('2d');
  99. startAndStop.addEventListener('click', () => {
  100. if (!streaming) {
  101. utils.clearError();
  102. utils.startCamera('qvga', onVideoStarted, 'videoInput');
  103. } else {
  104. utils.stopCamera();
  105. onVideoStopped();
  106. }
  107. });
  108. function onVideoStarted() {
  109. streaming = true;
  110. startAndStop.innerText = 'Stop';
  111. videoInput.width = videoInput.videoWidth;
  112. videoInput.height = videoInput.videoHeight;
  113. utils.executeCode('codeEditor');
  114. }
  115. function onVideoStopped() {
  116. streaming = false;
  117. canvasContext.clearRect(0, 0, canvasOutput.width, canvasOutput.height);
  118. startAndStop.innerText = 'Start';
  119. }
  120. utils.loadOpenCv(() => {
  121. let faceCascadeFile = 'haarcascade_frontalface_default.xml';
  122. utils.createFileFromUrl(faceCascadeFile, faceCascadeFile, () => {
  123. // startAndStop.removeAttribute('disabled');
  124. });
  125. });
  126. </script>
  127. </body>
  128. </html>

4.8.0文档

4.8.0相对于4.5.0新增了深度学习模块。

深度神经网络

 

选择相应的model,从对应的链接中得到对应的数据集文件,看第一块代码的配置项是否一致。 

 上传对应的文件,点击运行。

起初认为是时间问题,晚上挂了一段时间,早上起来依旧没有任何数据 ,打开浏览器审查页面,发现有很多报错,无论如何我都得不到想要的结果,这块就放弃了。

补充

完整文件

gitee获取

去掉文本代码区域

在实际开发中我们是不需要textarea的,所以必须去掉它。但是我尝试更改了html文件和js文件,没有成功,所以干脆直接让textarea区域:display:none。

<textarea class="code" rows="9" cols="100" id="codeEditor" spellcheck="false" style="display: none;"></textarea>

参考文献

代码哈士,opencv.js人脸识别简单

opencv.js官网的js文件不是真正的js文件,官网文档里需要建立其环境,这片文章介绍通过爬取得到官方的资源。 

opencv.js官网 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/137924
推荐阅读
相关标签
  

闽ICP备14008679号