Face Recognition in Python

face recognition in python

What is Face Recognition?

What is face recognition? Or what is recognition? When you look at a mango fruit, your mind immediately tells you that this is a mango fruit. This process, your mind telling you that this is a mango fruit is recognition in simple words. So, what is face recognition then?

When you look at your friend walking down the street or a picture of him, you recognize that he is your friend John. Interestingly when you look at your friend or a picture of him you look at his face first before looking at anything else. Ever wondered why you do that? This is so that you can recognize him by looking at his face. Well, this is you doing face recognition.

Here, the question is how does face recognition works? It is quite simple and intuitive. Take a real-life example, when you meet someone first time in your life you don’t recognize him, right? While he talks or shakes hands with you, you look at his face, eyes, nose, mouth, color, and overall look. This is your mind learning/training for the face recognition of that person by gathering face data. Then he tells you that his name is John. At this point, your mind knows that the face data it just learned belongs to John. Now your mind is trained and ready to do face recognition on John’s face. Next time when you will see John or his face in a picture you will immediately recognize him. This is how to face recognition work. The more you will meet John, the more data your mind will collect about Paulo and especially his face, and the better you will become at recognizing him.

The next question is how to code for face recognition with OpenCV. You might say that our mind can do these things easily but to actually code them into a computer is difficult? Don’t worry, it is not. The programming steps for face recognition are the same as we discussed in the real-life example above.

  • Training Data Gathering: Gather face data (face data from the images) of the persons you want to recognize
  • Training of Recognizer: Feed that face data to the face recognizer so that it can learn.
  • Recognition: Feed new faces of the persons and see if the face recognizer you just trained recognizes them or not.

OpenCV comes furnished with a worked-in face recognizer, you should simply take care of it the face information. The outcomes will be similar to this:Face Recognition in Python 

OpenCV Face Recognizers

OpenCV has three worked in face recognizers and gratitude to OpenCV’s spotless coding, you can utilize any of them by simply changing a solitary line of code. The following are the names of those face recognizers and their OpenCV calls.

  1. Eigen Faces Face Recognizer Recognize – cv2.face.createEigenFaceRecognizer()
  2. Fisher Faces Face Recognizer Recognizer – cv2.face.createFisherFaceRecognizer()
  3. Local Binary Patterns Histograms (LBPH) Face Recognizer – cv2.face.createLBPHFaceRecognizer()

Coding Face Recognition with OpenCV

The Face Recognition process is divided into three steps.

  1. Prepare training data: In this step, we will read training images for each person/subject along with their labels, detect faces from each image, and assign each detected face an integer label of the person it belongs to.
  2. Train Face Recognizer: In this step, we will train OpenCV’s LBPH face recognizer by feeding it the data we prepared in step 1.
  3. Testing: In this step, we will pass some test images to the face recognizer and see if it predicts them correctly.

Import Required Modules

Before beginning the real coding we have to import the necessary modules for coding. So how about we import them first.

  • CV2: is an OpenCV module for Python which we will use for face discovery and face acknowledgment.
  • OS: We will utilize this Python Training module to peruse our preparation catalogs and record names.
  • NumPy: We will utilize this module to change over Python records to NumPy clusters as OpenCV face recognizers acknowledge NumPy exhibits.

Face Recognition in Python Modules

Training Data

Images should be utilized in preparing as much as possible. Typically a ton of pictures are utilized for preparing a face recognizer so it can learn various looks of a similar individual, for instance with glasses, without glasses, giggling, pitiful, cheerful, crying, with facial hair, without whiskers, and so forth. To keep our instructional exercise basic we are going to utilize just 12 pictures for every individual.

So our preparation information comprises of all-out 2 people with 12 pictures of every individual. All preparation information is inside the preparing information envelope. preparing information organizer contains one envelope for every individual and every envelope is named with a group label (for example s1, s2) where name is really the number mark allotted to that individual. For instance, an envelope named s1 implies that this organizer contains pictures for individual 1. The index structure tree for preparing information is as per the following: Data Analytic with Python
Training Data - Face Recognition in PythonThe test-information organizer contains pictures that we will use to test our face recognizer after it has been effectively prepared. As OpenCV face recognizer acknowledges names as whole numbers so we have to characterize a mapping between number names and people real names so beneath I am characterizing a mapping of people number marks and their separate names.

Note: As we have not allotted name 0 to any individual so the mapping for mark 0 is vacant.Data - Face Recognition in Python

Prepare training data

You might be asking why information planning, isn’t that so? Indeed, OpenCV face recognizer acknowledges information in a particular arrangement. It acknowledges two vectors, one vector is of countenances of the considerable number of people and the subsequent vector is of whole number names for each face with the goal that when handling a face the face recognizer knows which individual that specific face has a place as well.

For instance, on the off chance that we had 2 people and 2 pictures for every individual.

Prepare training data - Face Recognition in Python

At that point, the get-ready information step will create the following face and mark vectors.Training Data - Face Recognition in Python

Getting ready information step can be additionally partitioned into the following sub-steps.

  1. Read all the organizer names of subjects/people gave in preparing the information envelope. So for instance, right now have organizer names: s1, s2.
  2. For each subject, extricate the name number. Our organizers have an extraordinary naming show. Envelope names follow the configuration label where Label is a number speaking to the mark we have doled out to that subject. In this way, for instance, envelope name s1 implies that the subject has name 1, s2 implies the subject mark is 2, etc. The name removed right now doled out to each face recognized in the subsequent stage.
  3. Read all the pictures of the subject, distinguish a face from each picture.
  4. Add each face to faces vector with comparing subject name (extricated in above advance) added to marks vector.

Prepare training data - Face Recognition in PythonI am utilizing OpenCV’s LBP face finder. On line 4, I convert the picture to grayscale in light of the fact that most activities in OpenCV are acted in dark scale, at that point on line 8 I load LBP face identifier utilizing cv2.CascadeClassifier class. After that online 12, I use cv2.CascadeClassifier class’ detectMultiScale technique to distinguish all the appearances in the picture. on line 20, from identified faces, I just pick the primary face on the grounds that in one picture there will be just one face (under the presumption that there will be just a single unmistakable face). As appearances returned by detectMultiScale technique are really square shapes (x, y, width, stature) and not genuine face pictures so we need to extricate the face picture region from the fundamental picture. So online 23 I separate face territory from the dark picture and return both the face picture region and face square shape.

Presently you have a face finder and you realize the 4 stages to set up the information, so would you say you are prepared to code the get ready information step? Indeed? So how about we do it.Prepare training data - Face Recognition in Python

I have characterized a capacity that takes the way, where preparing subjects’ organizers are put away, as parameters. This capacity follows a similar 4 plan information substeps referenced previously.

(step-1) On line 8 I am utilizing the os.listdir strategy to peruse names of all organizers put away on way went to work as a parameter. On lines 10-13 I am characterizing marks and faces vectors.

(step-2) After that, I navigate through the entirety of subjects’ organizer names, and from each subject’s envelope name on line 27, I am extricating the name data. As envelope names follow the label naming show so expelling the letter s from the organizer name will give us the name doled out to that subject.

(step-3) On line 34, I read all the pictures names of the present subject being navigated and on line 39-66 I cross those pictures individually. On lines 53-54 I am utilizing OpenCV’s show(window_title, picture) alongside OpenCV’s Waitley(interval) strategy to show the present picture being crossed. The Waitley(interval) technique delays the code stream for the given interim (milliseconds), I am utilizing it with 100ms interim so we can see the picture window for 100ms. On line 57, I recognize the face from the present picture being crossed.

(step-4) On lines 62-66, I include the distinguished face and mark to their particular vectors.

Be that as it may, a capacity can’t do anything except if we consider it on certain information that it needs to plan. Along these lines, I have information of two excellent and acclaimed big names. I am certain you will remember them!

We should call this capacity on pictures of these excellent superstars to get ready information for preparing our Face Recognizer. The following is a basic code to do that.0Prepare training data - Face Recognition in PythonThis was likely the exhausting part, isn’t that so? Try not to stress, the enjoyment stuff is coming up straightaway. It’s a great opportunity to prepare our own face recognizer so once prepared it can perceive new faces of the people it was prepared on. Peruse? Alright at that point how about we train our face recognizer.

Train Face Recognizer

As we probably are aware, OpenCV comes furnished with three face recognizers.

  1. EigenFace Recognizer: This can be made with cv2.face.createEigenFaceRecognizer()
  2. FisherFace Recognizer: This can be made with cv2.face.createFisherFaceRecognizer()
  3. Local Binary Patterns Histogram (LBPH): This can be made with cv2.face.LBPHFisherFaceRecognizer()

Prepare training data - Face Recognition in PythonI am going to utilize LBPH face recognizer yet you can utilize any face recognizer of your decision. Regardless of which of the OpenCV’s face recognizer you utilize the code will continue as before. You simply need to transform one line, the face recognizer introduction line given beneath.

Prepare training data - Face Recognition in Python

Since we have introduced our face recognizer and we additionally have arranged our preparation information, it’s a great opportunity to prepare the face recognizer. We will do that by calling the train(faces-vector, names vector) strategy for face recognizer. Did you notice that as opposed to passing marks vector legitimately to confront recognizer I am first changing over it to NumPy exhibit? This is on the grounds that OpenCV expects the names vector to be a NumPy cluster.

Prediction

Presently comes my preferred part, the expectation part. This is the place we really find a good pace our calculation is really perceiving our prepared subject’s appearances or not. We will step through two examination pictures of our big names, identify faces from every one of them, and afterward pass those appearances to our prepared face recognizer to check whether it remembers them.

The following are some utility capacities that we will use for drawing a jumping box (square shape) around the face and putting big-name close to the face bounding box.

Prepare training data - Face Recognition in Python

First capacity draw_rectangle draws a square shape on picture dependent on passed square shape arranges. It utilizes OpenCV’s worked in work cv2.rectangle(IMG, top left point, bottom right point, RGB color, lineWidth) to draw the square shape. We will utilize it to draw a square shape around the face identified in the test picture.

Second capacity draw_text utilizes OpenCV’s worked in work cv2.putText(img, content, startPoint, textual style, fontSize, rgbColor, lineWidth) to draw message on picture.

Since we have the drawing capacities, we simply need to consider the face recognizer’s predict(face) strategy to test our face recognizer on test pictures. Following capacity does the forecast for us.

  • line-6 read the test picture
  • line-7 recognize a face from test picture
  • line-11 perceive the face by considering face recognizer’s predict(face) strategy. This technique will restore a label
  • line-12 get the name related with the name
  • line-16 draw a square shape around the distinguished face
  • line-18 draw the name of the anticipated subject above the face square shape

Since we have the forecast work very much characterized, the subsequent stage is to really call this capacity on our test pictures and show those test pictures to check whether our face recognizer effectively remembered them. So how about we do it. This is the thing that we have been sitting tight for.

Prepare training data - Face Recognition in Python

Simply the images will be like this:

Face Recognition in Python

Our output will be like this:

Face Recognition in Python

End Notes

Face Recognition is a captivating plan to chip away at and OpenCV has made it very straightforward and simple for us to code it. It just takes a couple of lines of code to have a completely working face acknowledgment application and we can switch between each of the three face recognizers with a solitary line of code change. It’s that basic.

In spite of the fact that EigenFaces, FisherFaces, and LBPH face recognizers are acceptable however there are far better approaches to perform face acknowledgment like utilizing Histogram of Oriented Gradients (HOGs) and Neural Networks. So the further developed face acknowledgment calculations are presently days executed utilizing a blend of OpenCV and Machine learning.

About

Codec Networks provides IT Trainings from EC Council CEH ECSA, LPT, CHFI, Network Security, Penetration Testing, ISACA, ISC2, PECB ISO 27001LA LI, Cisco Networking CCNA CCNP, Linux Administration RHCE, Prog Languages JAVA, Advanced Java, android development. We also offer B2B Industry Solutions and Services in IT | Information|Cyber Security in Delhi NCR India.

View all posts by

Leave a Reply

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