Face Recognition OpenCV – Training A Face Recognizer

To perform face recognition we need to train a face recognizer, using a pre labeled dataset, In my previous post we created a labeled dataset for our face recognition system, now its time to use that dataset to train a face recognizer using opencv python,

Lets Train A Face RecognizerTwitter

First create a python “trainner.py” file in the same folder where we saved out dataset generator script in the previous post, and then create a folder in the same directory name it “trainner”, this is the folder where we are going to save our recognizer after training.

So the folder structure till now should be something like this

Those who don’t know how we got the dataset folder and script , it was created in my previous post you should check that first.

Before we start actual coding we need a new library called pillow,
open your cmd (run as administrator )and type following command to navigate to your python pip directory:
“cd c:/python27/scripts/”
now type the following to install pillow:
pip install pillow
this will install the latest version of pillow in your python library

Lets Start Coding

So now we rare ready to code the trainner,
Just like all the other opencv script we need:

import the opencv / cv2 library,
we will need the os to accress the file list in out dataset folder,
we also need to import the numpy library,
and we need to import the pillow / PIL library we installed before,

It will look something like this:

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

Now we need to initialize the recognizer and the face detector

recognizer = cv2.createLBPHFaceRecognizer()
detector= cv2.CascadeClassifier("haarcascade_frontalface_default.xml");

Load The Training Data

Ok, now we will are going to create a function which will grab the training images from the dataset folder, and will also get the corresponding Ids from its file name, (remember we formatted the filename to be like User.id.samplenumber in our previous script)

So I am going to name this function “getImagesAndLabels”  we need the path of the dataset folder so we will provide the folder path as argument. So the function will be like this

def getImagesAndLabels(path):

So now inside this function we are going to do the following

  • Load the training images from dataset folder
  • capture the faces and Id from the training images
  • Put them In a List of Ids and FaceSamples  and return it

To load the image we need to create the paths of the image

    imagePaths=[os.path.join(path,f) for f in os.listdir(path)] 

this will get the path of each images in the folder.
now we need to create two lists for faces and Ids to store the faces and Ids

    faceSamples=[]
    Ids=[]

Now we will loop the images using the image path and will load those images and Ids, we will add that in your lists

    for imagePath in imagePaths:
        pilImage=Image.open(imagePath).convert('L')
        imageNp=np.array(pilImage,'uint8')
        Id=int(os.path.split(imagePath)[-1].split(".")[1])
        faces=detector.detectMultiScale(imageNp)
        for (x,y,w,h) in faces:
            faceSamples.append(imageNp[y:y+h,x:x+w])
            Ids.append(Id)

In the above code we used used “Image.open(imagePath).convert(‘L’)” is loading the image and converting it to gray scale, but now its a PIL image we need to convert it to numpy array.
for that we are converting it to numpy array “imageNP=np.array(pilImage,’uint8′)”.
To get the Id we split the image path and took the first from the last part (which is “-1” in python) and that is the name of the imagefile. now here is the trick, we saved the file name in our previous program like this “User.Id.SampleNumber” so if we split this using “.” the we will get 3 token in a list “User”, “Id”, “SampleNumber”
so to get the Id we will choone 1st index (index starts from 0)

So we get:

        Id=int(os.path.split(imagePath)[-1].split(".")[1])

Now we are using the detector to extract the faces and append them in the faceSamples list with the Id

which looks like:

        for (x,y,w,h) in faces:
            faceSamples.append(imageNp[y:y+h,x:x+w])
            Ids.append(Id)

So we are done now we just have to return that value

return faceSamples,Ids

Now the entire function will look like this

def getImagesAndLabels(path):
    #get the path of all the files in the folder
    imagePaths=[os.path.join(path,f) for f in os.listdir(path)] 
    #create empth face list
    faceSamples=[]
    #create empty ID list
    Ids=[]
    #now looping through all the image paths and loading the Ids and the images
    for imagePath in imagePaths:
        #loading the image and converting it to gray scale
        pilImage=Image.open(imagePath).convert('L')
        #Now we are converting the PIL image into numpy array
        imageNp=np.array(pilImage,'uint8')
        #getting the Id from the image
        Id=int(os.path.split(imagePath)[-1].split(".")[1])
        # extract the face from the training image sample
        faces=detector.detectMultiScale(imageNp)
        #If a face is there then append that in the list as well as Id of it
        for (x,y,w,h) in faces:
            faceSamples.append(imageNp[y:y+h,x:x+w])
            Ids.append(Id)
    return faceSamples,Ids

Almost Done!!

We are almost finished, now we just have to call that function and feed the data to the recognizer to train

faces,Ids = getImagesAndLabels('dataSet')
recognizer.train(faces, np.array(Ids))
recognizer.save('trainner/trainner.yml')

Thats it!!

Now if we run this code it will create a “trainner.yml” file inside the trainner folder,
We will use this file in our next post to actually recognize the faces that we trained the face recognizer to recognize,

The Complete Code

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

recognizer = cv2.createLBPHFaceRecognizer()
detector= cv2.CascadeClassifier("haarcascade_frontalface_default.xml");

def getImagesAndLabels(path):
    #get the path of all the files in the folder
    imagePaths=[os.path.join(path,f) for f in os.listdir(path)] 
    #create empth face list
    faceSamples=[]
    #create empty ID list
    Ids=[]
    #now looping through all the image paths and loading the Ids and the images
    for imagePath in imagePaths:
        #loading the image and converting it to gray scale
        pilImage=Image.open(imagePath).convert('L')
        #Now we are converting the PIL image into numpy array
        imageNp=np.array(pilImage,'uint8')
        #getting the Id from the image
        Id=int(os.path.split(imagePath)[-1].split(".")[1])
        # extract the face from the training image sample
        faces=detector.detectMultiScale(imageNp)
        #If a face is there then append that in the list as well as Id of it
        for (x,y,w,h) in faces:
            faceSamples.append(imageNp[y:y+h,x:x+w])
            Ids.append(Id)
    return faceSamples,Ids

faces,Ids = getImagesAndLabels('dataSet')
recognizer.train(faces, np.array(Ids))
recognizer.save('trainner/trainner.yml')

 

Now The Complete Video Tutorial

 

Updates:
Github links:

For this code: you can visit: https://github.com/thecodacus/Face-Recognition

nazmi69 has done a good job converting the code for python 3.x and opencv 3.0
available at https://github.com/nazmi69/Face-Recognition

  • Alberto

    Hi, I’m new in the openCV programming. I’ve openCV 3.2 (+ openCV contrib) installed on my PC with python 2.7 and when I compile the code appears this error:
    recognizer = cv2.createEigenFaceRecognizer()
    AttributeError: ‘module’ object has no attribute ‘face’

    I’ve searched in forums and they suggest to put ‘face.’ before ‘createEigenFaceRecognizer()’ or to substitute ‘cv2.createEigenFaceRecognizer()’ with ‘cv2.createFisherFaceRecognizer()’ etc… I haven’t solved my problem… What can I do?
    Thanks in advance and sorry for my bad English.

    • Anirban

      Why dont you try opencv2, you wont find and any issue with that
      here is the step by step tutorial how to setup opencv2
      http://thecodacus.com/opencv-python-face-detection/

    • Anish jain

      The reason this no longer works is because the face recognition module has been moved out of the core OpenCV library from version 3. It now exists in a separate sub-library called ‘opencv_contrib’ which is less well supported and harder to get up and running. The joys of open source.

      The Github page for the contributions module is here:
      https://github.com/Itseez/o

      Theoretically if you can get that installed and built you should be able to access the face recognition module by changing ‘cv2.createLBPHFaceRecognizer’ to cv2.face.createLBPHFaceRecognizer’ but its possible the python bindings might not be up to date.

  • Jose Morales

    When the next? Thanks for the tuto

    • Anirban

      it will be updated soon, you can subscribe to this blog, you will get notified when ever there is a new post

  • Mehdi

    Hello Anirban;
    When i use
    recognizer = cv2.createEigenFaceRecognizer()
    an error occur that say “In Eigenfaces method all input samples (training images) must be of equal size! “.All images must be in same height and width (in pixel).
    How can i create a dataset with this condition?
    Thanks in advance.

    • Anirban

      you can use
      face=cv2.resize(imageNp[y:y+h,x:x+w],(100,100))
      faceSamples.append(face)

      now all the samples will be same size always

  • patel

    Getting following error,

    File “two_train.py”, line 35, in
    faces,Ids = getImagesAndLabels(‘dataSet’)
    File “two_train.py”, line 19, in getImagesAndLabels
    pilImage=Image.open(imagePath).convert(‘L’)
    File “/home/pi/.virtualenvs/cv/lib/python3.4/site-packages/PIL/Image.py”, line 2349, in open
    % (filename if filename else fp))
    OSError: cannot identify image file ‘dataSet/.DS_Store’

    P.S: it takes 50% of data set and then it shows this error.

    • Anirban

      Actually there is a hidden file “dataSet/.DS_Store” -> “.DS_Store” and the script is taking that as input but not able to convert it as image, try to remove that file from the folder and it will work

      • gaurav

        where does that hidden file reside?

        • hey gaurav sorry for not replying sooner,
          try this split the image path in by “.” so the 2nd part will be jpeg after “.” if its not then its not an image

          token=imagePath.split(“.”)
          if(token[-1]==’jpg’):
          # put the rest of the code inside

          • Xiao

            Hello Anirban, I am having a similar issue, I am trying to filter out the hidden .DS_Store file, but I don’t quite understand your solution

            Where should this occur?
            token=imagePath.split(“.”)
            if(token[1]==’jpeg’):
            # put the rest of the code inside

            As a component of this?
            Id= int(os.path.split(imagePath)[-1].split(“.”)[1])

            Thank you so much! Your tutorial has been great so far

          • I will modify the code in the blog itself so that every one can see this

  • shyamsundar PL

    HI, I am Shyam I using ubuntu 16.04 how could I have to do and save trainer/trainer .yml file can you help me for that. And you had changed the code in a video but you not mentioned in website.Did you face any problem in it?

    anticipating for yor answer ,,,,

    • Anirban

      Hi Shyam, If you have saved your python script in the same folder as the “trainner” folder like in the image that is shown in the post then.

      recognizer.save(‘trainner/trainner.yml’)

      this above line will save the yml file inside the “trainner” folder. even if you are using ubuntu its not a problem and if your script is in a different location then

      recognizer.save(‘[Fullpath]/trainner/trainner.yml’)

      you have to enter the full path of the “trainner” folder then it will work

      and about the difference in the blog and the video, its actually i didn’t wrote the both code same time so there is some difference in code but both logic is same you can follow any of them both will work. while writing the blog i broken down the code so that it becomes easier to explain in the blog that’s it

  • shyamsundar PL

    Hey That I am have been another doubt.This tutorial is for save yml file or we can check the face of our ID. It is confusing me can you explain to me.

    This is only for face checking or Id checking

    • Anirban

      its just for training the recognizer with the dataset that we created in the previous post and save the recognizer in a file, in the next post we are using that file to recognize the faces

  • gaurav

    Getting this error how to resolve it please help

    Traceback (most recent call last):
    File “F:\New folder (6)\face reco\trainner.py”, line 21, in
    Ids,faces=getImagesWithID(path)
    File “F:\New folder (6)\face reco\trainner.py”, line 12, in getImagesWithID
    faceImg=Image.open(imagePath).convert(‘L’);
    File “C:\Python27\lib\site-packages\PIL\Image.py”, line 2349, in open
    % (filename if filename else fp))
    IOError: cannot identify image file ‘dataSet\\Thumbs.db’

    • Arshad Ali

      I’m having the same error.. somebody please fix this !

    • Arshad Ali

      This is fixed after doing this :

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

      recognizer = cv2.createLBPHFaceRecognizer()
      detector= cv2.CascadeClassifier(“haarcascade_frontalface_default.xml”);

      def getImagesAndLabels(path):
      #get the path of all the files in the folder
      imagePaths=[os.path.join(path,f) for f in os.listdir(path)]
      #create empth face list
      faceSamples=[]
      #create empty ID list
      Ids=[]
      #now looping through all the image paths and loading the Ids and the images
      for imagePath in imagePaths:
      token = imagePath.split(“.”)
      if(token[1] ==’jpeg’):
      #loading the image and converting it to gray scale
      pilImage=Image.open(imagePath).convert(‘L’)
      #Now we are converting the PIL image into numpy array
      imageNp=np.array(pilImage,’uint8′)
      #getting the Id from the image
      Id=int(os.path.split(imagePath)[-1].split(“.”)[1])
      # extract the face from the training image sample
      faces=detector.detectMultiScale(imageNp)
      #If a face is there then append that in the list as well as Id of it
      for (x,y,w,h) in faces:
      faceSamples.append(imageNp[y:y+h,x:x+w])
      Ids.append(Id)
      return faceSamples,Ids

      faces,Ids = getImagesAndLabels(‘dataSet’)
      recognizer.train(faces, np.array(Ids))
      recognizer.save(‘trainner/trainner.yml’)

      But having a new error :

      Traceback (most recent call last):
      File “C:\Users\User\Desktop\python opencv\Face Recog\trainner.py”, line 34, in
      recognizer.train(faces, np.array(Ids))
      error: ..\..\..\..\opencv\modules\contrib\src\facerec.cpp:917: error: (-210) Empty training data was given. You’ll need more than one sample to learn a model. in function cv::LBPH::train

    • lucky

      same error

      • sorry did a mistake there,
        in the dataset generator we used the extension as “jpg”
        so if we change the line “if(token[1] ==’jpeg’):” to “if(token[-1] ==’jpg’):” it will work..

  • Rithesh

    Getting this error. unable to solve. ( I followed your code from youTube)
    Traceback (most recent call last):
    File “K:/python project/face recognition/trainer.py”, line 25, in
    recognizer.train(faces,Ids)
    TypeError: labels data type = 18 is not supported.

    when i pasted the code from this blog it works fine.

    • Anirban

      So it’s working then… maybe you mistype something previously

  • Mike

    Hello,
    How could I apply the same detection and recognition techniques but with a browse button instead of capturing it through the cam?

    • Anirban

      use cv2.VideoCapture(‘videofilepath’) instead of cv2.VideoCapture(0)… works well for avi

  • Taru

    hey Anirban,
    Thanks for the tutorial. But i am getting this error.
    Traceback (most recent call last):
    File “C:/Python27/prog/facerecog/trainner.py”, line 33, in
    recognizer.train(faces, np.array(Ids))
    TypeError: labels data type = 17 is not supported

    • Anirban

      change the variable name “id” if you are using it anywhere.. it causes issue in some cases

  • kundan kumar

    i am facing ….”””AttributeError: ‘module’ object has no attribute ‘createLBPHFaceRecognizer “”‘
    error …. what should i do….

    • Anirban

      this tutorials are based on opencv 2.. so opencv3 wont work with these.. try with opencv 2

    • Apurva Agrawal

      use cv2.face.createEigenFaceRecognizer()

    • Anish jain

      The reason this no longer works is because the face recognition module has been moved out of the core OpenCV library from version 3. It now exists in a separate sub-library called ‘opencv_contrib’ which is less well supported and harder to get up and running. The joys of open source.

      The Github page for the contributions module is here:
      https://github.com/Itseez/o

      Theoretically if you can get that installed and built you should be able to access the face recognition module by changing ‘cv2.createLBPHFaceRecognizer’ to cv2.face.createLBPHFaceRecognizer’ but its possible the python bindings might not be up to date.

  • Musab

    hello sir

    am developing your code on Anaconda navigator (Spyder) and python 3

    all the previous codes works fine but only in this code I get this error

    recognizer= cv2.createLBPHFaceRecognizer()

    AttributeError: module ‘cv2’ has no attribute ‘createLBPHFaceRecognizer’

    • Anirban

      did you check your opencv version ?? is it 2/3? cuz in opencv 3 you dont have “createLBPHFaceRecognizer” out of the box

      • younid

        So, if we are download open cv3. how can we solve this issue with this version.

        • you have to compile the contrib library and face module. then you can use “cv2.face.createLBPHFaceRecognizer()”

          • joseph

            hello sir I am using opencv 3.1.0 on raspberry pi and I am faced with the error

            recognizer= cv2.createLBPHFaceRecognizer()
            AttributeError: module ‘cv2’ has no attribute
            I have downloaded the contrib library how do I compile it I currently have the library in my download do have to move it to my opencv3.1.0 folder

          • Anish jain

            And how to do it ?

      • hello. I’m having this error.
        AttributeError: ‘module’ object has no attribute ‘createLBPHFaceRecognizer’

        • you must be using opencv3

    • Anish jain

      The reason this no longer works is because the face recognition module has been moved out of the core OpenCV library from version 3. It now exists in a separate sub-library called ‘opencv_contrib’ which is less well supported and harder to get up and running. The joys of open source.

      The Github page for the contributions module is here:
      https://github.com/Itseez/o

      Theoretically if you can get that installed and built you should be able to access the face recognition module by changing ‘cv2.createLBPHFaceRecognizer’ to cv2.face.createLBPHFaceRecognizer’ but its possible the python bindings might not be up to date.

  • Divide

    hi Sir
    I copy your code and run it but it error
    Traceback (most recent call last):

    File “trainner.py”, line 31, in
    faces,Ids = getImagesAndLabels(‘dataSet’)
    File “trainner.py”, line 22, in getImagesAndLabels
    Id=int(os.path.split(imagePath)[-1].split(“.”)[1])
    ValueError: invalid literal for int() with base 10: ‘cam’
    Can you help me ? Thanks

    • what are the image file name in your dataset folder?

      • Divide

        I save it in dataSet folder : ex: home/Desktop/code/dataSet/User.cam.18.jpg

        • Your File Name is wrong… its suppose to be the user id in place of “cam” in the file name “User.cam.18.jpg”

      • Divide
      • Divide

        sorry to bother. I was stupid so the ID was a string

        • Sorry for late reply, yes, your ID should be a integer

          • Jyoti

            How to solve the above problem ?

  • pradeep

    cv2 has no attribute createlbphfacerecognizer() how i slove this

    • use opencv2 it wont happen

    • Anish jain

      The reason this no longer works is because the face recognition module has been moved out of the core OpenCV library from version 3. It now exists in a separate sub-library called ‘opencv_contrib’ which is less well supported and harder to get up and running. The joys of open source.

      The Github page for the contributions module is here:
      https://github.com/Itseez/o

      Theoretically if you can get that installed and built you should be able to access the face recognition module by changing ‘cv2.createLBPHFaceRecognizer’ to cv2.face.createLBPHFaceRecognizer’ but its possible the python bindings might not be up to date.

  • gaurav

    Getting this error how to resolve it please help

    Traceback (most recent call last):
    File “F:\New folder (6)\face reco\trainner.py”, line 21, in
    Ids,faces=getImagesWithID(path)
    File “F:\New folder (6)\face reco\trainner.py”, line 12, in getImagesWithID
    faceImg=Image.open(imagePath).convert(‘L’);
    File “C:\Python27\lib\site-packages\PIL\Image.py”, line 2349, in open
    % (filename if filename else fp))
    IOError: cannot identify image file ‘dataSet\\Thumbs.db’

    • hey gaurav sorry for not replying sooner,
      try this split the image path in by “.” so the 2nd part will be jpeg after “.” if its not then its not an image

      token=imagePath.split(“.”)
      if(token[-1]==’jpg’):
      # put the rest of the code inside

  • Shyam Saravanan

    Hi there Anirban,
    When I tried running the code you have given here, I get the error
    Traceback (most recent call last):
    File “”, line 1, in
    exec file(“D:/Pjjjjjj/pythonsamp/trainner.py”)
    File “D:/Pjjjjjj/pythonsamp/trainner.py”, line 33, in
    recognizer.save(‘trainner/trainner.yml’)
    error: ..\..\..\..\opencv\modules\contrib\src\facerec.cpp:390: error: (-2) File can’t be opened for writing! in function cv::FaceRecognizer::save

    What should I do?

    • check that you have write permission in that folder

      • Shyam Saravanan

        Yes, it does

        • Shyam Saravanan

          I deleted the folder and created it again. Now it works.
          Thank you.

  • Arif

    #Here is my code…..

    import os
    import cv2
    import numpy as np
    cv2.imread(“filepath”)

    recognizer=cv2.createLBPHFaceRecognizer()
    path=’dataSet’

    def getImagesWithID(path):
    imagePaths=[os.path.join(path,f) for f in os.listdir(path)]
    faces=[]
    IDs=[]
    for imagePath in imagePaths:
    faceNp=cv2.imread(imagepath,0)
    ID=int(os.path.split(imagePath)[-1].split(“.”)[1])
    faces.append(faceNp)
    print ID
    IDs.append(ID)
    cv2.imshow(“training”,faceNp)
    cv2.waitKey(10)
    return IDs,faces

    IDs,faces = getImagesWithID(path)
    recognizer.train(faces, np.array(Ids))
    recognizer.save(‘recognizer/trainningData.yml’)
    cv2.destroyAllWindow()

    #And this error occurs…..

    Traceback (most recent call last):
    File “E:/Project & thesis/Face Recognition/trainner.py”, line 23, in
    IDs,faces = getImagesWithID(path)
    File “E:/Project & thesis/Face Recognition/trainner.py”, line 14, in getImagesWithID
    faceNp=cv2.imread(imagepath,0)
    NameError: global name ‘imagepath’ is not defined

    # What should I do? Please help.

    • you variable name is imagePath not imagepath

      • Arif

        Thanks a lot. Now it works fine.
        Now I am facing a problem in Sqlite Database.

        Here is my code…..
        import cv2
        import sqlite3
        cam = cv2.VideoCapture(0)
        detector=cv2.CascadeClassifier(‘Classifiers/face.xml’)

        def insertOrUpdate(Id,Name):
        conn=sqlite3.connect(“FaceBase.db”)
        cmd=”SELECT * FROM People WHERE ID=”+str(Id)
        cursor=conn.execute(cmd)
        isRecordExist=0
        for row in cursor:
        isRecordExist=1
        if(isRecordExist==1):
        cmd=”UPDATE People SET Name=”+str(Name)+” WHERE ID=”+str(Id)
        else:
        cmd=”INSERT INTO People(ID,Name) Values(“+str(Id)+”,”+str(Name)+”)”
        conn.execute(cmd)
        conn.commit()
        conn.close()

        id=raw_input(‘enter your id’)
        name=raw_input(‘enter your name’)
        insertOrUpdate(id,name)
        sampleNum=0
        while(True):
        ret, img =cam.read()
        gray=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        faces=detector.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5, minSize=(100, 100), flags=cv2.CASCADE_SCALE_IMAGE)
        for (x,y,w,h) in faces:
        sampleNum=sampleNum+1
        cv2.imwrite(“dataSet/user.”+id +’.’+ str(sampleNum) + “.jpg”, gray[y:y+h,x:x+w])
        cv2.rectangle(img,(x-50,y-50),(x+w+50,y+h+50),(225,0,0),2)
        cv2.imshow(‘img’,img)
        cv2.waitKey(100)
        if sampleNum>20:
        cam.release()
        cv2.destroyAllWindows()
        break

        # My problem is this code runs well but doesn’t detect my face and doesn’t create the dataSet of my face. Now what should I do?

        • AMAL SAAD

          I m facing this problem in ur code can u plz help

          TypeError: labels data type = 18 is not supported

  • Arshad Ali

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

    recognizer = cv2.createLBPHFaceRecognizer()
    detector = cv2.CascadeClassifier(“haarcascade_frontalface_default.xml”);

    def getImagesAndLabels(path):
    #get the path of all the files in the folder
    imagePaths=[os.path.join(path,f) for f in os.listdir(path)]
    #create empth face list
    faceSamples=[]
    #create empty ID list
    Ids=[]
    #now looping through all the image paths and loading the Ids and the images
    for imagePath in imagePaths:
    token = imagePath.split(“.”)
    if(token[1] ==’jpeg’):
    #loading the image and converting it to gray scale
    pilImage=Image.open(imagePath).convert(‘L’)
    #Now we are converting the PIL image into numpy array
    imageNp=np.array(pilImage,’uint8′)
    #getting the Id from the image
    Id=int(os.path.split(imagePath)[-1].split(“.”)[1])
    # extract the face from the training image sample
    faces=detector.detectMultiScale(imageNp)
    #If a face is there then append that in the list as well as Id of it
    for (x,y,w,h) in faces:
    faceSamples.append(imageNp[y:y+h,x:x+w])
    Ids.append(Id)
    cv2.imshow(“Adding faces to trainning set. . . “,imageNp[y:y+h,x:x+w])
    cv2.waitKey(50)
    return faceSamples,Ids

    faces,Ids = getImagesAndLabels(‘dataSet’)
    recognizer.train(faces, np.array(Ids))
    recognizer.save(‘trainner/trainner.yml’)

    ERROR :
    Traceback (most recent call last):
    File “C:\Users\User\Desktop\python opencv\Face Recog\trainner.py”, line 37, in
    recognizer.train(faces, np.array(Ids))
    error: ..\..\..\..\opencv\modules\contrib\src\facerec.cpp:917: error: (-210) Empty training data was given. You’ll need more than one sample to learn a model. in function cv::LBPH::train

    Please fix the error….

    • most probably your dataset folder is empty

  • Juliet

    How to solve this error
    recognizer=cv2.createLBPHFaceRecognizer()
    AttributeError: ‘module’ object has no attribute ‘createLBPHFaceRecognizer’

    • To follow along with this tutorial you need opencv 2.4.. you must be using opencv3

      • Anish jain

        Is there a way by which we can use the same code in OpenCV 3.2.0 ?

      • how to apply FaceRecognizer using openCV3?

        • you need to add the face module

  • Damilola

    I am getting this error:
    Image.register_decoder(‘MSP’, MspDecoder)

    AttributeError: ‘module’ object has no attribute ‘register_decoder’

    • Damilola

      I was able to fix the first problem by installing PIL; now I get this error:
      the first image.

      IOError: cannot identify image file ‘dataSet/.DS_Store’

      • open our terminal.. change your directory to the dataset folder and run this “rm .DS_Store”

    • I cant relate it with this post.. what are you trying to to here exactly?

  • Rodrigo

    Hi, first of all thank you a lot, this is helping me for my school proyect so much.
    I have a problem with the Face module of opencv I can not find how to install it, I saw some videos and they use cmake and visual studio, but right now Im using Liclipse. Also I tried to install opencv 2.3 and 2.4 but none of them had the module. Could you help me please?

  • zuhair

    Traceback (most recent call last):
    File “E:/face recgn/trainner/trainner.py”, line 3, in
    from PIL import Image
    ImportError: No module named PIL

  • muhammad zaheer

    hello
    bro your tutorial wthas awesome . i have one question.is cv2.createLBPHFaceRecognizer() a built-in function. which method u used for recognition. i mean for features extraction. thanks

  • Logesh

    C:\Python27\Scripts>pip install Pillow
    Collecting Pillow
    Using cached Pillow-4.1.1-cp27-cp27m-win32.whl
    Collecting olefile (from Pillow)
    Using cached olefile-0.44.zip
    Complete output from command python setup.py egg_info:

    —————————————-
    Command “python setup.py egg_info” failed with error code 1 in c:\users\logesh\appdata\local\temp\pip-build-onu8pb\olefile

  • Anish jain

    I’m getting the following error when I run Trainer.py —

    AttributeError: module ‘cv2’ has no attribute ‘createLBPHFaceRecognizer’

    Please help me with how to resolve this error without changing the OpenCV version.
    I’m using OpenCV-3.2.0 and Python 2.7 on a RaspberryPi 3

  • Anish jain

    Hi,
    When I run trainer.py , I get the following error —

    OpenCV Error: Unspecified error (File can’t be opened for writing!) in save, file /home/pi/opencv_contrib-3.2.0/modules/face/src/facerec.cpp, line 70
    Traceback (most recent call last):
    File “trainer.py”, line 33, in
    recognizer.save(‘trainer/trainer.yml’)
    cv2.error: /home/pi/opencv_contrib-3.2.0/modules/face/src/facerec.cpp:70: error: (-2) File can’t be opened for writing! in function save

    PS: I’m using Opencv-3.2.0 , Python – 2.7 and RaspberryPi – 3.

  • chan

    HI Anirban , You did fantastic job !! Congratulations. I have one question – I tried this out – even when unknown faces are presented to webcam it says the same name for which Id is defined and trained.. It does not say Unkown.. any clue – what I can modify to make it work ?

    • I mentioned in my post on how to identify unknown face, check that out

  • wander mi

    hola me puedes ayudar con este problema por favor:

    OpenCV Error: Bad argument (The number of samples (src) must equal the number of labels (labels). Was len(samples)=20, len(labels)=0.) in cv::LBPH::train, file ..\..\..\..\opencv\modules\contrib\src\facerec.cpp, line 791
    Traceback (most recent call last):
    File “trainner.py”, line 30, in
    recognizer.train(faces,Ids )
    cv2.error: ..\..\..\..\opencv\modules\contrib\src\facerec.cpp:791: error: (-5) The number of samples (src) must equal the number of labels (labels). Was len(samples)=20, len(labels)=0. in function cv::LBPH::train

    este es el codigo:

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

    recognizer = cv2.createLBPHFaceRecognizer()
    detector= cv2.CascadeClassifier(“haarcascade_frontalface_default.xml”);

    path=’E:\Vision Artificial\dataSet’
    def getImagesAndLabels(path):
    imagePaths=[os.path.join(path,f) for f in os.listdir(path)]
    facesSamples=[]
    Ids=[]
    for imagePath in imagePaths:
    Image_pil=Image.open(imagePath).convert(‘L’)
    faceNp=np.array(Image_pil, ‘uint8’)

    Id=int(os.path.split(imagePath)[-1].split(“.”)[1])
    facesSamples=detector.detectMultiScale(faceNp)
    Ids.append(Id)
    cv2.imshow(‘entrenamiento’, faceNp)
    cv2.waitKey(10)
    return np.array(Ids), facesSamples

    faces, Ids = getImagesAndLabels(path)
    print(Ids)

    recognizer.train(faces,Ids )
    recognizer.save(‘recognizer/trainningData.yml’)

    cv2.destroyAllWindows()

    gracias

    • In the “getImagesAndLabels” method
      you are returning “return np.array(Ids), facesSamples”

      but you switched the variables in this line
      “faces, Ids = getImagesAndLabels(path)”

      the order should be same
      Ids, faces = getImagesAndLabels(path)

  • Aladdin Hammodi

    hello Anriban
    in the training folder i have only pictures of one person, and i dont want to put any other pictures because i want the recognizer to return true or false when comparing with any other picture. i just want him to return True if the recognizer successfully recognize me other wise False.
    how could i do this
    Thx a lot
    Aladdin

    • in the next tutorial I used a section where I put a condition “if(conf<50):" which is a threshold to determine how much confident the recognizer is about its prediction… thats your answer