当前位置:网站首页>Opencv + clion face recognition + face model training
Opencv + clion face recognition + face model training
2022-04-23 04:45:00 【Vivid_ Mm】
OpenCV windows Version compilation +CLion Project import reference :
CLion+OpenCV Identification ID number --- Identification card number _xxwbwm The blog of -CSDN Blog
Code :
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
class CascadeDetectorAdapter : public DetectionBasedTracker::IDetector {
public:
CascadeDetectorAdapter(cv::Ptr<cv::CascadeClassifier> detector) :
IDetector(),
Detector(detector) {
CV_Assert(detector);
}
void detect(const cv::Mat &Image, std::vector<cv::Rect> &objects) {
Detector->detectMultiScale(Image, objects, scaleFactor, minNeighbours, 0, minObjSize, maxObjSize);
}
~CascadeDetectorAdapter() {
}
private:
CascadeDetectorAdapter();
cv::Ptr<cv::CascadeClassifier> Detector;
};
DetectionBasedTracker *getTracker() {
// OpenCV Built in model location F:\opencvWin\opencv\build\etc\lbpcascades
String path = "F:\\opencvWin\\opencv\\build\\etc\\lbpcascades\\lbpcascade_frontalface.xml";
// String path = "F:\\opencvWin\\facetrain\\samples\\data\\cascade.xml";// Can't recognize
Ptr<CascadeClassifier> classifier = makePtr<CascadeClassifier>(path);
// Adapter
Ptr<CascadeDetectorAdapter> mainDetector = makePtr<CascadeDetectorAdapter>(classifier);
Ptr<CascadeClassifier> classifier1 = makePtr<CascadeClassifier>(path);
// Adapter
Ptr<CascadeDetectorAdapter> trackingDetector = makePtr<CascadeDetectorAdapter>(classifier1);
// tracker
DetectionBasedTracker::Parameters DetectorParams;
DetectionBasedTracker *tracker = new DetectionBasedTracker(mainDetector, trackingDetector, DetectorParams);
return tracker;
}
int main() {
DetectionBasedTracker *tracker = getTracker();
// Turn on the tracker
tracker->run();
// Get camera data
VideoCapture capture(0);
Mat Sourceimg;
Mat gray;
Mat test;
while (1) {
capture >> Sourceimg;
// Gray image processing
cvtColor(Sourceimg, gray, COLOR_BGR2GRAY);
// Enhance contrast ( Histogram equalization )
equalizeHist(gray, gray);
// Create and save a vector set of detected faces
std::vector<Rect> faces;
// Gray image recognition processing
tracker->process(gray);
// To get the results
tracker->getObjects(faces);
for (Rect face : faces) {
// Assign separately bgra
if (face.x < 0 || face.width < 0 || face.x + face.width > Sourceimg.cols ||
face.y < 0 || face.height < 0 || face.y + face.height > Sourceimg.rows) {
continue;
}
// Draw the frame in the original picture
rectangle(Sourceimg, face, Scalar(255, 0, 255));
#if 0
int i = 0;
while (true){
// Make positive samples
Mat m;
// hold img Copy the face part in to m in
Sourceimg(face).copyTo(m);
// Put the face Reset to 24x24 Size picture
resize(m, m, Size(24, 24));
// Converted to grayscale
cvtColor(m, m, COLOR_BGR2GRAY);
char p[200];
sprintf(p, "F:/opencvWin/facetrain/samples/vivid/%d.jpg", i++);
// hold mat Write as jpg file
imwrite(p, m);
m.release();
if(i == 100){
break;
}
}
#endif
}
imshow("camera", Sourceimg);
//27 == ESC wait for 30 MS exit
if (waitKey(30) == 27) {
break;
}
}
if (!Sourceimg.empty()) Sourceimg.release();
if (!gray.empty()) gray.release();
capture.release();
tracker->stop();
delete tracker;
return 0;
}
Loaded above lbpcascade_frontalface.xml The face model is opencv The source code comes with , We can use this model to train our own face model , Next, I will introduce training my own face model , Open this code , When we recognize the human face, we save the face information into a size of 24*24( Pixels ) Pictures of the , keep in storage 100 Zhang , reason : I only 300 Negative sample picture ( Do not include face )
#if 0
int i = 0;
while (true){
// Make positive samples
Mat m;
// hold img Copy the face part in to m in
Sourceimg(face).copyTo(m);
// Put the face Reset to 24x24 Size picture
resize(m, m, Size(24, 24));
// Converted to grayscale
cvtColor(m, m, COLOR_BGR2GRAY);
char p[200];
sprintf(p, "F:/opencvWin/facetrain/samples/vivid/%d.jpg", i++);
// hold mat Write as jpg file
imwrite(p, m);
m.release();
if(i == 100){
break;
}
}
#endif
Positive sample :
Sort the positive sample information into xxx.xxx( Any file name , Any file suffix ) I saved it here to vivid.data file
vivid.data The contents of the document :
vivid/0.jpg 1 0 0 24 24
vivid/1.jpg 1 0 0 24 24
vivid/2.jpg 1 0 0 24 24
vivid/3.jpg 1 0 0 24 24
vivid/4.jpg 1 0 0 24 24
vivid/5.jpg 1 0 0 24 24
vivid/6.jpg 1 0 0 24 24
vivid/7.jpg 1 0 0 24 24
vivid/8.jpg 1 0 0 24 24
vivid/9.jpg 1 0 0 24 24
Parameter meaning :
vivid/x.jpg Sample location ( The full path is :F:\opencvWin\facetrain\samples\vivid\x.jpg)
1 It means there is only one face
0 0 Indicates the starting position of the face
24 24 Represents the size of the face , That is, the end position
If one picture has more than one face ,eg:2 Zhang Renren
vivid/0.jpg 2 0 0 50 50 80 80 130 130
0 0 50 50 The starting and ending point of the first face ,80 80 130 130 The starting and ending point of the second face
Writing of document contents , Can be done in code :
import java.io.*;
public class GeneateFile{
public static void main(String[] args) throws Exception{//FileOutputStream
FileOutputStream fos = new FileOutputStream("F:/opencvWin/facetrain/samples/vivid/vivid.data");
for(int i =0 ;i<100;i++){
String content = String.format("vivid/%d.jpg 1 0 0 24 24\n",i);
fos.write(content.getBytes());
}
fos.close();
}
}
take vivid.data File conversion to vec Sample file
opencv_createsamples -info vivid.data -vec vivid.vec -num 100 -w 24 -h 24
-info: Positive sample description
-vec : Output positive sample file
-num : Number of positive samples
-w -h: The size of the output sample
opencv_createsamples -info vivid.data -vec vivid.vec -num 100 -w 24 -h 24
result :
F:\opencvWin\facetrain\samples>opencv_createsamples -info vivid.data -vec vivid.vec -num 100 -w 24 -h 24
Info file name: vivid.data
Img file name: (NULL)
Vec file name: vivid.vec
BG file name: (NULL)
Num: 100
BG color: 0
BG threshold: 80
Invert: FALSE
Max intensity deviation: 40
Max x angle: 1.1
Max y angle: 1.1
Max z angle: 0.5
Show samples: FALSE
Width: 24
Height: 24
Max Scale: -1
Create training samples from images collection...
Done. Created 100 samples
F:\opencvWin\facetrain\samples>
The above display indicates success .
Modify generation vivid.data Of java Code , Generating negative samples bg.data file
import java.io.*;
public class GeneateFileOpp{
public static void main(String[] args) throws Exception{//FileOutputStream
FileOutputStream fos = new FileOutputStream("F:/opencvWin/facetrain/samples/bg/bg.data");
for(int i =0 ;i<300;i++){
String content = String.format("bg/%d.jpg\n",i);
fos.write(content.getBytes());
}
fos.close();
}
}
bg.data The contents of the document ( Omit the part ): Note that there is no face information
bg/0.jpg
bg/1.jpg
bg/2.jpg
bg/3.jpg
bg/4.jpg
bg/5.jpg
bg/6.jpg
bg/7.jpg
bg/8.jpg
bg/9.jpg
bg/10.jpg
Training :
opencv_traincascade -data data -vec vivid.vec -bg bg.data -numPos 100 -numNeg 300 -numStages
15 -featureType LBP -w 24 -h 24
-data : The target directory needs to be created manually , Used to store the generated model , The name can be customized
-vec : Positive sample
-bg : Negative sample
-numPos : The number of positive samples used in the training of each classifier
-numNeg : The number of negative samples used in the training of each classifier , Can be greater than -bg number
-numStages: Train the number of classifiers , If there are many layers , The error of classifier is smaller , But the detection speed is slow .(15-20)
-featureType: LBP
-w -h
Execution success results :
Training until now has taken 0 days 0 hours 0 minutes 18 seconds.
===== TRAINING 7-stage =====
<BEGIN
POS count : consumed 100 : 100
NEG count : acceptanceRatio 0 : 0
Required leaf false alarm rate achieved. Branch training terminated.
At this time in data The directory will have the following files :
Modify the above model loading path , You can check whether the model is valid , The operation is as follows :
版权声明
本文为[Vivid_ Mm]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220558084086.html
边栏推荐
- QML进阶(五)-通过粒子模拟系统实现各种炫酷的特效
- MySQL time function query
- Spark optimization
- Summary of Android development posts I interviewed in those years (attached test questions + answer analysis)
- 基于英飞凌MCU GTM模块的无刷电机驱动方案开源啦
- PIP3 installation requests Library - the most complete pit sorting
- win10, mysql-8.0.26-winx64.zip 安装
- Windows remote connection to redis
- Unity3D 实用技巧 - 理论知识库(一)
- /etc/bash_ completion. D directory function (the user logs in and executes the script under the directory immediately)
猜你喜欢
那些年我面试过的Android开发岗总结(附面试题+答案解析)
New terminal play method: script guidance independent of technology stack
Unity rawimage background seamlessly connected mobile
基于英飞凌MCU GTM模块的无刷电机驱动方案开源啦
MySQL queries users logged in for at least N consecutive days
[timing] empirical evaluation of general convolution and cyclic networks for sequence modeling based on TCN
IDE Idea 自动编译 与 On Upate Action 、 On Frame Deactivation 的配置
Ali's ten-year technical experts jointly created the "latest" jetpack compose project combat drill (with demo)
Use recyclerview to realize left-right side-by-side classification selection
Druid -- JDBC tool class case
随机推荐
Installation du compilateur croisé de la plateforme zynq
Differences among electric drill, electric hammer and electric pick
JS generates a specified number of characters according to some words
Bridge between ischemic stroke and intestinal flora: short chain fatty acids
No such file or directory problem while executing shell
重剑无锋,大巧不工
Eksctl deploying AWS eks
解决ValueError: Argument must be a dense tensor: 0 - got shape [198602], but wanted [198602, 16].
Apache Bench(ab 压力测试工具)的安装与使用
Leetcode003 -- judge whether an integer is a palindrome number
Youqilin 22.04 lts version officially released | ukui 3.1 opens a new experience
Mysql50 basic exercises
KVM error: Failed to connect socket to ‘/var/run/libvirt/libvirt-sock‘
Code007 -- determine whether the string in parentheses matches
[timing] empirical evaluation of general convolution and cyclic networks for sequence modeling based on TCN
The unity camera rotates with the mouse
QML advanced (V) - realize all kinds of cool special effects through particle simulation system
A heavy sword without a blade is a great skill
Open the past and let's start over.
QML advanced (IV) - drawing custom controls