#include "sample_face_search.h"
|
#include "STFaceCache.h"
|
#include <logger.h>
|
|
#include <vector>
|
#include <stdio.h>
|
#include <cv_face.h>
|
|
#include <opencv2/opencv.hpp>
|
|
using namespace std;
|
using namespace cv;
|
|
//static cv_handle_t handle_verify = nullptr;
|
//static cv_handle_t handle_db = nullptr;
|
//static cv_handle_t handle_detect = nullptr;
|
|
#define _MAX_PATH 260
|
|
cv_feature_t *stface_extract_feature(stface_handles& handles, const char *image_path) {
|
Mat bgr_image = imread(image_path); // CV_PIX_FMT_BGR888
|
if (!bgr_image.data) {
|
return nullptr;
|
}
|
|
cv_feature_t *p_feature = nullptr;
|
cv_face_t *p_face = nullptr;
|
int face_count = 0;
|
cv_result_t st_result = CV_OK;
|
st_result = cv_face_detect(handles.handle_detect, bgr_image.data, CV_PIX_FMT_BGR888,
|
bgr_image.cols, bgr_image.rows, bgr_image.step,
|
CV_FACE_UP, &p_face, &face_count);
|
if (face_count >= 1) {
|
st_result = cv_verify_get_feature(handles.handle_verify,
|
(unsigned char *)bgr_image.data, CV_PIX_FMT_BGR888,
|
bgr_image.cols, bgr_image.rows, bgr_image.step,
|
p_face, &p_feature, nullptr);
|
if (st_result != CV_OK) {
|
LOGP(DEBUG, "cv_verify_get_feature failed, error code %d\n", st_result);
|
}
|
} else {
|
LOGP(DEBUG, "can't find face in %s", image_path);
|
}
|
// release the memory of face
|
cv_face_release_detector_result(p_face, face_count);
|
return p_feature;
|
}
|
|
cv_feature_t *stface_extract_feature(stface_handles& handles, const STFaceImage& image);//#todo
|
|
int stface_db_add(stface_handles& handles, const char *image_path) {
|
cv_feature_t *p_feature = stface_extract_feature(handles, image_path);
|
if (!p_feature) {
|
return -1;
|
}
|
int idx;
|
cv_result_t cv_result = cv_verify_add_face(handles.handle_db, p_feature, &idx);
|
if (cv_result != CV_OK) {
|
LOGP(DEBUG, "cv_verify_add_face failed, error code %d\n", cv_result);
|
}
|
cv_verify_release_feature(p_feature);
|
return idx;
|
}
|
|
int stface_db_add(stface_handles& handles, const STFaceImage& image);//#todo
|
|
bool stface_db_del(stface_handles& handles, int idx) {
|
if (idx < 0) {
|
LOGP(DEBUG, "invalid idx!\n");
|
return false;
|
}
|
cv_result_t cv_result = CV_OK;
|
cv_result = cv_verify_delete_face(handles.handle_db, idx);
|
if (cv_result != CV_OK) {
|
LOGP(DEBUG, "cv_verify_delete_face failed, error code %d\n", cv_result);
|
}
|
else {
|
LOGP(DEBUG, "delete succeed\n");
|
}
|
}
|
|
bool stface_db_save(stface_handles& handles, const char *db_path) {
|
cv_result_t cv_result = CV_OK;
|
cv_result = cv_verify_save_db(handles.handle_db, db_path);
|
if (cv_result != CV_OK) {
|
LOGP(DEBUG, "cv_verify_save_db failed, error code %d\n", cv_result);
|
return false;
|
}
|
else {
|
LOGP(DEBUG, "save done!\n");
|
}
|
|
return true;
|
}
|
|
bool stface_db_load(stface_handles& handles, const char *db_path) {
|
cv_result_t cv_result = CV_OK;
|
cv_result = cv_verify_load_db(handles.handle_db, db_path);
|
if (cv_result != CV_OK) {
|
LOGP(DEBUG, "cv_verify_load_db failed, error code %d\n", cv_result);
|
return false;
|
}
|
else {
|
LOGP(DEBUG, "load done!\n");
|
}
|
|
return true;
|
}
|
|
bool stface_db_gen(stface_handles& handles, char *image_list, char *output_db_path) {
|
bool bresult = true;
|
FILE *fp_path = fopen(image_list, "r");
|
if(!fp_path) {
|
LOGP(DEBUG, "failed to load %s\n", image_list);
|
return false;
|
}
|
std::vector<cv_feature_t *> list_feature;
|
list_feature.clear();
|
for (;;) {
|
char image_path[1024];
|
int num = fscanf(fp_path, "%s", image_path);
|
if (num != 1) {
|
bresult = false;
|
break;
|
}
|
LOGP(DEBUG, "extracting %s\n", image_path);
|
|
// get the face feature
|
cv_feature_t *p_feature = stface_extract_feature(handles, image_path);
|
if (!p_feature) {
|
LOGP(DEBUG, "failed to extract image: %s\n", image_path);
|
continue;
|
}
|
list_feature.push_back(p_feature);
|
}
|
fclose(fp_path);
|
cv_verify_destroy_db(handles.handle_db);
|
cv_result_t cv_result = CV_OK;
|
cv_verify_create_db(&handles.handle_db);
|
cv_result = cv_verify_build_db(handles.handle_db, &list_feature[0], list_feature.size());
|
if (cv_result != CV_OK) {
|
LOGP(DEBUG, "cv_verify_build_db failed, error code %d\n", cv_result);
|
bresult = false;
|
}
|
cv_verify_save_db(handles.handle_db, output_db_path);
|
for (int i = 0; i < list_feature.size(); i++) {
|
cv_verify_release_feature(list_feature[i]);
|
}
|
cout << "db gen done!" << endl;
|
|
return bresult;
|
}
|
|
bool stface_search_db(stface_handles& handles, char *image_path) {
|
FILE *fp_path = fopen(image_path, "r");
|
if (fp_path == nullptr) {
|
LOGP(DEBUG, "invalid path !\n");
|
return false;
|
}
|
fclose(fp_path);
|
|
cv_feature_t *p_feature = stface_extract_feature(handles, image_path);
|
if (p_feature == nullptr) {
|
LOGP(DEBUG, "extract failed !\n");
|
return false;
|
}
|
|
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(handles.handle_verify, handles.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;
|
LOGP(DEBUG, "%d\t", top_idxs[t]);
|
LOGP(DEBUG, "%0.2f\n", top_scores[t]);
|
}
|
}
|
else {
|
LOGP(DEBUG, "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 stface_search_db(stface_handles& handles, const STFaceImage& image, top_idx_score_vect_t& result);//#todo
|
|
bool stface_search_list(stface_handles& handles, char *image_path, char *list_path) {
|
cv_feature_t *p_feature = stface_extract_feature(handles, image_path);
|
if (p_feature == nullptr) {
|
LOGP(DEBUG, "failed to extract image: %s\n", image_path);
|
return false;
|
}
|
|
FILE *fp_path = fopen(list_path, "r");
|
if(!fp_path) {
|
LOGP(DEBUG, "failed to load %s\n", list_path);
|
return false;
|
}
|
std::vector<cv_feature_t *> list_feature;
|
list_feature.clear();
|
for (;;) {
|
char image_path[_MAX_PATH];
|
int num = fscanf(fp_path, "%s", image_path);
|
if (num != 1) {
|
break;
|
}
|
LOGP(DEBUG, "extracting %s\n", image_path);
|
|
// get the face feature
|
cv_feature_t *p_feature = stface_extract_feature(handles, image_path);
|
if (!p_feature) {
|
LOGP(DEBUG, "failed to extract image: %s\n", image_path);
|
continue;
|
}
|
list_feature.push_back(p_feature);
|
}
|
fclose(fp_path);
|
|
|
const int top_k = 10;
|
int top_idxs[top_k];
|
float top_scores[top_k];
|
unsigned int result_length = 0;
|
cv_result_t cv_result = cv_verify_search_face_from_list(handles.handle_verify,
|
&list_feature[0], list_feature.size(),
|
p_feature, top_k,
|
top_idxs, top_scores, &result_length);
|
|
if (cv_result == CV_OK) {
|
for (unsigned int t = 0; t < result_length; t++) {
|
LOGP(DEBUG, "%d\t", top_idxs[t]);
|
LOGP(DEBUG, "%0.2f\n", top_scores[t]);
|
}
|
} else {
|
LOGP(DEBUG, "search face failed");
|
}
|
cv_verify_release_feature(p_feature);
|
|
for (int i = 0; i < list_feature.size(); i++) {
|
cv_verify_release_feature(list_feature[i]);
|
}
|
LOGP(DEBUG, "list search done!\n");
|
|
return true;
|
}
|
|
void stface_get_help() {
|
LOGP(DEBUG, "Usage: help | Get cmd list\n");
|
LOGP(DEBUG, "Usage: search p_image_colorpath | Search image in db\n");
|
LOGP(DEBUG, "Usage: add p_image_colorpath | Add image in db, return idx in db, if idx < 0 means failed\n");
|
LOGP(DEBUG, "Usage: del idx | Delete image in db\n");
|
LOGP(DEBUG, "Usage: save db_file | Save current db in db_file\n");
|
LOGP(DEBUG, "Usage: load db_file | Load db in db_file\n");
|
LOGP(DEBUG, "Usage: gen p_image_colorlist db_file | Gen images in p_image_colorlist and save in db_file\n");
|
LOGP(DEBUG, "Usage: exit | Exit the program\n");
|
}
|
|
int stface_main(stface_handles& handles, int argc, char *argv[]) {
|
cv_result_t cv_result = cv_face_create_detector(&handles.handle_detect, nullptr, CV_DETECT_ENABLE_ALIGN_21);
|
if (cv_result != CV_OK){
|
LOGP(DEBUG, "create detect handle failed, error code %d\n", cv_result);
|
}
|
cv_result = cv_verify_create_handle(&handles.handle_verify, "../../../models/verify.model");
|
if (cv_result != CV_OK){
|
LOGP(DEBUG, "create verify handle failed, error code %d\n", cv_result);
|
}
|
cv_result = cv_verify_create_db(&handles.handle_db);
|
if (cv_result != CV_OK){
|
LOGP(DEBUG, "create db handle failed, error code %d\n", cv_result);
|
}
|
|
if (handles.handle_detect != nullptr && handles.handle_verify != nullptr && handles.handle_db != nullptr) {
|
// stface_db_gen("list.txt","out.db");
|
LOGP(DEBUG, "Database is empty at the beginning\n");
|
LOGP(DEBUG, "Please input 'help' to get the cmd list\n");
|
char input_code[256];
|
char image_path[_MAX_PATH];
|
char db_path[_MAX_PATH];
|
char command[256];
|
input_code[0] = 0;
|
while (1) {
|
LOGP(DEBUG, ">>");
|
if (!fgets(input_code, 256, stdin)) {
|
LOGP(DEBUG, "read nothing\n");
|
continue;
|
}
|
int input_length = strlen(input_code);
|
if (input_length > 0 && input_code[input_length - 1] == '\n') {
|
input_code[--input_length] = 0;
|
}
|
if (input_length == 0) {
|
continue;
|
}
|
std::string str_input_code(input_code);
|
if (strcmp(str_input_code.c_str(), "help") == 0) {
|
stface_get_help();
|
}
|
else if (strcmp(str_input_code.substr(0, 3).c_str(), "add") == 0) {
|
int input_number = sscanf(input_code, "%s%s", command, image_path);
|
if (input_number != 2) {
|
LOGP(DEBUG, "invalid! Usage: add p_image_colorpath\n");
|
continue;
|
}
|
int idx = stface_db_add(handles, image_path);
|
cout << "idx = " << idx << endl;
|
}
|
else if (strcmp(str_input_code.substr(0, 3).c_str(), "del") == 0) {
|
int idx = -1;
|
int input_number = sscanf(input_code, "%s%d", image_path, &idx);
|
if (input_number != 2) {
|
LOGP(DEBUG, "invalid! Usage: del idx(unsigned int\n");
|
continue;
|
}
|
stface_db_del(handles, idx);
|
}
|
else if (strcmp(str_input_code.substr(0, 4).c_str(), "save") == 0) {
|
int input_number = sscanf(input_code, "%s%s", command, image_path);
|
if (input_number != 2) {
|
LOGP(DEBUG, "invalid! Usage: save db_file\n");
|
continue;
|
}
|
stface_db_save(handles, image_path);
|
}
|
else if (strcmp(str_input_code.substr(0, 4).c_str(), "load") == 0) {
|
int input_number = sscanf(input_code, "%s%s", command, image_path);
|
if (input_number != 2) {
|
LOGP(DEBUG, "invalid! Usage: load db_file\n");
|
continue;
|
}
|
stface_db_load(handles, image_path);
|
}
|
else if (strcmp(str_input_code.c_str(), "exit") == 0) {
|
break;
|
}
|
else if (strcmp(str_input_code.substr(0, 3).c_str(), "gen") == 0) {
|
int input_number = sscanf(input_code, "%s%s%s", command, image_path, db_path);
|
if (input_number != 3) {
|
LOGP(DEBUG, "invalid! Usage: gen p_image_colorlist_file db_file\n");
|
continue;
|
}
|
stface_db_gen(handles, image_path, db_path);
|
}
|
else if (strcmp(str_input_code.substr(0, 6).c_str(), "search") == 0) {
|
int input_number = sscanf(input_code, "%s%s", command, image_path);
|
if (input_number != 2) {
|
LOGP(DEBUG, "invalid! Usage: search p_image_colorpath\n");
|
continue;
|
}
|
stface_search_db(handles, image_path);
|
}
|
else if (strcmp(str_input_code.substr(0, 10).c_str(), "listsearch") == 0) {
|
char search_path[_MAX_PATH];
|
int input_number = sscanf(input_code, "%s%s%s", command, search_path,
|
image_path);
|
if (input_number != 3) {
|
LOGP(DEBUG, "invalid! Usage: listsearch p_image_colorsrcpath p_image_colorlistpath\n");
|
continue;
|
}
|
stface_search_list(handles, search_path, image_path);
|
}
|
else {
|
LOGP(DEBUG, "invalid cmd, please input 'help' to get the cmd list\n");
|
}
|
}
|
}
|
|
cv_face_destroy_detector(handles.handle_detect);
|
cv_verify_destroy_db(handles.handle_db);
|
cv_verify_destroy_handle(handles.handle_verify);
|
return 0;
|
}
|