dev
houxiao
2017-04-21 f757a27218a09fd24a852f34b6490d32fe237017
FaceServer/STFaceCache.cpp
@@ -7,18 +7,64 @@
#include <dirent.h>
#include "sample_face_search.h"
#include <cv_face.h>
struct STFaceContext
struct STFaceCacheContext
{
   int dbid;
   std::string dbfn;
   stface_handles handles;
   cv_handle_t handle_verify;
   cv_handle_t handle_detect;
   STFaceCacheContext() : handle_verify(nullptr), handle_detect(nullptr)
   {
   }
};
typedef std::map<int, STFaceContext> stface_ctx_map_t; // <dbid, ctx>
struct STFaceDBContext
{
   int dbid;
   std::string dbfname;
   std::string dbfpath;
   stface_handles handles;
   bool dbLoadOK;
   STFaceDBContext() : dbid(0), dbfname(), dbfpath(), handles(), dbLoadOK(false)
   {}
   bool init_db(const STFaceCacheContext& ctx)
   {
      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);
      return dbLoadOK;
   }
   void close_db()
   {
      handles.handle_verify = nullptr;
      handles.handle_detect = nullptr;
      cv_verify_destroy_db(handles.handle_db);
      handles.handle_db = nullptr;
      dbLoadOK = false;
   }
   std::string db_full_path() const
   {
      if (dbfpath.empty() || dbfname.empty())
         return "";
      std::string fn(dbfpath);
      fn.append("/");
      fn.append(dbfname);
      return fn;
   }
};
typedef std::map<int, STFaceDBContext> stface_ctx_map_t; // <dbid, ctx>
STFaceCache::STFaceCache(const std::string& _stfacedbPath)
   : stfacedbPath(_stfacedbPath), stfaceModels(STFACESDK_BASE "/models"), _cacheContext(new stface_ctx_map_t)
   : stfacedbPath(_stfacedbPath), stfaceModels(STFACESDK_BASE "/models"),
   _dbContext(new stface_ctx_map_t), _cacheContext(new STFaceCacheContext)
{
   LOG_INFO << "st face db: " << stfacedbPath << LOG_ENDL;
   LOG_INFO << "st face sdk models: " << stfaceModels << LOG_ENDL;
@@ -26,13 +72,50 @@
STFaceCache::~STFaceCache()
{
   delete (stface_ctx_map_t*)_cacheContext;
   delete (stface_ctx_map_t*)_dbContext;
   _dbContext = nullptr;
   delete (STFaceCacheContext*)_cacheContext;
   _cacheContext = nullptr;
}
bool STFaceCache::init()
{
   STFaceCacheContext& cacheContext(*(STFaceCacheContext*)_cacheContext);
   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);
      return false;
   }
   std::string modelsFile(stfaceModels);
   modelsFile.append("/verify.model");
   cv_result = cv_verify_create_handle(&cacheContext.handle_verify, modelsFile.c_str());
   if (cv_result != CV_OK)
   {
      LOGP(ERROR, "create verify handle failed, error code %d\n", cv_result);
      return false;
   }
   return true;
}
void STFaceCache::finit()
{
   STFaceCacheContext& cacheContext(*(STFaceCacheContext*)_cacheContext);
   cv_face_destroy_detector(cacheContext.handle_detect);
   cv_verify_destroy_handle(cacheContext.handle_verify);
   cacheContext.handle_detect = nullptr;
   cacheContext.handle_verify = nullptr;
}
bool STFaceCache::load_dbs()
{
   stface_ctx_map_t& cacheContext(*(stface_ctx_map_t*)_cacheContext);
   stface_ctx_map_t& dbContext(*(stface_ctx_map_t*)_dbContext);
   STFaceCacheContext& cacheContext(*(STFaceCacheContext*)_cacheContext);
   
   DIR* dp = opendir(stfacedbPath.c_str());
   if (dp != NULL)
@@ -42,9 +125,18 @@
      {
         //puts(ep->d_name);
         
         STFaceContext ctx;
         ctx.dbfn = ep->d_name;
         ctx.dbid = strtol(ctx.dbfn.c_str(), nullptr, 10);
         STFaceDBContext ctx;
         ctx.dbfname = ep->d_name;
         ctx.dbfpath = stfacedbPath;
         ctx.dbid = strtol(ctx.dbfname.c_str(), nullptr, 10);
         if (ctx.dbfname.find(".stfacedb") == std::string::npos)
            continue;
         if (ctx.dbid == 0)
            continue;
         dbContext.insert(std::make_pair(ctx.dbid, ctx));
      }
      closedir(dp);
   }
@@ -53,16 +145,123 @@
      LOG_ERROR << "Couldn't open the directory." << LOG_ENDL;
      return false;
   }
   for (stface_ctx_map_t::iterator iterCtx = dbContext.begin(); iterCtx != dbContext.end(); ++iterCtx)
   {
      STFaceDBContext& ctx(iterCtx->second);
      bool ret = ctx.init_db(cacheContext);
      LOG_INFO << "init stface db dbid=" << ctx.dbid << ", ret=" << ret << LOG_ENDL;
   }
}
void STFaceCache::close_dbs()
{
   stface_ctx_map_t& dbContext(*(stface_ctx_map_t*)_dbContext);
   for (stface_ctx_map_t::iterator iterCtx = dbContext.begin(); iterCtx != dbContext.end(); ++iterCtx)
   {
      STFaceDBContext& ctx(iterCtx->second);
      ctx.close_db();
   }
}
FDP_FaceDetectResult STFaceCache::detect(const STFaceImage& img)
{
   stface_ctx_map_t& cacheContext(*(stface_ctx_map_t*)_cacheContext);
   stface_ctx_map_t& dbContext(*(stface_ctx_map_t*)_dbContext);
   if (img.db_id == 0)
   {
      // search all db
      //#todo
   }
   else
   {
      stface_ctx_map_t::iterator iterCtx = dbContext.find(img.db_id);
      if (iterCtx == dbContext.end())
      {
         LOG_WARN << "no db find dbid=" << img.db_id << LOG_ENDL;
         return FDP_FaceDetectResult(0, 0);
      }
      STFaceDBContext& ctx(iterCtx->second);
      if (!ctx.dbLoadOK)
      {
         LOG_WARN << "dbLoadOK return false" << LOG_ENDL;
         return FDP_FaceDetectResult(0, 0);
      }
      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);
      }
      if (result.empty())
      {
         LOG_INFO << "stface_search_db return empty" << LOG_ENDL;
         return FDP_FaceDetectResult(0, 0);
      }
      LOGP(INFO, "stface_search_db return dbid=%d, idx=%d, score=%f", img.db_id, result[0].idx, result[0].score);
      return FDP_FaceDetectResult(img.db_id, result[0].idx);
   }
   return FDP_FaceDetectResult(0, 0);
}
FDP_FaceDetectResult STFaceCache::save(const STFaceImage& img)
FDP_FaceDetectResult STFaceCache::add(const STFaceImage& img)
{
   stface_ctx_map_t& cacheContext(*(stface_ctx_map_t*)_cacheContext);
   return FDP_FaceDetectResult(0, 0);
   stface_ctx_map_t& dbContext(*(stface_ctx_map_t*)_dbContext);
   STFaceCacheContext& cacheContext(*(STFaceCacheContext*)_cacheContext);
   if (img.db_id == 0)
   {
      LOG_WARN << "db_id=0 not ok" << LOG_ENDL;
      return FDP_FaceDetectResult(0, 0);
   }
   stface_ctx_map_t::iterator iterCtx = dbContext.find(img.db_id);
   if (iterCtx == dbContext.end())
   {
      // create db
      STFaceDBContext ctx;
      char dbfname[50];
      sprintf(dbfname, "%d.stfacedb", img.db_id);
      ctx.dbfname = dbfname;
      ctx.dbfpath = stfacedbPath;
      ctx.dbid = img.db_id;
      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);
      }
      if (!ctx.init_db(cacheContext))
      {
         LOG_WARN << "ctx.init_db return false" << LOG_ENDL;
         return FDP_FaceDetectResult(0, 0);
      }
      dbContext.insert(std::make_pair(ctx.dbid, ctx));
      iterCtx = dbContext.find(img.db_id);
   }
   if (iterCtx == dbContext.end())
   {
      LOG_ERROR << "no stfacedb found" << LOG_ENDL;
      return FDP_FaceDetectResult(0, 0);
   }
   STFaceDBContext& ctx(iterCtx->second);
   if (!ctx.dbLoadOK)
   {
      LOG_WARN << "dbLoadOK return false" << LOG_ENDL;
      return FDP_FaceDetectResult(0, 0);
   }
   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);
}