From a54e2c8ed81ec720f45470292cdd97e3fc86d8e9 Mon Sep 17 00:00:00 2001
From: xuxiuxi <xuxiuxi@454eff88-639b-444f-9e54-f578c98de674>
Date: 星期二, 25 四月 2017 15:34:10 +0800
Subject: [PATCH]
---
FaceServer/sample_face_search.cpp | 145 +++++++++++++++++++++++++++++++++++++++++++++++-
1 files changed, 142 insertions(+), 3 deletions(-)
diff --git a/FaceServer/sample_face_search.cpp b/FaceServer/sample_face_search.cpp
index 6876364..8889cac 100644
--- a/FaceServer/sample_face_search.cpp
+++ b/FaceServer/sample_face_search.cpp
@@ -1,4 +1,5 @@
#include "sample_face_search.h"
+#include <MaterialBuffer.h>
#include "STFaceCache.h"
#include <logger.h>
@@ -7,6 +8,11 @@
#include <cv_face.h>
#include <opencv2/opencv.hpp>
+#include <libyuv/convert.h>
+#define SUBSAMPLE(v, a) ((((v) + (a) - 1)) / (a))
+
+#define MAX_FACE_IMAGE_WIDTH 640
+#define MAX_FACE_IMAGE_HEIGHT 480
using namespace std;
using namespace cv;
@@ -46,7 +52,66 @@
return p_feature;
}
-cv_feature_t *stface_extract_feature(stface_handles& handles, const STFaceImage& image);//#todo
+cv_feature_t *stface_extract_feature(stface_handles& handles, const STFaceImage& image)
+{
+ cv_pixel_format stimgfmt = CV_PIX_FMT_GRAY8;
+
+ if (image.width > MAX_FACE_IMAGE_WIDTH || image.height > MAX_FACE_IMAGE_HEIGHT)
+ {
+ LOG_WARN << "image too big" << LOG_ENDL;
+ return nullptr;
+ }
+
+ uint8_t imgbuf[MAX_FACE_IMAGE_WIDTH * MAX_FACE_IMAGE_HEIGHT * 4];
+ size_t imgbufSize = 0;
+ if (image.mb_type == MB_Frame::MBFT_RGB565)
+ {
+ 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,
+ 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;
+ }
+ else
+ {
+ LOG_WARN << "mb frame type not support" << LOG_ENDL;
+ return nullptr;
+ }
+
+ Mat matImg(cv::Size(image.width, image.height), CV_8UC1, imgbuf);
+ if (!matImg.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, 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)
+ {
+ LOGP(DEBUG, "cv_verify_get_feature failed, error code %d\n", st_result);
+ }
+ }
+ else
+ {
+ LOGP(DEBUG, "can't find face in");
+ }
+ // release the memory of face
+ cv_face_release_detector_result(p_face, face_count);
+ return p_feature;
+}
int stface_db_add(stface_handles& handles, const char *image_path) {
cv_feature_t *p_feature = stface_extract_feature(handles, image_path);
@@ -62,7 +127,22 @@
return idx;
}
-int stface_db_add(stface_handles& handles, const STFaceImage& image);//#todo
+int stface_db_add(stface_handles& handles, const STFaceImage& image)
+{
+ cv_feature_t *p_feature = stface_extract_feature(handles, image);
+ 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;
+}
bool stface_db_del(stface_handles& handles, int idx) {
if (idx < 0) {
@@ -104,6 +184,25 @@
LOGP(DEBUG, "load done!\n");
}
+ return true;
+}
+
+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)
+ {
+ LOG_WARN << "cv_verify_create_db return false" << LOG_ENDL;
+ return false;
+ }
+
+ cv_result = cv_verify_save_db(handles.handle_db, db_path);
+ if (cv_result != CV_OK)
+ {
+ LOG_WARN << "cv_verify_save_db return false" << LOG_ENDL;
+ return false;
+ }
+
return true;
}
@@ -193,7 +292,47 @@
return true;
}
-bool stface_search_db(stface_handles& handles, const STFaceImage& image, top_idx_score_vect_t& result);//#todo
+bool stface_search_db(stface_handles& handles, const STFaceImage& image, top_idx_score_vect_t& result)
+{
+ cv_feature_t *p_feature = stface_extract_feature(handles, image);
+ 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]);
+
+ result.push_back(TopIdxScore(top_idxs[t], 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_list(stface_handles& handles, char *image_path, char *list_path) {
cv_feature_t *p_feature = stface_extract_feature(handles, image_path);
--
Gitblit v1.8.0