Face Detection-OpenCV
25 Lines of Python Code for Face Detection-OpenCV Technical Tutorial
Last updated
25 Lines of Python Code for Face Detection-OpenCV Technical Tutorial
Last updated
OpenCV is the most popular computer vision library. It was originally developed in C and C ++ and now supports Python.
It uses machine learning algorithms to search a person's face in an image. For something as complex as a human face, there is no simple detection that can make a conclusion as to whether a human face exists, and thousands of feature matches are required. The algorithm breaks down the face recognition task into thousands of small tasks, each of which is not difficult to handle. These tasks are also called classifiers.
For objects similar to faces, you may need no less than 6000 classifiers, each of which needs to be successfully matched (of course, with a fault tolerance rate) to detect a face. But there is a problem: for face recognition, the algorithm calculates data blocks from the upper left corner and keeps asking "Is this a face?" There are more than 6000 tests per data block, and the amount of calculations will reach millions of levels. The computer is likely to make you wait for all the flowers.
OpenCV uses cascades to avoid this. What is Cascade? The best answer is already in the dictionary: a waterfall or a continuous waterfall.
Like a continuous waterfall, OpenCV cascade breaks down the face detection problem into several steps. For each data block, it performs a rough, fast detection. If it passes, a more careful test will be performed, and so on. The algorithm has 30 to 50 such stages, or cascades. Only through all stages can the algorithm determine that a face is detected. The advantage of this is that most graphs generate negative feedback in the first few steps, so the algorithm does not need to test all 6000 features on it, which greatly saves time. Compared to the "normal process", which takes several hours, this can realize face detection in real time.
Its theory may sound complicated, but it is actually very simple to operate. These cascades are just a series of XML files containing OpenCV data. You use the cascade initialization code you want, and it will do what you want for you.
Due to the universality of face recognition, OpenCV has a series of built-in cascades that can detect various things, from eyes to hands to legs. There are even cascades for non-human objects. For example, if you run a fruit shop that sells bananas and want to monitor people who steal bananas, one guy has developed an algorithm for this scenario !
First, you need to find the correct settings file for your operating system .
I found that installing OpenCV was the hardest part. If you encounter strange, unexplainable errors, there may be a library crash, 32- and 64-bit compatibility issues, and so on. Personal experience is that it is easiest to install OpenCV from scratch using only a Linux virtual machine.
Once installed, you can open a Python session and type the following code to test if it works:
If no errors pop up, you can go to the next link.
The source code can be downloaded from the repository . Remember to get the face_detect.py text, abba.png image and haarcascade_frontalface_default.xml. Below, I break the code down.
Pass the image and cascade name as command line arguments. We will use Abba images and the default cascade provided by OpenCV for face detection.
Now we create a cascade and initialize it with the face cascade. This face cascade is imported into memory, so it is ready to use. Remember that the cascade is just an XML file containing the face detection data.
Read the picture and convert it to grayscale format.
What this function does is detect faces, which is the core part of the code. So let's go over the options.
The DetectMultiScale function is a general function for detecting objects. We call it on the face cascade, and it detects the face. The first option is a grayscale picture.
The second is scaleFactor. Some faces are closer to the camera and larger than others. ScaleFactor compensates for this.
The detection algorithm uses moving windows to detect objects. Before the system announces that a face is detected, minNeighbors defines how many objects are currently around it. MinSize gives the size of each window.
I use the values commonly used in these fields. In reality, you will experiment with parameters such as window size and expansion factor until you find the most suitable one.
When the function thinks it finds a face, it returns a rectangular list. Next, we loop until it thinks something is detected.
The function returns four values: the x and y coordinates of the rectangle, and its height and width. We use these values and the built-in rectangle () function to draw a matrix.
Finally, we display the model and wait for the user to press a key.