-
Notifications
You must be signed in to change notification settings - Fork 1
/
recognizer.cpp
68 lines (61 loc) · 1.75 KB
/
recognizer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "recognizer.h"
Recognizer *Recognizer::instance;
Recognizer::Recognizer()
{
if (access("haarcascade_frontalface_default.xml", F_OK) != 0) {
throw std::runtime_error("Face detector model doesn't exists.");
}
this->detector = new CascadeClassifier();
this->detector->load("haarcascade_frontalface_default.xml");
if (access(this->modelPath.c_str(), F_OK) == 0) {
struct stat statbuf;
stat(this->modelPath.c_str(), &statbuf);
if (statbuf.st_size > 100) {
this->modelTrained = true;
}
this->recoginzer = LBPHFaceRecognizer::create();
this->recoginzer->read(this->modelPath);
} else {
this->recoginzer = LBPHFaceRecognizer::create();
}
}
Mat Recognizer::faceDetect(Mat inp) {
vector<Rect> faceAreas;
detector->detectMultiScale(inp, faceAreas);
if (faceAreas.size() < 1) {
return Mat();
} else {
Rect face = faceAreas.at(0);
return inp(face);
}
}
int Recognizer::recognize(Mat face, double &confidence) {
if (!this->modelTrained) {
return -1;
}
int sid;
this->recoginzer->predict(face, sid, confidence);
if (confidence > 0.8) {
return -1;
}
return sid;
}
void Recognizer::trainModel(vector<Mat> faces, vector<int> ids) {
if (this->modelTrained) {
this->recoginzer->update(faces, ids);
} else {
this->recoginzer->train(faces, ids);
this->modelTrained = true;
}
}
Recognizer::~Recognizer () {
this->recoginzer->save(this->modelPath);
delete recoginzer;
delete detector;
}
Recognizer *Recognizer::getInstance() {
if (Recognizer::instance == nullptr) {
Recognizer::instance = new Recognizer();
}
return Recognizer::instance;
}