Class: OpenCV::FaceRecognizer

Inherits:
Algorithm show all
Defined in:
ext/opencv/lbph.cpp,
ext/opencv/eigenfaces.cpp,
ext/opencv/fisherfaces.cpp,
ext/opencv/facerecognizer.cpp

Direct Known Subclasses

EigenFaces, FisherFaces, LBPH

Instance Method Summary collapse

Methods inherited from Algorithm

#get_bool, #get_double, #get_int, #get_mat, #get_matvector, #get_string, #name, #set_algorithm, #set_bool, #set_double, #set_int, #set_mat, #set_matvector, #set_string

Instance Method Details

#load(filename) ⇒ Object

Loads a FaceRecognizer and its model state.



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'ext/opencv/facerecognizer.cpp', line 175

VALUE
rb_load(VALUE self, VALUE filename)
{
  Check_Type(filename, T_STRING);
  cv::FaceRecognizer *self_ptr = FACERECOGNIZER(self);
  try {
    char* s = StringValueCStr(filename);
    self_ptr->load(std::string(s));
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }

  return Qnil;
}

#predict(src) ⇒ Object

Predicts a label and associated confidence (e.g. distance) for a given input image.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'ext/opencv/facerecognizer.cpp', line 129

VALUE
rb_predict(VALUE self, VALUE src)
{
  cv::Mat mat = cv::Mat(CVMAT_WITH_CHECK(src));
  cv::FaceRecognizer *self_ptr = FACERECOGNIZER(self);
  int label;
  double confidence;
  try {
    self_ptr->predict(mat, label, confidence);
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }

  return rb_ary_new3(2, INT2NUM(label), DBL2NUM(confidence));
}

#save(filename) ⇒ Object

Saves this model to a given filename, either as XML or YAML.



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'ext/opencv/facerecognizer.cpp', line 153

VALUE
rb_save(VALUE self, VALUE filename)
{
  Check_Type(filename, T_STRING);
  cv::FaceRecognizer *self_ptr = FACERECOGNIZER(self);
  try {
    char* s = StringValueCStr(filename);
    self_ptr->save(std::string(s));
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }

  return Qnil;
}

#train(src, labels) ⇒ Object

Trains a FaceRecognizer with given data and associated labels.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'ext/opencv/facerecognizer.cpp', line 55

VALUE
rb_train(VALUE self, VALUE src, VALUE labels)
{
  Check_Type(src, T_ARRAY);
  Check_Type(labels, T_ARRAY);

  VALUE *src_ptr = RARRAY_PTR(src);
  int src_size = RARRAY_LEN(src);
  std::vector<cv::Mat> images;
  for (int i = 0; i < src_size; i++) {
    images.push_back(cv::Mat(CVMAT_WITH_CHECK(src_ptr[i])));
  }

  VALUE *labels_ptr = RARRAY_PTR(labels);
  int labels_size = RARRAY_LEN(labels);
  std::vector<int> local_labels;
  for (int i = 0; i < labels_size; i++) {
    local_labels.push_back(NUM2INT(labels_ptr[i]));
  }

  cv::FaceRecognizer *self_ptr = FACERECOGNIZER(self);
  try {
    self_ptr->train(images, local_labels);
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }

  return Qnil;
}

#udpate(src, labels) ⇒ Object

Updates a FaceRecognizer with given data and associated labels. Only valid on LBPH models.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'ext/opencv/facerecognizer.cpp', line 92

VALUE
rb_update(VALUE self, VALUE src, VALUE labels)
{
  Check_Type(src, T_ARRAY);
  Check_Type(labels, T_ARRAY);

  VALUE *src_ptr = RARRAY_PTR(src);
  int src_size = RARRAY_LEN(src);
  std::vector<cv::Mat> images;
  for (int i = 0; i < src_size; i++) {
    images.push_back(cv::Mat(CVMAT_WITH_CHECK(src_ptr[i])));
  }

  VALUE *labels_ptr = RARRAY_PTR(labels);
  int labels_size = RARRAY_LEN(labels);
  std::vector<int> local_labels;
  for (int i = 0; i < labels_size; i++) {
    local_labels.push_back(NUM2INT(labels_ptr[i]));
  }

  cv::FaceRecognizer *self_ptr = FACERECOGNIZER(self);
  try {
    self_ptr->update(images, local_labels);
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }

  return Qnil;
}