当前位置:网站首页>Realization of mask recognition based on OpenCV
Realization of mask recognition based on OpenCV
2022-07-02 23:45:00 【I'm not Xiao Haiwa~~~~】
Then the first step to do , Is to train the classifier we need , I choose OpenCV in ml Modular SVM Classifier to train mask recognition classifier . The code for the training section is as follows :
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);
First read all training images , Contains positive samples ( Wear a mask ) Images and negative samples ( Don't wear a mask ) Images , Then pack the positive and negative sample sets into vector type , Pass in the training function trainSVM() in , This function is defined in the header file “face_mask.h” in .
In the process of training , We are not training by fully unfolding the image , But through feature extraction , Get the image of each sample HOG features , Recalculate each HOG Feature descriptor of feature , Train through feature descriptors SVM classifier .
It should be noted that , We are not doing a complete sample image HOG Feature extraction and description , Instead, the face region of the sample image is extracted first , The extracted face region image is then HOG Feature extraction and description and training .
meanwhile , You also need to label the positive and negative sample sets , Positive samples are marked as 1, Negative samples are marked as -1.
The code is as follows :
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();
The function for face extraction faceDetected() Definition in header file “face.h” in . Here we use opencv_face_detector_uint8.pb Face detection model .
So at this point , It realizes the detection of whether to wear a mask SVM Training of classifiers , The model files obtained from training are as follows :
Next , We are going to load this xml File and detect the input image . among , The function used for detection is FaceMaskDetect(), This function is defined in “face_mask.h” Header file .
auto detecModel = ml::SVM::load("face_mask_detection.xml");
Mat test_image = imread("D:/BaiduNetdiskDownload/ Face mask detection data set /val/test_00004577.jpg");
FaceMaskDetect(test_image, detecModel);
imshow("test_image", test_image);
Come here , We achieved from training , To the process of running detection , Let's see how the operation works :
First look at the image without mask , If it is detected that the mask is not worn , Then the face is framed in red , And marked red “ Not Face Mask ” word :

边栏推荐
- CADD课程学习(4)-- 获取没有晶体结构的蛋白(SWISS-Model)
- 【Proteus仿真】51单片机+LCD12864推箱子游戏
- 高数有多难?AI 卷到数学圈,高数考试正确率 81%!
- (stinger) use pystinger Socks4 to go online and not go out of the network host
- CDN acceleration requires the domain name to be filed first
- RuntimeError: no valid convolution algorithms available in CuDNN
- 万物并作,吾以观复|OceanBase 政企行业实践
- How to maintain the brand influence of clothing enterprises
- [error record] the flutter reports an error (could not resolve io.flutter:flutter_embedding_debug:1.0.0.)
- JDBC練習案例
猜你喜欢

理想汽车×OceanBase:当造车新势力遇上数据库新势力

Dishes launcher small green program and directory management (efficiency tool)

Where is the win11 automatic shutdown setting? Two methods of setting automatic shutdown in win11

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

How does win11 turn on visual control? Win11 method of turning on visual control

How difficult is it to be high? AI rolls into the mathematics circle, and the accuracy rate of advanced mathematics examination is 81%!

Writing of head and bottom components of non routing components
![[Verilog tutorial]](/img/15/d5e188a15e22fa44f1756fc492099d.jpg)
[Verilog tutorial]

2022 latest and complete interview questions for software testing

Container runtime analysis
随机推荐
MySQL Foundation
MarkDown基本语法
vim区间删行注释
Brief introduction to common sense of Zhongtai
Print out mode of go
开发知识点
公司里只有一个测试是什么体验?听听他们怎么说吧
Bean load control
理想汽车×OceanBase:当造车新势力遇上数据库新势力
流媒体技术优化
Where is the win11 automatic shutdown setting? Two methods of setting automatic shutdown in win11
Integration of revolution and batch normalization
[array] binary search
Simple square wave generating circuit [51 single chip microcomputer and 8253a]
PHP get real IP
Remote connection of raspberry pie by VNC viewer
CDN acceleration requires the domain name to be filed first
Fudian bank completes the digital upgrade | oceanbase database helps to layout the distributed architecture of the middle office
"A good programmer is worth five ordinary programmers!"
基于OpenCV实现口罩识别