Detect faces in Images using OpenCV and Python
In this article, we’ll look at an amazingly simple way of performing face detection using Python and the open-source library OpenCV.One among the many reasons that make computer vision a fascinating field is the truth that it enables us to build many advanced and futuristic-sounding technologies with ease. Face detection is one of the most popular feature among them. OpenCV has an integrated facility to carry out face detection, which has truly endless applications in the real world in almost every area, from security to entertainment.
So let's see how we can detect faces in images using OpenCV and python. First of all, you need to install OpenCV on your system. You can do it using pip with the following command: pip install OpenCV-python.
Here we are using Haar Cascades for detecting faces and this is a machine learning-based approach where a cascade function is trained with a set of input data. OpenCV already contains many pre-trained classifiers for face, eyes, smiles, etc. Today we will be using the face classifier. You need to download the trained classifier XML file (haarcascade_frontalface_default.xml), which is available in OpenCV's GitHub repository.
The first and most basic way to perform face detection is to load an image and detect faces in it. To make the result visually meaningful, we will draw rectangles around faces on the original image. Now that you have haar cascades included in your project, let’s go ahead and create a python script to perform face detection.
Let's go through the code.
face_detection.py
First, we need to import OpenCV. Secondly, we declare a face_cascade variable, which is a CascadeClassifier object for faces, and responsible for face detection.
import cv2
face_cascade = cv2.CascadeClassifier("C:\\Users\Cyril Tom Mathew\Desktop\haarcascade_frontalface_default.xml")
We then load the image on which we need to perform face detection with cv2.imread, and convert it to grayscale, because detection works only on grayscale images. So it is important to convert the color image to grayscale.
image = cv2.imread("C:\\Users\Cyril Tom Mathew\Desktop\ggg.jpg")
gray_img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
The next step is where the actual face detection happens. The detectmultiScale function takes 3 arguments, they are the input image, scaleFactor, and minNeighbours. scaleFactor determines the percentage reduction of the image at each iteration of the face detection process, and minNeighbours specifies the minimum number of neighbors retained by each face rectangle at each iteration. You may have to slightly change these values to get the best results.
This detection operation returns a list of coordinates for the rectangular regions where faces were found. We use these coordinates to draw the rectangles in our image. We also print the number of faces detected in the image, after the detection operation.
faces = face_cascade.detectMultiScale(gray_img, 1.2, 5)
print("{0} Faces Detected".format(len(faces)))
The method, cv2.rectangle, allows us to draw rectangles at the specified coordinates (x and y represent the left and top coordinates, w and h represent the width and height of the face rectangle).
We will draw green rectangles around all the faces we find by looping through the faces
variable, make sure you are drawing rectangles on the original image and not on the grayscale version.
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w,y+h), (0,255,0),2)
Finally, we create a namedWindow instance and display the resulting processed image in it.
cv2.imshow("Faces Detected!!", image)
cv2.waitKey(0)
That's it guys, let's run the code.
| Output |
Performing face detection on a still image or a video feed is an extremely similar operation. The latter is just the sequential version of the former. Face detection on videos is simply, face detection applied to each frame read into the program from the camera. So the underlying theory is the same for both and we will discuss it in detail, in the very next post.
Hope you found this useful. If you have any trouble implementing this or if you need any help, feel free to comment below.
Thank You.
Face Detection in Python using OpenCV, in just 10 lines of code
Reviewed by Cyril Tom Mathew
on
July 15, 2019
Rating:
No comments: