This tutorial conveniently makes use of opencv (cv2) library in Python.

You can learn how to get a Webcam input, make use of While Loops, implement Face Recognition, use pre-trained xml files and discover opencv library.

If you’d like to see a Screen Capture version of this face recognition Python tutorial instead of Webcam just check out this post here.

Used Where?

  • To get Webcam Input
  • Face Recognition
  • Interacting with games and software
  • Research
  • Security
  • Surveillance

Let’s import cv2 library:

import cv2

Here is a very straightforward implementation of face recognition using Python’s VideoCapture module from cv2 library.

Estimated Time

10 mins

Skill Level

Intermediate

Modules

VideoCapture, detectMultiScale, CascadeClassifier, cv2.rectangle, cv2.destroyAllWindows, cv2.imshow, cv2.waitKey

Libraries

cv2

Tutorial Provided by

HolyPython.com

Classifier xml files (already trained)

We need already trained xml files that will facilitate the algorithms for face recognition. You can simply assign them to a variable using cv2.CascadeClassifier.

You will need one xml file for each eye recognition and face recognition as below:

You can get the files here.
You can find bunch of different, high quality classifiers also on official Github page of opencv here. Also go ahead and support this awesome open source project if you can.

face_cascade = cv2.CascadeClassifier(r'c:\Users\tt\Desktop\haarcascade_frontalface_default.xml') 
eye_cascade = cv2.CascadeClassifier(r'c:\Users\tt\Desktop\haarcascade_eye.xml')  

cv2.VideoCapture for Webcam input

Now we can create a Video Capture input of your screen using VideoCapture module of cv2 library as following:

cap = cv2.VideoCapture(0)

Core of the code

At this point all we need is to create a while loop in order to use the face recognition 

while True:  
    ret, img = cap.read()  
    gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 
    faces = face_cascade.detectMultiScale(gray_img, 1.25, 4) 
  
    for (x,y,w,h) in faces: 
        cv2.rectangle(img,(x,y),(x+w,y+h),(255,255,0),2)  
        rec_gray = gray_img[y:y+h, x:x+w] 
        rec_color = img[y:y+h, x:x+w] 
  
        eyes = eye_cascade.detectMultiScale(rec_gray)  
  
        for (0x,0y,0w,0h) in eyes: 
            cv2.rectangle(rec_color,(0x,0y),(0x+0w,0y+0h),(0,127,255),2) 
  
    cv2.imshow('Face Recognition',img) 
  
    k = cv2.waitKey(30) & 0xff
    if k == 27: 
        break
  
cap.release() 
cv2.destroyAllWindows()
Webcam Face and Eye recognition (Demonstration only, image taken from John Krasinski's SGN show)

Code breakdown

Now let’s break down the code above and analyse some critical lines for learning:

  1. Infinite While loop reads your webcam continously.
  2. Using image from the webcam input, a gray version is created.
  3. face_cascade.detectMultiScale is used to facilitate object detection based on trained face recognition file.
    • This method take a few parameters that can be important. First one (gray here) is the gray version of our image input from the webcam.
    • Secondly, scaleFactor helps reduce image sizes from the input. This parameter can be important because trained data has a fixed size for each face and you might want to use reduction by a scale factor so that your algorithm runs faster. For instance: 1.04 will only reduce input image sizes by 4% but this will take more computing resources. On the other hand you can use something like 1.5 which will use a reduction factor of 50% and this will be way more efficient however, this may cost some of the faces to be not recognized in your input. This is definitely something to experiment with to find an optimal value.
    • minNeighbors is another important parameter. This signifies the minimum neighbor each rectangle needs to have for a face to be detected. Usually takes a value between 3-6
  4. Next is to draw the rectangles on each face.
  5. After that, another for loop is used to recognize the eyes for each face and then draw the boxes for the eyes similarly.
  6. Finally using imshow() method you can show the image with the boxes of face recognition and eye recognition.
  7. k variable is defined to help exit the Python webcam window by pressing “ESC” key which is mapped to number 27.
  8. At last Webcam input is released and  all cv2 windows are terminated to clean up the operation.

Conclusion

Voila! It’s not as intimidating as it looks. Especially in Python code.

Do you have any innovative ideas about what else can be done using this tech?

Don’t worry if you don’t have any. Just try to replicate the code by yourself and this process usually inspires people and helps get inspired for more ideas.

Also it’s good coding practice and pure fun!

Recommended Posts