当前位置:网站首页>Face recognition sample code analysis (2) - face recognition analysis
Face recognition sample code analysis (2) - face recognition analysis
2022-08-09 17:09:00 【LayeZhao】
In the previous article, we initially analyzed the following sample codefacedetectorThe program parameter parsing section of .The process of parameter parsing is as follows:
其中第4Steps marked in yellowcascadeName,are the main parameters of our face recognition classifier.
在openCV中,实现了著名的haar特征检测算法,And rely on the algorithm classifier,We can achieve face recognition、定位.
The algorithm calculates image features based on the sliding of feature templates,Thereby distinguishing the salient contours of objects.
These feature templates look like the following image:
Just imagine using these black and white binary images”蒙”on your image or part of it,Then your original image is split into“黑”“白”两类区域,Finally do a simple addition and subtraction with the pixels in the black and white areas,The basic features of the image can be obtained.
例如,We will template the window(3)“蒙”in the nose area,matching characteristics can be obtained,Move the template window(4)Put it on the eyes,It is also possible to obtain features that fit the parts of the eyes.
But how to determine the size of the feature template window according to different images,and where they should be placed.比如上图,How do we know which will be a specific size“上黑下白”Feature templates are fixed at the height of the image3/4上?
答案就是,We let computers that don't work tirelessly,Test various positions and sizes.
这是一个巨大的工作量,First of all, if we determine the specific feature template window size is 24,24.Then we have to divide the image to be detected into several24,24大小区域,Try to detect with different feature templates respectively*Whether the area image is“匹配”a feature,Then the number of matches in a single window is greater than160000,This will still be a huge amount of work.
But if the cascade classifier is introduced——Cascade Classifiers,Then the workload will be greatly reduced.
Take face detection as an example,Use some features first,Determine the area image“有脸”以及“无脸“,丢掉”无脸“部分,Then use other features to detect,to further determine whether the feature exists,and determine its characteristic location.
This process allows our process of matching features to produce a “级联“的工作模式,Go down each floor,就会”丢掉“some unrelated areas,This way the features become more and more precise.
We can see from the above process,A single feature alone does not determine the final result,Rather, it is the cascading combination of multiple features that ultimately enables us to make an accurate judgment.
We call the detection of a single feature a weak classifier(Because it cannot determine the final result alone),The final classifier formed by cascading these weak classifiers becomes a strong classifier.It can be seen that the final strong classifier is constructed on the basis of the ensemble algorithm.
好吧,言归正传,openCVImplemented this strong classifier,用CascadeClassifierClass encapsulates this classifier.You can instantiate objects of this class,and design a training program,to train the desired feature detector.
For face detection,openCVThe classifier has already been trained for us,The parameter data for this classifier is stored in a xml文件中.
在实际使用过程中,The path to this file will be passed as an important parameter along with the detected imageCascadeClassifier对象,Then locate the face in the detected image.
And in the previous one,The parsed parameterscascadeName,It is this file path
我们接着上一篇,继续往下做,得到cascadeName后,构造CascadeClassifier对象”cascade”.
然后利用classifierPerform face prediction on the detected image.
同样,We simplify the source code,Only a single image is detected this time.
//According to the classifier template pathcascadeNameConstruct cascade classifiers(CascadeClassifier)对象"cascade"
cv::CascadeClassifier cascade(cascadeName);
//Read in the detected image
Mat image = imread("D:/试验/test/baby.png");
//Define a grayscale image,Used to temporarily store the grayscale image corresponding to the original image.
Mat gray;
//Convert the original detected image from a true color image to a grayscale image
cvtColor(image, gray, cv::COLOR_BGR2GRAY);
//定义一个向量(数组),Used to store the detected face location(注意:There may not be just one face)
vector<Rect> faces;
//启动检测
cascade.detectMultiScale(gray,faces);
//Draw the result of the face position with a red rectangle
for (int i = 0; i < faces.size(); i++)
{
cv::rectangle(image, faces[i], Scalar(255, 0, 0));
}
//The show was finally taken“标记”的图像
imshow("检测人脸", image);
//Wait for the user to enter any key to end the program
waitKey();
运行结果:
Finally post about the classCasadeClassifier的官方文档解释,This blog post is mostly a summary of the documenthttps://docs.opencv.org/3.4/db/d28/tutorial_cascade_classifier.html
边栏推荐
猜你喜欢
随机推荐
【深度学习】attention机制
【SQL】595. 大的国家
【力扣】516. 最长回文子序列
堆(heap)系列_0x03:堆块 + malloc/new底层 + LFH(WinDbg分析)
Vim实用技巧_4.管理多个文件(打开 + 切分 + 保存 + netrw)
【力扣】1995. 统计特殊四元组
hugging face tutorial - Chinese translation - share a model
[Deep Learning] Original Problem and Dual Problem (6)
深入浅出最优化(7) 罚函数法
图解转置卷积原理
【深度学习】SVM解决线性不可分情况(八)
Visual Studio 2019新手使用(安装并创建第一个程序详细教程)
【力扣】128. 最长连续序列
NLP-阅读理解任务学习总结概述
QNX 7.1 交叉编译 boost 1.76
【原理+源码详细解读】从Transformer到ViT
【研究生工作周报】(第十二周)
大唐杯5G练习题(二)
深度神经网络中的多任务学习研究综述
go语言基础学习(一起学习go语言)