Image-Recognition

The project is aimed to recognize every individual from a group photo consisting of several individuals.

This is achieved in Python (3.8.5), OpenCV (4.5.2), imutils (0.5.4)

The program is divided into 3 steps:

  1. Extract embeddings
  • Responsible for using a deep learning feature extractor to generate a 128-D vector describing a face. All faces in our dataset will be passed through the neural network to generate embeddings.
  1. Train Model
  • Our Linear SVM model will be trained by this script. We’ll detect faces, extract embeddings, and fit our SVM model to the embeddings data.
  1. Recognize
  • We’ll recognize faces in images. We’ll detect faces, extract embeddings, and query our SVM model to determine who is in an image. We’ll upload this data to the DB. No need draw boxes.

File Structure:

  • dataset*
    • person 1
    • person 2
    • ...
    • ...
    • person n
  • face_detection_model
  • output
    • embeddings.pickle
    • le.pickle
    • recognizer.pickle
  • python_files
    • extract_embeddings.py
    • train_model.py
    • recognize.py
    • server.py
  • images
    • image.jpg (temp-ly)

All the python files are executed using run-script.sh script. Along with the script use any of the following arguments to perform corresponging function:

  • extract
  • train
  • recognize

Ex: ./run-script.sh train

Output:

[INFO] loading face embeddings...
[INFO] encoding labels...
[INFO] training model...

(Note: Rename the subdirectories of the "dataset" as per the name of the person.)

Step #1: Extract embeddings from face dataset

File: extract_embeddings.py

Libraries used are:

  1. os
  2. cv2
  3. numpy
  4. pickle
  5. imutils
  6. argparse
  7. "paths" from imutils

CL-Arguments it takes:

  1. "--dataset" - path to input directory of faces + images
  2. "--detector" - path to output serialized db of facial embeddings
  3. "--embeddings" - path to OpenCV's deep learning face detector
  4. "--confidence" - minimum probability to filter weak detections

Models Used:

  1. openface.nn4.small2.v1.t7
  2. deploy.prototxt
  3. Caffe Model

Step #2: Train face recognition model

File: train_model.py

Libraries used:

  1. "LabelEncoder" from sklearn.preprocessing
  2. "SVC" from sklearn.svm
  3. argparse
  4. pickle

CL-Arguments it takes:

  1. "--embeddings" - path to serialized db of facial embeddings
  2. "--recognizer" - path to output model trained to recognize faces
  3. "--le" - path to output label encoder

Model used:

  1. SVC from sklearn.svm

Step #3: Recognize faces with OpenCV

File: recognize.py

Libraries used:

  1. os
  2. cv2
  3. pickle
  4. imutils
  5. argparse
  6. numpy

CL-Arguments it takes:

  1. "--image" - path to input image
  2. "--detector" - path to OpenCV's deep learning face detector
  3. "--recognizer" - path to model trained to recognize faces
  4. "--le" - path to label encoder
  5. "--confidence" - minimum probability to filter weak detections

Models Used:

  1. openface.nn4.small2.v1.t7
  2. deploy.prototxt
  3. Caffe Model

Flask Server

File: server.py

We use a Flask Server to perform POST & GET Requests.

For Image recognition make a post request with form-data as:

  • key: image, value: "select-file"
  • key: course, value: "course-name"