Face detection with OpenCV and deep learning

Vinu Vish
5 min readJun 6, 2019

What is the Open CV?
OpenCV is a library of programming functions mainly aimed at real-time computer vision.

What is Deep learning?
Deep learning is part of a broader family of machine learning methods based on artificial neural networks. Learning can be supervised, semi-supervised or unsupervised.

Why Deep learning for face detection?
previously we used the haar cascade pre-trained model for face detection .its bit old school method. But with deep learning face detection, You can perform fast, accurate face detection with OpenCV using a pre-trained deep learning face detector model shipped with the library.

Face detection in images using OpenCV and deep learning

what most OpenCV users do not know is that Rybnikov has included a more accurate, deep learning-based face detector included in the official release of OpenCV.
First, we need to download, Deep neural network module and Caffe models

prototxt file(s) which define the model architecture (i.e., the layers themselves)
caffemodel file which contains the weights for the actual layers

Following the link, you can download prototxt and caffemodel
Link

Now we are going to playing with python (happy codding)
pre-requirement : OpenCV latest ,numpy

  1. Create a new python project and create src/ and models/ Directory inside the model Director copy your downloaded prototxt and caffemodel.
  2. Create python file name call FaceDetectorImage.py inside the src/and code following lines
# import the necessary packages
import numpy as np
import argparse
import cv2
#defining argument parsers
ap = argparse.ArgumentParser()
ap.add_argument("-i","--image",required=True,help="Input imagepath")
args = vars(ap.parse_args())

#defining prototext and caffemodel paths
caffeModel = /Enter_YOUR_ABSOLUTE_PATH/res10_300x300_ssd_iter_140000.caffemodel"
prototextPath = "Enter_YOUR ABSOLUTE_PATH/deploy.prototxt.txt"

Abow code snip, we are importing the necessary packages, parsing command line arguments and defining prototext and caffemodel ABSOLUTE paths.

3. Now we are going to load our model and create a blob from our image

#Load Model
print("Loading model...................")
net = cv2.dnn.readNetFromCaffe(prototextPath,caffeModel)

#procesess the import image with opencv
image = cv2.imread(args.get("image"))

# extract the dimensions , Resize image into 300x300 and converting image into blobFromImage
(h,w) = image.shape[:2]
# blobImage convert RGB (104.0, 177.0, 123.0)
blob = cv2.dnn.blobFromImage(cv2.resize(image,(300,300)),1.0,(300,300),(104.0, 177.0, 123.0))

Abow code snip we are Loading our prototxt and cafe model. Then we load the image, extract the dimensions, resize to 300px and create a blob using DNN.

4. Nex we are passing blob through the network to detect and prediction

#passing blob through the network to detect and pridiction
net.setInput(blob)
detections = net.forward()

Abow code snip we are passing blob to DNN(deep neural network) To detect faces,

5. Then we’ll loop over the detections and draw boxes around the detected faces

# loop over the detections
for i in range(0, detections.shape[2]):
# extract the confidence and prediction
confidence = detections[0, 0, i, 2]

# filter detections by confidence greater than the minimum #confidence
print(confidence)
if confidence > 0.5:
# compute the (x, y)-coordinates of the bounding box for the
# object
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
(startX, startY, endX, endY) = box.astype("int")

# draw the bounding box of the face along Confidance
# probability
text = "{:.2f}%".format(confidence * 100)
y = startY - 10 if startY - 10 > 10 else startY + 10
cv2.rectangle(image, (startX, startY), (endX, endY),
(0, 0, 255), 2)
cv2.putText(image, text, (startX, y),
cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2)

# show the output image
cv2.imshow("Output", image)
cv2.waitKey(0)

Abow code We looping over the detections then extracting the confidence level and face boundaries data that are analysed from the DNN. And we are filtering by the minimum confidence level. If confidence meets minimum confident level it getting the boundaries and draw the boxes using cv2.rectangle along with confidence level.

6. Now open a terminal and execute the following command

python3 src/faceDetectorImage.py -i /home/vinu/Desktop/Vishnu.jpg

Output Results………..

Output Face detection

Face detection in live video with OpenCV and deep learning

  1. Create python file name call FaceDetectorVideo.py inside the src/and code following lines
# import the necessary packages
from imutils.video import VideoStream
import numpy as np
import argparse
import imutils
import time
import cv2
#defining prototext and caffemodel paths
caffeModel = /Enter_YOUR_ABSOLUTE_PATH/res10_300x300_ssd_iter_140000.caffemodel"
prototextPath = "Enter_YOUR ABSOLUTE_PATH/deploy.prototxt.txt"

Abow code snip, we are importing the necessary packages, parsing command line arguments and defining prototext and caffemodel ABSOLUTE paths.

2. Now we are going to initialize video stream to get the live video frames

#Load Model
print("Loading model...................")
net = cv2.dnn.readNetFromCaffe(prototextPath,caffeModel)

# initialize the video stream to get the live video frames
print("[INFO] starting video stream...")
vs = VideoStream(src=0).start()
time.sleep(2.0)

Abow code snip we are Loading our prototxt and cafe model. And start VideoStream to switch on or startup the video source(if webcam src =1).

3. Now we are going to loop the frames from the VideoStream

while True :
#Get the frams from the video stream and resize to 400 px
frame = vs.read()
frame = imutils.resize(frame,width=400)

# extract the dimensions , Resize image into 300x300 and converting image into blobFromImage
(h, w) = frame.shape[:2]
# blobImage convert RGB (104.0, 177.0, 123.0)
blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)), 1.0,
(300, 300), (104.0, 177.0, 123.0))

Above code, while loop will be loop until frame available. if frame available inside the while loop, reading the frame and resize it to 400px then converting blob using DNN(deep neural network)

4. Nex we are passing blob through the network to detect and prediction

# passing blob through the network to detect and pridiction
net.setInput(blob)
detections = net.forward()

Abow code snip we are passing blob to DNN(deep neural network) To detect faces. make sure the code will be inside the while loop.

5. Then we’ll loop over the detections and draw boxes around the detected faces inside the while loop

for i in range(0, detections.shape[2]):
# extract the confidence and prediction

confidence = detections[0, 0, i, 2]

# filter detections by confidence greater than the minimum confidence
if confidence < 0.5 :
continue

# Determine the (x, y)-coordinates of the bounding box for the
# object
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
(startX, startY, endX, endY) = box.astype("int")

print(confidence)
# draw the bounding box of the face along with the associated
text = "{:.2f}%".format(confidence * 100)
y = startY - 10 if startY - 10 > 10 else startY + 10
cv2.rectangle(frame, (startX, startY), (endX, endY),
(0, 0, 255), 2)
cv2.putText(frame, text, (startX, y),
cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2)

# show the output frame
cv2.imshow("Frame", frame)
key = cv2.waitKey(1) & 0xFF

# if the `q` key was pressed, break from the loop
if key == ord("q"):
break

# do a bit of cleanup
cv2.destroyAllWindows()
vs.stop()

Abow code We looping over the detections then extracting the confidence level and face boundaries data that are analysed from the DNN. And we are filtering by the minimum confidence level. If confidence meets minimum confident level it getting the boundaries and draw the boxes using cv2.rectangle along with confidence level. and it will display the frame on the screen and wait for a keypress

6. Now open a terminal and execute the following command
python3 faceDetectorVideo.py

Output Result……

Repository for compleat project Link

Thanks for reading………………………………………..
If you face any trouble feel free and comment below
cheers
Happy ImageProcessing & DeepLearning

--

--