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

边栏推荐
- CADD课程学习(4)-- 获取没有晶体结构的蛋白(SWISS-Model)
- Integration of revolution and batch normalization
- 返回二叉树两个节点间的最大距离
- 【Proteus仿真】51单片机+LCD12864推箱子游戏
- RuntimeError: no valid convolution algorithms available in CuDNN
- SharedPreferences save list < bean > to local and solve com google. gson. internal. Linkedtreemap cannot be cast to exception
- The difference between new and make in golang
- 67页新型智慧城市整体规划建设方案(附下载)
- leetcode 650. 2 Keys Keyboard 只有两个键的键盘(中等)
- Convolution和Batch normalization的融合
猜你喜欢

Go basic data type

非路由组件之头部组件和底部组件书写

CDN acceleration requires the domain name to be filed first

LINQ usage collection in C #

JDBC practice cases

【Redis笔记】压缩列表(ziplist)

MySQL Foundation

MarkDown基本语法

Bean load control

Where is the win11 automatic shutdown setting? Two methods of setting automatic shutdown in win11
随机推荐
Writing of head and bottom components of non routing components
JDBC练习案例
判断二叉树是否为满二叉树
第三方支付功能测试点【杭州多测师_王sir】【杭州多测师】
List of major chip Enterprises
Arduino - 字符判断函数
Integration of revolution and batch normalization
All things work together, and I will review oceanbase's practice in government and enterprise industry
Intranet penetration | teach you how to conduct intranet penetration hand in hand
Getting started with golang: for Range an alternative method of modifying the values of elements in slices
Hisilicon VI access video process
sourcetree 详细
Pandora IOT development board learning (HAL Library) - Experiment 3 key input experiment (learning notes)
Makefile configuration of Hisilicon calling interface
抖音实战~点赞数量弹框
How does win11 turn on visual control? Win11 method of turning on visual control
【Proteus仿真】51单片机+LCD12864推箱子游戏
2022 latest and complete interview questions for software testing
BBR encounters cubic
Load balancing cluster (LBC)