void TabListeEleve::addColumn() { Tree<Donnee> tree(bdd()->getArbre<Donnee>(1)); SelectDonneeDialog dial(tree,this); dial.exec(); Donnee dn(dial.result()); if(bdd()->get(dn)) ((ListeEleveModel *) m_model)->insertColumn(dn); }
/** * \fn void trainBdd(std::string bddName, int dim, int maxPts, int k) * \brief Trains the specified BDD. * * \param[in] bddName The name of the BDD. * \param[in] dim The dimension of the STIPs. * \param[in] maxPts The maximum number of points we want to compute. * \param[in] k The number of cluster (means). */ void im_train_bdd(std::string bddName, int k){ std::string path2bdd("bdd/" + bddName); std::string KMeansFile(path2bdd + "/" + "training.means"); //std::cout << path2bdd << std::endl; //int desc = getDescID(path2bdd); //int dim = getDim(desc); // Loading the DenseTrack settings // Loading BDD IMbdd bdd(bddName,path2bdd); bdd.load_bdd_configuration(path2bdd.c_str(),"imconfig.xml"); // In im_train_bdd all the people are the training people !! std::vector<std::string> trainingPeople = bdd.getPeople(); // Loading activities std::vector<std::string> activities = bdd.getActivities(); int nrActivities = activities.size(); int labels[nrActivities]; int index = 0; for(std::vector<std::string>::iterator activity = activities.begin(); activity != activities.end(); ++activity){ labels[index] = index + 1; index++; } if(k%nrActivities != 0){ std::cerr << "k is not divisible by nrActivities !" << std::endl; exit(EXIT_FAILURE); } // Saving KMeans settings bdd.changeKMSettings("specifical", k, "training.means"); im_create_specifics_training_means(bdd, trainingPeople); // SVM train MatrixC trainMC = MatrixC(nrActivities,labels); std::vector<std::string> testingPeople; MatrixC testMC = MatrixC(nrActivities, labels); double crossValidationAccuracy = im_svm_train(bdd, trainingPeople, trainMC, testingPeople, testMC); trainMC.calculFrequence(); trainMC.exportMC(path2bdd,"training_confusion_matrix.txt"); bdd.write_bdd_configuration(path2bdd.c_str(),"imconfig.xml"); std::cout << "Resume of the training phase:" << std::endl; bdd.show_bdd_configuration(); std::cout << "Train recognition rate:" << std::endl; std::cout << "\t cross validation accuracy=" << crossValidationAccuracy << std::endl; std::cout << "\t tau_train=" << trainMC.recognitionRate*100 << "%" << std::endl; std::cout << "\t total number of train BOWs=" << trainMC.nrTest << std::endl; }
TabListeEleve::TabListeEleve(int idClasse, TabModule *parent) : TabAbstractClasse(idClasse,parent) { if(m_classe.isValid()) { m_delegate = new ListeElevesDelegate(this); m_model = new ListeEleveModel(bdd(),m_classe,this); m_view = new QTableView(); m_addColumnButton = new QPushButton(tr("Ajouter une nouvelle colonne au tableau")); m_saveButton = new QPushButton(tr("Sauvegarder")); m_view->setModel(m_model); m_view->setItemDelegate(m_delegate); m_view->setSelectionMode(QAbstractItemView::ExtendedSelection); m_view->setSortingEnabled(true); m_view->sortByColumn(0,Qt::AscendingOrder); connect(m_addColumnButton,&QPushButton::clicked,this,&TabListeEleve::addColumn); connect(m_saveButton,&QPushButton::clicked,this,&TabAbstractTableau::save); m_mainLayout->addWidget(m_view); m_mainLayout->addWidget(m_saveButton); m_mainLayout->addWidget(m_addColumnButton); } }
void im_leave_one_out(std::string bddName, int k){ std::string path2bdd("bdd/" + bddName); std::string KMeansFile(path2bdd + "/" + "training.means"); // std::cout << path2bdd << std::endl; // Loading BDD IMbdd bdd(bddName,path2bdd); bdd.load_bdd_configuration(path2bdd.c_str(),"imconfig.xml"); // Saving KMeans settings bdd.changeKMSettings("specifical", k, "training.means"); // Loading feature points settings std::string descriptor = bdd.getDescriptor(); // Loading activities std::vector<std::string> activities = bdd.getActivities(); int nrActivities = activities.size(); int labels[nrActivities]; int index = 0; for(std::vector<std::string>::iterator activity = activities.begin(); activity != activities.end(); ++activity){ labels[index] = index + 1; index++; } if(k%nrActivities != 0){ std::cerr << "k is not divisible by nrActivities !" << std::endl; exit(EXIT_FAILURE); } MatrixC trainMC = MatrixC(nrActivities,labels); MatrixC testMC = MatrixC(nrActivities,labels); double crossValidationAccuracy = 0; std::vector<std::string> people = bdd.getPeople(); // Leave One Person Out for(std::vector<std::string>::iterator person = people.begin() ; person != people.end(); ++person){ std::vector<std::string> testingPeople; std::vector<std::string> trainingPeople; testingPeople.push_back(*person); // Filling trainingPeople for(std::vector<std::string>::iterator trainingPerson = people.begin() ; trainingPerson != people.end(); ++trainingPerson){ if((*trainingPerson).compare(*person) != 0) trainingPeople.push_back(*trainingPerson); } std::cout << "Testing" << *person << std::endl; im_create_specifics_training_means(bdd, trainingPeople); crossValidationAccuracy += im_svm_train(bdd, trainingPeople, trainMC, testingPeople, testMC); std::cout << "Test " << *person << " ok!" << std::endl; } crossValidationAccuracy /= people.size(); trainMC.calculFrequence(); trainMC.exportMC(path2bdd,"training_confusion_matrix.txt"); trainMC.output(); testMC.calculFrequence(); testMC.exportMC(path2bdd,"testing_confusion_matrix.txt"); testMC.output(); std::cout << "#######################################" << std::endl; std::cout << "######## RESUME OF THE TEST ###########" << std::endl; std::cout << "#######################################" << std::endl; std::cout << "Number of people: " << people.size() << std::endl; std::cout << "Descriptor ID: " << descriptor << std::endl; std::cout << "Number of means: " << k << std::endl; std::cout << "Train recognition rate:" << std::endl; std::cout << "\t cross validation accuracy=" << crossValidationAccuracy << std::endl; std::cout << "\t tau_train=" << trainMC.recognitionRate*100 << "%" << std::endl; std::cout << "\t total number of train BOWs=" << trainMC.nrTest << std::endl; std::cout << "Test recognition rate:" << std::endl; std::cout << "\t tau_test=" << testMC.recognitionRate*100 << "%" << std::endl; std::cout << "\t total number of test BOWs=" << testMC.nrTest << std::endl; }
/** * \fn void addVideos(std::string bddName, std::string activity, int nbVideos, std::string* videoPaths, int dim, int maxPts) * \brief Adds a new video in the choosen activity of the specified BDD. * \param[in] bddName The name of the BDD. * \param[in] activity The name of the activity. * \param[in] nbVideos The number of videos we want to add. * \param[in] videoPaths The different paths to the videos. */ void addVideos(std::string bddName, std::string activity, int nbVideos, std::string* videoPaths){ std::string path2bdd("bdd/" + bddName); //int desc = getDescID(path2bdd); //int dim = getDim(desc); // Loading the bdd IMbdd bdd(bddName,path2bdd); bdd.load_bdd_configuration(path2bdd.c_str(),"imconfig.xml"); // Saving its parameters int maxPts = bdd.getMaxPts(); int scale_num = bdd.getScaleNum(); std::string descriptor = bdd.getDescriptor(); int dim = bdd.getDim(); // Loading the mapping file to get the video label activitiesMap *am; int nbActivities = mapActivities(path2bdd,&am); int i = 0; while(am[i].activity.compare(activity) != 0 && i < nbActivities) i++; if(am[i].activity.compare(activity) != 0){ std::cerr << "Activity not found!\n" << std::endl; exit(EXIT_FAILURE); } int label = am[i].label; delete []am; // Import videos in the selected database string strlabel = inttostring(label); std::string copypath(path2bdd + "/" + strlabel + "/avi"); int nbFiles = nbOfFiles(copypath); int j = nbFiles + 1; for(int i=0 ; i<nbVideos ; i++){ string idFile = inttostring(j); string cmd("cp " + videoPaths[i] + " " + copypath + "/" + strlabel + idFile + ".avi"); system(cmd.c_str()); j++; } // Extract STIPs from the videos and save them in the repertory /path/to/bdd/label/ string fpointspath(path2bdd + "/" + strlabel + "/fp"); j = nbFiles + 1; for(int i=0 ; i<nbVideos ; i++){ KMdata dataPts(dim,maxPts); string idFile = inttostring(j); string videoInput(copypath + "/" + strlabel + idFile + ".avi"); string fpOutput(fpointspath + "/" + strlabel + "-" + idFile + ".fp"); int nPts; nPts = extract_feature_points(videoInput, scale_num, descriptor, dim, maxPts, dataPts); if(nPts != 0){ dataPts.setNPts(nPts); exportSTIPs(fpOutput, dim,dataPts); } j++; } }
/** * \fn void predictActivity(std::string, std::string bddName, int maxPts) * \brief Predict the activity done in a video with an existant trained BDD. * * \param[in] The path to the video to predict. * \param[in] bddName The name of the BDD containing videos. * \param[in] maxPts The maximum number of STIPs we can extract. * \return The name of the activity. */ void predictActivity(std::string videoPath, std::string bddName ){ std::string path2bdd("bdd/" + bddName); // Loading parameters IMbdd bdd(bddName,path2bdd); bdd.load_bdd_configuration(path2bdd.c_str(),"imconfig.xml"); int scale_num = bdd.getScaleNum(); std::string descriptor = bdd.getDescriptor(); int dim = bdd.getDim(); int k = bdd.getK(); int maxPts = bdd.getMaxPts(); //double p = getTrainProbability(path2bdd); // Computing feature points KMdata dataPts(dim,maxPts); int nPts = 0; nPts = extract_feature_points(videoPath, scale_num, descriptor, dim, maxPts, dataPts); if(nPts == 0){ std::cerr << "No activity detected !" << std::endl; exit(EXIT_FAILURE); } std::cout << nPts << " vectors extracted..." << std::endl; dataPts.setNPts(nPts); dataPts.buildKcTree(); KMfilterCenters ctrs(k, dataPts); importCenters(bdd.getFolder() + "/" + bdd.getKMeansFile(), dim, k, &ctrs); std::cout << "KMeans centers imported..." << std::endl; activitiesMap *am; int nbActivities = mapActivities(path2bdd,&am); struct svm_problem svmProblem = computeBOW(0, dataPts, ctrs); double means[k], stand_devia[k]; load_gaussian_parameters(bdd, means, stand_devia); // simple, gaussian, both, nothing if(bdd.getNormalization().compare("") != 0) bow_normalization(bdd,svmProblem); std::cout << "Bag of words normalized..." << std::endl; struct svm_model** pSVMModels = new svm_model*[nbActivities]; std::vector<std::string> modelFiles(bdd.getModelFiles()); int i=0; for (std::vector<std::string>::iterator it = modelFiles.begin() ; it != modelFiles.end() ; ++it){ pSVMModels[i]= svm_load_model((*it).c_str()); i++; } std::cout << "SVM models imported..." << std::endl; double probs[nbActivities]; double label = svm_predict_ovr_probs(pSVMModels, svmProblem.x[0], nbActivities, probs, 2); std::cerr<<"Probs: "; for(int j=0 ; j<nbActivities ; j++){ std::cout << setw(2) << setiosflags(ios::fixed) << probs[j]*100<<" "; } destroy_svm_problem(svmProblem); int index = searchMapIndex(label, am, nbActivities); std::cout << "Activity predicted: "; std::cout << am[index].activity << "(" << am[index].label << ")"; std::cout << std::endl; for(int m=0 ; m<nbActivities ; m++){ svm_free_and_destroy_model(&pSVMModels[m]); } }