Model Model::create(vector<Alumno *> students){ ostringstream s, s2; QString sDir = QFileDialog::getSaveFileName(0, "Nombre Modelo", "/home"); s << "INSERT INTO Model(" "directory) VALUES('" << sDir.toStdString().c_str() << "');"; QSqlQuery query(QString(s.str().c_str())); cout << s.str() << endl; int idModel = query.lastInsertId().toInt(); s2 << "INSERT INTO ModelStudent(" "id_student," "id_model) VALUES"; system("echo '' > FaceRecognition/modelos.csv"); for(int i = 0; i < students.size(); i++){ string com ="bash createCSV.sh " + students[i]->getDirectory() + " " + to_string(students[i]->getId()) + " >> FaceRecognition/modelos.csv"; cout << com << endl; system(com.c_str()); s2 << "(" << students[i]->getId() << ", " << idModel << ")"; if(i == students.size() - 1){ s2 << ";"; } else s2 << ", \n"; } cout << s2.str() << endl; QSqlQuery query2(QString(s2.str().c_str())); vector<Mat> images; vector<int> labels; try{ read_csv("FaceRecognition/modelos.csv", images, labels); }catch (cv::Exception& e) { cerr << "ERROR OPENING CSV FILE" << endl; exit(1); } for(int i = 0; i < images.size(); i++){ equalizeHist(images[i], images[i]); cv::resize(images[i], images[i], Size(48,48)); } Model model = Model(createEigenFaceRecognizer(0, 3000)); model->train(images, labels); model->save(sDir.toStdString()); model.setId(idModel); model.setDirectory(sDir.toStdString()); model.setStudents(students); cout << "END" << endl; return model; }
Ptr<FaceRecognizer> gender_detection(string fn_csv) { string output_folder; //string fn_csv = string("/Users/xueqianjiang/Desktop/male.txt"); // These vectors hold the images and corresponding labels. vector<Mat> images; vector<int> labels; // Read in the data. This can fail if no valid // input filename is given. try { read_csv(fn_csv, images, labels); } catch (Exception& e) { cerr << "Error opening file \"" << fn_csv << "\". Reason: " << e.msg << endl; // nothing more we can do exit(1); } if(images.size() <= 1) { string error_message = "This demo needs at least 2 images to work. Please add more images to your data set!"; CV_Error(CV_StsError, error_message); } // this part will add in the EigenFaceRecognizer in order to in reduce the dimension further // int height = images[0].rows; //PCA model //Ptr<FaceRecognizer> model = createEigenFaceRecognizer(); //train the PCA model and without loss of information //model->train(images, labels); //save the results of the train // model->save("eigenface.yml"); //take out the feature values from the eigenfaces and then rank them from largest to smallest //Mat eigenvalues = model->getMat("eigenvalues"); //take out the feature values from the model,eigenvectures and the eigenvalues are in the same order //Mat W = model->getMat("eigenvectors"); //打算保留前121个特征向量,代码中没有体现原因,但选择121是经过斟酌的,首先,在我的实验中,"前121个特征值之和/所有特征值总和>0.97";其次,121=11^2,可以将结果表示成一个11*11的2维图像方阵,交给fisherface去计算 // int xth = 121; //after PCA //vector<Mat> reduceDemensionimages; //choose the first xth eigenvalues and get rid of the rest // Mat evs = Mat(W, Range::all(), Range(0, xth)); //for(int i=0;i<images.size();i++) //{ // Mat mean= model->getMat("mean"); //subspaceProjection //Mat projection = subspaceProject(W, mean, images[i].reshape(1,1)); //reduced dimensionality and save them //reduceDemensionimages.push_back(projection.reshape(1,sqrt(xth))); // } // Quit if there are not enough images for this demo. // Ptr<FaceRecognizer> fishermodel = createFisherFaceRecognizer(); //fishermodel->train(reduceDemensionimages,labels); Ptr<FaceRecognizer> model = createFisherFaceRecognizer(); model->train(images, labels); return model; }
haar_cascade::haar_cascade(const string& haar_path, const string & csv_path):haar_path(haar_path),csv_path(csv_path) { read_csv(this->csv_path,this->images,this->labels); }