当前位置: 首页>>代码示例>>C++>>正文


C++ TrainingSet::normalizer方法代码示例

本文整理汇总了C++中TrainingSet::normalizer方法的典型用法代码示例。如果您正苦于以下问题:C++ TrainingSet::normalizer方法的具体用法?C++ TrainingSet::normalizer怎么用?C++ TrainingSet::normalizer使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在TrainingSet的用法示例。


在下文中一共展示了TrainingSet::normalizer方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: makeFaceRecognizer

FaceRecognizer::FaceRecognizer(const TrainingSet& set, const double avgThRangeScale, const double maxThreshold):
  imgNorm_( set.normalizer() ),
  faceRecognizer_( makeFaceRecognizer() )
{
  if( set.entries().size() < 2 )
    throw Util::Exception{ UTIL_LOCSTRM << "training samples set needs to have at least 2 elements" };

  int                  nextFreeLabel = 0;
  std::vector<cv::Mat> faces;
  std::vector<int>     labels;
  std::vector<cv::Mat> testSet;
  // prepare space for the destination data
  faces.reserve( set.samples() );
  labels.reserve( set.samples() );
  labMap_.reserve( set.samples() );
  testSet.reserve( set.samples() );

  // prepare data in the format used by the learning algorithm
  for( const auto& e: set.entries() )
  {
    const std::string& name = e.first;
    const cv::Mat&     face = e.second;
    // find/create id to assign
    int id = -1;
    // initially this vector is ordered by string names
    const LabelMap::value_type searchValue(-1/*whatever*/, name);
    const auto                 swo = [](const LabelMap::value_type& lhs, const LabelMap::value_type& rhs) -> bool { return lhs.second < rhs.second; };
    const auto                 it  = std::lower_bound( labMap_.begin(), labMap_.end(), searchValue, swo );
    if( it==labMap_.end() || it->second!=name )
    {
      // assign new label
      id = nextFreeLabel;
      ++nextFreeLabel;
      labMap_.insert( it, LabelMap::value_type{id, name} );
      // first image from every class keep as a test one
      testSet.push_back(face);
      continue;
    }
    else
    {
      // use already assigned id
      assert( it->second==name );
      id = it->first;
    }
    // add to the containers
    faces.push_back(face);
    labels.push_back(id);
  }

  // learn data
  assert( faceRecognizer_.get() != nullptr );
  assert( faces.size() == labels.size() );
  //for(size_t i=0; i<faces.size(); ++i)
  faceRecognizer_->train(faces, labels);

  // set threshold according to what has been learned
  double thMin = 99999999999999;
  double thMax = 0;
  for(const auto& face: testSet)
  {
    int    label = -1;
    double dist  = -1;
    faceRecognizer_->predict(face, label, dist);
    if(dist<thMin)
      thMin = dist;
    if(dist>thMax)
      thMax = dist;
  }
  const double diff = thMax - thMin;
  threshold_ = thMin + diff * avgThRangeScale;
  if( threshold_ > maxThreshold )
    threshold_ = maxThreshold;

  // final version needs to be sorted by int ids, for easier search
  {
    const auto swo = [](const LabelMap::value_type& lhs, const LabelMap::value_type& rhs) -> bool { return lhs.first < rhs.first; };
    std::sort( labMap_.begin(), labMap_.end(), swo );
  }
  assert( labMap_.size() == static_cast<size_t>(nextFreeLabel) );
}
开发者ID:el-bart,项目名称:TIGER,代码行数:80,代码来源:FaceRecognizer.cpp


注:本文中的TrainingSet::normalizer方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。