facerec.cpp 2.88 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright (c) 2011,2012. Philipp Wagner <bytefish[at]gmx[dot]de>.
 * Released to public domain under terms of the BSD Simplified license.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   * Neither the name of the organization nor the names of its contributors
 *     may be used to endorse or promote products derived from this software
 *     without specific prior written permission.
 *
 *   See <http://www.opensource.org/licenses/bsd-license>
 */
#include "precomp.hpp"
19
#include "opencv2/face.hpp"
20 21 22 23 24 25

namespace cv
{
namespace face
{

26
std::vector<int> FaceRecognizer::getLabelsByString(const String &str) const
27
{
28 29 30 31 32 33 34 35
  std::vector<int> labels;
  for (std::map<int, String>::const_iterator it = _labelsInfo.begin(); it != _labelsInfo.end(); it++)
  {
      size_t found = (it->second).find(str);
      if (found != String::npos)
          labels.push_back(it->first);
  }
  return labels;
36 37
}

38
String FaceRecognizer::getLabelInfo(int label) const
39
{
40 41
    std::map<int, String>::const_iterator iter(_labelsInfo.find(label));
    return iter != _labelsInfo.end() ? iter->second : "";
42 43
}

44
void FaceRecognizer::setLabelInfo(int label, const String &strInfo)
45
{
46
    _labelsInfo[label] = strInfo;
47 48
}

49
void FaceRecognizer::update(InputArrayOfArrays src, InputArray labels)
50
{
51 52 53 54
    (void)src;
    (void)labels;
    String error_msg = format("This FaceRecognizer does not support updating, you have to use FaceRecognizer::train to update it.");
    CV_Error(Error::StsNotImplemented, error_msg);
55 56
}

57
void FaceRecognizer::read(const String &filename)
58
{
59 60
    FileStorage fs(filename, FileStorage::READ);
    if (!fs.isOpened())
61
        CV_Error(Error::StsError, "File can't be opened for reading!");
62
    this->read(fs.getFirstTopLevelNode());
63
    fs.release();
64 65
}

66
void FaceRecognizer::write(const String &filename) const
67
{
68 69 70
    FileStorage fs(filename, FileStorage::WRITE);
    if (!fs.isOpened())
        CV_Error(Error::StsError, "File can't be opened for writing!");
71
    fs << getDefaultName() << "{";
72
    this->write(fs);
73
    fs << "}";
74
    fs.release();
75 76
}

77 78 79 80 81 82 83 84
int FaceRecognizer::predict(InputArray src) const {
    int _label;
    double _dist;
    predict(src, _label, _dist);
    return _label;
}

void FaceRecognizer::predict(InputArray src, CV_OUT int &label, CV_OUT double &confidence) const {
85 86 87 88
    Ptr<StandardCollector> collector = StandardCollector::create(getThreshold());
    predict(src, collector);
    label = collector->getMinLabel();
    confidence = collector->getMinDist();
89 90
}

91 92 93
}
}