Skip to content

Commit 958ca95

Browse files
added some more nodejs examples + move all examples into examples directory
1 parent d31c4a6 commit 958ca95

Some content is hidden

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

82 files changed

+148
-65
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ tmp
55
proto
66
weights_uncompressed
77
weights_unused
8-
docs
8+
docs
9+
out

.npmignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ node_modules
22
.rpt2_cache
33

44
examples
5-
examples-nodejs
65
proto
76
weights
87
weights_uncompressed

examples-nodejs/faceDetection.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.

examples-nodejs/faceLandmarkDetection.ts

Lines changed: 0 additions & 26 deletions
This file was deleted.
File renamed without changes.

examples/public/js/bbt.js renamed to examples/examples-browser/public/js/bbt.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const classes = ['amy', 'bernadette', 'howard', 'leonard', 'penny', 'raj', 'sheldon', 'stuart']
22

33
function getFaceImageUri(className, idx) {
4-
return `images/${className}/${className}${idx}.png`
4+
return `${className}/${className}${idx}.png`
55
}
66

77
function renderFaceImageSelectList(selectListId, onChange, initialValue) {

examples/public/js/commons.js renamed to examples/examples-browser/public/js/commons.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
function getImageUri(imageName) {
2-
return `images/${imageName}`
3-
}
4-
51
async function requestExternalImage(imageUrl) {
62
const res = await fetch('fetch_external_image', {
73
method: 'post',

examples/public/js/imageSelectionControls.js renamed to examples/examples-browser/public/js/imageSelectionControls.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ function renderImageSelectList(selectListId, onChange, initialValue) {
1717
renderOption(
1818
select,
1919
imageName,
20-
getImageUri(imageName)
20+
imageName
2121
)
2222
)
2323
}
2424

2525
renderSelectList(
2626
selectListId,
2727
onChange,
28-
getImageUri(initialValue),
28+
initialValue,
2929
renderChildren
3030
)
3131
}

examples/server.js renamed to examples/examples-browser/server.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ app.use(express.urlencoded({ extended: true }))
1010
const viewsDir = path.join(__dirname, 'views')
1111
app.use(express.static(viewsDir))
1212
app.use(express.static(path.join(__dirname, './public')))
13-
app.use(express.static(path.join(__dirname, '../weights')))
14-
app.use(express.static(path.join(__dirname, '../weights_uncompressed')))
15-
app.use(express.static(path.join(__dirname, '../dist')))
16-
app.use(express.static(path.join(__dirname, './node_modules/axios/dist')))
13+
app.use(express.static(path.join(__dirname, '../images')))
14+
app.use(express.static(path.join(__dirname, '../media')))
15+
app.use(express.static(path.join(__dirname, '../../weights')))
16+
app.use(express.static(path.join(__dirname, '../../dist')))
1717

1818
app.get('/', (req, res) => res.redirect('/face_and_landmark_detection'))
1919
app.get('/face_and_landmark_detection', (req, res) => res.sendFile(path.join(viewsDir, 'faceAndLandmarkDetection.html')))

examples/views/videoFaceTracking.html renamed to examples/examples-browser/views/videoFaceTracking.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<div class="indeterminate"></div>
1919
</div>
2020
<div style="position: relative" class="margin">
21-
<video src="media/bbt.mp4" id="inputVideo" autoplay muted loop></video>
21+
<video src="bbt.mp4" id="inputVideo" autoplay muted loop></video>
2222
<canvas id="overlay" />
2323
</div>
2424

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// import nodejs bindings to native tensorflow,
2+
// not required, but will speed up things drastically (python required)
3+
import '@tensorflow/tfjs-node';
4+
5+
// implements nodejs wrappers for HTMLCanvasElement, HTMLImageElement, ImageData
6+
import * as canvas from 'canvas';
7+
8+
import * as faceapi from '../../../src';
9+
10+
// patch nodejs environment, we need to provide an implementation of
11+
// HTMLCanvasElement and HTMLImageElement, additionally an implementation
12+
// of ImageData is required, in case you want to use the MTCNN
13+
const { Canvas, Image, ImageData } = canvas
14+
faceapi.env.monkeyPatch({ Canvas, Image, ImageData })
15+
16+
export { canvas, faceapi }
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { NeuralNetwork } from 'tfjs-image-recognition-base';
2+
3+
import { faceapi } from './env';
4+
5+
export const faceDetectionNet = faceapi.nets.ssdMobilenetv1
6+
// export const faceDetectionNet = tinyFaceDetector
7+
// export const faceDetectionNet = mtcnn
8+
9+
// SsdMobilenetv1Options
10+
const minConfidence = 0.5
11+
12+
// TinyFaceDetectorOptions
13+
const inputSize = 408
14+
const scoreThreshold = 0.5
15+
16+
// MtcnnOptions
17+
const minFaceSize = 50
18+
const scaleFactor = 0.8
19+
20+
function getFaceDetectorOptions(net: NeuralNetwork<any>) {
21+
return net === faceapi.nets.ssdMobilenetv1
22+
? new faceapi.SsdMobilenetv1Options({ minConfidence })
23+
: (net === faceapi.nets.tinyFaceDetector
24+
? new faceapi.TinyFaceDetectorOptions({ inputSize, scoreThreshold })
25+
: new faceapi.MtcnnOptions({ minFaceSize, scaleFactor })
26+
)
27+
}
28+
29+
export const faceDetectionOptions = getFaceDetectorOptions(faceDetectionNet)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export { canvas, faceapi } from './env';
2+
export { faceDetectionNet, faceDetectionOptions } from './faceDetection';
3+
export { saveFile } from './saveFile';
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import * as fs from 'fs';
2+
import * as path from 'path';
3+
4+
const baseDir = path.resolve(__dirname, '../out')
5+
6+
export function saveFile(fileName: string, buf: Buffer) {
7+
if (!fs.existsSync(baseDir)) {
8+
fs.mkdirSync(baseDir)
9+
}
10+
11+
fs.writeFileSync(path.resolve(baseDir, fileName), buf)
12+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { canvas, faceapi, faceDetectionNet, faceDetectionOptions, saveFile } from './commons';
2+
3+
async function run() {
4+
5+
await faceDetectionNet.loadFromDisk('../../weights')
6+
7+
const img = await canvas.loadImage('../images/bbt1.jpg')
8+
const detections = await faceapi.detectAllFaces(img, faceDetectionOptions)
9+
10+
const out = faceapi.createCanvasFromMedia(img) as any
11+
faceapi.drawDetection(out, detections)
12+
13+
saveFile('faceDetection.jpg', out.toBuffer('image/jpeg'))
14+
}
15+
16+
run()
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { canvas, faceapi, faceDetectionNet, faceDetectionOptions, saveFile } from './commons';
2+
3+
async function run() {
4+
5+
await faceDetectionNet.loadFromDisk('../../weights')
6+
await faceapi.nets.faceLandmark68Net.loadFromDisk('../../weights')
7+
8+
const img = await canvas.loadImage('../images/bbt1.jpg')
9+
const results = await faceapi.detectAllFaces(img, faceDetectionOptions)
10+
.withFaceLandmarks()
11+
12+
const out = faceapi.createCanvasFromMedia(img) as any
13+
faceapi.drawDetection(out, results.map(res => res.detection))
14+
faceapi.drawLandmarks(out, results.map(res => res.faceLandmarks), { drawLines: true, color: 'red' })
15+
16+
saveFile('faceLandmarkDetection.jpg', out.toBuffer('image/jpeg'))
17+
}
18+
19+
run()
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { canvas, faceapi, faceDetectionNet, faceDetectionOptions, saveFile } from './commons';
2+
3+
const REFERENCE_IMAGE = '../images/bbt1.jpg'
4+
const QUERY_IMAGE = '../images/bbt4.jpg'
5+
6+
async function run() {
7+
8+
await faceDetectionNet.loadFromDisk('../../weights')
9+
await faceapi.nets.faceLandmark68Net.loadFromDisk('../../weights')
10+
await faceapi.nets.faceRecognitionNet.loadFromDisk('../../weights')
11+
12+
const referenceImage = await canvas.loadImage(REFERENCE_IMAGE)
13+
const queryImage = await canvas.loadImage(QUERY_IMAGE)
14+
15+
const resultsRef = await faceapi.detectAllFaces(referenceImage, faceDetectionOptions)
16+
.withFaceLandmarks()
17+
.withFaceDescriptors()
18+
19+
const resultsQuery = await faceapi.detectAllFaces(queryImage, faceDetectionOptions)
20+
.withFaceLandmarks()
21+
.withFaceDescriptors()
22+
23+
const faceMatcher = new faceapi.FaceMatcher(resultsRef)
24+
25+
const labels = faceMatcher.labeledDescriptors
26+
.map(ld => ld.label)
27+
const refBoxesWithText = resultsRef
28+
.map(res => res.detection.box)
29+
.map((box, i) => new faceapi.BoxWithText(box, labels[i]))
30+
const outRef = faceapi.createCanvasFromMedia(referenceImage) as any
31+
faceapi.drawDetection(outRef, refBoxesWithText)
32+
saveFile('referenceImage.jpg', outRef.toBuffer('image/jpeg'))
33+
34+
const queryBoxesWithText = resultsQuery.map(res => {
35+
const bestMatch = faceMatcher.findBestMatch(res.descriptor)
36+
return new faceapi.BoxWithText(res.detection.box, bestMatch.toString())
37+
})
38+
const outQuery = faceapi.createCanvasFromMedia(queryImage) as any
39+
faceapi.drawDetection(outQuery, queryBoxesWithText)
40+
saveFile('queryImage.jpg', outQuery.toBuffer('image/jpeg'))
41+
}
42+
43+
run()
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

examples/public/images/bbt1.jpg

-251 KB
Binary file not shown.

0 commit comments

Comments
 (0)