Пример #1
0
  int CharsIdentify::identify(std::vector<cv::Mat> inputs, std::vector<std::pair<std::string, std::string>>& outputs,
    std::vector<bool> isChineseVec) {
    Mat featureRows;
    size_t input_size = inputs.size();
    for (size_t i = 0; i < input_size; i++) {
      Mat input = inputs[i];
      cv::Mat feature = charFeatures(input, kPredictSize);
      featureRows.push_back(feature);
    }

    std::vector<int> maxIndexs;
    std::vector<float> maxVals;
    classify(featureRows, maxIndexs, maxVals, isChineseVec);

    for (size_t row_index = 0; row_index < input_size; row_index++) {
      int index = maxIndexs[row_index];
      if (index < kCharactersNumber) {
        outputs[row_index] = std::make_pair(kChars[index], kChars[index]);
      }
      else {
        const char* key = kChars[index];
        std::string s = key;
        std::string province = kv_->get(s);
        outputs[row_index] = std::make_pair(s, province);
      }
    }
    return 0;
  }
Пример #2
0
  bool CharsIdentify::isCharacter(cv::Mat input, std::string& label, float& maxVal, bool isChinese) {
    cv::Mat feature = charFeatures(input, kPredictSize);
    auto index = static_cast<int>(classify(feature, maxVal, isChinese));

    if (isChinese) {
      //std::cout << "maxVal:" << maxVal << std::endl;
    }


    float chineseMaxThresh = 0.2f;
    //float chineseMaxThresh = CParams::instance()->getParam2f();

    if (maxVal >= 0.9 || (isChinese && maxVal >= chineseMaxThresh)) {
      if (index < kCharactersNumber) {
        label = std::make_pair(kChars[index], kChars[index]).second;
      }
      else {
        const char* key = kChars[index];
        std::string s = key;
        std::string province = kv_->get(s);
        label = std::make_pair(s, province).second;
      }
      return true;
    }
    else
      return false;
  }
Пример #3
0
  std::pair<std::string, std::string> CharsIdentify::identifyChinese(cv::Mat input) {
    cv::Mat feature = charFeatures(input, kPredictSize);
    float maxVal = -2;

    int result = -1;

    cv::Mat output(1, kChineseNumber, CV_32FC1);
    ann_->predict(feature, output);

    for (int j = 0; j < kChineseNumber; j++) {
      float val = output.at<float>(j);
      // std::cout << "j:" << j << "val:" << val << std::endl;
      if (val > maxVal) {
        maxVal = val;
        result = j;
      }
    }

    auto index = result + kCharsTotalNumber - kChineseNumber;
    const char* key = kChars[index];
    std::string s = key;
    std::string province = kv_->get(s);

    return std::make_pair(s, province);
  }
Пример #4
0
  void CharsIdentify::classify(std::vector<CCharacter>& charVec){
    size_t charVecSize = charVec.size();

    if (charVecSize == 0)
      return;

    Mat featureRows;
    for (size_t index = 0; index < charVecSize; index++) {
      Mat charInput = charVec[index].getCharacterMat();
      Mat feature = charFeatures(charInput, kPredictSize);
      featureRows.push_back(feature);
    }

    cv::Mat output(charVecSize, kCharsTotalNumber, CV_32FC1);
    ann_->predict(featureRows, output);

    for (size_t output_index = 0; output_index < charVecSize; output_index++) {
      CCharacter& character = charVec[output_index];
      Mat output_row = output.row(output_index);

      int result = -1;
      float maxVal = -2.f;
      std::string label = "";

      bool isChinses = character.getIsChinese();
      if (!isChinses) {
        result = 0;
        for (int j = 0; j < kCharactersNumber; j++) {
          float val = output_row.at<float>(j);
          //std::cout << "j:" << j << "val:" << val << std::endl;
          if (val > maxVal) {
            maxVal = val;
            result = j;
          }
        }
        label = std::make_pair(kChars[result], kChars[result]).second;
      }
      else {
        result = kCharactersNumber;
        for (int j = kCharactersNumber; j < kCharsTotalNumber; j++) {
          float val = output_row.at<float>(j);
          //std::cout << "j:" << j << "val:" << val << std::endl;
          if (val > maxVal) {
            maxVal = val;
            result = j;
          }
        }
        const char* key = kChars[result];
        std::string s = key;
        std::string province = kv_->get(s);
        label = std::make_pair(s, province).second;
      }
      /*std::cout << "result:" << result << std::endl;
      std::cout << "maxVal:" << maxVal << std::endl;*/
      character.setCharacterScore(maxVal);
      character.setCharacterStr(label);
    }
  }
Пример #5
0
  void CharsIdentify::classifyChinese(std::vector<CCharacter>& charVec){
    size_t charVecSize = charVec.size();

    if (charVecSize == 0)
      return;

    Mat featureRows;
    for (size_t index = 0; index < charVecSize; index++) {
      Mat charInput = charVec[index].getCharacterMat();
      Mat feature = charFeatures(charInput, kChineseSize);
      featureRows.push_back(feature);
    }

    cv::Mat output(charVecSize, kChineseNumber, CV_32FC1);
    annChinese_->predict(featureRows, output);

    for (size_t output_index = 0; output_index < charVecSize; output_index++) {
      CCharacter& character = charVec[output_index];
      Mat output_row = output.row(output_index);
      bool isChinese = true;

      float maxVal = -2;
      int result = -1;

      for (int j = 0; j < kChineseNumber; j++) {
        float val = output_row.at<float>(j);
        //std::cout << "j:" << j << "val:" << val << std::endl;
        if (val > maxVal) {
          maxVal = val;
          result = j;
        }
      }

      // no match
      if (-1 == result) {
        result = 0;
        maxVal = 0;
        isChinese = false;
      }

      auto index = result + kCharsTotalNumber - kChineseNumber;
      const char* key = kChars[index];
      std::string s = key;
      std::string province = kv_->get(s);

      /*std::cout << "result:" << result << std::endl;
      std::cout << "maxVal:" << maxVal << std::endl;*/

      character.setCharacterScore(maxVal);
      character.setCharacterStr(province);
      character.setIsChinese(isChinese);
    }
  }
Пример #6
0
 std::pair<std::string, std::string> CharsIdentify::identify(cv::Mat input, bool isChinese) {
   cv::Mat feature = charFeatures(input, kPredictSize);
   float maxVal = -2;
   auto index = static_cast<int>(classify(feature, maxVal, isChinese));
   if (index < kCharactersNumber) {
     return std::make_pair(kChars[index], kChars[index]);
   }
   else {
     const char* key = kChars[index];
     std::string s = key;
     std::string province = kv_->get(s);
     return std::make_pair(s, province);
   }
 }
Пример #7
0
  std::pair<std::string, std::string> CharsIdentify::identifyChinese(cv::Mat input, float& out, bool& isChinese) {
    cv::Mat feature = charFeatures(input, kChineseSize);
    float maxVal = -2;

    int result = -1;

    cv::Mat output(1, kChineseNumber, CV_32FC1);
    annChinese_->predict(feature, output);

    for (int j = 0; j < kChineseNumber; j++) {
      float val = output.at<float>(j);
      //std::cout << "j:" << j << "val:" << val << std::endl;
      if (val > maxVal) {
        maxVal = val;
        result = j;
      }
    }

    // no match
    if (-1 == result) {
      result = 0;
      maxVal = 0;
      isChinese = false;
    }
    else if (maxVal > 0.9){
      isChinese = true;
    }

    auto index = result + kCharsTotalNumber - kChineseNumber;
    const char* key = kChars[index];
    std::string s = key;
    std::string province = kv_->get(s);

    out = maxVal;

    return std::make_pair(s, province);
  }