Detecting faces in an image
To work with images on NodeJS, the node-opencv module is used, which is based on the OpenCV computer vision library. The Viola-Jones method and its implementation in OpenCV are used to detect faces in the image.
To implement the Viola-Jones method, the browser uses part of the code from the tracking.js library.
Information about eye centers is used to align the face image. The eyes in the image are the same method as the face, but using a different classifier. The approximate search areas for each eye are refined, then the eye search is started. The center of the eye is calculated as the center of mass of the rectangles that the Viola-Jones method finds for each eye. Then the distance between the centers and the angle of inclination of the head are determined, after which the face is rotated, scaled and trimmed. The output is an aligned photo of the face.
Running on the server
The source code is available here: face-alignment. To run Debian/Ubuntu on your computer, do the following:
sudo apt install nodejs libopencv-dev
git clone https://github.com/meefik/face-alignment
Running from the command line is performed as follows:
node detect.js input.png face.png out.png
face.png file contains only the face, and the
out.png file contains the original image with labels.
Launching in the browser
The web server starts with the following command:
After that you just need to access the address http://localhost:3000 in your browser.