From 52bd84663ef5bff417588dccd324937d506b9d95 Mon Sep 17 00:00:00 2001
From: houxiao <houxiao@454eff88-639b-444f-9e54-f578c98de674>
Date: 星期四, 27 四月 2017 18:38:13 +0800
Subject: [PATCH] optimize

---
 FaceServer/STFaceCache.cpp |   86 +++++++++++++++++++++++++++++++++---------
 1 files changed, 67 insertions(+), 19 deletions(-)

diff --git a/FaceServer/STFaceCache.cpp b/FaceServer/STFaceCache.cpp
index 6190f82..405fd71 100644
--- a/FaceServer/STFaceCache.cpp
+++ b/FaceServer/STFaceCache.cpp
@@ -9,7 +9,11 @@
 #include "sample_face_search.h"
 #include <cv_face.h>
 
+#define FDP_FDR_INVALID FDP_FaceDetectResult(0, 0)
 #define ENABLE_AUTO_CREATE_STFACEDB
+#define ENABLE_SEARCH_IN_NEGATIVE_DBID
+#define ENABLE_SEARCH_IN_NEGATIVE_DBID
+#define RESULT_CONFIDENCE 0.70
 
 struct STFaceCacheContext
 {
@@ -36,13 +40,22 @@
 	{
 		handles.handle_verify = ctx.handle_verify;
 		handles.handle_detect = ctx.handle_detect;
-		dbLoadOK = stface_db_load(handles, db_full_path().c_str());
-		dbLoadOK &= (handles.handle_db != nullptr);
+		if (handles.handle_db == nullptr)
+		{
+			dbLoadOK = stface_db_load(handles, db_full_path().c_str());
+			dbLoadOK &= (handles.handle_db != nullptr);
+		}
+		else
+		{
+			dbLoadOK = true;
+		}
 		return dbLoadOK;
 	}
 	
 	void close_db()
 	{
+		stface_db_save(handles, db_full_path().c_str());
+		
 		handles.handle_verify = nullptr;
 		handles.handle_detect = nullptr;
 		cv_verify_destroy_db(handles.handle_db);
@@ -85,10 +98,18 @@
 {
 	STFaceCacheContext& cacheContext(*(STFaceCacheContext*)_cacheContext);
 	
+	if (stface_init_license("./license.lic") != 0)
+	{
+		LOG_ERROR << "stface_init_license failed" << LOG_ENDL;
+		return false;
+	}
+	
+	cv_face_algorithm_info();
+	
 	cv_result_t cv_result = cv_face_create_detector(&cacheContext.handle_detect, nullptr, CV_DETECT_ENABLE_ALIGN_21);
 	if (cv_result != CV_OK)
 	{
-		LOGP(ERROR, "create detect handle failed, error code %d\n", cv_result);
+		LOGP(ERROR, "create detect handle failed, error code %d", cv_result);
 		return false;
 	}
 	
@@ -169,9 +190,27 @@
 	}
 }
 
+FDP_FaceDetectResult STFaceCache::detect_neg(const STFaceImage& img)
+{
+	if (img.db_id <= 0)
+	{
+		return FDP_FDR_INVALID;
+	}
+	
+#ifdef ENABLE_SEARCH_IN_NEGATIVE_DBID
+			STFaceImage imgNeg(img);
+			imgNeg.db_id = -1;
+			return detect(imgNeg);
+#else
+			return FDP_FDR_INVALID;
+#endif
+}
+
 FDP_FaceDetectResult STFaceCache::detect(const STFaceImage& img)
 {
 	stface_ctx_map_t& dbContext(*(stface_ctx_map_t*)_dbContext);
+	
+	LOG_WARN << "detect in dbid=" << img.db_id << LOG_ENDL;
 	
 	if (img.db_id == 0)
 	{
@@ -184,34 +223,41 @@
 		if (iterCtx == dbContext.end())
 		{
 			LOG_WARN << "no db find dbid=" << img.db_id << LOG_ENDL;
-			return FDP_FaceDetectResult(0, 0);
+			return detect_neg(img);
 		}
 		
 		STFaceDBContext& ctx(iterCtx->second);
 		if (!ctx.dbLoadOK)
 		{
 			LOG_WARN << "dbLoadOK return false" << LOG_ENDL;
-			return FDP_FaceDetectResult(0, 0);
+			return detect_neg(img);
 		}
 		
 		top_idx_score_vect_t result;
 		if (!stface_search_db(ctx.handles, img, result))
 		{
 			LOG_WARN << "stface_search_db return false" << LOG_ENDL;
-			return FDP_FaceDetectResult(0, 0);
+			return detect_neg(img);
 		}
 		
 		if (result.empty())
 		{
 			LOG_INFO << "stface_search_db return empty" << LOG_ENDL;
-			return FDP_FaceDetectResult(0, 0);
+			return detect_neg(img);
 		}
 		
 		LOGP(INFO, "stface_search_db return dbid=%d, idx=%d, score=%f", img.db_id, result[0].idx, result[0].score);
+
+		if (result[0].score < RESULT_CONFIDENCE)
+		{
+			LOGP(INFO, "low score not accepted");
+			return detect_neg(img);
+		}
+		
 		return FDP_FaceDetectResult(img.db_id, result[0].idx);
 	}
 
-	return FDP_FaceDetectResult(0, 0);
+	return FDP_FDR_INVALID;
 }
 
 FDP_FaceDetectResult STFaceCache::add(const STFaceImage& img)
@@ -219,10 +265,12 @@
 	stface_ctx_map_t& dbContext(*(stface_ctx_map_t*)_dbContext);
 	STFaceCacheContext& cacheContext(*(STFaceCacheContext*)_cacheContext);
 	
+	LOG_WARN << "add in dbid=" << img.db_id << LOG_ENDL;
+	
 	if (img.db_id == 0)
 	{
 		LOG_WARN << "db_id=0 not ok" << LOG_ENDL;
-		return FDP_FaceDetectResult(0, 0);
+		return FDP_FDR_INVALID;
 	}
 	
 	stface_ctx_map_t::iterator iterCtx = dbContext.find(img.db_id);
@@ -237,19 +285,16 @@
 		ctx.dbfname = dbfname;
 		ctx.dbfpath = stfacedbPath;
 		ctx.dbid = img.db_id;
+		ctx.init_db(cacheContext); // copy handlers
 		
 		if (!stface_db_create(ctx.handles, ctx.db_full_path().c_str()))
 		{
 			LOG_WARN << "stface_db_create return false" << LOG_ENDL;
-			return FDP_FaceDetectResult(0, 0);
+			return FDP_FDR_INVALID;
 		}
 		
-		if (!ctx.init_db(cacheContext))
-		{
-			LOG_WARN << "ctx.init_db return false" << LOG_ENDL;
-			return FDP_FaceDetectResult(0, 0);
-		}
-		
+		ctx.dbLoadOK = true;
+
 		dbContext.insert(std::make_pair(ctx.dbid, ctx));
 		iterCtx = dbContext.find(img.db_id);
 	}
@@ -258,17 +303,20 @@
 	if (iterCtx == dbContext.end())
 	{
 		LOG_ERROR << "no stfacedb found" << LOG_ENDL;
-		return FDP_FaceDetectResult(0, 0);
+		return FDP_FDR_INVALID;
 	}
 	
 	STFaceDBContext& ctx(iterCtx->second);
 	if (!ctx.dbLoadOK)
 	{
 		LOG_WARN << "dbLoadOK return false" << LOG_ENDL;
-		return FDP_FaceDetectResult(0, 0);
+		return FDP_FDR_INVALID;
 	}
 	
 	int idx = stface_db_add(ctx.handles, img);
 	LOG_INFO << "stface_db_add dbid=" << img.db_id << ", idx=" << idx << LOG_ENDL;
-	return FDP_FaceDetectResult(img.db_id, idx);
+	if (idx <= 1)
+		return FDP_FDR_INVALID;
+	else
+		return FDP_FaceDetectResult(img.db_id, idx);
 }

--
Gitblit v1.8.0