Face Recognition Using OpenCV | Loading Recognizer

In my previous post we learnt to train a recognizer using a dataset, in this post we are loading recognizer to see how we can use that recognizer to recognize faces.

If you are following my previous posts then you already have the trained recognizer with you inside a folder named “trainner” and “trainner.yml” file inside it. Now we are going to use that training data to recognize some faces we previously trained .

Lets Start By Importing The Libraries

import cv2
import numpy as np

Yes that’s it, thats all we need for this projects.

Now Loading Recognizer

next we create a recognizer object using opencv library and load the training data (before that just sve your script in the same location where your “trainner” folder is located)

recognizer = cv2.createLBPHFaceRecognizer()
recognizer.load('trainner/trainner.yml')

Now we will create a cascade classifier using haar cascade for face detection, assuming u have the cascade file in the same location,

cascadePath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascadePath);

Now we will create the video capture object

cam = cv2.VideoCapture(0)

Next we need a “font” that’s because we are going to write the name of that person in the image so we need a font for the text

font = cv2.cv.InitFont(cv2.cv.CV_FONT_HERSHEY_SIMPLEX, 1, 1, 0, 1, 1) 

Okay so the first parameter is the font name, 2nd and 3rd is the horizontal and the vertical scale,4rth is shear (like italic), 5th is thickness of line, 6th is line type

So we have all setup

Lets Start the main Loop

Lets start the main loop and do the following basic steps

  • Starts capturing frames from the camera object
  • Convert it to Gray Scale
  • Detect and extract faces from the images
  • Use the recognizer to recognize the Id of the user
  • Put predicted Id/Name and Rectangle on detected face
while True:
    ret, im =cam.read()
    gray=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
    faces=faceCascade.detectMultiScale(gray,1.2,5)
    for(x,y,w,h) in faces:
        cv2.rectangle(im,(x-50,y-50),(x+w+50,y+h+50),(225,0,0),2)
        Id, conf = recognizer.predict(gray[y:y+h,x:x+w])
        cv2.cv.PutText(cv2.cv.fromarray(im),str(Id), (x,y+h),font, 255)
    cv2.imshow('im',im)
    if cv2.waitKey(10) & 0xFF==ord('q'):
        break

So its pretty similar to the face detection code the only difference is the followingย lines

        Id, conf = recognizer.predict(gray[y:y+h,x:x+w])
        cv2.cv.PutText(cv2.cv.fromarray(im),str(Id), (x,y+h),font, 255)

in the above two line the recognizer is predicting the user Id and confidence of the prediction respectively
in the next line we are writing the User ID in the screen below the face, which is (x, y+h) coordinate

 

Just Little Finishing Touch (For Unknown Faces)Twitter

 

Now with this we are pretty much done we can add some more finishing touch like its showing user Id instead of the name,
and it cant handle unknown faces,

So to add this additional features we can do the following,

        Id, conf = recognizer.predict(gray[y:y+h,x:x+w])
        if(conf<50):
            if(Id==1):
                Id="Anirban"
            elif(Id==2):
                Id="Obama"
        else:
            Id="Unknown"
        cv2.cv.PutText(cv2.cv.fromarray(im),str(Id), (x,y+h),font, 255)

 

Now Some Cleanup

cam.release()
cv2.destroyAllWindows()

Now that everything is done, we need to close the camera and the windows. and we are done!!!!

And this is the results

Loading recognizer which is previously trained, and using it in face recognition system, names are being displayed below there faces

 

The Complete Face Recognition Code In One PieceTwitter

Now as Promised

import cv2
import numpy as np

recognizer = cv2.createLBPHFaceRecognizer()
recognizer.load('trainner/trainner.yml')
cascadePath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascadePath);


cam = cv2.VideoCapture(0)
font = cv2.cv.InitFont(cv2.cv.CV_FONT_HERSHEY_SIMPLEX, 1, 1, 0, 1, 1)
while True:
    ret, im =cam.read()
    gray=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
    faces=faceCascade.detectMultiScale(gray, 1.2,5)
    for(x,y,w,h) in faces:
        cv2.rectangle(im,(x,y),(x+w,y+h),(225,0,0),2)
        Id, conf = recognizer.predict(gray[y:y+h,x:x+w])
        if(conf<50):
            if(Id==1):
                Id="Anirban"
            elif(Id==2):
                Id="Sam"
        else:
            Id="Unknown"
        cv2.cv.PutText(cv2.cv.fromarray(im),str(Id), (x,y+h),font, 255)
    cv2.imshow('im',im) 
    if cv2.waitKey(10) & 0xFF==ord('q'):
        break
cam.release()
cv2.destroyAllWindows()

Complete Video Tutorial

Feel Free to Subscribe my blog and my youtube channel

 

Updates:
Github links:

For this code: you can visit:ย https://github.com/thcodacus/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

  • Mark Summerfield

    Anirban – this is the best explanation and example code of how to get started with opencv face recognition that I have found. Your code works first time and your explanations of what it is doing are excellent – thank you very much. The only mistake I made (and this may be of use to others) is that I did not give the full path to haarcascade_frontalface_default.xml (your code assumes it is in the current folder – my files are with the opencv installed files) . Simply nothing happens if the path is wrong – and after a short worry I realised my mistake and everything worked. Brilliant.

    • Anirban

      Thanks for the feedback ๐Ÿ™‚

  • Jose Morales

    Excelent the next is here.. thanks

  • ajith

    I worked on the coding on seeing your fouth video but i got two errors on compiling in the datasetgenerator file. There is no face.xml file in the opencv so how to solve it.

    • Anirban

      follow from the first video.. cuz 4rth one have some variation in code cuz I reorganized my code after the previous code was deleted by mistake.. So if you follow from he 1st you wont have any problem.. and ignore the variations… (I renamed the same xml file and placed it is a new folder called cascade to make it clean)

      • ajith

        But i getting two errors while running the last coding in your fourth video , upto third video everything is alright.

  • Oleg p

    Hello Anirban

    How could I do the following:

    1) Browse photos from a folder
    2) detect faces from these photos and save it to another folder
    3) recognize faces from 2 different photos

    thanks for your help in advance

    • Anirban

      if you see the post about training recognizer, you will have clue on how to load images from files.. and if you go back to the dataset creation post you will see we already used some way to same a captures faces.. I think with these two methods you can do what you are wanting to do

  • Abhishek

    can you make a tutorial for java also?

    • Anirban

      sorry I never used opencv in java

    • Great arictle, thank you again for writing.

  • ajith

    I want to connect my wifi camera with my laptop. My main cam in laptop should not open instead of that my SJ5000x wifi cam to be run , which is to detect faces from longer distance. Do you have any coding to connect wifi cam with laptop or any ideas.

    • Musab

      cam = cv2.VideoCapture(0)

      try changing 0 to 1 or 2 and so on tell it work

    • Anirban

      Yes Mussab is almost correct, but before that you need to add a driver so that your ip webcam acts as usb device

      for that if you are using Windows and you have a video your coming from your wifi cam,
      example : “192.168.1.12:8080/video”

      then you can use a Ip webcm adapter to make that link a virtual webcam

      check this ip webcam adapter here
      http://ip-webcam.appspot.com/static/doc.html

      and then you can directly use that cam as
      cv2.VideoCapture(1)

  • Ruben V

    I’ve just got it working on OpenCV3.0 on my rPi. Had to tweak some opencv syntax to make up for the difference in 2.x3.0. I also had to use the PiCamera() to gather and record the images because the image/videocapturing through OpenCV didn’t work.

    Only problem I still have is that my own face only get’s to a ‘conf’ of 60-70, so my face remains unknown.

    • Anirban

      thrushold for conf can vary depending on how well you trained your recognizer.. just increase the number to 75 maybe

  • Ash

    hi great tutorial on face recognition. i am having trouble in recognition of my face. i had saved my face by running datasetcreator.py code and trainner.py code. but when i run the main program for recognition it always shows unknown as Id. i dont know how that is happening as i had set Id =1 for my face. i did it 3-4 times but it always shows unknown as Id. pls help !!!!….

    • Anirban

      you have that line for “conf<50" increase the threshold to 70 and try again. i hope that will work

      • Ash

        can u tell me that what does the code:

        conf<50

        Exactly meant for ??

        And thanks btw my issue has been solved. Thanks again. Great tutorial.

        • Anirban

          in conf variable, the recognizer will store the confidence level of its prediction, the lower the value to more stronger the prediction.
          So conf<50 here we are setting a border between known and unknown faces if the confidence level is very low (i.e. the value is very large) then we are considering it as unknown regardless of whatever the prediction is

          • Ash

            Okay thanks very much.

            Also I would like to know about object detection and recognition. Since for Face detection and recognition we have this haar cascade classifiers xml files right, so for object detection like simply table or an apple or a chair do we have any xml files for it???

          • Anirban

            Ash, I already made an object recognition tutorial in my youtube channel, I will write it in my blog soon,
            and yes you can use xmls that whould be best but you dont have xmls for those ready for you, and training your own xmls are too much time consuming,
            So we can use something called feature matching. and opencv has few feature matching algorithm built in, (i.e. Surf, Sift) we can use those

  • ram

    Thank you it works perfectly but if a person not in the database appears infront of camera, its showing the existing persons name while recognising. I want to predict them as others who are not in the database list. How to change the code sir?

    • Anirban

      if(conf<50): change the number to a lower one for example 40 and try again

      • ram

        In which code file that conf line is in trainer or detector dataset creator?

        • in detector, “rec.predict()” this returns conf

          • ram

            marvellous thanks

  • vinod kumar

    Hi Anirban , Hope you are doing Good!, i have followed your tutorial it was really nice and interesting,it was helped a lop in my development.

    with opencv mainly I am facing the issue with CPU utilization it is very high is there any way to reduce that?? .

    Now we are recognising the face ,please let me know the how to recognise number plates as well if possible !!!!

    Thanks in advance!!…..

    • Anirban

      your camera resolution is maybe very high, and the larger the image the more cpu intensive i becomes, resize the image after capturing it to lower that 640×480, that should do it, I am guessing

  • Shweta Agarwal

    I need the script for the database for face recognition in python. so, please this script.

  • Jose

    hello
    Traceback (most recent call last):
    File “recognizer.py”, line 10, in
    font = cv2.cv.InitFont(cv2.CV_FONT_HERSHEY_COMPLEX_SMALL, 5, 1, 0, 4)
    AttributeError: ‘module’ object has no attribute ‘cv’

    • what is your opencv version?

      • Jose

        opencv 3.1.0

    • tbanda

      in your Open CV installation folder “C:\CV Packages\opencv\build\python\2.7\x86\cv2.pyd”
      if you are using a 32Bit Python copy the cv2.pyd and paste it in “C:\Python27\Lib\site-packages\cv2.pyd”

      after doing so type:
      import cv2
      if there is no error cv2 will have imported properly

  • Shyam Saravanan

    Great tutorial Anirban.
    It’s awesome.
    Thanks a lot. ๐Ÿ˜€ ๐Ÿ˜€ ๐Ÿ˜€

  • sagar

    File “faceRecog.py”, line 19, in
    id,conf=rec.predict(gray[y:y+h , x:x+w]);
    TypeError: ‘int’ object is not iterable

    i am getting this error in 3 part detection my python version is 3 and opencv is 3.1.0
    please help me solve

    • use this instead
      Id=rec.predict(gray[y:y+h , x:x+w]);

      • sagar

        Thanks bro its working now .
        By the way nice tutorials it was amazing time learning from ur videos

        • Nasha Goran

          How working for you
          Please help me i have some problem
          When i change error say me:
          TypeError: ‘rec’ object is not iterable

          Plz help me

      • Doan-hq

        Thank for share, but I have a question:

        if I use this “Id=rec.predict(gray[y:y+h , x:x+w]);”
        next I have to write if(Id<50) ?

        • if you are using opencv3 it doesn’t return conf variable it only returns ID do

          • Doan-hq

            So, how to increase accuracy of detector.
            there are many mistake when i detector face.
            Example: people A but detect to people B
            Please help me

  • MP

    Hi! Firstly thank you so much for this tutorial, it helped immensely! There were a lot of errors occurring,and after a lot of sweating finally everything is working. BUT. And I cry when I say this, the recognition part isn’t really doing well. ID keeps showing numbers which I didn’t even save. I trained for ID 1 and 2 only, and for others gave ID=”unknown”, but on running detector.py I get ID sometimes 19 sometimes 20. I tried to put conf<70 even 75. Nothing is working. Please suggest something?

    • your trainer is taking sample numbers as id instead of the id from the dataset images

      • MS

        Thanks! But my code is the same as given here…

      • MS

        How would you suggest I could deal with that? It would be a bummer if this didn’t work after all the sweating ๐Ÿ™ Thanks anyway!

        • what is the images naming format.. can you copy one of the name in the comment

          • MS

            Sure! They are saved as
            User1.1.jpg
            User1.2.jpg

            User3.1.jpg
            You are definitely right about the fact that it is taking the sample number instead of the id.

          • you missed a dot it should be “User.1.1.jpg”… thats the reason it was taking sample number instead of id.. it counts dots to get separate them

          • MS

            Oh dear! So silly of me! Thanks a ton ton! Will definitely subscribe to your blog and look forward to your future posts! ๐Ÿ™‚

          • Thanks ๐Ÿ™‚

  • doan-hq

    Thank you for share. But I have a question. Can you help me
    When other people in camera. It don’t show “Unknow” face, in some case it recognizer mistake.
    My code:
    Id =rec.predict(gray[y:y+h,x:x+w])
    if(Id<70):

    else:
    Id="Unknow"

    • chan

      same issue I see anirban.. pls advise.. ?

  • Arefin

    Thank you so much for sharing a great tutorial. Indeed, it’s a really great work. I have a question to you. Is it possible to print recognised face’s name or id below the square sign on the face.If possible, how ? Thanks again.

    • i am actually doing the same thing

  • gourav

    hey Anirban i am doing the same thing in raspberry pi i have trained i got the trainner.yml file but when i load it says it is read-only can u help me

    • can you paste the exact error

  • ankit pratap singh

    hey anirban,
    Your explanation is pretty good but I have stuck, that my code is not recognising the face in the dataSet. I followed all three videos.

    • can you please explain little more

  • Ash

    Hey Anirban, what if i want to hear the name of the person being detected every time? Then what changes i shouldโ€‹ do in the code? I know that for speech output in raspberry pi there are available software like espeak and festival but how to work with them in ur code please suggest any answer….

    • if you have a text to speech library for python.. then you can add a list in the beginning of the code and every time a new name comes up which is not in the list you add it to the list and use text2speech to call the name.. just an idea i didn’t test it

  • vishwam k

    hi anirban
    thanks for the great tutorial
    but I have a problem in detecting Multiple user id’s
    It shows 1st id to all the remaining other Ids also. the trainer is not differentiating form one Id to Other as expected.
    can you please help me solve this problem

    • check the images… the file name should show the ids.. are they all same?

      • vishwam k

        no gave different ids for different faces i have checked many times

  • Vasantha

    Hai Anirban, thank u for guiding us from this tutorials. I too done the same code and got output. I am trying to count the recognised face of one person. and want to check how many times his face is recognised by incrementing numbers and store that number in excel sheet. I have done seperate coding for writing into excel sheet, but inside this face recognition code this will not work. Can anyone help me to do the code..

  • Nice

  • Vasantha

    Hai Anirban, can i detect multiple face in this code by using different IDs in the same dataSet.

    • yes in fact i did that in the video

  • avaneesh kumar

    Can You help me with this error: though this error was aforementioned by someone else also, the solution is not clear.

    import cv2
    import numpy as np

    cascadePath=’/Users/avaneeshkumar/opencv/data/haarcascades/haarcascade_frontalface_default.xml’
    faceCascade=cv2.CascadeClassifier(cascadePath)
    recognizer = cv2.face.createLBPHFaceRecognizer()
    recognizer.load(‘trainner/trainner.yml’)
    id=0
    #cam = cv2.VideoCapture(0)
    font = cv2.cv.InitFont(cv2.cv.CV_FONT_HERSHEY_SIMPLEX, 1, 1, 0, 1, 1)
    while True:
    ret, im =cam.read()
    gray=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
    faces=faceCascade.detectMultiScale(gray, 1.2,5)
    for(x,y,w,h) in faces:
    cv2.rectangle(im,(x,y),(x+w,y+h),(225,0,0),2)
    id, conf = recognizer.predict(gray[y:y+h,x:x+w])
    cv2.PutText(cv2.cv.fromarray(im),str(Id), (x,y+h),font, 255)
    cv2.imshow(‘im’,im)
    if (cv2.waitKey(1)==ord(‘q’)):
    break
    cam.release()
    cv2.destroyAllWindows()

    —————————————————————————
    AttributeError Traceback (most recent call last)
    in ()
    8 id=0
    9 #cam = cv2.VideoCapture(0)
    —> 10 font = cv2.InitFont(cv2.cv.CV_FONT_HERSHEY_SIMPLEX, 1, 1, 0, 1, 1)
    11 while True:
    12 ret, im =cam.read()

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

    • avaneesh kumar

      I removed cv from the place due to some debugging,
      else

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

      is the error

      • avaneesh kumar

        Done :

        Other issue is

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

  • Anish Jain

    Hi, thanks for the great tutorial.
    Can you please provide the above python code for the detector.py file, for OpenCV version 3.2.0 ?
    I’m having syntax errors in the “font, puttext, and fromarray” parts.
    It would be of great help to me as well as many people.
    Hope to hear from you soon. ๐Ÿ™‚

  • longuyen

    hi anirban
    thanks for the great tutorial
    but I have a problem erro:
    gray=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
    cv2.error: ..\..\..\..\opencv\modules\imgproc\src\color.cpp:3737: error: (-215) scn == 3 || scn == 4 in function cv::cvtColor

    can you please help me solve this problem

  • 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 ?

  • Nasha Goran

    how to increase accuracy of detector.
    there are many mistake when i run detector.py
    For Example: people 1 but detect to people 2
    Please help me
    Regards

    • For this technique you have to use quality and variety of images of a person.. for more accuracy and the webcam also have to be good

  • kazi mosiur rahman

    Thanks for the immense support in explaining the process pretty well. I have been able to recognize one particular face recognized. But whenever there is more than one image to recognize the program stops and throws the error. Any particular solution to this. The error thrown is as follows

    OpenCV Error: Assertion failed (s >= 0) in cv::setSize, file ……modulescoresrcmatrix.cpp, line 306