xuxiuxi
2017-07-23 94e704acde59df60bd42f6f90b94d462d1bfa37c
FaceServer/sample_face_search.cpp
@@ -33,17 +33,17 @@
   cv_feature_t *p_feature = nullptr;
   cv_face_t *p_face = nullptr;
   int face_count = 0;
   cv_result_t st_result = CV_OK;
   st_result = cv_face_detect(handles.handle_detect, bgr_image.data, CV_PIX_FMT_BGR888,
   cv_result_t result = CV_OK;
   result = cv_face_detect(handles.handle_detect, bgr_image.data, CV_PIX_FMT_BGR888,
            bgr_image.cols, bgr_image.rows, bgr_image.step,
            CV_FACE_UP, &p_face, &face_count);
   if (face_count >= 1) {
      st_result = cv_verify_get_feature(handles.handle_verify,
      result = cv_verify_get_feature(handles.handle_verify,
                  (unsigned char *)bgr_image.data, CV_PIX_FMT_BGR888,
                  bgr_image.cols, bgr_image.rows, bgr_image.step,
                  p_face, &p_feature, nullptr);
      if (st_result != CV_OK) {
         LOGP(DEBUG, "cv_verify_get_feature failed, error code %d", st_result);
      if (result != CV_OK) {
         LOGP(DEBUG, "cv_verify_get_feature failed, error code %d", result);
      }
   } else {
      LOGP(DEBUG, "can't find face in %s", image_path);
@@ -56,7 +56,6 @@
cv_feature_t *stface_extract_feature(stface_handles& handles, const STFaceImage& image)
{
   cv_pixel_format stimgfmt = CV_PIX_FMT_GRAY8;
   int matType = 0;
   
   if (image.width > MAX_FACE_IMAGE_WIDTH || image.height > MAX_FACE_IMAGE_HEIGHT)
   {
@@ -73,7 +72,13 @@
      
      imgbufSize = image.height * image.width;
      stimgfmt = CV_PIX_FMT_GRAY8;
      matType = CV_8UC1;
   }
   else if (image.mb_type == MB_Frame::MBFT_NV12)
   {
      memcpy(imgbuf, image.buff, image.size);//#todo avoid mem cpy
      imgbufSize = image.height * image.width * 1.5;
      stimgfmt = CV_PIX_FMT_NV12;
   }
   else if (image.mb_type == MB_Frame::MBFT_RGB565)
   {
@@ -90,11 +95,10 @@
         image.width, image.height
         );
      
      imgbufSize = image.height * image.width;
      stimgfmt = CV_PIX_FMT_GRAY8;
      matType = CV_8UC1;
      imgbufSize = image.height * image.width * 1.5;
      stimgfmt = CV_PIX_FMT_YUV420P;
   }
   else if (image.mb_type == MB_Frame::MBFT_ARGB8888)
   else if (image.mb_type == MB_Frame::MBFT_RGB888)
   {
      //int ret = libyuv::ARGBToBGRA(
      //   image.buff, image.width * 4,
@@ -106,9 +110,8 @@
      //
      //imgbufSize = image.height * image.width * 4;
      //stimgfmt = CV_PIX_FMT_BGRA8888;
      //matType = CV_8UC4;
      int ret = libyuv::ARGBToI400(
      int ret = libyuv::ABGRToI400(
            image.buff, image.width * 4,
            imgbuf, image.width,
            image.width, image.height
@@ -118,7 +121,28 @@
      
      imgbufSize = image.height * image.width;
      stimgfmt = CV_PIX_FMT_GRAY8;
      matType = CV_8UC1;
      //{
      //   static int f = 0;
      //   ++f;
      //
      //   char fname[50];
      //   sprintf(fname, "st-%d-w%d-h%d.y8", f, image.width, image.height);
      //   FILE *pFile = fopen(fname, "wb");
      //   fwrite(imgbuf, 1, imgbufSize, pFile);
      //   fclose(pFile);
      //}
   }
   else if (image.mb_type == MB_Frame::MBFT_JPEG)
   {
      Mat matTmp = imdecode(_InputArray(image.buff, image.size), CV_LOAD_IMAGE_COLOR);
      //imwrite("aaa.jpg", matTmp);
      imgbufSize = matTmp.total() * matTmp.elemSize();
      memcpy(imgbuf, matTmp.ptr(), imgbufSize);
      stimgfmt = CV_PIX_FMT_BGR888;
   }
   else
   {
@@ -131,34 +155,28 @@
   //   ++f;
   //   
    //    char fname[50];
    //    sprintf(fname, "st-%d.rgb", f);
    //    sprintf(fname, "st-%d.img", f);
    //    FILE *pFile = fopen(fname, "wb");
    //    fwrite(image.buff, 1, image.size, pFile);
    //    fclose(pFile);
   //   
   //   sprintf(fname, "st-%d.yuv", f);
   //   sprintf(fname, "st-%d.img", f);
    //    pFile = fopen(fname, "wb");
    //    fwrite(imgbuf, 1, imgbufSize, pFile);
    //    fclose(pFile);
    //}
   Mat matImg(cv::Size(image.width, image.height), matType, imgbuf);
   if (!matImg.data)
   {
      return nullptr;
   }
   cv_feature_t *p_feature = nullptr;
   cv_face_t *p_face = nullptr;
   int face_count = 0;
   cv_result_t st_result = CV_OK;
   st_result = cv_face_detect(handles.handle_detect, matImg.data, stimgfmt, matImg.cols, matImg.rows, matImg.step, CV_FACE_UP, &p_face, &face_count);
   cv_result_t result = CV_OK;
   result = cv_face_detect(handles.handle_detect, (unsigned char *)imgbuf, stimgfmt, image.width, image.height, image.width, CV_FACE_UP, &p_face, &face_count);
   if (face_count >= 1)
   {
      st_result = cv_verify_get_feature(handles.handle_verify, (unsigned char *)matImg.data, stimgfmt, matImg.cols, matImg.rows, matImg.step, p_face, &p_feature, nullptr);
      if (st_result != CV_OK)
      result = cv_verify_get_feature(handles.handle_verify, (unsigned char *)imgbuf, stimgfmt, image.width, image.height, image.width, p_face, &p_feature, nullptr);
      if (result != CV_OK)
      {
         LOGP(DEBUG, "cv_verify_get_feature failed, error code %d", st_result);
         LOGP(DEBUG, "cv_verify_get_feature failed, error code %d", result);
      }
   }
   else
@@ -171,6 +189,35 @@
   return p_feature;
}
float stface_compare(stface_handles& handles, const STFaceImage& image1, const STFaceImage& image2)
{
   cv_feature_t* f1 = stface_extract_feature(handles, image1);
   if (f1 == nullptr)
   {
      LOGP(INFO, "can't find face in image1");
      return -1.0;
   }
   cv_feature_t* f2 = stface_extract_feature(handles, image2);
   if (f2 == nullptr)
   {
      cv_verify_release_feature(f1);
      LOGP(INFO, "can't find face in image2");
      return -1.0;
   }
   float score = -1.0;
   cv_result_t cv_result = cv_verify_compare_feature(handles.handle_verify, f1, f2, &score);
   if (cv_result != CV_OK) {
      LOGP(DEBUG, "cv_verify_compare_feature failed, error code %d", cv_result);
      score = -1.0;
   }
   cv_verify_release_feature(f1);
   cv_verify_release_feature(f2);
   return score;
}
int stface_db_add(stface_handles& handles, const char *image_path) {
   cv_feature_t *p_feature = stface_extract_feature(handles, image_path);
   if (!p_feature) {