Graduate

Opencv.js face detection

...@@ -41,53 +41,48 @@ if (navigator.mediaDevices.getUserMedia){ ...@@ -41,53 +41,48 @@ if (navigator.mediaDevices.getUserMedia){
41 } 41 }
42 </script> 42 </script>
43 <script> 43 <script>
44 -function openCvReady() { 44 + let video = document.getElementById('videoElement');
45 - cv['onRuntimeInitialized']=()=>{ 45 + let src = new cv.Mat(video.height, video.width, cv.CV_8UC4);
46 - // do all your work here 46 + let dst = new cv.Mat(video.height, video.width, cv.CV_8UC4);
47 - let video = document.getElementById('videoElement'); 47 + let gray = new cv.Mat();
48 - let src = new cv.Mat(video.height, video.width, cv.CV_8UC4); 48 + let cap = new cv.RectVector();
49 - let dst = new cv.Mat(video.height, video.width, cv.CV_8UC4); 49 + let classifier = new cv.CascadeClassifier();
50 - let gray = new cv.Mat(); 50 + classifier.load("{{url_for('static', filename='js/haarcascade_frontalface_default.xml')}}")
51 - let cap = new cv.RectVector(); 51 + const FPS = 30;
52 - let classifier = new cv.CascadeClassifier(); 52 + function processVideo() {
53 - classifier.load("{{url_for('static', filename='js/haarcascade_frontalface_default.xml')}}") 53 + try {
54 - const FPS = 30; 54 + if (!streaming) {
55 - function processVideo() { 55 + // clean and stop.
56 - try { 56 + src.delete();
57 - if (!streaming) { 57 + dst.delete();
58 - // clean and stop. 58 + gray.delete();
59 - src.delete(); 59 + faces.delete();
60 - dst.delete(); 60 + classifier.delete();
61 - gray.delete(); 61 + return;
62 - faces.delete(); 62 + }
63 - classifier.delete(); 63 + let begin = Date.now();
64 - return; 64 + // start processing.
65 - } 65 + cap.read(src);
66 - let begin = Date.now(); 66 + src.copyTo(dst);
67 - // start processing. 67 + cv.cvtColor(dst, gray, cv.COLOR_RGBA2GRAY, 0);
68 - cap.read(src); 68 + // detect faces.
69 - src.copyTo(dst); 69 + classifier.detectMultiScale(gray, faces, 1.1, 3, 0);
70 - cv.cvtColor(dst, gray, cv.COLOR_RGBA2GRAY, 0); 70 + // draw faces.
71 - // detect faces. 71 + for (let i = 0; i < faces.size(); ++i) {
72 - classifier.detectMultiScale(gray, faces, 1.1, 3, 0); 72 + let face = faces.get(i);
73 - // draw faces. 73 + let point1 = new cv.Point(face.x, face.y);
74 - for (let i = 0; i < faces.size(); ++i) { 74 + let point2 = new cv.Point(face.x + face.width, face.y + face.height);
75 - let face = faces.get(i); 75 + cv.rectangle(dst, point1, point2, [255, 0, 0, 255]);
76 - let point1 = new cv.Point(face.x, face.y); 76 + }
77 - let point2 = new cv.Point(face.x + face.width, face.y + face.height); 77 + cv.imshow('canvasOutput', dst);
78 - cv.rectangle(dst, point1, point2, [255, 0, 0, 255]); 78 + // schedule the next one.
79 - } 79 + let delay = 1000/FPS - (Date.now() - begin);
80 - cv.imshow('canvasOutput', dst); 80 + setTimeout(processVideo, delay);
81 - // schedule the next one. 81 + } catch (err) {
82 - let delay = 1000/FPS - (Date.now() - begin); 82 + utils.printError(err);
83 - setTimeout(processVideo, delay); 83 + }
84 - } catch (err) { 84 + // schedule the first one.
85 - utils.printError(err); 85 + setTimeout(processVideo, 0);
86 - }
87 - // schedule the first one.
88 - setTimeout(processVideo, 0);
89 - };
90 -}
91 </script> 86 </script>
92 </body> 87 </body>
93 </html> 88 </html>
......