Facial Detection pt.2

Harsh Kumar Khatri
Harsh Kumar Khatri
4 min readMay 10, 2020

--

As we have discussed in the previous article about training our model for the images which we have stored in the folders, in this article we will be seeing how we will use the saved dataset to predict for the faces in the live video.

Lets dive into it.

If you have not installed the dependencies then you can install them by typing the command below in the terminal.

pip install numpy 
pip install pickle
pip install opencv-contrib-python

Once we have those installed we will then be importing them in our main python file.

import numpy as np 
import cv2
import pickle

Now loading the pre trained cascade file available in the face_cascade variable.

face_cascade=cv2.CascadeClassifier('cascades/data/haarcascade_frontalface_alt2.xml')

We can also use the eye cascade classifier to detect eyes in the live video. (This step is completely optional)

eye_cascade=cv2.CascadeClassifier('cascades/data/haarcascade_eye.xml')

We will then be initializing our face recognizer from the face module available in cv2 and with the loaded recognizer we will be reading the data which we have saved in the ‘.yml’ file.Also we will have an empty dictionary which will store the labels of the identified person.

recognizer=cv2.face.LBPHFaceRecognizer_create() recognizer.read('face-trainner.yml')

We will also load the ‘.pickle’ file which also has the data stored from the previous tutorial after training. We have also loaded the data from the file inside a variable. It is stored in the form of key value pairs. We will use it and store the filpped keyvalue pair in the dictionary we have defined above.

with open("face-labels.pickle","rb") as f: og_labels=pickle.load(f) labels={v:k for k,v in og_labels.items()}

We will start the video capturing and will be initializing the loop with the help of which we will be seeing the live video frame by frame

cap=cv2.VideoCapture(0) while(True):

We will then read the captured video in the boolean variable ret and frame will have numpy values for the pixels inside our frame.

ret,frame=cap.read()

Now we will convert frame to gray which is useful for the identification

gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)

Now we will be detecting the faces as we have done in the previous article. We will be having the scale factor which will scale the images and will be seeing the nearby neighbors.

faces=face_cascade.detectMultiScale(gray,scaleFactor=1.5,minNeighbors=5)

Now we will be detecting the region of interest for the identified face

for (x,y,w,h) in faces: # print(x,y,w,h) # this will be the coordinates of the rectangle around our face roi_gray=gray[y:y+h,x:x+w] roi_color = frame[y:y+h, x:x+w]

The roi_gray will be used for the predicting with the help of the deep learning model. We will be using two variable one will be the idea which is the label number which we have stored in the data.We have also used the confidence performance as conf.

id_,conf=recognizer.predict(roi_gray) if conf>=75 and conf<=150: print(id_) print(labels[id_]) font=cv2.FONT_HERSHEY_SIMPLEX name=labels[id_] color=(255,255,255) stroke=2 cv2.putText(frame,name,(x,y),font,1,color,stroke,cv2.LINE_AA)

Once we have the confidence percentage to the value we want, we will be printing the label id and the name for which we have the label id for.We have the font loaded which is the Hershey simplex with which the text will be written over the image. Next we will be storing the name in the name variable and will have the color which we have chosen to be white. Stroke is the thickness with which we will be drawing the line.

Also we will be putting the text over the frame with the data which we have passed inside putText.

We will save an image with the name we have specified.

img_item="mg_img.png" cv2.imwrite(img_item,roi_gray)

We can see this image before the final output to see if the detecting boundary is name is accurate or not.

Next we will be using the rectangle model to draw the rectangle over the image.

cv2.rectangle(frame,(x,y),(x+w,y+h),color,stroke)

As ai have mentioned above we can also detect the eyes also from the live video with the classifier.

eyes=eye_cascade.detectMultiScale(roi_gray) for (ex,ey,ew,eh) in eyes: cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)

We have passed the gray region of interest as we do not need the full image as eyes will be present inside the face.

Finally we will be be showing the output with the imshow function.

cv2.imshow('frame',frame)

We will then have a exit key which will be pressed to exit the output.

if cv2.waitKey(20) & 0xFF==ord('q'): break

This will break us out of the while loop.

Now we will be releasing the cap which is the video capture and then we will be destroying the windows which all were created as the output.

cap.release() cv2.destroyAllWindows()

The total code which we have used in this article is given below for your better reference. Also i will be attaching the repo containing the code and all the related assets.

import numpy as np import cv2 import pickle # Loading the facecascader face_cascade=cv2.CascadeClassifier('cascades/data/haarcascade_frontalface_alt2.xml') #loading the eye cascader eye_cascade=cv2.CascadeClassifier('cascades/data/haarcascade_eye.xml') # Loading the recognizer recognizer=cv2.face.LBPHFaceRecognizer_create() recognizer.read('face-trainner.yml') labels={} # Loading the labels from pickle file with open("face-labels.pickle","rb") as f: og_labels=pickle.load(f) labels={v:k for k,v in og_labels.items()} cap=cv2.VideoCapture(0) while(True): # Capturing the frames ret,frame=cap.read() # converting the frames to gray for the cascade to determine gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) faces=face_cascade.detectMultiScale(gray,scaleFactor=1.5,minNeighbors=5) for (x,y,w,h) in faces: # print(x,y,w,h) # this will be the coordinates of the rectangle around our face roi_gray=gray[y:y+h,x:x+w] roi_color = frame[y:y+h, x:x+w] # Recognizing-deep learning model id_,conf=recognizer.predict(roi_gray) if conf>=75 and conf<=150: print(id_) print(labels[id_]) font=cv2.FONT_HERSHEY_SIMPLEX name=labels[id_] color=(255,255,255) stroke=2 cv2.putText(frame,name,(x,y),font,1,color,stroke,cv2.LINE_AA) # print("Id is ",id_) # print("confidence is ",conf) img_item="mg_img.png" cv2.imwrite(img_item,roi_gray) # Drawing a rectangle arround are face color=(255,0,0) stroke=2 cv2.rectangle(frame,(x,y),(x+w,y+h),color,stroke) eyes=eye_cascade.detectMultiScale(roi_gray) for (ex,ey,ew,eh) in eyes: cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2) # Displaying the resulting frame cv2.imshow('frame',frame) if cv2.waitKey(20) & 0xFF==ord('q'): break # Releasing the capture cap.release() cv2.destroyAllWindows()

The repo is given below.

Hope you have understood this. If you have any doubts then please comment it down. I would be happy to help.

This brings us to the end of the 6 day ‘OpenCV Series’. From now the articles will be coming twice a week until next series.

Originally published at https://harshblog.xyz on May 10, 2020.

--

--