pans
2017-01-11 734e1a94bc026ba8e7e71de0a9ddabe12e07b13b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include "faceDB.h"
//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"<<std::endl;
    if (top_scores[0] != 0) {
        indx=top_idxs[0];
        std::cout<<"indx="<<indx<<std::endl;
    }
    if (top_idxs) {
        delete[]top_idxs;
    }
    if (top_scores) {
        delete[]top_scores;
    }
    cv_verify_release_feature(p_feature);
    std::cout<<"return indx"<<std::endl;
    return indx;
}
 
//@brief ÌáÈ¡ÌØÕ÷Öµ
//@param Í¼Æ¬
//@return ÌØÕ÷Öµ
cv_feature_t* FaceDB::extract_feature(cv::Mat image_color)
{
    if (handle_verify != NULL) {
        fprintf(stderr, "fdb.handle_verify is not null!\n");
    } else {
        fprintf(stderr, "fdb.handle_verify is null!\n");
    }
 
    //¸Ã±äÁ¿ÄÚ´æ»áÔÚdb_add()ÖУ¬Ìí¼Ó³É¹¦ºóÊÍ·Å¡£
    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.data, CV_PIX_FMT_BGR888,
                               image_color.cols, image_color.rows, image_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.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;
}