Computer Vision - Face Detection
I am fascinated with Computer Vision and wanted to learn OpenCV. I took a stab at OpenCV a few years ago before diving into it more this year. The math of machine learning can easily get pretty complicated, and working on datasets to get production quality ML results is quite hard. For someone who just wanted to build products that did the job, OpenCV was the correct beginner tool. I decided to learn OpenCV by doing projects like face detection, pose estimation, license plate detection and other tools.
Below is the code sample for face detection with OpenCV:
import cv2
faceCascade = cv2.CascadeClassifier("../haarcascades/haarcascade_frontalface_default.xml"
img = cv2.imread("../images/krishna.png")
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(imgGray, 1.1, 4)
for (x, y, w, h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
cv2.imshow("Result", img)
cv2.waitKey(0)
If you are learning ML in university, you will be taught how to train a CNN (Convolutional Neural Network). This program does not use CNN but uses haarcascade classifier. I am using the haarcascade classifier.
The haarcascade classifier can be thought of as a trained model with the knowledge that is necessary for face detection. In other words, you don't need to train your model, your model is already trained when you load the classifier. When building a program for production, you typically don't want your program to train on the fly, you want to use an already trained model and simply apply the model to new data.
Haarcascade classifier
This is a feature detection classifier that uses edge detection on images to identify the face. Here is a useful video that explains the haarcascade classifier.
If you don't want to watch the video above, here is quick summary.
Color Images are usually represented mathematically (and in code) as 3-channel matrices. That is, 3 matrices where each matrix represents colors Red, Green and Blue respectively. A matrix has rows and columns, and each element in the matrix represents the pixel value of the image. These pixel values range from 0 to 255, with 0 representing the lowest pixel intensity and 255 representing the highest pixel intensity. A grayscale image is a single channel matrix where 0 represents white and 255 represents black.
The haarcascade works well with grayscale image matrices by applying a concept known as kerneling. Kernel is the process of stepping through isolated portions of an image, then identifying the most useful bit of information. In this case, the most useful information are the edges. It uses these edges to identify whether it has detected a face.
How does a kernel detect an edge?
By checking the pixel intensity (matrix values) and identifying the line of separation (or edge) between light intensities versus darker intensities.
The Actual Detection
This line of code,
faces = faceCascade.detectMultiScale(imgGray, 1.1, 4)
does the actual face detection and returns the coordinates of the image that it detects as being the face.
We use these coordinates to draw a rectangular box.
for (x, y, w, h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
And there we have it. Face Detection with OpenCV.
Credit: I'd like to credit Muratza from https://www.computervision.zone/ . His videos showed me how to develop with OpenCV.