mirror of
https://github.com/ruby-opencv/ruby-opencv
synced 2023-03-27 23:22:12 -04:00
add FisherFaces
This commit is contained in:
parent
7da8b403fc
commit
5626b55cc5
8 changed files with 7746 additions and 0 deletions
|
@ -18,6 +18,27 @@ __NAMESPACE_BEGIN_FACERECOGNIZER
|
|||
|
||||
VALUE rb_klass;
|
||||
|
||||
std::map<long, cv::Ptr<cv::FaceRecognizer> > ptr_guard_map;
|
||||
|
||||
void
|
||||
guard_facerecognizer(void* data_ptr, cv::Ptr<cv::FaceRecognizer> ptr) {
|
||||
ptr_guard_map[(long)data_ptr] = ptr;
|
||||
}
|
||||
|
||||
void
|
||||
release_facerecognizer(void *ptr) {
|
||||
long key = (long)ptr;
|
||||
ptr_guard_map[key].release();
|
||||
ptr_guard_map.erase(key);
|
||||
}
|
||||
|
||||
VALUE
|
||||
allocate_facerecognizer(VALUE klass)
|
||||
{
|
||||
return Data_Wrap_Struct(klass, 0, release_facerecognizer, NULL);
|
||||
}
|
||||
|
||||
|
||||
VALUE
|
||||
rb_class()
|
||||
{
|
||||
|
|
|
@ -25,6 +25,10 @@ VALUE rb_train(VALUE self, VALUE src, VALUE labels);
|
|||
VALUE rb_save(VALUE self, VALUE filename);
|
||||
VALUE rb_load(VALUE self, VALUE filename);
|
||||
|
||||
void guard_facerecognizer(void* data_ptr, cv::Ptr<cv::FaceRecognizer> ptr);
|
||||
void release_facerecognizer(void *ptr);
|
||||
VALUE allocate_facerecognizer(VALUE klass);
|
||||
|
||||
__NAMESPACE_END_FACERECOGNIZER
|
||||
|
||||
inline cv::FaceRecognizer*
|
||||
|
|
67
ext/opencv/fisherfaces.cpp
Normal file
67
ext/opencv/fisherfaces.cpp
Normal file
|
@ -0,0 +1,67 @@
|
|||
/************************************************************
|
||||
|
||||
fisherfaces.cpp -
|
||||
|
||||
$Author: ser1zw $
|
||||
|
||||
Copyright (C) 2013 ser1zw
|
||||
|
||||
************************************************************/
|
||||
#include <stdio.h>
|
||||
#include "fisherfaces.h"
|
||||
/*
|
||||
* Document-class: OpenCV::FisherFaces
|
||||
*
|
||||
*/
|
||||
__NAMESPACE_BEGIN_OPENCV
|
||||
__NAMESPACE_BEGIN_FISHERFACES
|
||||
|
||||
VALUE rb_klass;
|
||||
|
||||
VALUE
|
||||
rb_class()
|
||||
{
|
||||
return rb_klass;
|
||||
}
|
||||
|
||||
/*
|
||||
* call-seq:
|
||||
* FisherFaces.new(num_components=0, threshold=DBL_MAX)
|
||||
*/
|
||||
VALUE
|
||||
rb_initialize(int argc, VALUE argv[], VALUE self)
|
||||
{
|
||||
VALUE num_components_val, threshold_val;
|
||||
rb_scan_args(argc, argv, "02", &num_components_val, &threshold_val);
|
||||
|
||||
int num_components = NIL_P(num_components_val) ? 0 : NUM2INT(num_components_val);
|
||||
double threshold = NIL_P(threshold_val) ? DBL_MAX : NUM2DBL(threshold_val);
|
||||
|
||||
free(DATA_PTR(self));
|
||||
cv::Ptr<cv::FaceRecognizer> ptr = cv::createFisherFaceRecognizer(num_components, 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, "FisherFaces", 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_FISHERFACES
|
||||
__NAMESPACE_END_OPENCV
|
||||
|
31
ext/opencv/fisherfaces.h
Normal file
31
ext/opencv/fisherfaces.h
Normal file
|
@ -0,0 +1,31 @@
|
|||
/************************************************************
|
||||
|
||||
fisherfaces.h
|
||||
|
||||
$Author: ser1zw $
|
||||
|
||||
Copyright (C) 2013 ser1zw
|
||||
|
||||
************************************************************/
|
||||
#ifndef RUBY_OPENCV_FISHERFACES_H
|
||||
#define RUBY_OPENCV_FISHERFACES_H
|
||||
|
||||
#include "opencv.h"
|
||||
|
||||
#define __NAMESPACE_BEGIN_FISHERFACES namespace cFisherFaces {
|
||||
#define __NAMESPACE_END_FISHERFACES }
|
||||
|
||||
__NAMESPACE_BEGIN_OPENCV
|
||||
__NAMESPACE_BEGIN_FISHERFACES
|
||||
|
||||
VALUE rb_class();
|
||||
|
||||
void define_ruby_class();
|
||||
VALUE rb_allocate(VALUE klass);
|
||||
VALUE rb_initialize(int argc, VALUE argv[], VALUE self);
|
||||
|
||||
__NAMESPACE_END_FISHERFACES
|
||||
__NAMESPACE_END_OPENCV
|
||||
|
||||
#endif // RUBY_OPENCV_FISHERFACES_H
|
||||
|
|
@ -758,6 +758,7 @@ extern "C" {
|
|||
mOpenCV::cAlgorithm::define_ruby_class();
|
||||
mOpenCV::cFaceRecognizer::define_ruby_class();
|
||||
mOpenCV::cEigenFaces::define_ruby_class();
|
||||
mOpenCV::cFisherFaces::define_ruby_class();
|
||||
|
||||
mOpenCV::mGUI::define_ruby_module();
|
||||
mOpenCV::mGUI::cWindow::define_ruby_class();
|
||||
|
|
|
@ -133,6 +133,7 @@ extern "C" {
|
|||
#include "algorithm.h"
|
||||
#include "facerecognizer.h"
|
||||
#include "eigenfaces.h"
|
||||
#include "fisherfaces.h"
|
||||
|
||||
// GUI
|
||||
#include "gui.h"
|
||||
|
|
7530
test/fisherfaces_save.xml
Normal file
7530
test/fisherfaces_save.xml
Normal file
File diff suppressed because it is too large
Load diff
91
test/test_fisherfaces.rb
Executable file
91
test/test_fisherfaces.rb
Executable file
|
@ -0,0 +1,91 @@
|
|||
#!/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::FisherFaces
|
||||
class TestFisherFaces < OpenCVTestCase
|
||||
def setup
|
||||
@fisherfaces = FisherFaces.new
|
||||
|
||||
@fisherfaces_trained = FisherFaces.new
|
||||
@images = [CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_GRAYSCALE)] * 2
|
||||
@fisherfaces_trained.train(@images, [1, 2])
|
||||
end
|
||||
|
||||
def test_initialize
|
||||
[FisherFaces.new, FisherFaces.new(1), FisherFaces.new(1, 99999)].each { |ff|
|
||||
assert_equal(FisherFaces, ff.class)
|
||||
}
|
||||
|
||||
assert_raise(TypeError) {
|
||||
FisherFaces.new(DUMMY_OBJ)
|
||||
}
|
||||
|
||||
assert_raise(TypeError) {
|
||||
FisherFaces.new(1, DUMMY_OBJ)
|
||||
}
|
||||
end
|
||||
|
||||
def test_train
|
||||
assert_nil(@fisherfaces.train(@images, [1, 2]))
|
||||
|
||||
assert_raise(TypeError) {
|
||||
@fisherfaces.train(DUMMY_OBJ, [1])
|
||||
}
|
||||
|
||||
assert_raise(TypeError) {
|
||||
@fisherfaces.train(@images, DUMMY_OBJ)
|
||||
}
|
||||
end
|
||||
|
||||
def test_predict
|
||||
label = 1
|
||||
assert_equal(1, @fisherfaces_trained.predict(@images[0]))
|
||||
|
||||
assert_raise(TypeError) {
|
||||
@fisherfaces_trained.predict(DUMMY_OBJ)
|
||||
}
|
||||
end
|
||||
|
||||
def test_save
|
||||
filename = "fisherfaces_save-#{DateTime.now.strftime('%Y%m%d%H%M%S')}.xml"
|
||||
begin
|
||||
@fisherfaces_trained.save(filename)
|
||||
assert(File.exist? filename)
|
||||
ensure
|
||||
File.delete filename
|
||||
end
|
||||
assert_raise(TypeError) {
|
||||
@fisherfaces_trained.save(DUMMY_OBJ)
|
||||
}
|
||||
end
|
||||
|
||||
def test_load
|
||||
assert_nothing_raised {
|
||||
@fisherfaces.load('fisherfaces_save.xml')
|
||||
}
|
||||
assert_raise(TypeError) {
|
||||
@fisherfaces.load(DUMMY_OBJ)
|
||||
}
|
||||
end
|
||||
|
||||
def test_name
|
||||
assert_equal('FaceRecognizer.Fisherfaces', @fisherfaces.name)
|
||||
end
|
||||
|
||||
def test_get_mat
|
||||
mat = @fisherfaces_trained.get_mat('eigenvalues')
|
||||
assert_not_nil(mat)
|
||||
assert_equal(CvMat, mat.class)
|
||||
|
||||
assert_raise(TypeError) {
|
||||
@fisherfaces_trained.get_mat(DUMMY_OBJ)
|
||||
}
|
||||
end
|
||||
end
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue