#include "faceAPI.h" #include FaceDB *fdb; FaceAPI::FaceAPI() { //创建静态人脸识别句柄,同时创建状态标量 cv_result_t cv_result = 0; //FaceDB* f_db = FaceDB.GetInstance(); fdb=new FaceDB(); } FaceAPI::~FaceAPI() { } //@brief 搜索功能 //@param 图片 //@return 人脸id int FaceAPI::do_reasch(cv::Mat image) { if (!image.data) { fprintf(stderr, "fail to read img\n"); return -1; } else fprintf(stderr, "read img\n"); p_f = fdb->extract_feature(image); fprintf(stderr, "fdb->extract_feature(image)\n"); if (!p_f) { fprintf(stderr, "p_f is null\n"); return -2; } return p_img_search(p_f); } //@brief 搜索功能 //@param 字符串格式的特征值 //@return 人脸id int FaceAPI::do_reasch(char* s_feature) { get_feature(s_feature,p_f); return p_img_search(p_f); } //@brief 注册功能 //@param 图片 //@return 人脸id int FaceAPI::do_register(cv::Mat image) { p_f = fdb->extract_feature(image); int indx = fdb->db_add(p_f); if(fdb->db_save()) return indx; return -2; } //@brief 在数据库中搜索人脸id //@param 特征值 //@return 人脸id int FaceAPI::p_img_search(cv_feature_t *p_feature) { std::cout<<"p img search"<search_db(p_feature); } //@brief 特征值转换为字符串 //@param 特征值 //@param 字符串格式的特征值 //@return 状态量 int FaceAPI::get_char(cv_feature_t *p_feature,char* feature_str) { return cv_verify_serialize_feature(p_feature,feature_str); } //@brief 字符串转换为特征值 //@param 字符串格式的特征值 //@param 特征值 //@return 状态量 int FaceAPI::get_feature(char *feature_str,cv_feature_t *p_feature) { p_feature = cv_verify_deserialize_feature(feature_str); if(p_feature != NULL) { return 0; } else return -1; } //faceDB实现部分 //"./out1.db" //char *db_path = "out.db"; FaceDB::FaceDB() { } FaceDB::~FaceDB() {} //初始化设置 bool FaceDB::init(char* db_path_t) { db_path = db_path_t; //加载一次可多次使用。 cv_verify_create_handle(&handle_verify, "../models/verify.model"); fprintf(stderr, "create cv_verify_create_handle\n"); cv_face_create_detector(&handle_detect, NULL, CV_DETECT_ENABLE_ALIGN_21); fprintf(stderr, "create cv_face_create_detector\n"); // 创建人脸数据库句柄,在db_save()中,数据库保存成功,会释放该句柄 cv_verify_create_db(&handle_db); fprintf(stderr, "create cv_verify_create_db\n"); if(db_load()) { fprintf(stderr, "db load\n"); return true; } fprintf(stderr, "db load falied\n"); return false; } //调用数据库保存 bool FaceDB::finally() { if(db_save()) { return true; } return false; } //@brief 数据库添加数据 //@param 特征值 //@return 人脸id,添加失败返回-1 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); return -1; } cv_verify_release_feature(p_feature); return idx; } //@brief 保存数据库 //@return 状态量 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"); } cv_verify_destroy_db(handle_db); return true; } //@brief 加载数据库 //@return 状态量 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; } //@brief 搜索数据库 //@param 人脸特征值 //@return 人脸id 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); } std::cout<<"this is hits"<= 1) { st_result = cv_verify_get_feature(handle_verify, (unsigned char *)image_color.data, CV_PIX_FMT_BGR888, image_color.cols, image_color.rows, image_color.step, p_face, &p_feature, NULL); if (st_result != CV_OK) { fprintf(stderr, "cv_verify_get_feature failed, error code %d\n", st_result); } } else { fprintf(stderr, "can't find face in "); } // release the memory of face cv_face_release_detector_result(p_face, face_count); return p_feature; }