mirror of
https://github.com/ruby-opencv/ruby-opencv
synced 2023-03-27 23:22:12 -04:00
update FaceRecognizer#predict to return both label and confidence
This commit is contained in:
parent
54016ff24c
commit
61d54cb3f0
5 changed files with 10 additions and 51 deletions
|
@ -92,28 +92,6 @@ rb_train(VALUE self, VALUE src, VALUE labels)
|
|||
*/
|
||||
VALUE
|
||||
rb_predict(VALUE self, VALUE src)
|
||||
{
|
||||
cv::Mat mat = cv::Mat(CVMAT_WITH_CHECK(src));
|
||||
cv::FaceRecognizer *self_ptr = FACERECOGNIZER(self);
|
||||
int label;
|
||||
try {
|
||||
label = self_ptr->predict(mat);
|
||||
}
|
||||
catch (cv::Exception& e) {
|
||||
raise_cverror(e);
|
||||
}
|
||||
|
||||
return INT2NUM(label);
|
||||
}
|
||||
|
||||
/*
|
||||
* call-seq:
|
||||
* predict_with_confidence(src)
|
||||
*
|
||||
* Predicts a label and associated confidence (e.g. distance) for a given input image.
|
||||
*/
|
||||
VALUE
|
||||
rb_predict_with_confidence(VALUE self, VALUE src)
|
||||
{
|
||||
cv::Mat mat = cv::Mat(CVMAT_WITH_CHECK(src));
|
||||
cv::FaceRecognizer *self_ptr = FACERECOGNIZER(self);
|
||||
|
@ -188,7 +166,6 @@ define_ruby_class()
|
|||
rb_klass = rb_define_class_under(opencv, "FaceRecognizer", cAlgorithm::rb_class());
|
||||
rb_define_method(rb_klass, "train", RUBY_METHOD_FUNC(rb_train), 2);
|
||||
rb_define_method(rb_klass, "predict", RUBY_METHOD_FUNC(rb_predict), 1);
|
||||
rb_define_method(rb_klass, "predict_with_confidence", RUBY_METHOD_FUNC(rb_predict_with_confidence), 1);
|
||||
rb_define_method(rb_klass, "save", RUBY_METHOD_FUNC(rb_save), 1);
|
||||
rb_define_method(rb_klass, "load", RUBY_METHOD_FUNC(rb_load), 1);
|
||||
}
|
||||
|
|
|
@ -22,6 +22,7 @@ VALUE rb_class();
|
|||
|
||||
void define_ruby_class();
|
||||
VALUE rb_train(VALUE self, VALUE src, VALUE labels);
|
||||
VALUE rb_predict(VALUE self, VALUE src);
|
||||
VALUE rb_save(VALUE self, VALUE filename);
|
||||
VALUE rb_load(VALUE self, VALUE filename);
|
||||
|
||||
|
|
|
@ -48,26 +48,15 @@ class TestEigenFaces < OpenCVTestCase
|
|||
img = CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_GRAYSCALE)
|
||||
label = 1
|
||||
@eigenfaces.train([img], [label])
|
||||
assert_equal(label, @eigenfaces.predict(img))
|
||||
predicted_label, predicted_confidence = @eigenfaces.predict(img)
|
||||
assert_equal(1, predicted_label)
|
||||
assert_in_delta(0.0, predicted_confidence, 0.01)
|
||||
|
||||
assert_raise(TypeError) {
|
||||
@eigenfaces.predict(DUMMY_OBJ)
|
||||
}
|
||||
end
|
||||
|
||||
def test_predict_with_confidence
|
||||
img = CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_GRAYSCALE)
|
||||
label = 1
|
||||
@eigenfaces.train([img], [label])
|
||||
lbl, conf = @eigenfaces.predict_with_confidence(img)
|
||||
assert_equal(label, lbl)
|
||||
assert_equal(0.0, conf)
|
||||
|
||||
assert_raise(TypeError) {
|
||||
@eigenfaces.predict_with_confidence(DUMMY_OBJ)
|
||||
}
|
||||
end
|
||||
|
||||
def test_save
|
||||
img = CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_GRAYSCALE)
|
||||
label = 1
|
||||
|
|
|
@ -44,25 +44,15 @@ class TestFisherFaces < OpenCVTestCase
|
|||
end
|
||||
|
||||
def test_predict
|
||||
label = 1
|
||||
assert_equal(1, @fisherfaces_trained.predict(@images[0]))
|
||||
predicted_label, predicted_confidence = @fisherfaces_trained.predict(@images[0])
|
||||
assert_equal(1, predicted_label)
|
||||
assert_in_delta(0.0, predicted_confidence, 0.01)
|
||||
|
||||
assert_raise(TypeError) {
|
||||
@fisherfaces_trained.predict(DUMMY_OBJ)
|
||||
}
|
||||
end
|
||||
|
||||
def test_predict_with_confidence
|
||||
label = 1
|
||||
lbl, conf = @fisherfaces_trained.predict_with_confidence(@images[0])
|
||||
assert_equal(1, lbl)
|
||||
assert_equal(0.0, conf)
|
||||
|
||||
assert_raise(TypeError) {
|
||||
@fisherfaces_trained.predict_with_confidence(DUMMY_OBJ)
|
||||
}
|
||||
end
|
||||
|
||||
def test_save
|
||||
filename = "fisherfaces_save-#{DateTime.now.strftime('%Y%m%d%H%M%S')}.xml"
|
||||
begin
|
||||
|
|
|
@ -52,7 +52,9 @@ class TestLBPH < OpenCVTestCase
|
|||
end
|
||||
|
||||
def test_predict
|
||||
assert_equal(1, @lbph_trained.predict(@images[0]))
|
||||
predicted_label, predicted_confidence = @lbph_trained.predict(@images[0])
|
||||
assert_equal(1, predicted_label)
|
||||
assert_in_delta(0.0, predicted_confidence, 0.01)
|
||||
|
||||
assert_raise(TypeError) {
|
||||
@lbph_trained.predict(DUMMY_OBJ)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue