This article was published as a part of the Data Science Blogathon.
In this section, we will build a face detection algorithm using Caffe model, but only OpenCV is not involved this time. Instead, along with the computer vision techniques, deep learning skills will also be required, i.e. We will use the deep learning pre-trained model to detect the faces from the image of different angles, and the model that we will use is Caffe model.
We already have multiple options for detecting the face, so in this section, we will discuss the benefits of using the deep learning model for seeing the faces.
We will first import the required libraries.
import os import cv2 import dlib from time import time import matplotlib.pyplot as plt
We have come across many algorithms that are used to detect faces both in real-time and in images. We use the deep learning approach that uses a ResNet-10 neural network architecture to see the multiple faces in the image, which is more accurate than the OpenCV HAAR cascade face detection method.
For loading the deep learning-based face detector, we have two options in hand,
For loading the Caffe model we will use the cv2.dnn.readNetFromCaffe() and if we want to load the Tensorflow model, then cv2.dnn.readNetFromTensorflow() function will be used. Just make sure to use the appropriate arguments.
opencv_dnn_model = cv2.dnn.readNetFromCaffe(prototxt="models/deploy.prototxt", caffeModel="models/res10_300x300_ssd_iter_140000_fp16.caffemodel") opencv_dnn_model
Output:
So it’s time to make a face detection function which will be named as cvDnnDetectFaces()
Note: The more the confidence value of detection, the more accurate the results will be.
def cvDnnDetectFaces(image, opencv_dnn_model, min_confidence=0.5, display = True): image_height, image_width, _ = image.shape output_image = image.copy() preprocessed_image = cv2.dnn.blobFromImage(image, scalefactor=1.0, size=(300, 300), mean=(104.0, 117.0, 123.0), swapRB=False, crop=False) opencv_dnn_model.setInput(preprocessed_image) start = time() results = opencv_dnn_model.forward() end = time() for face in results[0][0]: face_confidence = face[2] if face_confidence > min_confidence: bbox = face[3:] x1 = int(bbox[0] * image_width) y1 = int(bbox[1] * image_height) x2 = int(bbox[2] * image_width) y2 = int(bbox[3] * image_height) cv2.rectangle(output_image, pt1=(x1, y1), pt2=(x2, y2), color=(0, 255, 0), thickness=image_width//200) cv2.rectangle(output_image, pt1=(x1, y1-image_width//20), pt2=(x1+image_width//16, y1), color=(0, 255, 0), thickness=-1) cv2.putText(output_image, text=str(round(face_confidence, 1)), org=(x1, y1-25), fontFace=cv2.FONT_HERSHEY_COMPLEX, fontScale=image_width//700, color=(255,255,255), thickness=image_width//200) if display: cv2.putText(output_image, text='Time taken: '+str(round(end - start, 2))+' Seconds.', org=(10, 65), fontFace=cv2.FONT_HERSHEY_COMPLEX, fontScale=image_width//700, color=(0,0,255), thickness=image_width//500) plt.figure(figsize=[20,20]) plt.subplot(121);plt.imshow(image[:,:,::-1]);plt.title("Original Image");plt.axis('off'); plt.subplot(122);plt.imshow(output_image[:,:,::-1]);plt.title("Output");plt.axis('off'); else: return output_image, results
We are done with all the image pre-processing tasks, and we have also created a function. Now it’s time to display the visual results with the help of the function that we have created.
image = cv2.imread('media/test.jpg') cvDnnDetectFaces(image, opencv_dnn_model, display=True)
Output:
So from the above output, it is visible how well our model has performed to even detect the multiple faces in the image with a reasonable confidence score and show the time taken for the face to be detected.
Here we have come to the conclusion part of this article. So let’s discuss what we have learned so far in a nutshell or the key takeaways from the article:
Here’s the repo link to this article. I hope you liked my article on Face detection using the Caffe model. If you have any opinions or questions, then comment below.
Connect with me on LinkedIn for further discussion.
The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.
How to identify who are them using detected that with the database