Graduate

Opencv.js face detect

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