#include "faceDB.h"
|
|
#include <iostream>
|
|
//"./out1.db"
|
char *db_path = "out.db";
|
|
faceDB::faceDB(){
|
// ´´½¨ÈËÁ³Êý¾Ý¿â¾ä±ú
|
cv_verify_create_db(&handle_db);
|
cv_verify_create_handle(&handle_verify, "verify.model");
|
db_load();
|
}
|
|
faceDB::~faceDB(){}
|
|
/*
|
faceDB* faceDB::GetInstance(){
|
if (db == NULL)
|
{
|
db = new faceDB();
|
}
|
|
return db;
|
}*/
|
|
//Êý¾Ý¿âÌí¼ÓÊý¾Ý£¬²¢·µ»Ø¼Ç¼µÃid
|
int faceDB::db_add(cv_feature_t* p_feature) {
|
int idx;
|
cv_result_t cv_result = cv_verify_add_face(handle_db, p_feature, &idx);
|
if (cv_result != CV_OK) {
|
fprintf(stderr, "cv_verify_add_face failed, error code %d\n", cv_result);
|
}
|
cv_verify_release_feature(p_feature);
|
return idx;
|
}
|
|
|
//Êý¾Ý¿â±£´æ
|
bool faceDB::db_save() {
|
cv_result = cv_verify_save_db(handle_db, db_path);
|
if (cv_result != CV_OK) {
|
fprintf(stderr, "cv_verify_save_db failed, error code %d\n", cv_result);
|
return false;
|
}
|
else {
|
fprintf(stderr, "save done!\n");
|
}
|
|
return true;
|
}
|
|
//Êý¾Ý¿â¼ÓÔØ
|
bool faceDB::db_load() {
|
if (handle_db != NULL)
|
{
|
fprintf(stderr, "handle_db is not null!%s\n",db_path);
|
}
|
else
|
{
|
fprintf(stderr, "handle_db is null!%s\n",db_path);
|
}
|
cv_result = cv_verify_load_db(handle_db, db_path);
|
if (cv_result != CV_OK) {
|
fprintf(stderr, "cv_verify_load_db failed, error code %d\n", cv_result);
|
return false;
|
}
|
else {
|
fprintf(stderr, "load done!\n");
|
}
|
|
return true;
|
}
|
|
//ËÑË÷Êý¾Ý¿â
|
int faceDB::search_db(cv_feature_t *p_feature) {
|
|
|
int indx=-1;
|
//²éѯǰ10Ìõ
|
int top_k = 3;
|
int *top_idxs = new int[top_k];
|
float *top_scores = new float[top_k];
|
unsigned int result_length = 0;
|
cv_result = cv_verify_search_face(handle_verify, handle_db,
|
p_feature, top_k,
|
top_idxs, top_scores, &result_length);
|
|
|
if (cv_result == CV_OK) {
|
//Êä³ö¼ì²é½á¹û
|
for (unsigned int t = 0; t < result_length; t++) {
|
// const cv_feature_t item = result[t].item;
|
fprintf(stderr, "%d\t", top_idxs[t]);
|
fprintf(stderr, "%0.2f\n", top_scores[t]);
|
}
|
} else {
|
fprintf(stderr, "cv_verify_search_face failed, error code %d\n", cv_result);
|
}
|
indx=top_idxs[0];
|
if (top_idxs) {
|
delete[]top_idxs;
|
}
|
if (top_scores) {
|
delete[]top_scores;
|
}
|
cv_verify_release_feature(p_feature);
|
return indx;
|
}
|