kaschmo / sh_face_rec

Face recognition system for any IP Camera and openHAB
30 stars 10 forks source link

sh_face_rec - Smart Home Face Recognition

A simple face recognition system that can be used with any streaming camera and works with OpenHAB via REST communication. Runs on a Raspberry Pi.

Animation

Check the Wiki for detailed description of possible Face Detection/Recognition Frameworks incl. evaluation. Wiki

Face Processing Pipeline

Processing Pipeline This pipeline is heavily inspired by the OpenFace Pipeline. See further down for information on training the neural networks of each pipeline step. For basic image handling (reading/streaming, writing, transforming, extracing and showing) openCV is used.

Face Detection

As a first step we need to detect any shapes in the video frames that look like faces. Luckily there are many frameworks available which can do that. The challenge is to find “good enough” performance for my usecase (640x480 blurry and dark camera feed) and balance with resource requirements = processing FPS (frames per second). See The Wiki for the framework evaluation. I went for an MTCNN implemetation of a face detector. Implementation: davidsandberg's implementation of MTCNN for face detection Github

Face Alignment and Crop

Once I have all boundary boxes around the faces in the frame I use dlibs pose predictor to align all the faces and crop them to a standard format of 64x64 pixels. Implementation: Face Alignment and Landmark Extraction: dlib (pose_predictor 5point)

Face Embedding

This step creates a 128bit representation (embeddings) of every face that can then be used by a classifier to determine if we have a match or not. I went for the FaceNet approach of using a pretrained Neural Network to create these face embeddings. See the Wiki for the frameworks that I evaluated. I decided to use dlibs ResNet network to create 128bit vectors of the faces. Implementation: dlib CNN face encoder Dlib. Comes with pretrained network on 3Mio face images from VGG and facescrub dataset.

Face Classification

With the 128bit embeddings I can train a classifier to detect known faces. In this pipeline step every new detected face embedding is fed into the classifier to determine if we have a known face (threshold comparison) or not. Implementation: knn classifier from sklearn

Setup and Performance

The application is written to work on a Raspberry Pi3. Python3 is required for multiprocessing. (Multithreading is terribly slow) I used Anaconda as environment manager. Use the env.yml file in the root directory to set up an working Anaconda environment. Configuration is pretty self-explanatory in confi.ini

Image/Video Handling is done with openCV, so any streaming IP camera or file system videos should work (not tested) Performance with 640x480 videos (mjpg) on the RPi3:

Setup Pis

Repository Folder Structure

Startup

Preparation

Production (uWSGI Server)

Training & Models

Testing

For visual inspection of the pipeline the two testcases

Test_Facerecognizer

Test_Classifier

API Examples

The flask server API takes simple REST calls I use Postman to debug the API and the application. API calls in openHAB via restcall.

Examples

New Streaming Job from CAM_URL for 10s on server SERVER_URL:PORT curl -i -H "Content-Type: application/json" -X POST -d {'"URL":"CAM_URL", "Time": "10"}' SERVER_URL:SERVER_PORT/detectJSON

List Statistics curl -i -H SERVER_URL:SERVER_PORT/getStats

Get i Known Person curl -i -H SERVER_URL:SERVER_PORT/getKnown/<int:index>

Get i Known Face curl -i -H SERVER_URL:SERVER_PORT/getKnownFace/<int:index>

Get i Unknown Face curl -i -H SERVER_URL:SERVER_PORT/getUnknownFace/<int:index>

Get last complete Frame curl -i -H SERVER_URL:SERVER_PORT/getLastFrame