xuxiuxi
2017-05-11 109ffe9a777658936a38d0c146579a67c60a0d17
FaceServer/main_face_daemon.cpp
@@ -6,7 +6,7 @@
#include "face_daemon_proto.h"
#include "sample_face_search.h"
#include "STFaceCache.h"
#include <PbFaceList.pb.h>
#include "PbFaceList.pb.h"
#include <signal.h>
@@ -25,11 +25,11 @@
   server_stop();
}
bool send_SensetimeFaceDetectResultJson(EVClientStub& client, const fdr_vec_t& result)
bool send_SensetimeFaceDetectResultJson(EVClientStub& client, const fdr_vec_t& result, int ret = 0)
{
   std::stringstream ss;
   ss << "{" << LOG_ENDL;
   ss << "\"ret\":" << WRAPPER_TEXT(0) << "," << LOG_ENDL;
   ss << "\"ret\":" << WRAPPER_TEXT(ret) << "," << LOG_ENDL;
   ss << "\"count\":" << WRAPPER_TEXT(result.size()) << "," << LOG_ENDL;
   ss << "\"result\":[";
   for(fdr_vec_t::const_iterator iter = result.begin(); iter != result.end(); ++iter)
@@ -42,8 +42,8 @@
   ss << "}";
   std::string output(ss.str());//#todo avoid copy
   client.sendBuffSize = output.size() + 1;
   client.sendBuff = new uint8_t[client.sendBuffSize];
   client.sendBuffSize = output.size();
   client.sendBuff = new uint8_t[output.size() + 1];
   strcpy((char*)client.sendBuff, output.c_str());
   client.deleteSendBuff = true;
   
@@ -61,6 +61,7 @@
   
   EVPHeader* evpHeader = (EVPHeader*)client.recvBuff;
   FDP_FaceDetectPB* fdpFaceDetectPB = (FDP_FaceDetectPB*)(client.recvBuff + sizeof(EVPHeader));
   fdpFaceDetectPB->ntoh();
   
   PbFaceList pbFaceList;
   pbFaceList.ParseFromArray(client.recvBuff + sizeof(EVPHeader) + sizeof(FDP_FaceDetectPB), evpHeader->size - sizeof(EVPHeader) - sizeof(FDP_FaceDetectPB));
@@ -103,6 +104,39 @@
   return send_SensetimeFaceDetectResultJson(client, result);
}
bool ev_proc_SensetimeFaceDetectSave(EVClientStub& client)
{
   EVPHeader* evpHeader = (EVPHeader*)client.recvBuff;
   FDP_Image* fdpImage = (FDP_Image*)(client.recvBuff + sizeof(EVPHeader));
   fdpImage->ntoh();
   STFaceImage stfaceImg;
   stfaceImg.db_id = fdpImage->db_id;
   stfaceImg.mb_type = fdpImage->mb_type;
   stfaceImg.width = fdpImage->width;
   stfaceImg.height = fdpImage->height;
   stfaceImg.size = evpHeader->size - sizeof(EVPHeader) - sizeof(FDP_Image);
   stfaceImg.buff = fdpImage->buff;
   LOGP(DEBUG, "stfaceImg db_id=%d, mb_type=%d, width=%d, height=%d, size=%d",
      (int)stfaceImg.db_id, (int)stfaceImg.mb_type, (int)stfaceImg.width, (int)stfaceImg.height, (int)stfaceImg.size);
   //char imgfn[100 * 1024];
   //static int i = 0;
   //sprintf(imgfn, "IMG_%d_%d_w%d_h%d.rgb565", stfaceImg.db_id, ++i, stfaceImg.width, stfaceImg.height);
   //FILE * pFile = fopen(imgfn, "wb");
   //fwrite(stfaceImg.buff, sizeof(char), stfaceImg.size, pFile);
   //fclose(pFile);
   //pFile = nullptr;
   fdr_vec_t result;
   FDP_FaceDetectResult fdrResult = g_STFaceCache.add(stfaceImg);
   result.push_back(fdrResult);
   int ret = (fdrResult.db_id == 0 ? -1 : 0);
   return send_SensetimeFaceDetectResultJson(client, result, ret);
}
bool ev_dispatcher_proto_pb(EVClientStub& client)
{
   LOG_DEBUG << "ev_dispatcher_proto_pb" << LOG_ENDL;
@@ -131,6 +165,33 @@
   return false;
}
bool ev_dispatcher_proto_rawbin(EVClientStub& client)
{
   LOG_DEBUG << "ev_dispatcher_proto_rawbin" << LOG_ENDL;
   EVPHeader* evpHeader = (EVPHeader*)client.recvBuff;
   if (evpHeader->size != client.recvBuffSize)
   {
      LOG_WARN << "Truncated buffer " << (evpHeader->size - client.recvBuffSize) << " bytes" << LOG_ENDL;
      return false;
   }
   switch(evpHeader->cmd)
   {
   case FaceDaemonCommand::FDC_SENSETIMEFACEDETECT_SAVE:
      return ev_proc_SensetimeFaceDetectSave(client);
   break;
   default:
      LOG_WARN << "Unknown command" << LOG_ENDL;
      ev_send_status_packet(client, EVPStatus::EVPS_COMMAND_ERROR);
      return false;
   break;
   }
   // return false to disconnect
   return false;
}
bool ev_dispatcher_proto(EVClientStub& client)
{
   LOG_DEBUG << "ev_dispatcher_proto" << LOG_ENDL;
@@ -147,6 +208,9 @@
   case EVPProto::EVPP_PROTOBUF:
      return ev_dispatcher_proto_pb(client);
   break;
   case EVPProto::EVPP_RAW_BIN:
      return ev_dispatcher_proto_rawbin(client);
   break;
   default:
      LOG_WARN << "Unknown proto" << LOG_ENDL;
      ev_send_status_packet(client, EVPStatus::EVPS_PROTO_ERROR);