Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Python-OpenCV enables cat face detection


Jun 01, 2021 Article blog



This article was reproduced to Know ID: Charles (Bai Lu) knows his personal column

Lead

Cat face detection with Python-OpenCV.

It's interesting to use OpenCV's built-in Viola-Jones target detection framework for cat face detection.

Let's start happily


Related documents

Baidu web download link: https://pan.baidu.com/s/1AZ8t49wtG1GpDb4fj7XGQA

Password: v55f


Develop tools

Python version: 3.6.4

Related modules:

cv2 module;

and some Python's own modules.

Environment construction

Install Python and add it to the environment variable, and pip installs the relevant modules that are required.


Introduction to the principle

In order to further improve the quality of the article, I decided to talk briefly about the Haar classifier, the Viola-Jones identifier.

A detailed description of the principles can be found in two papers in the relevant documents:

Rapid Object Detection using a Boosted Cascade of Simple Features;

Robust Real-Time Face Detection.

(1) Haar-like features

Haar-like rectangular features are digital image features for object detection, consisting of two or more adjacent black-and-white rectangles whose characteristic values are the sum of the grayscale values of a white rectangle minus the sum of the grayscale values of the black rectangles. In general, we think rectangular features are sensitive to simple graphical structures (segments, edges), etc.:

 Python-OpenCV enables cat face detection1

Specifically for cat face detection, we believe that placing such a rectangle in a non-cat face area results in a different feature value than being placed in a cat face area.

Using the above feature-based detection algorithm, it is not only possible to encode the state of a specific area, but also more efficient than the pixel-based detection algorithm.

(2) Integral chart

Let's consider how to calculate the characteristic value of a rectangle. For any point A (x, y) in an image, define the integral diagram of that point as the sum of all pixel values in its upper left corner, i.e.:

 Python-OpenCV enables cat face detection2

Therefore, to calculate the feature value of the rectangular template, that is, to calculate the pixels and difference between the two regions, you only need to use the integration diagram of the end of the feature area to perform a simple addition and subtraction operation:

 Python-OpenCV enables cat face detection3

(3) Haar Classifier

Haar classifier is a supervised learning classifier, to carry out target detection, first of all, the image of histogram equalization and normalization processing, and then detect whether it contains objects to be detected.

The flow framework diagram is (Haar classifiers are essentially composed of Haar feature extractors, discrete strong classifiers, and strong classification cascades):

 Python-OpenCV enables cat face detection4

The Haar classifier uses the Adaboost algorithm, but organizes it into a filtered cascading classifier, and in any one-level calculation, once the input is not in the detection class, the calculation is terminated, and only through all levels of the classifier can it be considered to have detected the target object, thus improving detection efficiency.

About the AdaBoost algorithm, I do not expand the introduction, interested students can find their own relevant information to learn. I'll go into more detail when I have time.

(4) The scope of application

Suitable for "basic rigid" object detection, such as face, car, human body and bicycle, etc.

(5) Summary

The core idea of the Viola-Jones target detection framework is to scan the image (multiscale scan) by sliding the window, and then enter the Haar feature values for each window into a filtered cascading classifier to determine whether the window contains a target object for target detection.

The implementation

OpenCV has built-in Haar classifiers based on Viola-Jones target detection frameworks and provides a pre-trained model for cat face detection. So it's easy to implement.

The implementation process is detailed in the source code in the relevant file.

The effect is demonstrated

How to use it:

Modify the picture name in the source code for the picture you need to detect:

 Python-OpenCV enables cat face detection5

Run the DetectCatFace.py file in the cmd window.

effect:

Original figure 1:

 Python-OpenCV enables cat face detection6

Test result 1:

 Python-OpenCV enables cat face detection7

Original figure 2:

 Python-OpenCV enables cat face detection8

Test result 2 (does not distinguish between dogs and cats very well):

 Python-OpenCV enables cat face detection9

That'all~


more

Code as of 2018-06-28 test is correct.

Then will be introduced based on deep learning-based target detection algorithm cases, their detection effect is still very good