From 109ffe9a777658936a38d0c146579a67c60a0d17 Mon Sep 17 00:00:00 2001 From: xuxiuxi <xuxiuxi@454eff88-639b-444f-9e54-f578c98de674> Date: 星期四, 11 五月 2017 17:48:48 +0800 Subject: [PATCH] --- FaceServer/sample_face_search.cpp | 250 +++++++++++++++++++++++++++++++++++--------------- 1 files changed, 175 insertions(+), 75 deletions(-) diff --git a/FaceServer/sample_face_search.cpp b/FaceServer/sample_face_search.cpp index 8889cac..25da1ac 100644 --- a/FaceServer/sample_face_search.cpp +++ b/FaceServer/sample_face_search.cpp @@ -1,14 +1,15 @@ #include "sample_face_search.h" +#include <cv_face.h> #include <MaterialBuffer.h> #include "STFaceCache.h" #include <logger.h> #include <vector> #include <stdio.h> -#include <cv_face.h> #include <opencv2/opencv.hpp> #include <libyuv/convert.h> +#include <libyuv/convert_from_argb.h> #define SUBSAMPLE(v, a) ((((v) + (a) - 1)) / (a)) #define MAX_FACE_IMAGE_WIDTH 640 @@ -32,17 +33,17 @@ 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, + cv_result_t result = CV_OK; + 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, + 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); + if (result != CV_OK) { + LOGP(DEBUG, "cv_verify_get_feature failed, error code %d", result); } } else { LOGP(DEBUG, "can't find face in %s", image_path); @@ -55,6 +56,7 @@ cv_feature_t *stface_extract_feature(stface_handles& handles, const STFaceImage& image) { cv_pixel_format stimgfmt = CV_PIX_FMT_GRAY8; + int matType = 0; if (image.width > MAX_FACE_IMAGE_WIDTH || image.height > MAX_FACE_IMAGE_HEIGHT) { @@ -64,28 +66,94 @@ uint8_t imgbuf[MAX_FACE_IMAGE_WIDTH * MAX_FACE_IMAGE_HEIGHT * 4]; size_t imgbufSize = 0; - if (image.mb_type == MB_Frame::MBFT_RGB565) + + if (image.mb_type == MB_Frame::MBFT_Y8) { + memcpy(imgbuf, image.buff, image.size);//#todo avoid mem cpy + + imgbufSize = image.height * image.width; + stimgfmt = CV_PIX_FMT_GRAY8; + matType = CV_8UC1; + } + else if (image.mb_type == MB_Frame::MBFT_RGB565) + { + //#todo libyuv can not deal no-normal-size image uint8* dst_y = (uint8*)(imgbuf); uint8* dst_u = (uint8*)(dst_y + (image.height * image.width)); uint8* dst_v = (uint8*)(dst_u + (image.height * image.width / 4)); int ret = libyuv::RGB565ToI420( - image.buff, image.width, + image.buff, image.width * 2,//#todo test dst_y, image.width, dst_u, SUBSAMPLE(image.width, 2), dst_v, SUBSAMPLE(image.width, 2), image.width, image.height - ); - imgbufSize = image.height * image.width * 1.5; + ); + + imgbufSize = image.height * image.width; + stimgfmt = CV_PIX_FMT_GRAY8; + matType = CV_8UC1; + } + else if (image.mb_type == MB_Frame::MBFT_ABGR8888) + { + //int ret = libyuv::ARGBToBGRA( + // image.buff, image.width * 4, + // imgbuf, image.width * 4, + // image.width, image.height + // ); + // + ////memcpy(imgbuf, image.buff, image.size); + // + //imgbufSize = image.height * image.width * 4; + //stimgfmt = CV_PIX_FMT_BGRA8888; + //matType = CV_8UC4; + + int ret = libyuv::ABGRToI400( + image.buff, image.width * 4, + imgbuf, image.width, + image.width, image.height + ); + + //memcpy(imgbuf, image.buff, image.size); + + imgbufSize = image.height * image.width; + stimgfmt = CV_PIX_FMT_GRAY8; + matType = CV_8UC1; + + //{ + // static int f = 0; + // ++f; + // + // char fname[50]; + // sprintf(fname, "st-%d-w%d-h%d.y8", f, image.width, image.height); + // FILE *pFile = fopen(fname, "wb"); + // fwrite(imgbuf, 1, imgbufSize, pFile); + // fclose(pFile); + //} } else { LOG_WARN << "mb frame type not support" << LOG_ENDL; return nullptr; } + + //{ + // static int f = 0; + // ++f; + // + // char fname[50]; + // sprintf(fname, "st-%d.rgb", f); + // FILE *pFile = fopen(fname, "wb"); + // fwrite(image.buff, 1, image.size, pFile); + // fclose(pFile); + // + // sprintf(fname, "st-%d.yuv", f); + // pFile = fopen(fname, "wb"); + // fwrite(imgbuf, 1, imgbufSize, pFile); + // fclose(pFile); + //} - Mat matImg(cv::Size(image.width, image.height), CV_8UC1, imgbuf); + Mat matImg(cv::Size(image.width, image.height), matType, imgbuf); if (!matImg.data) { return nullptr; @@ -94,20 +162,21 @@ 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, matImg.data, stimgfmt, matImg.cols, matImg.rows, matImg.step, CV_FACE_UP, &p_face, &face_count); + cv_result_t result = CV_OK; + result = cv_face_detect(handles.handle_detect, matImg.data, stimgfmt, matImg.cols, matImg.rows, matImg.step, CV_FACE_UP, &p_face, &face_count); if (face_count >= 1) { - st_result = cv_verify_get_feature(handles.handle_verify, (unsigned char *)matImg.data, stimgfmt, matImg.cols, matImg.rows, matImg.step, p_face, &p_feature, nullptr); - if (st_result != CV_OK) + result = cv_verify_get_feature(handles.handle_verify, (unsigned char *)matImg.data, stimgfmt, matImg.cols, matImg.rows, matImg.step, p_face, &p_feature, nullptr); + if (result != CV_OK) { - LOGP(DEBUG, "cv_verify_get_feature failed, error code %d\n", st_result); + LOGP(DEBUG, "cv_verify_get_feature failed, error code %d", result); } } else { - LOGP(DEBUG, "can't find face in"); + LOGP(DEBUG, "can't find face in STFaceImage"); } + // release the memory of face cv_face_release_detector_result(p_face, face_count); return p_feature; @@ -121,7 +190,7 @@ 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); + LOGP(DEBUG, "cv_verify_add_face failed, error code %d", cv_result); } cv_verify_release_feature(p_feature); return idx; @@ -138,7 +207,7 @@ 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); + LOGP(DEBUG, "cv_verify_add_face failed, error code %d", cv_result); } cv_verify_release_feature(p_feature); return idx; @@ -146,16 +215,16 @@ bool stface_db_del(stface_handles& handles, int idx) { if (idx < 0) { - LOGP(DEBUG, "invalid idx!\n"); + LOGP(DEBUG, "invalid idx!"); 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); + LOGP(DEBUG, "cv_verify_delete_face failed, error code %d", cv_result); } else { - LOGP(DEBUG, "delete succeed\n"); + LOGP(DEBUG, "delete succeed"); } } @@ -163,11 +232,11 @@ 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); + LOGP(DEBUG, "cv_verify_save_db failed, error code %d", cv_result); return false; } else { - LOGP(DEBUG, "save done!\n"); + LOGP(DEBUG, "save done!"); } return true; @@ -175,13 +244,26 @@ bool stface_db_load(stface_handles& handles, const char *db_path) { cv_result_t cv_result = CV_OK; + + if (handles.handle_db == nullptr) + { + cv_result = cv_verify_create_db(&handles.handle_db); + if (cv_result != CV_OK) + { + LOGP(DEBUG, "cv_verify_create_db failed, error code %d", cv_result); + return false; + } + } + 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); + if (cv_result != CV_OK) + { + LOGP(DEBUG, "cv_verify_load_db failed, error code %d", cv_result); return false; } - else { - LOGP(DEBUG, "load done!\n"); + else + { + LOGP(DEBUG, "load done!"); } return true; @@ -189,13 +271,22 @@ bool stface_db_create(stface_handles& handles, const char *db_path) { - cv_result_t cv_result = cv_verify_create_db(&handles.handle_db); - if (cv_result != CV_OK) + cv_result_t cv_result = CV_OK; + + if (handles.handle_db == nullptr) { - LOG_WARN << "cv_verify_create_db return false" << LOG_ENDL; - return false; + cv_result = cv_verify_create_db(&handles.handle_db); + if (cv_result != CV_OK) + { + LOGP(DEBUG, "cv_verify_create_db failed, error code %d", cv_result); + return false; + } } - + + // add a bias image so that stfacesdk can save db + int idx = stface_db_add(handles, "./bias.jpg"); + cout << "idx = " << idx << endl; + cv_result = cv_verify_save_db(handles.handle_db, db_path); if (cv_result != CV_OK) { @@ -210,7 +301,7 @@ bool bresult = true; FILE *fp_path = fopen(image_list, "r"); if(!fp_path) { - LOGP(DEBUG, "failed to load %s\n", image_list); + LOGP(DEBUG, "failed to load %s", image_list); return false; } std::vector<cv_feature_t *> list_feature; @@ -222,12 +313,12 @@ bresult = false; break; } - LOGP(DEBUG, "extracting %s\n", image_path); + LOGP(DEBUG, "extracting %s", 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); + LOGP(DEBUG, "failed to extract image: %s", image_path); continue; } list_feature.push_back(p_feature); @@ -238,7 +329,7 @@ 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); + LOGP(DEBUG, "cv_verify_build_db failed, error code %d", cv_result); bresult = false; } cv_verify_save_db(handles.handle_db, output_db_path); @@ -253,14 +344,14 @@ 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"); + LOGP(DEBUG, "invalid path !"); 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"); + LOGP(DEBUG, "extract failed !"); return false; } @@ -274,12 +365,11 @@ 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]); + LOGP(DEBUG, "%d\t%0.2f", top_idxs[t], top_scores[t]); } } else { - LOGP(DEBUG, "cv_verify_search_face failed, error code %d\n", cv_result); + LOGP(DEBUG, "cv_verify_search_face failed, error code %d", cv_result); } if (top_idxs) { delete[]top_idxs; @@ -297,7 +387,7 @@ cv_feature_t *p_feature = stface_extract_feature(handles, image); if (p_feature == nullptr) { - LOGP(DEBUG, "extract failed !\n"); + LOGP(DEBUG, "extract failed !"); return false; } @@ -311,15 +401,14 @@ 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]); + LOGP(DEBUG, "%d\t%0.2f", top_idxs[t], top_scores[t]); result.push_back(TopIdxScore(top_idxs[t], top_scores[t])); } } else { - LOGP(DEBUG, "cv_verify_search_face failed, error code %d\n", cv_result); + LOGP(DEBUG, "cv_verify_search_face failed, error code %d", cv_result); } if (top_idxs) { @@ -337,13 +426,13 @@ 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); + LOGP(DEBUG, "failed to extract image: %s", image_path); return false; } FILE *fp_path = fopen(list_path, "r"); if(!fp_path) { - LOGP(DEBUG, "failed to load %s\n", list_path); + LOGP(DEBUG, "failed to load %s", list_path); return false; } std::vector<cv_feature_t *> list_feature; @@ -354,12 +443,12 @@ if (num != 1) { break; } - LOGP(DEBUG, "extracting %s\n", image_path); + LOGP(DEBUG, "extracting %s", 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); + LOGP(DEBUG, "failed to extract image: %s", image_path); continue; } list_feature.push_back(p_feature); @@ -378,8 +467,7 @@ 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]); + LOGP(DEBUG, "%d\t%0.2f", top_idxs[t], top_scores[t]); } } else { LOGP(DEBUG, "search face failed"); @@ -389,40 +477,40 @@ for (int i = 0; i < list_feature.size(); i++) { cv_verify_release_feature(list_feature[i]); } - LOGP(DEBUG, "list search done!\n"); + LOGP(DEBUG, "list search done!"); 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"); + LOGP(DEBUG, "Usage: help | Get cmd list"); + LOGP(DEBUG, "Usage: search p_image_colorpath | Search image in db"); + LOGP(DEBUG, "Usage: add p_image_colorpath | Add image in db, return idx in db, if idx < 0 means failed"); + LOGP(DEBUG, "Usage: del idx | Delete image in db"); + LOGP(DEBUG, "Usage: save db_file | Save current db in db_file"); + LOGP(DEBUG, "Usage: load db_file | Load db in db_file"); + LOGP(DEBUG, "Usage: gen p_image_colorlist db_file | Gen images in p_image_colorlist and save in db_file"); + LOGP(DEBUG, "Usage: exit | Exit the program"); } 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); + LOGP(DEBUG, "create detect handle failed, error code %d", 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); + LOGP(DEBUG, "create verify handle failed, error code %d", 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); + LOGP(DEBUG, "create db handle failed, error code %d", 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"); + LOGP(DEBUG, "Database is empty at the beginning"); + LOGP(DEBUG, "Please input 'help' to get the cmd list"); char input_code[256]; char image_path[_MAX_PATH]; char db_path[_MAX_PATH]; @@ -431,7 +519,7 @@ while (1) { LOGP(DEBUG, ">>"); if (!fgets(input_code, 256, stdin)) { - LOGP(DEBUG, "read nothing\n"); + LOGP(DEBUG, "read nothing"); continue; } int input_length = strlen(input_code); @@ -448,7 +536,7 @@ 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"); + LOGP(DEBUG, "invalid! Usage: add p_image_colorpath"); continue; } int idx = stface_db_add(handles, image_path); @@ -458,7 +546,7 @@ 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"); + LOGP(DEBUG, "invalid! Usage: del idx(unsigned int"); continue; } stface_db_del(handles, idx); @@ -466,7 +554,7 @@ 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"); + LOGP(DEBUG, "invalid! Usage: save db_file"); continue; } stface_db_save(handles, image_path); @@ -474,7 +562,7 @@ 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"); + LOGP(DEBUG, "invalid! Usage: load db_file"); continue; } stface_db_load(handles, image_path); @@ -485,7 +573,7 @@ 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"); + LOGP(DEBUG, "invalid! Usage: gen p_image_colorlist_file db_file"); continue; } stface_db_gen(handles, image_path, db_path); @@ -493,7 +581,7 @@ 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"); + LOGP(DEBUG, "invalid! Usage: search p_image_colorpath"); continue; } stface_search_db(handles, image_path); @@ -503,13 +591,13 @@ 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"); + LOGP(DEBUG, "invalid! Usage: listsearch p_image_colorsrcpath p_image_colorlistpath"); continue; } stface_search_list(handles, search_path, image_path); } else { - LOGP(DEBUG, "invalid cmd, please input 'help' to get the cmd list\n"); + LOGP(DEBUG, "invalid cmd, please input 'help' to get the cmd list"); } } } @@ -520,3 +608,15 @@ return 0; } +int stface_init_license(const char* lic_path) +{ + //鍔犺浇鎺堟潈璇佷功 + cv_result_t cv_result = cv_face_public_init_license(lic_path, "license"); + if(cv_result != CV_OK) + { + LOGP(ERROR, "cv_face_public_init_license error %d", cv_result); + return -1; + } + + return 0; +} -- Gitblit v1.8.0