Face recognition with Node.js

Last week at Geoblink we organised our second hackathon (we call it Geothon), and a lot of interesting projects were presented. In my case, I had a lot of fun working on a face recognition app and I wanted to share some of the details about this project.

I decided to build the application in Node.js, since it’s our main server language. For face recognition I used OpenCV, a library focused on real-time image and video processing, with the main purpose of making computers able to understand and process images. OpenCV allows us to recognize lots of types of objects such as faces, eyes, a mouse, or even a full body, using Haar Cascades.

To build this kind of application, we firstly have to write the code for face recognition using a webcam. This can be done with just a few lines of code, using socket.io to stream data to the client.

  try {
    const camera = new cv.VideoCapture(0)
    setInterval(function () {
      camera.read(function (err, im) {
        if (err) throw err
        im.detectObject(cv.FACE_CASCADE, {}, function (err, faces) {
          const face = faces[0]
          im.rectangle([face.x, face.y], [face.width, face.height], color, thickness)
          socket.emit(‘image’, {buffer: im.toBuffer()})
    }, cameraInterval)
  } catch (e) {
    console.log(`Could not start camera: ${e}`)

The second part consists on recognizing whom the face belongs to. For this purpose, we can use OpenCV FaceRecognizer algorithms.

First of all, you will need to train your recognizer uploading some photos of every person you want to identify.

After this, the server can start predicting who appears on the images using the previous code. This prediction will display who appears on the image, with a probability percentage showing how confident the software is about the result. The more you train the recognizer, the better results it will yield.

Screen Shot 2017-03-06 at 16.21.43

Face recognition is a very useful tool that is already being used by some companies such as Facebook or Apple, for example to help the user save time when tagging people on the social platform. Other uses could be for house security through face recognition. This technology is starting to be widely used for various purposes, so expect more and applications recognizing your face!

By Jose Luis Pillado "Fofi"