- Có thể lưu trữ thông tin quá trình nhận dạng được vào thiết bị lưu trữ Ứng dụng hệ thống nhận dạng khuôn mặt trong thực tiễn như điều khiển
PHỤ LỤC MÃ NGUỐN CHƯƠNG TRÌNH
MÃ NGUỐN CHƯƠNG TRÌNH #include "opencv2/core/core.hpp" #include "opencv2/contrib/contrib.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/objdetect/objdetect.hpp" #include "/home/pi/pierre/libfacerec-0.04/include/facerec.hpp" #include <iostream> #include <fstream> #include <sstream> #include "time.h" using namespace cv; using namespace std;
// so nguoi dua vao huan luyen+ id cua moi nguoi #define MAX_PEOPLE 4
#define TUAN 0
#define LANG 1 #define LONG 2 #define PHUONG 3 #define TRACE 1
#define DEBUG_MODE 0
#define DEBUG if (DEBUG_MODE==1) // load thuat toan haar-cascades string glasses_cascade_name = "/usr/share/opencv/haarcascades/haarcascade_eye_tree_eyeglasses.xml"; string eyes_cascade_name = "/usr/share/opencv/haarcascades/haarcascade_eye.xml"; CvPoint Myeye_left; CvPoint Myeye_right; CascadeClassifier eyes_cascade; CascadeClassifier glasses_cascade; // ten cua nguoi
string people[MAX_PEOPLE]; int nbSpeak[MAX_PEOPLE]; int bHisto;
// so buc anh duoc hoc voi mot nguoi int nPictureById[MAX_PEOPLE]; ///////////////////////////////////////////////// // trace if TRACE==1 ///////////////////////////////////////////////// void trace(string s) { if (TRACE==1) { cout<<s<<"\n"; } } ///////////////////////////////////////////////// // // doc file csv. // ////////////////////////////////////////////////
static void read_csv(const string& filename, vector<Mat>& images, vector<int>& labels, char separator = ';') {
std::ifstream file(filename.c_str(), ifstream::in); if (!file) {
string error_message = "(E) khong load duoc file anh."; CV_Error(CV_StsBadArg, error_message);
}
string line, path, classlabel; int nLine=0;
while (getline(file, line)) { stringstream liness(line);
getline(liness, path, separator); getline(liness, classlabel);
if(!path.empty() && !classlabel.empty()) {
images.push_back(imread(path, 0)); 65
labels.push_back(atoi(classlabel.c_str())); nPictureById[atoi(classlabel.c_str())]++; nLine++; } }
// dua ra so anh dao tao voi moi nguoi // (what a pity)
char sTmp[128];
sprintf(sTmp,"(init) %d so anh dua vao dao tao",nLine); trace((string)(sTmp));
for (int j=0;j<MAX_PEOPLE;j++) {
sprintf(sTmp,"(init) %d anh cua %s (%d) duoc dao tao",nPictureById[j],people[j].c_str(),j);
trace((string)(sTmp)); }
}
///////////////////////////////////////////////// void traceStep(int nStep)
{ cout<<"("<<nStep<<")"<<clock()<<"\n"; } /////////////////////////////////////////////////// // // main // /////////////////////////////////////////////////// int main(int argc, const char *argv[]) {
// kiem tra thong so nhap vao cout<<"start\n";
if ((argc != 4)&&(argc!=3)) {
cout << "usage: " << argv[0] << " ext_files seuil(opt) \n files.ext histo(0/1) 5000 \n" << endl;
exit(1); }
int PREDICTION_SEUIL ;
// thiet lap gia tri mac dinh cho muc nguong = gia tri toi //thieu thi nhan dang
if (argc==3) { trace("(init) prediction treeshold = 4500.0 by default");PREDICTION_SEUIL = 4500.0;}
if (argc==4) PREDICTION_SEUIL = atoi(argv[3]); // Lua chon can bang mau sac hay khong ? bHisto=atoi(argv[2]);
// Nhan dang mat
if( !eyes_cascade.load( eyes_cascade_name ) ){ printf("-- (!)Error loading\n"); return -1; };
if( !glasses_cascade.load( glasses_cascade_name ) ){ printf("-- (!)Error loading\n"); return -1; };
//
// init people //
Mat gray, frame,original,face,face_resized; //
// Hien thi thong tin nguoi nhan dang. people[TUAN] = "TUAN";
people[LANG] = "LANG"; people[LONG] = "LONG"; people[PHUONG] = "PHUONG"; for (int i=0;i>MAX_PEOPLE;i++) {
nbSpeak[i] =0; nPictureById[i]=0; }
int bFirstDisplay =1;
trace("(init) People initialized"); string fn_csv = string(argv[1]); string fn_haar = "/usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml"; DEBUG cout<<"(OK) csv="<<fn_csv<<"\n"; vector<Mat> images; vector<int> labels;
// Kiem tra file co so du lieu try {
read_csv(fn_csv, images, labels);
DEBUG cout<<"(OK) read CSV ok\n"; }
catch (cv::Exception& e) {
cerr << "Error opening file \"" << fn_csv << "\". Reason: " << e.msg << endl;
exit(1); }
// lay kich thuoc cua anh dau tien--> tat ca kich thuoc cua cac anh phai giong nhau
int im_width = images[0].cols; int im_height = images[0].rows;
trace("(init) taille images ok"); //
// tao lop nhan dang khuon mat va dao tao anh //
Eigenfaces model;
// Tien hanh hoc anh tu co so du lieu trace("(init) start train images"); model.train(images, labels);
trace("(init) train images : ok"); 67
CascadeClassifier face_cascade; if (!face_cascade.load(fn_haar))
{
cout <<"(E) face cascade model khong load thanh cong :"+fn_haar+"\n";
return -1;
}
trace("(init) Load modele : ok");
// hien thi video CvCapture* capture;
capture = cvCaptureFromCAM(-1);
// dat kich thuoc cho webcam 320x240
cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH,320); cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, 240); trace("(init) webcam initialized : ok");
if (!capture) {
cout << "(E) Khong load duoc webcam." << endl; return -1; } int nFirst=0; for(;;) {
// chup anh tu webcam
original= cvQueryFrame( capture); char key;
// chuyen anh mau thanh anh xam cvtColor(original, gray, CV_BGR2GRAY);
// Bieu do can bang
if (bHisto)equalizeHist( gray, gray); vector< Rect_<int> > faces;
// phat hien khuon mat
face_cascade.detectMultiScale(gray, faces, 1.1, 3, CV_HAAR_SCALE_IMAGE, Size(60,60));
// voi moi khuon mat da tim duoc
for(int i = 0; i < faces.size(); i++)
{
// cat lay phan khuon mat
Rect face_i = faces[i]; face = gray(face_i);
// chinh sua lai kich thuoc va hien thi anh cv::resize(face, face_resized, Size(im_width, im_height), 1.0, 1.0, INTER_CUBIC);
imshow("face", face_resized);
if (bFirstDisplay) // cho phep nhieu thoi gian o lan hien thi dau tien
{
key = (char) waitKey(100); bFirstDisplay = 0;
} else {
key = (char) waitKey(10); }
nFirst++;
// O buoc nay khuon mat da duoc phat hien // Bay gio co gang nhan dang ho la ai ? char sTmp[256];
double predicted_confidence = 0.0;
int prediction = -1;
model.predict(face_resized,prediction,predicted_confidence); // Tao hinh chu nhat bao quanh khuon mat nhan dang duoc rectangle(original, face_i, CV_RGB(0, 255 ,0), 1); if (predicted_confidence>PREDICTION_SEUIL) { // trace sprintf(sTmp,"+ prediction ok = %s (%d) confiance = (%d)",people[prediction].c_str(),prediction, (int)predicted_confidence); trace((string)(sTmp));
// Hien thi ten nguoi duoc nhan dang string box_text; if (prediction<MAX_PEOPLE) { box_text = "Id="+people[prediction]; } else { trace("(E) prediction id incohA©rent"); }
int pos_x = std::max(face_i.tl().x - 10, 0);
int pos_y = std::max(face_i.tl().y - 10,
0);
putText(original, box_text, Point(pos_x, pos_y), FONT_HERSHEY_PLAIN, 1.0, CV_RGB(0,255,0), 1.0);
/// Hien thi ket qua: imshow("Reco", original); key = (char) waitKey(10);
saySomething(prediction); }
else 69
{
sprintf(sTmp,"- prediction too low = %s (%d) confiance = (%d)",people[prediction].c_str(),prediction, (int)predicted_confidence); trace((string)(sTmp)); } } imshow("Reco", original);
// And display it:
key = (char) waitKey(100); }
return 0; }