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

边栏推荐
- Maybe you read a fake Tianlong eight
- Brief introduction to common sense of Zhongtai
- Warning: implicitly declaring library function 'printf' with type 'int (const char *,...)‘
- Catalogue of digital image processing experiments
- 一文掌握基于深度学习的人脸表情识别开发(基于PaddlePaddle)
- 容器运行时分析
- PHP get real IP
- [error record] the flutter reports an error (could not resolve io.flutter:flutter_embedding_debug:1.0.0.)
- Solution to boost library link error
- 潘多拉 IOT 开发板学习(HAL 库)—— 实验3 按键输入实验(学习笔记)
猜你喜欢

Wechat applet basic learning (wxss)

RuntimeError: no valid convolution algorithms available in CuDNN

ADC of stm32

抖音实战~点赞数量弹框

CDN 加速,需要域名先备案

RuntimeError: no valid convolution algorithms available in CuDNN

数字图像处理实验目录

Maybe you read a fake Tianlong eight

【STL源码剖析】仿函数(待补充)

Remote connection of raspberry pie by VNC viewer
随机推荐
RuntimeError: no valid convolution algorithms available in CuDNN
Data set - fault diagnosis: various data and data description of bearings of Western Reserve University
How to maintain the brand influence of clothing enterprises
leetcode 650. 2 Keys Keyboard 只有两个键的键盘(中等)
Tiktok actual combat ~ number of likes pop-up box
Golang common settings - modify background
基于Pyqt5工具栏按钮可实现界面切换-2
Program analysis and Optimization - 9 appendix XLA buffer assignment
What experience is there only one test in the company? Listen to what they say
LINQ usage collection in C #
【直播预约】数据库OBCP认证全面升级公开课
Difference between NVIDIA n card and amda card
内网渗透 | 手把手教你如何进行内网渗透
Yolox enhanced feature extraction network panet analysis
潘多拉 IOT 开发板学习(HAL 库)—— 实验3 按键输入实验(学习笔记)
Interface switching based on pyqt5 toolbar button -1
抖音实战~点赞数量弹框
第三方支付功能测试点【杭州多测师_王sir】【杭州多测师】
JSON数据传递参数
Win11自动关机设置在哪?Win11设置自动关机的两种方法