当前位置:网站首页>基于OpenCV实现口罩识别
基于OpenCV实现口罩识别
2022-07-02 22:38:00 【我不是萧海哇~~~~】
那么要做的第一步,就是训练出我们需要的分类器,我选用OpenCV中ml模块的SVM分类器来训练口罩识别分类器。训练部分的代码如下:
string positive_path = "D:\\opencv_c++\\opencv_tutorial\\data\\test\\positive\\";
string negative_path = "D:\\opencv_c++\\opencv_tutorial\\data\\test\\negative\\";
vector<string> positive_images_str, negative_images_str;
glob(positive_path, positive_images_str);
glob(negative_path, negative_images_str);
vector<Mat>positive_images, negative_images;
for (int i = 0; i < positive_images_str.size(); i++)
{
Mat positive_image = imread(positive_images_str[i]);
positive_images.push_back(positive_image);
}
for (int j = 0; j < negative_images_str.size(); j++)
{
Mat negative_image = imread(negative_images_str[j]);
negative_images.push_back(negative_image);
}
string savePath = "face_mask_detection.xml";
trainSVM(positive_images, negative_images, savePath);
首先读取所有的训练图像,包含正样本(戴口罩)图像和负样本(不戴口罩)图像,然后分别将正负样本集打包成vector类型,传入训练函数trainSVM()中,这个函数定义在头文件 “face_mask.h” 中。
在训练过程中,我们不是把图像完全展开进行训练,而是通过特征提取,得到每个样本图像的HOG特征,再计算每个HOG特征的特征描述子,通过特征描述子来训练SVM分类器。
要注意的是,我们并不是对完整的样本图像进行HOG特征的提取与描述,而是对样本图像先进行人脸区域的提取,将提取出来的人脸区域图像再进行HOG特征提取与描述并进行训练。
同时,还需要对正负样本集进行标注,正样本标记为1,负样本标记为-1。
代码如下:
for (int i = 0; i < positive_num; i++)
{
Mat positive_face;
Rect positive_faceBox;
if (faceDetected(positive_images[i], positive_face, positive_faceBox))
{
resize(positive_face, positive_face, Size(64, 128));
Mat gray;
cvtColor(positive_face, gray, COLOR_BGR2GRAY);
vector<float> descriptor;
hog_train->compute(gray, descriptor);
train_descriptors.push_back(descriptor);
labels.push_back(1);
}
}
for (int j = 0; j < negative_num; j++)
{
Mat negative_face;
Rect negative_faceBox;
if (faceDetected(negative_images[j], negative_face, negative_faceBox))
{
resize(negative_face, negative_face, Size(64, 128));
Mat gray;
cvtColor(negative_face, gray, COLOR_BGR2GRAY);
vector<float> descriptor;
hog_train->compute(gray, descriptor);
train_descriptors.push_back(descriptor);
labels.push_back(-1);
}
}
int width = train_descriptors[0].size();
int height = train_descriptors.size();
Mat train_data = Mat::zeros(Size(width, height), CV_32F);
for (int r = 0; r < height; r++)
{
for (int c = 0; c < width; c++)
{
train_data.at<float>(r, c) = train_descriptors[r][c];
}
}
auto train_svm = ml::SVM::create();
train_svm->trainAuto(train_data, ml::ROW_SAMPLE, labels);
train_svm->save(path);
hog_train->~HOGDescriptor();
train_svm->clear();
其中进行人脸提取的函数faceDetected()定义在头文件 “face.h” 中。在这里我们使用opencv_face_detector_uint8.pb人脸检测模型。
那么到这一步,就实现了检测是否佩戴口罩的SVM分类器的训练工作,训练得到的模型文件如下:
接下来,我们就要加载这个xml文件并且对输入的图像进行检测啦。其中,检测用的的函数是FaceMaskDetect(),这个函数定义在 “face_mask.h” 头文件中。
auto detecModel = ml::SVM::load("face_mask_detection.xml");
Mat test_image = imread("D:/BaiduNetdiskDownload/人脸口罩检测数据集/val/test_00004577.jpg");
FaceMaskDetect(test_image, detecModel);
imshow("test_image", test_image);
到这里,我们就实现了从训练,到运行检测的过程,下面来看一下运行的效果怎样:
先看下没带口罩的图像,如果检测到没佩戴口罩,那么人脸就用红色框框出,而且标记红色的 “ Not Face Mask ” 字样:
边栏推荐
- Win11自动关机设置在哪?Win11设置自动关机的两种方法
- Why can't the start method be called repeatedly? But the run method can?
- 理想汽车×OceanBase:当造车新势力遇上数据库新势力
- Markdown basic grammar
- 返回二叉树中最大的二叉搜索子树的大小
- 非路由组件之头部组件和底部组件书写
- Interface switching based on pyqt5 toolbar button -2
- JDBC practice cases
- Hisilicon VI access video process
- Where is the win11 microphone test? Win11 method of testing microphone
猜你喜欢
JSON数据传递参数
Master the development of facial expression recognition based on deep learning (based on paddlepaddle)
Highly available cluster (HAC)
Convolution和Batch normalization的融合
【直播预约】数据库OBCP认证全面升级公开课
Dishes launcher small green program and directory management (efficiency tool)
Program analysis and Optimization - 9 appendix XLA buffer assignment
Go basic anonymous variable
BBR encounters cubic
Optimization of streaming media technology
随机推荐
vim区间删行注释
基于Pyqt5工具栏按钮可实现界面切换-2
Makefile configuration of Hisilicon calling interface
[ml] Li Hongyi III: gradient descent & Classification (Gaussian distribution)
[error record] the flutter reports an error (could not resolve io.flutter:flutter_embedding_debug:1.0.0.)
Where is the win11 automatic shutdown setting? Two methods of setting automatic shutdown in win11
The difference between new and make in golang
潘多拉 IOT 开发板学习(HAL 库)—— 实验3 按键输入实验(学习笔记)
Win11启用粘滞键关闭不了怎么办?粘滞键取消了但不管用怎么解决
Print out mode of go
【直播预约】数据库OBCP认证全面升级公开课
【STL源码剖析】仿函数(待补充)
YOLOX加强特征提取网络Panet分析
Tiktok actual combat ~ number of likes pop-up box
Submit code process
JDBC練習案例
Why can't the start method be called repeatedly? But the run method can?
返回二叉树中最大的二叉搜索子树的根节点
Bean load control
[live broadcast appointment] database obcp certification comprehensive upgrade open class