Python+OpenCV:级联分类器(Cascade Classifier)

82 篇文章 20 订阅

Python+OpenCV:级联分类器(Cascade Classifier)

目标

  • We will learn how the Haar cascade object detection works.
  • We will see the basics of face detection and eye detection using the Haar Feature-based Cascade Classifiers.
  • We will use the cv::CascadeClassifier class to detect objects in a video stream. Particularly, we will use the functions:

理论

Object Detection using Haar feature-based cascade classifiers is an effective object detection method proposed by Paul Viola and Michael Jones in their paper,

"Rapid Object Detection using a Boosted Cascade of Simple Features" in 2001.

It is a machine learning based approach where a cascade function is trained from a lot of positive and negative images.

It is then used to detect objects in other images.

Here we will work with face detection.

Initially, the algorithm needs a lot of positive images (images of faces) and negative images (images without faces) to train the classifier.

Then we need to extract features from it. For this, Haar features shown in the below image are used.

They are just like our convolutional kernel.

Each feature is a single value obtained by subtracting sum of pixels under the white rectangle from sum of pixels under the black rectangle.

Now, all possible sizes and locations of each kernel are used to calculate lots of features.

(Just imagine how much computation it needs? Even a 24x24 window results over 160000 features).

For each feature calculation, we need to find the sum of the pixels under white and black rectangles.

To solve this, they introduced the integral image.

However large your image, it reduces the calculations for a given pixel to an operation involving just four pixels.

Nice, isn't it? It makes things super-fast.

But among all these features we calculated, most of them are irrelevant.

For example, consider the image below. The top row shows two good features.

The first feature selected seems to focus on the property that the region of the eyes is often darker than the region of the nose and cheeks.

The second feature selected relies on the property that the eyes are darker than the bridge of the nose.

But the same windows applied to cheeks or any other place is irrelevant.

So how do we select the best features out of 160000+ features? It is achieved by Adaboost.

For this, we apply each and every feature on all the training images.

For each feature, it finds the best threshold which will classify the faces to positive and negative.

Obviously, there will be errors or misclassifications.

We select the features with minimum error rate, which means they are the features that most accurately classify the face and non-face images.

(The process is not as simple as this. Each image is given an equal weight in the beginning. After each classification, weights of misclassified images are increased.

Then the same process is done. New error rates are calculated. Also new weights.

The process is continued until the required accuracy or error rate is achieved or the required number of features are found).

The final classifier is a weighted sum of these weak classifiers.

It is called weak because it alone can't classify the image, but together with others forms a strong classifier.

The paper says even 200 features provide detection with 95% accuracy. Their final setup had around 6000 features.

(Imagine a reduction from 160000+ features to 6000 features. That is a big gain).

So now you take an image. Take each 24x24 window. Apply 6000 features to it. Check if it is face or not. Wow.. Isn't it a little inefficient and time consuming?

Yes, it is. The authors have a good solution for that.

In an image, most of the image is non-face region. So it is a better idea to have a simple method to check if a window is not a face region.

If it is not, discard it in a single shot, and don't process it again.

Instead, focus on regions where there can be a face. This way, we spend more time checking possible face regions.

For this they introduced the concept of Cascade of Classifiers.

Instead of applying all 6000 features on a window, the features are grouped into different stages of classifiers and applied one-by-one.

(Normally the first few stages will contain very many fewer features).

If a window fails the first stage, discard it. We don't consider the remaining features on it.

If it passes, apply the second stage of features and continue the process. The window which passes all stages is a face region. How is that plan!

The authors' detector had 6000+ features with 38 stages with 1, 10, 25, 25 and 50 features in the first five stages.

(The two features in the above image are actually obtained as the best two features from Adaboost).

According to the authors, on average 10 features out of 6000+ are evaluated per sub-window.

So this is a simple intuitive explanation of how Viola-Jones face detection works.

Read the paper for more details or check out the references in the Additional Resources section.

Haar-cascade Detection in OpenCV

OpenCV provides a training method (see Cascade Classifier Training) or pretrained models, that can be read using the cv::CascadeClassifier::load method.

The pretrained models are located in the data folder in the OpenCV installation or can be found here.

The following code example will use pretrained Haar cascade models to detect faces and eyes in an image.

First, a cv::CascadeClassifier is created and the necessary XML file is loaded using the cv::CascadeClassifier::load method.

Afterwards, the detection is done using the cv::CascadeClassifier::detectMultiScale method, which returns boundary rectangles for the detected faces or eyes.

This tutorial code's is shown lines below:

####################################################################################################
# 级联分类器检测与显示(Cascade Classifier)
def lmc_cv_cascade_detect_display(face_cascade, eyes_cascade, frame):
    frame_gray = lmc_cv.cvtColor(frame, lmc_cv.COLOR_BGR2GRAY)
    frame_gray = lmc_cv.equalizeHist(frame_gray)
    # detect faces
    faces = face_cascade.detectMultiScale(frame_gray)
    for (x, y, w, h) in faces:
        center = (x + w // 2, y + h // 2)
        frame = lmc_cv.ellipse(frame, center, (w // 2, h // 2), 0, 0, 360, (255, 0, 255), 4)
        face_roi = frame_gray[y:y + h, x:x + w]
        # in each face, detect eyes
        eyes = eyes_cascade.detectMultiScale(face_roi)
        for (x2, y2, w2, h2) in eyes:
            eye_center = (x + x2 + w2 // 2, y + y2 + h2 // 2)
            radius = int(round((w2 + h2) * 0.25))
            frame = lmc_cv.circle(frame, eye_center, radius, (255, 0, 0), 4)
    lmc_cv.imshow('Cascade Classifier: Face Detection', frame)
####################################################################################################
# 级联分类器(Cascade Classifier)
def lmc_cv_cascade_classifier():
    """
        函数功能: 级联分类器(Cascade Classifier).
    """

    # 读取视频
    parser = argparse.ArgumentParser(description='Code for Cascade Classifier tutorial.')
    parser.add_argument('--face_cascade', help='Path to face cascade.',
                        default='D:/99-Research/TestData/gpu/haarcascade/haarcascade_frontalface_alt.xml')
    parser.add_argument('--eyes_cascade', help='Path to eyes cascade.',
                        default='D:/99-Research/TestData/gpu/haarcascade/haarcascade_eye_tree_eyeglasses.xml')
    parser.add_argument('--input', type=str, help='Path to a video or a sequence of image.',
                        default='D:/99-Research/TestData/image/1.mp4')
    args = parser.parse_args()
    face_cascade_name = args.face_cascade
    eyes_cascade_name = args.eyes_cascade
    face_cascade = lmc_cv.CascadeClassifier()
    eyes_cascade = lmc_cv.CascadeClassifier()
    # 1. load the cascades
    if not face_cascade.load(lmc_cv.samples.findFile(face_cascade_name)):
        print('--(!)Error: loading face cascade!')
        exit(0)
    if not eyes_cascade.load(lmc_cv.samples.findFile(eyes_cascade_name)):
        print('--(!)Error: loading eyes cascade!')
        exit(0)
    # 2. read the video stream
    capture = lmc_cv.VideoCapture(lmc_cv.samples.findFileOrKeep(args.input))
    if not capture.isOpened:
        print('--(!)Error: Unable to open: ' + args.input)
        exit(0)
    while True:
        ret, frame = capture.read()
        if frame is None:
            print('--(!) No captured frame -- Break!')
            break
        lmc_cv_cascade_detect_display(face_cascade, eyes_cascade, frame)
        # quit
        keyboard = lmc_cv.waitKey(1)
        if keyboard == 'q' or keyboard == 27:
            break
    return

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值