当前位置:网站首页>基于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 ” 字样:

边栏推荐
- JSON data transfer parameters
- The difference between new and make in golang
- 潘多拉 IOT 开发板学习(HAL 库)—— 实验4 串口通讯实验(学习笔记)
- Solution to boost library link error
- 解决:exceptiole ‘xxxxx.QRTZ_LOCKS‘ doesn‘t exist以及mysql的my.cnf文件追加lower_case_table_names后启动报错
- 程序分析与优化 - 9 附录 XLA的缓冲区指派
- YOLOX加强特征提取网络Panet分析
- What if win11 can't turn off the sticky key? The sticky key is cancelled but it doesn't work. How to solve it
- 【Proteus仿真】51单片机+LCD12864推箱子游戏
- 数字图像处理实验目录
猜你喜欢
![[redis notes] compressed list (ziplist)](/img/83/ff38fabb1baebc5fa4d3d72da315dc.png)
[redis notes] compressed list (ziplist)

(毒刺)利用Pystinger Socks4上线不出网主机

Flexible combination of applications is a false proposition that has existed for 40 years

内网渗透 | 手把手教你如何进行内网渗透

【ML】李宏毅三:梯度下降&分类(高斯分布)

How can cross-border e-commerce achieve low-cost and steady growth by laying a good data base

JDBC练习案例

Go project operation method

YOLOX加强特征提取网络Panet分析

Compose 中的 'ViewPager' 详解 | 开发者说·DTalk
随机推荐
Boost库链接错误解决方案
JSON数据传递参数
Warning: implicitly declaring library function 'printf' with type 'int (const char *,...)‘
YOLOX加强特征提取网络Panet分析
What is the official website address of e-mail? Explanation of the login entry of the official website address of enterprise e-mail
List of major chip Enterprises
返回二叉树中最大的二叉搜索子树的大小
The concepts of terminal voltage, phase voltage and line voltage in FOC vector control and BLDC control are still unclear
Catalogue of digital image processing experiments
Ideal car × Oceanbase: when the new forces of car building meet the new forces of database
容器运行时分析
【STL源码剖析】仿函数(待补充)
Win11启用粘滞键关闭不了怎么办?粘滞键取消了但不管用怎么解决
cocospods 的使用
万物并作,吾以观复|OceanBase 政企行业实践
Speech recognition Series 1: speech recognition overview
Difference between NVIDIA n card and amda card
Optimization of streaming media technology
【Proteus仿真】51单片机+LCD12864推箱子游戏
基于Pyqt5工具栏按钮可实现界面切换-1