#include #include #include #include #include "time_helper.h" #include #define DEFAULT_THRESHOLD (0.5) using namespace std; using namespace cv; //ÈËÁ³ÑéÖ¤ static cv_handle_t handle_verify = NULL; //Êý¾Ý¿â²Ù×÷ static cv_handle_t handle_db = NULL; //¾²Ì¬ÈËÁ³Ê¶±ð static cv_handle_t handle_detect = NULL; #define _MAX_PATH 260 //ÌáÈ¡Í¼Æ¬ÌØÕ÷Öµ cv_feature_t *extract_feature(Mat image_color) { Mat image_color_color; // cvtColor(image_color, image_color_color, CV_BGR2BGRA); // CV_PIX_FMT_BGRA8888 image_color_color = image_color; // CV_PIX_FMT_BGR888 cv_feature_t *p_feature = NULL; cv_face_t *p_face = NULL; int face_count = 0; cv_result_t st_result = CV_OK; //¼ì²âÈËÁ³ st_result = cv_face_detect(handle_detect, image_color_color.data, CV_PIX_FMT_BGR888, image_color_color.cols, image_color_color.rows, image_color_color.step, CV_FACE_UP, &p_face, &face_count); if (face_count >= 1) { //»ñÈ¡ÈËÁ³ÌØÕ÷ st_result = cv_verify_get_feature(handle_verify, (unsigned char *)image_color_color.data, CV_PIX_FMT_BGR888, image_color_color.cols, image_color_color.rows, image_color_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 "); } // ÊÍ·ÅÄÚ´æ cv_face_release_detector_result(p_face, face_count); return p_feature; } //Êý¾Ý¿âÌí¼ÓÊý¾Ý£¬²¢·µ»Ø¼Ç¼µÃid int db_add(cv_feature_t *p_feature) { /*cv_feature_t *p_feature = extract_feature(image_color); if (!p_feature) { return -1; }*/ 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; } //¸ù¾Ýidɾ³ý¼Ç¼ bool db_del(int idx) { if (idx < 0) { fprintf(stderr, "invalid idx!\n"); return false; } cv_result_t cv_result = CV_OK; cv_result = cv_verify_delete_face(handle_db, idx); if (cv_result != CV_OK) { fprintf(stderr, "cv_verify_delete_face failed, error code %d\n", cv_result); } else { fprintf(stderr, "delete succeed\n"); } } //Êý¾Ý¿â±£´æ bool db_save(char *db_path) { cv_result_t cv_result = CV_OK; 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 db_load(char *db_path) { cv_result_t cv_result = CV_OK; 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; } //ËÑË÷Êý¾Ý¿â bool search_db(Mat image_color,char *db_path) { cv_feature_t *p_feature = extract_feature(image_color); if (p_feature == NULL) { fprintf(stderr, "extract failed !\n"); return false; } db_load(db_path); //²éѯǰ10Ìõ int top_k = 10; int *top_idxs = new int[top_k]; float *top_scores = new float[top_k]; unsigned int result_length = 0; cv_result_t 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); } if (top_idxs) { delete[]top_idxs; } if (top_scores) { delete[]top_scores; } cv_verify_release_feature(p_feature); return true; } //ÅúÁ¿µ¼ÈëͼƬ½øÐзÖÎö bool db_gen(char *image_list, char *db_path) { bool bresult = true; //¶ÁȡͼƬÁбí FILE *fp_path = fopen(image_list, "r"); if(!fp_path) { fprintf(stderr, "failed to load %s\n", image_list); return false; } //ÈËÁ³ÌØÕ÷ÐÅÏ¢ Êý¾Ý½á¹¹ std::vector list_feature; list_feature.clear(); for (;;) { char image_path[1024]; int num = fscanf(fp_path, "%s", image_path); if (num != 1) { bresult = false; break; } fprintf(stderr, "extracting %s\n", image_path); // get the face feature Mat image_color = imread(image_path); if (!image_color.data) { return NULL; } cv_feature_t *p_feature = extract_feature(image_color); if (!p_feature) { fprintf(stderr, "failed to extract image: %s\n", image_path); continue; } list_feature.push_back(p_feature); } fclose(fp_path); cv_verify_destroy_db(handle_db); cv_result_t cv_result = CV_OK; cv_verify_create_db(&handle_db); //´´½¨ÐµÄÊý¾Ý¿â£¬Ó¦¸Ã¸ÄΪÅжϣ¬Èç¹ûÊý¾Ý¿â´æÔÚÔò½øÐвåÈ룬·ñÔòн¨¡£ if(db_load(db_path)){ for (int x = 0; x < list_feature.size(); x++) { db_add(list_feature[x]); } }else{ cv_result = cv_verify_build_db(handle_db, &list_feature[0], list_feature.size()); if (cv_result != CV_OK) { fprintf(stderr, "cv_verify_build_db failed, error code %d\n", cv_result); bresult = false; } for (int i = 0; i < list_feature.size(); i++) { cv_verify_release_feature(list_feature[i]); } } cv_verify_save_db(handle_db, db_path); return bresult; } int main(int argc, char *argv[]) { //Îļþ·¾¶ char *db_path = "./out1.db"; char *image_path = "../../test_image/face_08.jpg"; char *image_list = "../../test_image/imglist"; //´´½¨¾²Ì¬ÈËÁ³Ê¶±ð¾ä±ú£¬Í¬Ê±´´½¨×´Ì¬±êÁ¿ cv_result_t cv_result = cv_face_create_detector(&handle_detect, NULL, CV_DETECT_ENABLE_ALIGN_21); if (cv_result != CV_OK){ fprintf(stderr, "create detect handle failed, error code %d\n", cv_result); } //´´½¨ÈËÁ³ÑéÖ¤¾ä±ú²¢³õʼ»¯ cv_result = cv_verify_create_handle(&handle_verify, "../../../models/verify.model"); if (cv_result != CV_OK){ fprintf(stderr, "create verify handle failed, error code %d\n", cv_result); } // ´´½¨ÈËÁ³Êý¾Ý¿â¾ä±ú cv_result = cv_verify_create_db(&handle_db); if (cv_result != CV_OK){ fprintf(stderr, "create db handle failed, error code %d\n", cv_result); } //ÅúÁ¿²åÈëÊý¾Ý¿â db_gen(image_list,db_path); //´ýËÑË÷µÄͼÏñ Mat image_color = imread(image_path); if (!image_color.data) { return NULL; } //ËÑË÷Êý¾Ý¿â if(search_db(image_color,db_path)){ cout<<"search db"<