Skip to content

Commit 1b606bf

Browse files
committed
Adjustment for face recognition
1 parent 646525e commit 1b606bf

1 file changed

Lines changed: 28 additions & 6 deletions

File tree

‎script.js‎

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Promise.all([
44
faceapi.nets.tinyFaceDetector.loadFromUri('/models'),
55
faceapi.nets.faceLandmark68Net.loadFromUri('/models'),
66
faceapi.nets.faceRecognitionNet.loadFromUri('/models'),
7-
faceapi.nets.faceExpressionNet.loadFromUri('/models')
7+
faceapi.nets.ssdMobilenetv1.loadFromUri('/models'),
88
]).then(startVideo)
99

1010
function startVideo() {
@@ -21,11 +21,33 @@ video.addEventListener('play', () => {
2121
const displaySize = { width: video.width, height: video.height }
2222
faceapi.matchDimensions(canvas, displaySize)
2323
setInterval(async () => {
24-
const detections = await faceapi.detectAllFaces(video, new faceapi.TinyFaceDetectorOptions()).withFaceLandmarks().withFaceExpressions()
24+
const detections = await faceapi.detectAllFaces(video, new faceapi.TinyFaceDetectorOptions()).withFaceLandmarks().withFaceDescriptors()
2525
const resizedDetections = faceapi.resizeResults(detections, displaySize)
2626
canvas.getContext('2d').clearRect(0, 0, canvas.width, canvas.height)
27-
faceapi.draw.drawDetections(canvas, resizedDetections)
28-
faceapi.draw.drawFaceLandmarks(canvas, resizedDetections)
29-
faceapi.draw.drawFaceExpressions(canvas, resizedDetections)
27+
const labeledFaceDescriptors = await loadLabeledImages()
28+
const faceMatcher = new faceapi.FaceMatcher(labeledFaceDescriptors, 0.6)
29+
const results = resizedDetections.map(d => faceMatcher.findBestMatch(d.descriptor))
30+
results.forEach((result, i) => {
31+
const box = resizedDetections[i].detection.box
32+
const drawBox = new faceapi.draw.DrawBox(box, {label: result.toString() })
33+
drawBox.draw(canvas)
34+
})
35+
3036
}, 100)
31-
})
37+
})
38+
39+
function loadLabeledImages() {
40+
const labels = ['teste']
41+
return Promise.all(
42+
labels.map(async label => {
43+
const descriptions = []
44+
for (let i = 1; i <= 1; i++) {
45+
const img = await faceapi.fetchImage(`http://localhost:7777/img/${label}/${i}.jpg`)
46+
const detections = await faceapi.detectSingleFace(img).withFaceLandmarks().withFaceDescriptor()
47+
descriptions.push(detections.descriptor)
48+
}
49+
50+
return new faceapi.LabeledFaceDescriptors(label, descriptions)
51+
})
52+
)
53+
}

0 commit comments

Comments
 (0)