Skip to content

Commit b31de15

Browse files
added typings
1 parent 502b4a2 commit b31de15

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+1702
-4
lines changed

.gitattributes

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
*.sh* text eol=lf
1+
*.sh* text eol=lf
2+
*.d.ts linguist-vendored=false
3+
/examples/* linguist-documentation=false

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ coverage
99
coverage-report
1010
tmpdata
1111
data/dnn
12-
.idea/
12+
.idea/
13+
dist

.npmignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ coverage
1414
coverage-report
1515
tmpdata
1616
ci
17-
.dockerignore
17+
.dockerignore
18+
dist

examples/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "opencv4nodejs_examples",
33
"version": "1.1.0",
44
"author": "justadudewhohacks",
5-
"license": "ISC",
5+
"license": "MIT",
66
"dependencies": {},
77
"devDependencies": {
88
"eslint": "^3.18.0",

examples/typed/faceRecognition0.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import * as fs from 'fs';
2+
import * as path from 'path';
3+
import * as cv from '../../';
4+
5+
if (!cv.xmodules.face) {
6+
throw new Error('exiting: opencv4nodejs compiled without face module');
7+
}
8+
9+
const basePath = '../data/face-recognition';
10+
const imgsPath = path.resolve(basePath, 'imgs');
11+
const nameMappings = ['daryl', 'rick', 'negan'];
12+
13+
const imgFiles = fs.readdirSync(imgsPath);
14+
15+
const classifier = new cv.CascadeClassifier(cv.HAAR_FRONTALFACE_ALT2);
16+
const getFaceImage = (grayImg) => {
17+
const faceRects = classifier.detectMultiScale(grayImg).objects;
18+
if (!faceRects.length) {
19+
throw new Error('failed to detect faces');
20+
}
21+
return grayImg.getRegion(faceRects[0]);
22+
};
23+
24+
const images = imgFiles
25+
// get absolute file path
26+
.map(file => path.resolve(imgsPath, file))
27+
// read image
28+
.map(filePath => cv.imread(filePath))
29+
// face recognizer works with gray scale images
30+
.map(img => img.bgrToGray())
31+
// detect and extract face
32+
.map(getFaceImage)
33+
// face images must be equally sized
34+
.map(faceImg => faceImg.resize(80, 80));
35+
36+
const isImageFour = (_, i) => imgFiles[i].includes('4');
37+
const isNotImageFour = (_, i) => !isImageFour(_, i);
38+
// use images 1 - 3 for training
39+
const trainImages = images.filter(isNotImageFour);
40+
// use images 4 for testing
41+
const testImages = images.filter(isImageFour);
42+
// make labels
43+
const labels = imgFiles
44+
.filter(isNotImageFour)
45+
.map(file => nameMappings.findIndex(name => file.includes(name)));
46+
47+
const runPrediction = (recognizer) => {
48+
testImages.forEach((img) => {
49+
const result = recognizer.predict(img);
50+
console.log('predicted: %s, confidence: %s', nameMappings[result.label], result.confidence);
51+
cv.imshowWait('face', img);
52+
cv.destroyAllWindows();
53+
});
54+
};
55+
56+
const eigen = new cv.EigenFaceRecognizer();
57+
const fisher = new cv.FisherFaceRecognizer();
58+
const lbph = new cv.LBPHFaceRecognizer();
59+
eigen.train(trainImages, labels);
60+
fisher.train(trainImages, labels);
61+
lbph.train(trainImages, labels);
62+
63+
console.log('eigen:');
64+
runPrediction(eigen);
65+
66+
console.log('fisher:');
67+
runPrediction(fisher);
68+
69+
console.log('lbph:');
70+
runPrediction(lbph);

0 commit comments

Comments
 (0)