In this Python tutorial, learn to write a Python program for face and eye detection using OpenCV. OpenCV supports algorithms that are related to machine learning and computer vision. In addition, OpenCV offers support to many programming languages such C++, Java, and of course, Python. Platforms that OpenCS is available on are Android, iOS, Linux, OS X, and Windows. For this tutorial, I will be implementing the on Windows operating system with PyCharm.
What is OpenCV-Python
OpenCV-Python is a Python library that is designed to solve computer vision and machine learning problems. Python is a general programming language is very popular because of it’s code readability and simplicity. When Python is compared to other languages such as C or C++, Python is slower.
The great part is that Python can be extended with C/C++. C/C++ is a computationally intensive code which can can be combined with Python wrappers, as modules. This is a win-win, C/C++ offers fast code and Python is a simple programming language to code.
Since numerical operations are needed in solving problems for computer vision and machine learning, OpenCV-Python works with Numpy. OpenCV utilizes NumPy to convert OpenCV array structures to and from NumPy arrays.
OpenCV-Python Cascade Classifier Detection
There are two stages in a cascade classifier; detection and training. In this tutorial, we will focus on detection and OpenCV offers pre-trained classifiers such as eyes, face, and smile. In order to detect, those classifiers, there are XML files associated to the classifiers that must be imported into your code.
Below is the list of XML files for Haar-Cascades and can be easily found on GitHub:
Implementing Facial Detection with Python cv2
In order to begin this facial detection, review make sure to download both of these xml files for this tutorial:
Import NumPy and cv2
There are two packages that will need to be imported and below are the pip commands to install for command prompt if needed. Make sure to install in the scripts directory where pip is installed.
- pip install numpy
- pip install opencv-python
import numpy as np import cv2
cv2.CascadeClassifier() for Face and Eyes
Now that you in have installed and imported the two modules, put the haarcascade_eye.xml & haarcascade_frontalface_default.xml files in the same folder as your script. If you prefer to save these two files in another location, make sure to add the full path. I have added the full path in the below example for reference.
face_cascade = cv2.CascadeClassifier('C://path//to//frontalfaceXMLhaarcascade_frontalface_default.xml') eye_cascade = cv2.CascadeClassifier('C://path//to//eyeXMLhaarcascade_eye.xml')
cv2.imread() and cv2.cvtColor()
Nos is to create a variable for the image you wish to use facial detection with cv2.imread(). After this, create a gray scale image of this picture for use with cv2.cvtColor().
img = cv2.imread('F://FaceEyesDetectionHeadshot.jpg') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
detectMultiScale() and rectangle()
The detectMultiScale() function will create be used to create the faces variable for all detections in the image. In order to visualize each detection, we must iterate over each detection and place rectangles over each face that is detected. Personally, this is where it becomes very cool to learn because this is the basics of furthering your learning and knowledge in facial recognition.
So, how do detections work? A detection works by detecting it’s top-left corner coordinates. In addition to width and height of the rectangle.
The rectangle() function draws over the images and the rectangle is placed. In order to do so, the pixel coordinates of the bottom-right corner and top-left corner must be known.
The rectangle() function has the following arguments:
- Original image
- Coordinates to detect the bottom-right point
- Coordinates to detect the top-left point
- Rectangle color
- Rectangle line thickness
faces = face_cascade.detectMultiScale(gray, 1.3, 5) for (x,y,w,h) in faces: cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2) roi_gray = gray[y:y+h, x:x+w] roi_color = img[y:y+h, x:x+w] 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)
imshow(),waitKey() and destroyAllWindows()
The function imshow() will display the image. The waitKey() is very important because it waits for a keystroke before closing the window. If we were not to use this function, imshow() would display the image, but close the window immediately. Lastly, destroyAllWindows() closes the window when a key in pressed.
Face and Eye Detection Single Person Example
Take the below code and copy into a single Python file and run it. Once you execute the code you will see a similar facial recognition detection on your image.
Single Person Picture Before:
Single Person Picture After:
Face and Eye Detection Group Person Example
Now let’s try a group of people for facial and eye recognition.
Group Picture Before:
Group Picture After:
As you can see, the facial and eye recognition was right on point!