1
0
Fork 0
mirror of https://github.com/ruby-opencv/ruby-opencv synced 2023-03-27 23:22:12 -04:00

add LBPH face recognizer

This commit is contained in:
ser1zw 2013-10-06 05:49:20 +09:00
parent cf9b314b92
commit d08851893a
6 changed files with 4498 additions and 0 deletions

70
ext/opencv/lbph.cpp Normal file
View file

@ -0,0 +1,70 @@
/************************************************************
lbph.cpp -
$Author: ser1zw $
Copyright (C) 2013 ser1zw
************************************************************/
#include <stdio.h>
#include "lbph.h"
/*
* Document-class: OpenCV::LBPH
*
*/
__NAMESPACE_BEGIN_OPENCV
__NAMESPACE_BEGIN_LBPH
VALUE rb_klass;
VALUE
rb_class()
{
return rb_klass;
}
/*
* call-seq:
* LBPH.new(radius=1, neighbors=8, grid_x=8, grid_y=8, threshold=DBL_MAX) -> cvmat
*/
VALUE
rb_initialize(int argc, VALUE argv[], VALUE self)
{
VALUE radius_val, neighbors_val, grid_x_val, grid_y_val, threshold_val;
rb_scan_args(argc, argv, "05", &radius_val, &neighbors_val, &grid_x_val, &grid_y_val, &threshold_val);
int radius = NIL_P(radius_val) ? 1 : NUM2INT(radius_val);
int neighbors = NIL_P(neighbors_val) ? 8 : NUM2INT(neighbors_val);
int grid_x = NIL_P(grid_x_val) ? 8 : NUM2INT(grid_x_val);
int grid_y = NIL_P(grid_y_val) ? 8 : NUM2INT(grid_y_val);
double threshold = NIL_P(threshold_val) ? DBL_MAX : NUM2INT(threshold_val);
free(DATA_PTR(self));
cv::Ptr<cv::FaceRecognizer> ptr = cv::createLBPHFaceRecognizer(radius, neighbors, grid_x, grid_y, threshold);
DATA_PTR(self) = ptr;
cFaceRecognizer::guard_facerecognizer(DATA_PTR(self), ptr);
return self;
}
void
define_ruby_class()
{
if (rb_klass)
return;
/*
* opencv = rb_define_module("OpenCV");
*
* note: this comment is used by rdoc.
*/
VALUE opencv = rb_module_opencv();
rb_klass = rb_define_class_under(opencv, "LBPH", cFaceRecognizer::rb_class());
rb_define_alloc_func(rb_klass, cFaceRecognizer::allocate_facerecognizer);
rb_define_private_method(rb_klass, "initialize", RUBY_METHOD_FUNC(rb_initialize), -1);
}
__NAMESPACE_END_LBPH
__NAMESPACE_END_OPENCV

30
ext/opencv/lbph.h Normal file
View file

@ -0,0 +1,30 @@
/************************************************************
lbph.h
$Author: ser1zw $
Copyright (C) 2013 ser1zw
************************************************************/
#ifndef RUBY_OPENCV_LBPH_H
#define RUBY_OPENCV_LBPH_H
#include "opencv.h"
#define __NAMESPACE_BEGIN_LBPH namespace cLBPH {
#define __NAMESPACE_END_LBPH }
__NAMESPACE_BEGIN_OPENCV
__NAMESPACE_BEGIN_LBPH
VALUE rb_class();
void define_ruby_class();
VALUE rb_initialize(int argc, VALUE argv[], VALUE self);
__NAMESPACE_END_LBPH
__NAMESPACE_END_OPENCV
#endif // RUBY_OPENCV_LBPH_H

View file

@ -759,6 +759,7 @@ extern "C" {
mOpenCV::cFaceRecognizer::define_ruby_class();
mOpenCV::cEigenFaces::define_ruby_class();
mOpenCV::cFisherFaces::define_ruby_class();
mOpenCV::cLBPH::define_ruby_class();
mOpenCV::mGUI::define_ruby_module();
mOpenCV::mGUI::cWindow::define_ruby_class();

View file

@ -134,6 +134,7 @@ extern "C" {
#include "facerecognizer.h"
#include "eigenfaces.h"
#include "fisherfaces.h"
#include "lbph.h"
// GUI
#include "gui.h"

4304
test/lbph_save.xml Normal file

File diff suppressed because it is too large Load diff

92
test/test_lbph.rb Executable file
View file

@ -0,0 +1,92 @@
#!/usr/bin/env ruby
# -*- mode: ruby; coding: utf-8-unix -*-
require 'test/unit'
require 'opencv'
require 'date'
require File.expand_path(File.dirname(__FILE__)) + '/helper'
include OpenCV
# Tests for OpenCV::LBPH
class TestLBPH < OpenCVTestCase
def setup
@lbph = LBPH.new
@lbph_trained = LBPH.new
@images = [CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_GRAYSCALE)] * 2
@lbph_trained.train(@images, [1, 1])
end
def test_initialize
[LBPH.new, LBPH.new(1), LBPH.new(1, 2, 3, 4, 5.0)].each { |lbph|
assert_equal(LBPH, lbph.class)
}
assert_raise(TypeError) {
LBPH.new(DUMMY_OBJ)
}
assert_raise(TypeError) {
LBPH.new(1, DUMMY_OBJ)
}
assert_raise(TypeError) {
LBPH.new(1, 2, DUMMY_OBJ)
}
assert_raise(TypeError) {
LBPH.new(1, 2, 3, DUMMY_OBJ)
}
assert_raise(TypeError) {
LBPH.new(1, 2, 3, 4, DUMMY_OBJ)
}
end
def test_train
assert_nil(@lbph.train(@images, [1, 1]))
assert_raise(TypeError) {
@lbph.train(DUMMY_OBJ, [1, 1])
}
assert_raise(TypeError) {
@lbph.train(@images, DUMMY_OBJ)
}
end
def test_predict
assert_equal(1, @lbph_trained.predict(@images[0]))
assert_raise(TypeError) {
@lbph_trained.predict(DUMMY_OBJ)
}
end
def test_save
filename = "lbph_save-#{DateTime.now.strftime('%Y%m%d%H%M%S')}.xml"
begin
@lbph_trained.save(filename)
assert(File.exist? filename)
ensure
File.delete filename
end
assert_raise(TypeError) {
@lbph_trained.save(DUMMY_OBJ)
}
end
def test_load
assert_nothing_raised {
@lbph.load('lbph_save.xml')
}
assert_raise(TypeError) {
@lbph.load(DUMMY_OBJ)
}
end
def test_name
assert_equal('FaceRecognizer.LBPH', @lbph.name)
end
end