Face Recognition using OpenCV – Part 2

Face Detection and Face Recognition using OpenCV – training

Hello everyone, this is part two of the tutorial face recognition using OpenCV. In part one of the tutorial, we discussed How to set up virtualenv and install necessary dependencies. To get a general idea of what face recognition and face detection is and to follow along with the tutorial, I advise you to check out part one of the tutorial series first if you haven’t already. The part two of the series is titled, “Face Detection and Face Recognition using OpenCV – training”.  In this part of the tutorial, we are going to focus on how to write the necessary code implementation for recording and training the face recognition program. We can further divide this part into:

  1. Create database for face recognition
  2. Record faces
  3. Train Recognizer

Create Database for face recognition

We are going to first create a database which stores the name of the corresponding faces. We will be using SQLite 3 for this purpose. Make a file named create_database.py in the working directory and copy paste the code below:

import sqlite3

conn = sqlite3.connect('database.db')

c = conn.cursor()

sql = """
           id integer unique primary key autoincrement,
           name text



Run the python script using the command python3 create_database.pyThis will create a database with filename database.db on the current directory. The database contains a table named users with two columns id and name. Once the database is created, you can use DB Browser for SQLite to view the table structure as well as data and it is going to look something like:

DB Browser for SQLite, OpenCV

Record Faces

Now, we are going to prepare the dataset for face recognition. We will be using haarcascade_frontalface_default.xml file provided in the opencv/data/haarcascades directory of the opencv repo in github. Download the file and place it in the working directory. After that, make a file named record_face.py in the working directory and copy paste the code below:

import cv2
import numpy as np 
import sqlite3
import os

conn = sqlite3.connect('database.db')
if not os.path.exists('./dataset'):

c = conn.cursor()

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

cap = cv2.VideoCapture(0)

uname = input("Enter your name: ")

c.execute('INSERT INTO users (name) VALUES (?)', (uname,))

uid = c.lastrowid

sampleNum = 0

while True:
  ret, img = cap.read()
  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  faces = face_cascade.detectMultiScale(gray, 1.3, 5)
  for (x,y,w,h) in faces:
    sampleNum = sampleNum+1
    cv2.rectangle(img, (x,y), (x+w, y+h), (255,0,0), 2)
  if sampleNum > 20:



The code above, when run, will ask you to enter the name for the face first. It will then use the haarcascade to find the face in the camera stream. It will look for 20 samples each at the interval of 100ms. Once 20 sample faces have been found, it stores the sample data in the ‘dataset’ directory inside the working directory. In the next step, we are going to train recognizer for face recognition.

Train Recognizer

OpenCV provides three methods of face recognition:

  • Eigenfaces
  • Fisherfaces
  • Local Binary Patterns Histograms (LBPH)

All three methods perform the recognition by comparing the face to be recognized with some training set of known faces. In the training set, we supply the algorithm faces and tell it to which person they belong.

Eigenfaces and Fisherfaces find a mathematical description of the most dominant features of the training set as a whole. LBPH analyzes each face in the training set separately and independently. The LBPH method is somewhat simpler, in the sense that we characterize each image in the dataset locally; and when a new unknown image is provided, we perform the same analysis on it and compare the result to each of the images in the dataset. We will be using the LBPH Face recognizer for our purpose. To do so, create a file named trainer.py in the working directory and copy paste the code below:

import os
import cv2
import numpy as np 
from PIL import Image

recognizer = cv2.face.LBPHFaceRecognizer_create()
path = 'dataset'
if not os.path.exists('./recognizer'):

def getImagesWithID(path):
  imagePaths = [os.path.join(path,f) for f in os.listdir(path)]
  faces = []
  IDs = []
  for imagePath in imagePaths:
    faceImg = Image.open(imagePath).convert('L')
    faceNp = np.array(faceImg,'uint8')
    ID = int(os.path.split(imagePath)[-1].split('.')[1])
  return np.array(IDs), faces

Ids, faces = getImagesWithID(path)

Run it using the command python3 trainer.pyThis will create a file named trainingData.yml inside the ‘recognizer’ directory inside the working directory.

This brings us to end of part 2 of the tutorial series, face recognition using OpenCV.  In this part of the series, we created 3 files:

  1. create_database.py: To create database and table
  2. record_face.py: To capture face images and record the corresponding name in the database.
  3. trainer.py: Use of OpenCV’s LBPH Face Recognizer to train the dataset that outputs trainingData.yml file that we’ll be using later in the tutorial for face recognition.

Our face recognition app is almost complete now. All we need to do is recognize the faces and fetch data from SQLite now which is on part 3 of the tutorial series.


32 Comments on Face Recognition using OpenCV – Part 2

  1. python3 trainer.py
    Traceback (most recent call last):
    File “trainer.py”, line 5, in
    recognizer = cv2.face.LBPHFaceRecognizer_create()
    AttributeError: module ‘cv2’ has no attribute ‘face’

  2. Hi Bibek, thanks for the tutorial. I just have a little problem with this command, though OpenCV (3.4.0) works fine and I have the correct XML file: face_cascade = cv2.CascadeClassifier(‘haarcascade_frontalface_default.xml’)

    It always gives me this error:
    OpenCV Error: Unknown error code -49 (Input file is empty) in cvOpenFileStorage, file /Users/travis/build/skvark/opencv-python/opencv/modules/core/src/persistence.cpp, line 4484
    cv2.error: /Users/travis/build/skvark/opencv-python/opencv/modules/core/src/persistence.cpp:4484: error: (-49) Input file is empty in function cvOpenFileStorage

    The above exception was the direct cause of the following exception:

    Traceback (most recent call last):
    File “”, line 1, in
    SystemError: returned a result with an error set

    Could you please help me out?

  3. recognizer = cv2.face.LBPHFaceRecognizer_create() it throws error as
    AttributeError: ‘module’ object has no attribute ‘face’
    mine opencv2 version is 3.4.0

  4. Hello,

    I got this error:

    recognizer = c.face.LBPHFaceRecognizer_create()
    AttributeError: module ‘cv2.cv2’ has no attribute ‘face’

  5. Hello,
    I want to use same code. but instead of using camera, I hope to upload a picture from a file, and store only the name of the pic on the database, and the pic it will be stored on the working dir.
    could you help me how to do that?

    Thank you so much for this great article.

    • Hello Hadel, please go through the comments section in the tutorial series. Your query has already been answered 😉

  6. Hi Aryal, I replicated the whole project, was good with all the steps till I ran ‘python record_face.py’. I’m using Python 3.6.4 :: Anaconda, Inc., OS- Windows 10 enterprise, x64-based PC. Also, please mention how are we preparing dataset here. Do I need to connect webcam for camera streaming ? Below are two errors I’m getting on running ‘python record_face.py’ & ‘python trainer.py’. Please help. Thanks.

    OpenCV & Python version–>
    Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 10:22:32) [MSC v.1900 64 bit (AMD64)] on win32
    Type “help”, “copyright”, “credits” or “license” for more information.
    >>> import cv2
    >>> cv2.__version__

    C:\Users\md393e\Documents\Project\Face_Recognition_based_Surveillance>python record_face.py
    Enter your name: tarun
    OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cv::cvtColor, file C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp, line 11111
    Traceback (most recent call last):
    File “record_face.py”, line 26, in
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    cv2.error: C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:11111: error: (-215) scn == 3 || scn == 4 in function cv::cvtColor

    C:\Users\md393e\Documents\Project\Face_Recognition_based_Surveillance>python trainer.py
    OpenCV Error: Unsupported format or combination of formats (Empty training data was given. You’ll need more than one sample to learn a model.) in cv::face::LBPH::train, file C:\projects\opencv-python\opencv_contrib\modules\face\src\lbph_faces.cpp, line 359
    Traceback (most recent call last):
    File “trainer.py”, line 26, in
    cv2.error: C:\projects\opencv-python\opencv_contrib\modules\face\src\lbph_faces.cpp:359: error: (-210) Empty training data was given. You’ll need more than one sample to learn a model. in function cv::face::LBPH::train

    • The command returns ret false and img none on OpenCV 3.3.0. Please recheck to see if OpenCV installation had any error. If OpenCV was installed correctly, the problem then might be with the hardware itself. Type the following command in your Ubuntu terminal to see the list of all video capturing devices.
      ls /dev/ | grep video
      If you have VLC installed, you can check to see if the webcam is working using the command:
      vlc v4l2:///dev/video0
      It should open up your webcam video stream on VLC.

  7. This is my error :
    “Enter your name: pol
    Traceback (most recent call last):
    File “record_face.py”, line 16, in
    uname = input(“Enter your name: “)
    File “”, line 1, in
    NameError: name ‘pol’ is not defined”

    And I have the project from github, so I don’t think it’s a typo in a codeline.
    Should I first create something else in the database?

    • I have unistalled it all and now I don’t get that problem, but still get another error to line 26:
      “gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
      cv2.error:io/opencv/modules/imgproc/src/color.cpp:11111: error: (-215) scn == 3 || scn == 4 in function cvtColor

      • What OS are you using? Also, please check your OpenCV version. Open the python terminal, import cv2 and run the command cv2.__version__ and paste the output.

          • Hello Ian, I rechecked again and could not reproduce the recent error you are receiving. However, the earlier error seems to occur when the python version you are using is 2.7. This tutorial is designed for python version 3. Are you sure you’re executing the files with the command “python3” instead of “python”? Please do let me know if using python3 works for you.

            • I work with python3 commands. I found onther people on the internet with the same problem, but they have it when they first read.
              Always after I have entered my name, there is an error
              “openCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file :io/opencv/modules/imgproc/src/color.cpp, line 11111

              should the opencvenv map be in a specific place?

            • In line no 25 in the file record_face.py, cap.read() returns two values ret and img. Can you print those values and paste it here so that we can confirm that capturing a video frame was successful.
              Also, we could reproduce the same error on OpenCV version 3.3.0 but the code executes successfully on OpenCV 3.4.0. Could it be that the OpenCV was installed with error?

  8. I did everything until I had a problem with the record_face.py. I run it, it asks my name and after this I get an error :
    “NameError: name ‘pol’ is not defined.”
    What did I do wrong?

    • Hello. You might have a typo in some line. Which line does it say the error is on? Please recheck and do let me know if the problem persists.

  9. Thanks ! Great tutorial !
    I got an error after executing trainer.py : ” No module named PIL

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.