From 94e704acde59df60bd42f6f90b94d462d1bfa37c Mon Sep 17 00:00:00 2001
From: xuxiuxi <xuxiuxi@454eff88-639b-444f-9e54-f578c98de674>
Date: 星期日, 23 七月 2017 12:09:41 +0800
Subject: [PATCH] 

---
 FaceServer/STFaceCache.cpp |  133 +++++++++++++++++++++++++++++++++++++++----
 1 files changed, 119 insertions(+), 14 deletions(-)

diff --git a/FaceServer/STFaceCache.cpp b/FaceServer/STFaceCache.cpp
index 23621a4..6d5026d 100644
--- a/FaceServer/STFaceCache.cpp
+++ b/FaceServer/STFaceCache.cpp
@@ -9,12 +9,6 @@
 #include "sample_face_search.h"
 #include <cv_face.h>
 
-#define FDP_FDR_INVALID FDP_FaceDetectResult(0, 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
 {
 	cv_handle_t handle_verify;
@@ -72,7 +66,7 @@
 		handles.handle_db = nullptr;
 		dbLoadOK = false;
 	}
-	
+
 	std::string db_full_path() const
 	{
 		if (dbfpath.empty() || dbfname.empty())
@@ -89,7 +83,8 @@
 
 STFaceCache::STFaceCache(const std::string& _stfacedbPath)
 	: stfacedbPath(_stfacedbPath), stfaceModels(STFACESDK_BASE "/models"), 
-	_dbContext(new stface_ctx_map_t), _cacheContext(new STFaceCacheContext)
+	_dbContext(new stface_ctx_map_t), _cacheContext(new STFaceCacheContext), 
+	tempdbFaceCount(0)
 {
 	LOG_INFO << "st face db: " << stfacedbPath << LOG_ENDL;
 	LOG_INFO << "st face sdk models: " << stfaceModels << LOG_ENDL;
@@ -207,13 +202,22 @@
 		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;
+#ifdef ENABLE_DETECT_IN_NEGATIVE_DBID
+	STFaceImage imgNeg(img);
+	FDP_FaceDetectResult res(FDP_FDR_INVALID);
+	
+	imgNeg.db_id = STFS_DBID_VISITOR_1;
+	res = detect(imgNeg); // #todo optimize extract feature once
+	if (res.db_id != STFS_DBID_INVALID)
+		return res;
+	
+	imgNeg.db_id = STFS_DBID_TEMPDB_1;
+	res = detect(imgNeg);
+	if (res.db_id != STFS_DBID_INVALID)
+		return res;
 #endif
+
+	return FDP_FDR_INVALID;
 }
 
 FDP_FaceDetectResult STFaceCache::detect(const STFaceImage& img)
@@ -323,6 +327,10 @@
 		return FDP_FDR_INVALID;
 	}
 	
+#ifdef ENABLE_ADD_TO_TEMPDB_WHEN_NOT_DETECT
+	tempdbFaceCount++;
+#endif
+	
 	int idx = stface_db_add(ctx.handles, img);
 	LOG_INFO << "stface_db_add dbid=" << img.db_id << ", idx=" << idx << LOG_ENDL;
 	if (idx <= 1)
@@ -341,3 +349,100 @@
 	FDP_FaceDetectResult result(0, 0, int(c * 1000));
 	return result;
 }
+
+void STFaceCache::search(const STFaceImage& img, fdr_vec_t& topResult)
+{
+	stface_ctx_map_t& dbContext(*(stface_ctx_map_t*)_dbContext);
+	STFaceCacheContext& cacheContext(*(STFaceCacheContext*)_cacheContext);
+
+	for(stface_ctx_map_t::iterator iterCtx = dbContext.begin(); iterCtx != dbContext.end(); ++iterCtx)
+	{
+		STFaceDBContext& ctx(iterCtx->second);
+		if (!ctx.dbLoadOK)
+		{
+			LOG_WARN << "dbLoadOK return false" << LOG_ENDL;
+			continue;
+		}
+		
+		top_idx_score_vect_t result;
+		if (!stface_search_db(ctx.handles, img, result))
+		{
+			LOG_WARN << "stface_search_db return false" << LOG_ENDL;
+			continue;
+		}
+		
+		if (result.empty())
+		{
+			LOG_INFO << "stface_search_db return empty" << LOG_ENDL;
+			continue;
+		}
+		
+		for(top_idx_score_vect_t::iterator it = result.begin(); it != result.end(); ++it)
+		{
+			int16_t new_confidence = it->score * 1000;
+
+			fdr_vec_t::reverse_iterator rtTR = topResult.rbegin();
+			if(!topResult.empty())
+			{
+				while(new_confidence > rtTR->confidence)
+					++rtTR;
+			}
+
+			fdr_vec_t::iterator itTR(rtTR.base());
+			topResult.insert(itTR, FDP_FaceDetectResult(ctx.dbid, it->idx, new_confidence));
+
+			
+			while(topResult.size() > 5)
+				topResult.pop_back();
+
+			
+			//if(topResult.empty())
+			//	topResult.push_back(FDP_FaceDetectResult(ctx.dbid, it->idx, new_confidence));
+			//
+            //
+			//else if(new_confidence > topResult.rbegin()->confidence)
+			//{
+			//	while(topResult.size() > 4)
+			//		topResult.pop_back();
+			//	for(fdr_vec_t::reverse_iterator rtTR = topResult.rbegin() + 1; rtTR != topResult.rend(); ++rtTR)
+			//	{
+			//		if(new_confidence < rtTR->confidence)
+			//		{
+			//			fdr_vec_t::iterator itTR(rtTR.base());
+			//			topResult.insert(itTR, FDP_FaceDetectResult(ctx.dbid, it->idx, new_confidence));
+			//		}
+			//	}
+			//}
+		}
+
+		//LOGP(INFO, "stface_search_db return dbid=%d, idx=%d, score=%f", img.db_id, result[0].idx, result[0].score);
+	}
+	
+	/*
+	topResult.push_back(FDP_FaceDetectResult(1, 2, 3));
+	topResult.push_back(FDP_FaceDetectResult(-1, 2, 4));
+	topResult.push_back(FDP_FaceDetectResult(1, 6, 5));
+	*/
+}
+
+void STFaceCache::delete_db(int dbid)
+{
+	stface_ctx_map_t& dbContext(*(stface_ctx_map_t*)_dbContext);
+	STFaceCacheContext& cacheContext(*(STFaceCacheContext*)_cacheContext);
+
+	stface_ctx_map_t::iterator iterCtx = dbContext.find(dbid);
+	if (iterCtx == dbContext.end())
+	{
+		LOG_WARN << "no dbid=%d" << dbid << LOG_ENDL;
+		return;
+	}
+	
+	STFaceDBContext& ctx(iterCtx->second);
+	ctx.close_db();
+	
+	char mvDBtoBak[1000];
+	sprintf(mvDBtoBak, "cd %s; mv %s %d.bak;", stfacedbPath.c_str(), ctx.db_full_path().c_str(), dbid);
+	system(mvDBtoBak);
+	
+	dbContext.erase(iterCtx);
+}

--
Gitblit v1.8.0