Awesome Open Source
Awesome Open Source

Face recognition from camera with Dlib

Introduction

, / Detect and recognize single or multi faces from camera;

  1. Tkinter , () / Face register GUI with Tkinter, support setting (chinese) name when registering

    introduction/face_register_tkinter_GUI.png
  2. OpenCV / Simple face register GUI with OpenCV, tkinter not needed and cannot set name

introduction/face_register.png

, , "OUT OF RANGE" / Too close to the camera, or face ROI out of camera area, will have "OUT OF RANGE" warning;

introduction/face_register_warning.png
  1. / Generate face database from images captured
  2. / Face recognizer

face_reco_from_camera.py, / Do detection and recognition for every frame:

introduction/face_reco.png

face_reco_from_camera_single_face.py, <=1, FPS / Do re-reco only for new single face:

introduction/face_reco_single.png

face_reco_from_camera_ot.py, OT FPS / Use OT to instead of re-reco for every frame to improve FPS:

introduction/face_reco_ot.png

, / Show chinese name:

introduction/face_reco_chinese_name.png

** / About accuracy:

  • When using a distance threshold of 0.6, the dlib model obtains an accuracy of 99.38% on the standard LFW face recognition benchmark.

** / About algorithm

  • Residual Neural Network / CNN ;
  • This model is a ResNet network with 29 conv layers.

It's essentially a version of the ResNet-34 network from the paper Deep Residual Learning for Image Recognition by He, Zhang, Ren, and Sun with a few layers removed and the number of filters per layer reduced by half.

Overview

(no OT, +) /

Design of this repo, do detection and recognization for every frame:

Use OT can save the time for face descriptor computation to improve FPS;

Steps

  1. / Git clone source code
git clone https://github.com/coneypo/Dlib_face_recognition_from_camera
  1. / Install some python packages needed
pip install -r requirements.txt
  1. , Tkinter GUI / Register faces with Tkinter GUI

    # Install Tkinter
    sudo apt-get install python3-tk python3-pil python3-pil.imagetk
    
    python3 get_faces_from_camera_tkinter.py
    
  2. , OpenCV GUI / Register faces with OpenCV GUI, same with above step

    python3 get_face_from_camera.py
    
  3. features_all.csv / Features extraction and save into features_all.csv

python3 features_extraction_to_csv.py
  1. / Real-time face recognition
python3 face_reco_from_camera.py
  1. <=1, / Real-time face recognition (Better FPS compared with face_reco_from_camera.py)

    python3 face_reco_from_camera_single_face.py
    
  2. OT , / Real-time face recognition with OT (Better FPS)

python3 face_reco_from_camera_ot.py

About Source Code

/ Code structure:
   .
    get_faces_from_camera.py                   # Step 1. Face register GUI with OpenCV
    get_faces_from_camera_tkinter.py                # Step 1. Face register GUI with Tkinter
    features_extraction_to_csv.py              # Step 2. Feature extraction
    face_reco_from_camera.py                   # Step 3. Face recognizer
    face_reco_from_camera_single_face.py            # Step 3. Face recognizer for single person
    face_reco_from_camera_ot.py                     # Step 3. Face recognizer with OT
    face_descriptor_from_camera.py             # Face descriptor computation
    how_to_use_camera.py                       # Use the default camera by opencv
    data
     data_dlib                                 # Dlib's model
      dlib_face_recognition_resnet_model_v1.dat
      shape_predictor_68_face_landmarks.dat
     data_faces_from_camera                      # Face images captured from camera (will generate after step 1)
      person_1
       img_face_1.jpg
       img_face_2.jpg
      person_2
          img_face_1.jpg
          img_face_2.jpg
     features_all.csv                          # CSV to save all the features of known faces (will generate after step 2)
    README.rst
    requirements.txt                           # Some python packages needed

Dlib  / Dlib related functions used in this repo:
  1. Dlib (based on HOG), output: <class 'dlib.dlib.rectangles'> / Dlib frontal face detector

    detector = dlib.get_frontal_face_detector()
    faces = detector(img_gray, 0)
    
  2. Dlib landmark , output: <class 'dlib.dlib.full_object_detection'> / Dlib face landmark predictor, will use shape_predictor_68_face_landmarks.dat

    # This is trained on the ibug 300-W dataset (https://ibug.doc.ic.ac.uk/resources/facial-point-annotations/)
    # Also note that this model file is designed for use with dlib's HOG face detector.
    # That is, it expects the bounding boxes from the face detector to be aligned a certain way,
    the way dlib's HOG face detector does it.
    # It won't work as well when used with a face detector that produces differently aligned boxes,
    # such as the CNN based mmod_human_face_detector.dat face detector.
    
    predictor = dlib.shape_predictor("data/data_dlib/shape_predictor_68_face_landmarks.dat")
    shape = predictor(img_rd, faces[i])
    
  3. Dlib / Face recognition model, the object maps human faces into 128D vectors

    face_rec = dlib.face_recognition_model_v1("data/data_dlib/dlib_face_recognition_resnet_model_v1.dat")
    

Python / Source code:

  1. get_face_from_camera.py:

    / Face register with OpenCV GUI

    • , , ;
    • "out of range" ;
  2. get_faces_from_camera_tkinter.py:

    Tkinter GUI / Face register with Tkinter GUI

  3. features_extraction_to_csv.py:

    , CSV / Extract features from face images saved in step 1;

    • features_all.csv
    • Size: n*129 , n means n faces you registered and 129 means face name + 128D features of this face
  4. face_reco_from_camera.py:

    ; / This part will implement real-time face recognition;

    • , ;
    • Compare the faces captured from camera with the faces you have registered which are saved in features_all.csv;
  5. face_reco_from_camera_single_face.py:

    <=1 , face_reco_from_camera.py (+), ;

  6. face_reco_from_camera_ot.py:

    +, +;

  7. (optional) face_descriptor_from_camera.py

    ; / Real-time face descriptor computation;

More

  1. dlib , Dlib Python api / You can refer to this link for more information of how to use dlib: http://dlib.net/python/index.html
  2. Modify log level to logging.basicConfig(level=logging.DEBUG) to print info for every frame if needed (Default is logging.INFO)
  3. / No chinese characters in your code directory
  4. , N S / Press N before S
  5. face_reco_from_camera.py FPS , ; face_descriptor_from_camera.py face_reco_model.compute_face_descriptor FPS 5 ( 0.03s , 0.158s , 0.003s ); , OT ( face_reco_from_camera_ot.py ), +, 20 FPS -> 200 FPS

, / For more details, please visit my blog (in chinese) or send mail to [email protected]:

Thanks for your support.



Alternative Project Comparisons
Related Awesome Lists
Top Programming Languages

Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
Python (859,182
Bash (19,009
Camera (16,802
Convolutional Neural Networks (12,741
Face (11,986
Recognition (10,763
Center (5,251
Face Recognition (2,527
Dlib (1,119
Dlib Face Recognition (25
Resnet 34 (23