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

 

You May also like

Comments

comments

63 thoughts on “Face Recognition Using OpenCV | Loading Recognizer”

  1. 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.

  2. 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.

    1. 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)

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

  3. 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

    1. 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

  4. 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.

    1. 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)

  5. 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.

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

  6. 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 !!!!….

      1. 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.

        1. 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

          1. 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???

          2. 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

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

  8. 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!!…..

    1. 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

  9. 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’

    1. 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

  10. 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

      1. 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) ?

          1. 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

  11. 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?

      1. 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!

          1. 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.

          2. 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

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

  12. 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"

  13. 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.

  14. 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

  15. 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.

  16. 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….

    1. 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

  17. 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

Leave a Reply

Your email address will not be published. Required fields are marked *