In this article, we’ll introduce a very simple way to get started with face recognition using Python and the open source library OpenCV.

Matters needing attention:

1) Make sure you are using OpenCV V2.

2) Have a webcam available so that this script can work properly.

 OpenCV 

OpenCV is the most popular computer vision library. Originally written in C/C++, it now provides bindings for Python.

OpenCV uses machine learning algorithms to search for faces in images. Because faces are so complex, there’s no simple test that can tell you if you’ve found one. Instead, there are thousands of small patterns and features that must be matched. These algorithms break the task of recognizing faces into thousands of smaller, bite-sized tasks, each of which is easy to solve. These tasks are also called classifiers.

For something like a face, you might have 6,000 or more classifiers, all of which must match the face to be detected (within error limits, of course). But here’s the problem: For face detection, the algorithm starts at the top left corner of the picture, moves down to small chunks of data, looks at each chunk, and keeps asking, “Is this a face? … Is this a human face? … Is it a human face?” With 6,000 or more tests per block, you may need to perform millions of calculations, which can bring your computer to a halt.

To solve this problem, OpenCV uses cascading.

OpenCV cascades the face detection problem into several stages. For each block, it does a very rough and quick test. If it passes, it does a more detailed test, and so on.

The algorithm may have 30 to 50 such phases, or cascades, and will only detect a face if all of them pass. The advantage is that most images return negative values in the first few stages, which means the algorithm won’t waste time testing all 6,000 features.

Face testing can now be done in real time instead of taking hours. Although this theory may sound complicated, it is actually quite easy. The cascade itself is just a bunch of XML files that contain OpenCV data for detecting objects. You initialize the code with the desired cascade, and the code will work for you.

Because face detection is so common, OpenCV comes with a number of built-in cascades for detecting everything from face to eyes, hands to legs. There are even cascades of non-human things.

Install OpenCV

First, you need to find the correct installation files for your operating system. I found installing OpenCV to be the most difficult part of the task. If you encounter strange and unexplained errors, it could be due to library conflicts, 32/64 bit differences, and so on.

I found it easiest to just use a Linux virtual machine and install OpenCV from scratch. Once installed, you can test if it works by starting a Python session and typing the following:

import cv2
Copy the code

If no errors are received, proceed to the next section.

Understand the code

Let’s break down the actual code, which can be downloaded from the REPo. Get the face_detect.py script, the abba.png image, and haarcascade_frontalface_default.xml.

# Get user supplied values
imagePath = sys.argv[1]
cascPath = sys.argv[2]
Copy the code

The image and cascade name are first passed as command line arguments. We will use ABBA images and the default cascade provided by OpenCV to detect faces.

# Create the haar cascade
faceCascade = cv2.CascadeClassifier(cascPath)
Copy the code

Now let’s create the cascade and initialize it using our facial cascade. This cascades the face into memory, ready for use. Keep in mind that a cascade is just an XML file containing data for detecting faces.

# Read the image
image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
Copy the code

Here, we read the image and convert it to grayscale. Many operations in OpenCV are done in grayscale.

# Detect faces in the image faces. = faceCascade detectMultiScale (gray, scaleFactor = 1.1, minNeighbors = 5, minSize = (30, 30), flags = cv2.cv.CV_HAAR_SCALE_IMAGE )Copy the code

This function detects real faces and is a key part of our code. Let’s review the options:

  • The detectMultiScale function is a generic function that detects objects. Since we’re calling it on a cascade of faces, this is what it detects.
  • The first option is a grayscale image.
  • The second is scaleFactor. Because some faces may be closer to the camera, they will appear larger than the faces behind them. The scale factor compensates for this.
  • Detection algorithms use moving Windows to detect objects. MinNeighbors defines how many objects are detected near the current object before declaring that the face is found. Also, minSize gives the size of each window.

Note: I used the usual values for these fields. In real life, try using different window sizes and scale factor equivalents until you find the best value.

This function returns a list of rectangles in which it thinks it has found a face. Next, we’ll loop around where it thinks it found something.

print "Found {0} faces!" .format(len(faces)) # Draw a rectangle around the faces for (x, y, w, h) in faces: cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)Copy the code

This function returns four values: the rectangle’s x and y positions, and its width and height (w, h).

We use these values to draw a rectangle using the built-in Rectangle () function.

cv2.imshow("Faces found", image)
cv2.waitKey(0)
Copy the code

Finally, we display the image and wait for the user to press a key.

Check the result

Let’s test this against ABBA photos:

$ python face_detect.py abba.png haarcascade_frontalface_default.xml
Copy the code

It worked. How about another photo:

That…… Not face. Let’s try again. I changed the parameters and found that setting scaleFactor to 1.2 removed the wrong face.

What happened? Well, the first picture was taken with a high quality camera.

The second appears to have been taken from a distance, possibly with a mobile phone. This is why scaleFactor must be modified.

As I said, the algorithm must be set up on a case-by-case basis to avoid false positives. But be aware that since this is based on machine learning, the results will never be 100% accurate. In most cases, good enough results will be obtained, but sometimes the algorithm will recognize an incorrect object as a face.

The full code is here: github.com/shantnu/Fac…

Extension: Pedestrian detection

In order to cater to and meet the modern market demand, we developed EasyCVR, a video platform that supports the access of devices with various protocols. We used Python to conduct AI recognition test. The specific method is to turn on the camera of the local computer for real-time recognition, or directly pass in a picture for pedestrian detection. When the analysis code passes the data source to the identification, it sees source= ‘0’, but this parameter is to open the camera stream on the local computer, and then perform pedestrian detection.

However, we need to modify this part. RTSP flow is used for AI pedestrian recognition. Next, we need to analyze the code to find the place that can be modified, or touch a parameter to modify the RTSP flow.

We have found where the video stream is sent in. The following is the code in the analysis to change it into RTSP stream and write the RTSP stream in order to achieve real-time analysis and realize the effect of pedestrian detection.

The source parameter is only used by LoadStreams and is passed in directly.

Source =[source] : =[source] : =[source] : =[source] : =[source] : =[source] : =[source] In traversal, OpencV is also used to open the camera stream of the local computer, and then open a thread for real-time pedestrian recognition.

The code used in OpencV cv2.VideoCapture function, from the Internet to find the use of this function that this function can be directly into the RTSP stream address, so the problem is much easier to solve. VideoCapture cv2.VideoCapture this function can pass RTSP address, so pass RTSP address try, find pass RTSP address is no problem.

As long as you modify the source parameter, the detection is finally realized: