|
| 1 | +const cv = require("../"); |
| 2 | +const fs = require("fs"); |
| 3 | +const path = require("path"); |
| 4 | + |
| 5 | +if (!cv.xmodules.face) { |
| 6 | + throw new Error("exiting: opencv4nodejs compiled without face module"); |
| 7 | +} |
| 8 | + |
| 9 | +const facemarkModelPath = "../data/face/"; |
| 10 | +const modelFile = path.resolve(facemarkModelPath, "lbfmodel.yaml"); |
| 11 | + |
| 12 | +if (!fs.existsSync(modelFile)) { |
| 13 | + console.log("could not find landmarks model"); |
| 14 | + console.log( |
| 15 | + "download the model from: https://raw.githubusercontent.com/kurnianggoro/GSOC2017/master/data/lbfmodel.yaml" |
| 16 | + ); |
| 17 | + throw new Error("exiting: could not find landmarks model"); |
| 18 | +} |
| 19 | + |
| 20 | +const classifier = new cv.CascadeClassifier(cv.HAAR_FRONTALFACE_ALT2); |
| 21 | + |
| 22 | +// create the facemark object with the landmarks model |
| 23 | +const facemark = new cv.FacemarkLBF(); |
| 24 | +facemark.loadModel(modelFile); |
| 25 | + |
| 26 | +// give the facemark object it's face detection callback |
| 27 | +facemark.setFaceDetector(frame => { |
| 28 | + const { objects } = classifier.detectMultiScale(frame, 1.12); |
| 29 | + return objects; |
| 30 | +}); |
| 31 | + |
| 32 | +// retrieve faces using the facemark face detector callback |
| 33 | +const image = cv.imread("../data/got.jpg"); |
| 34 | +const gray = image.bgrToGray(); |
| 35 | +const faces = facemark.getFaces(gray); |
| 36 | + |
| 37 | +// use the detected faces to detect the landmarks |
| 38 | +const faceLandmarks = facemark.fit(gray, faces); |
| 39 | + |
| 40 | +for (let i = 0; i < faceLandmarks.length; i++) { |
| 41 | + const landmarks = faceLandmarks[i]; |
| 42 | + for (let x = 0; x < landmarks.length; x++) { |
| 43 | + image.drawCircle(landmarks[x], 1, new cv.Vec(0, 255, 0), 1, cv.LINE_8); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +cv.imshowWait("VideoCapture", image); |
0 commit comments