In my face detection project the browser could find faces, but it had no idea who they belonged to. This follow-up adds recognition: give it a few photos of each person and it will name them on camera and log the time they were seen, which makes a simple attendance tracker.
How it works
- Each person gets a folder of reference photos in
public/models_database/<Name>/. - When the Express server starts, it scans that folder and writes
models.json, the list of people and their photos. - The page loads the face-api.js models, builds a face descriptor (a 128-number fingerprint of a face) for every reference photo, and compares faces in the live video against them.
- When someone is recognized, their name and the time are added to a table that can be copied or exported to Excel or CSV.
Building the "database" on the server
There is no real database. On startup, a small controller uses directory-tree to turn the folder structure into JSON:
const dirTree = require('directory-tree')
const basePath = path.join(__dirname, '../public/models_database')
module.exports = {
generateDatabase: function () {
const tree = dirTree(basePath)
fs.writeFileSync(basePath + '/models.json', JSON.stringify(tree))
}
}
Adding a person is just a matter of creating a folder with their name, dropping in a few clear, front-facing photos and restarting the server.
Learning each face
For recognition I use the heavier but more accurate SSD MobileNet v1 detector, plus the landmark and recognition networks. Each reference photo is turned into a descriptor and grouped by person:
const img = await faceapi.fetchImage(`../${rootDir}/${modelName}/${fileName}`)
const detection = await faceapi
.detectSingleFace(img)
.withFaceLandmarks()
.withFaceDescriptor()
descriptions.push(detection.descriptor)
// ...
labeled.push(new faceapi.LabeledFaceDescriptors(modelName, descriptions))
Matching faces in the video
A FaceMatcher compares each face found in the video against all the labeled descriptors and returns the closest match, or "unknown" if nothing is close enough. The second argument is the distance threshold; 0.7 worked well for my test photos:
const faceMatcher = new faceapi.FaceMatcher(labeledDescriptors, 0.7)
setInterval(async () => {
const detections = await faceapi
.detectAllFaces(video)
.withFaceLandmarks()
.withFaceDescriptors()
const resized = faceapi.resizeResults(detections, displaySize)
resized.forEach((d) => {
const result = faceMatcher.findBestMatch(d.descriptor)
new faceapi.draw.DrawBox(d.detection.box, { label: result.toString() }).draw(canvas)
AddRowToTracker(result)
})
}, 100)
The attendance table
Recognized people go into a jQuery DataTables table with Copy, Export to Excel and Export to CSV buttons. Each person is only added once per session, with the time they were first seen, so the table reads like a sign-in sheet.
Run it yourself
git clone https://github.com/SriharshaShesham/AI-FaceRecognition.git
cd AI-FaceRecognition
npm install
node src/app.js
Open http://localhost:3000, allow camera access, and stand in front of the camera.
Built with
Node.js, Express, face-api.js, jQuery, DataTables and Bootstrap.
Comments
Post a Comment