Face recognition is becoming increasingly important as our identities are digitalized. We are moving away further from the passwords and PIN numbers. Today, biometric sensors are available on all security devices and smartphones.
Innovation such as biometric is widely used now in place of the PIN and password. However facial recognition has started to rule these and will offer another level of security and flexibility.
Let's learn how face recognition works!!
Face Recognition works in 4 steps:
- Detect face in image.
- Compute Facial features
- Compare facial feature against known faces.
- Make a prediction.
Thanks to OpenCV , it makes the face recognition easy for any beginner. OpenCV comes with three built in face recognizers algorithms.
- Eigen Faces : cv2.face.createEigenFaceRecognizer()
- FisherFaces: cv2.face.createFisherRecognizer()
- Local Binary Patterns Histograms: cv2.face.createLBPHFaceRecognizer()
Implementation in Python:
Let's go ahead and dive into some code.Training the classifier
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 , os | |
import numpy as np | |
#-------FUNCTIONS ------------------# | |
def detectFace(img): | |
faces = faceCascade.detectMultiScale(img, 1.3 , minSize = (200,200) ) | |
x,y,w,h = faces[0] | |
faceImg = img [ y:y+h , x:x+w ] | |
return faceImg | |
# load trained XML file | |
cascadePath = "haarcascade_frontalface_default.xml" | |
faceCascade = cv2.CascadeClassifier(cascadePath) | |
# We will be using LBPH | |
recognizerLBP = cv2.createEigenFaceRecognizer() | |
#recognizerEigenFace = cv2.createLBPHFaceRecognizer() | |
#recognizerFisher = cv2.createFisherFaceRecognizer() | |
# load all face images | |
allImgPath = "imgs" | |
faceImgs = [] | |
faceLabels = [] | |
for label in os.listdir(allImgPath): | |
imgPath = os.path.join(allImgPath,label) | |
# load imgs of each subject | |
for name in os.listdir(imgPath): | |
img = cv2.imread(os.path.join(imgPath , name)) | |
gray = cv2.cvtColor(img , cv2.COLOR_BGR2GRAY) | |
gray = cv2.resize(gray ,(0,0), fx = .2 ,fy = .2) | |
faceImg = detectFace(gray) | |
faceImg = cv2.resize(faceImg ,(500,500)) | |
faceImgs.append(faceImg) | |
faceLabels.append(int(label)) | |
#cv2.imshow("img" , faceImg ) | |
#cv2.waitKey(10) | |
# ------------------------------------Start Training-------------------------------------# | |
recognizerLBP.train(faceImgs, np.array(faceLabels)) | |
#recognizerEigenFace.train(faceImgs, np.array(faceLabels)) | |
#recognizerFisher.train(faceImgs, np.array(faceLabels)) | |
recognizerLBP.save("trainedFacesLBP.xml") | |
#recognizerEigenFace.save("trainedFacesEigen.xml") | |
#recognizerFisher.save("trainedFacesFisher.xml") | |
print ("Training finished ") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 , os | |
import numpy as np | |
def detectFace(img): | |
faces = faceCascade.detectMultiScale(img, 1.3 , minSize = (200,200) ) | |
x,y,w,h = faces[0] | |
faceImg = img [ y:y+h , x:x+w ] | |
faceImg = cv2.resize(faceImg ,(500,500)) | |
return faceImg | |
# load trained XML file | |
cascadePath = "haarcascade_frontalface_default.xml" | |
faceCascade = cv2.CascadeClassifier(cascadePath) | |
recognizerLBP = cv2.createEigenFaceRecognizer() | |
recognizerEigenFace = cv2.createLBPHFaceRecognizer() | |
recognizerFisher = cv2.createFisherFaceRecognizer() | |
recognizerLBP.load("trainedFacesLBP.xml") | |
#recognizerEigenFace.load("trainedFacesEigen.xml") | |
#recognizerFisher.load("trainedFacesFisher.xml") | |
img = cv2.imread("test6.jpg") | |
gray = cv2.cvtColor(img , cv2.COLOR_BGR2GRAY ) | |
gray = cv2.resize(gray ,(0,0), fx = .2 ,fy = .2) | |
faceImg = detectFace(gray) | |
predLBP = recognizerLBP.predict(faceImg) | |
#predEigen = recognizerEigenFace.predict(faceImg) | |
#predFisher= recognizerFisher.predict(faceImg) | |
print "LBP RESULT : " ,predLBP | |
#print "Eigen RESULT : " ,predEigen | |
#print "Fisher RESULT : " ,predFisher | |
#cv2.destroyAllWindows() |
Subscribe and Download Code.
If you like this post,.Subscribe
To download the code: Click here.
If you have any doubt or suggestion , feel free to ask anything in the comment section.
Thanks.
Comments
Post a Comment