From da5f42c998aaad43386ebcad7ae07566f33b7d0b Mon Sep 17 00:00:00 2001
From: pans <pans@454eff88-639b-444f-9e54-f578c98de674>
Date: 星期三, 28 十二月 2016 12:22:28 +0800
Subject: [PATCH] 

---
 RtspFace/demo/faceDB.cpp |   90 ++++++++++++++++++++++++++++++++-------------
 1 files changed, 64 insertions(+), 26 deletions(-)

diff --git a/RtspFace/demo/faceDB.cpp b/RtspFace/demo/faceDB.cpp
index 16f5575..2227f36 100644
--- a/RtspFace/demo/faceDB.cpp
+++ b/RtspFace/demo/faceDB.cpp
@@ -9,6 +9,7 @@
 	// 创建人脸数据库句柄
 	cv_verify_create_db(&handle_db);
 	cv_verify_create_handle(&handle_verify, "verify.model");
+	cv_face_create_detector(&handle_detect, NULL, CV_DETECT_ENABLE_ALIGN_21);
 	db_load();
 }
 
@@ -25,28 +26,28 @@
 }*/
 
 //数据库添加数据,并返回记录得id
-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);
-	}
-	cv_verify_release_feature(p_feature);
+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);
+	}
+	cv_verify_release_feature(p_feature);
 	return idx;
 }
 
 
 //数据库保存
 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_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;
 }
 
@@ -60,15 +61,15 @@
 	{
 		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");
-	}
-
+	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;
 }
 
@@ -97,7 +98,10 @@
 	} else {
 		fprintf(stderr, "cv_verify_search_face failed, error code %d\n", cv_result);
 	}
-	indx=top_idxs[0];
+	if (top_scores[0] != 0)
+	{
+		indx=top_idxs[0];
+	}
 	if (top_idxs) {
 		delete[]top_idxs;
 	}
@@ -106,4 +110,38 @@
 	}
 	cv_verify_release_feature(p_feature);
 	return indx;
+}
+
+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");
+	}
+
+	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;
 }
\ No newline at end of file

--
Gitblit v1.8.0