Permalink
Browse files

add updateSync

  • Loading branch information...
1 parent 0efbd11 commit 8e55b380cd04d1af5eba06ecbd8740aa5d30b372 @peterbraden committed Mar 15, 2013
Showing with 68 additions and 18 deletions.
  1. +1 −1 smoke/smoketest.js
  2. +60 −14 src/FaceRecognizer.cc
  3. +7 −3 src/FaceRecognizer.h
View
@@ -11,7 +11,7 @@ for (var i = 1; i< 41; i++){
cv.readImage("/Users/peterbraden/Downloads/orl_faces/s6/10.pgm", function(e, im){
var facerec = cv.FaceRecognizer.createFisherFaceRecognizer();
- facerec.train(trainingData);
+ facerec.trainSync(trainingData);
console.log(facerec.predictSync(im));
View
@@ -5,6 +5,9 @@
#include "Matrix.h"
+#define EIGEN 0
+#define LBPH 1
+#define FISHER 2
// Todo, move somewhere useful
cv::Mat fromMatrixOrFilename(Local<Value> v){
@@ -39,7 +42,7 @@ FaceRecognizerWrap::Init(Handle<Object> target) {
NODE_SET_METHOD(constructor, "createEigenFaceRecognizer", CreateEigen);
NODE_SET_METHOD(constructor, "createFisherFaceRecognizer", CreateFisher);
- NODE_SET_PROTOTYPE_METHOD(constructor, "train", Train);
+ NODE_SET_PROTOTYPE_METHOD(constructor, "trainSync", TrainSync);
NODE_SET_PROTOTYPE_METHOD(constructor, "predictSync", PredictSync);
@@ -55,7 +58,7 @@ FaceRecognizerWrap::New(const Arguments &args) {
// By default initialize LBPH
cv::Ptr<cv::FaceRecognizer> f = cv::createLBPHFaceRecognizer(1, 8, 8, 8, 80.0);
- FaceRecognizerWrap *pt = new FaceRecognizerWrap(f);
+ FaceRecognizerWrap *pt = new FaceRecognizerWrap(f, LBPH);
pt->Wrap(args.This());
return args.This();
@@ -82,7 +85,7 @@ FaceRecognizerWrap::CreateLBPH(const Arguments &args) {
cv::Ptr<cv::FaceRecognizer> f = cv::createLBPHFaceRecognizer(
radius, neighbors, grid_x, grid_y, threshold
);
- FaceRecognizerWrap *pt = new FaceRecognizerWrap(f);
+ FaceRecognizerWrap *pt = new FaceRecognizerWrap(f, LBPH);
pt->Wrap(n);
return n;
@@ -103,7 +106,7 @@ FaceRecognizerWrap::CreateEigen(const Arguments &args) {
cv::Ptr<cv::FaceRecognizer> f = cv::createEigenFaceRecognizer(
components, threshold
);
- FaceRecognizerWrap *pt = new FaceRecognizerWrap(f);
+ FaceRecognizerWrap *pt = new FaceRecognizerWrap(f, EIGEN);
pt->Wrap(n);
return n;
@@ -124,23 +127,21 @@ FaceRecognizerWrap::CreateFisher(const Arguments &args) {
cv::Ptr<cv::FaceRecognizer> f = cv::createFisherFaceRecognizer(
components, threshold
);
- FaceRecognizerWrap *pt = new FaceRecognizerWrap(f);
+ FaceRecognizerWrap *pt = new FaceRecognizerWrap(f, FISHER);
pt->Wrap(n);
return n;
}
-FaceRecognizerWrap::FaceRecognizerWrap(cv::Ptr<cv::FaceRecognizer> f){
- rec = f;
+FaceRecognizerWrap::FaceRecognizerWrap(cv::Ptr<cv::FaceRecognizer> f, int type){
+ rec = f;
+ typ = type;
}
-Handle<Value>
-FaceRecognizerWrap::Train(const Arguments& args){
- SETUP_FUNCTION(FaceRecognizerWrap)
- cv::vector<cv::Mat> images;
- cv::vector<int> labels;
+Handle<Value> UnwrapTrainingData(const Arguments& args, cv::vector<cv::Mat>* images, cv::vector<int>* labels){
+
if (args.Length() < 1 || !args[0]->IsArray()){
JSTHROW("FaceRecognizer.train takes a list of [<int> label, image] tuples")
@@ -166,15 +167,55 @@ FaceRecognizerWrap::Train(const Arguments& args){
cv::Mat im = fromMatrixOrFilename(valarr->Get(1));
im = im.clone();
cv::cvtColor(im, im, CV_RGB2GRAY);
- labels.push_back(label);
- images.push_back(im);
+ labels->push_back(label);
+ images->push_back(im);
+ }
+ return v8::Undefined();
+}
+
+Handle<Value>
+FaceRecognizerWrap::TrainSync(const Arguments& args){
+ SETUP_FUNCTION(FaceRecognizerWrap)
+
+ cv::vector<cv::Mat> images;
+ cv::vector<int> labels;
+
+ Handle<Value> exception = UnwrapTrainingData(args, &images, &labels);
+ if (!exception->IsUndefined()){
+ return exception;
}
self->rec->train(images, labels);
return scope.Close(v8::Undefined());
}
+Handle<Value>
+FaceRecognizerWrap::UpdateSync(const Arguments& args){
+ SETUP_FUNCTION(FaceRecognizerWrap)
+
+
+ if (self->typ == EIGEN){
+ JSTHROW("Eigen Recognizer does not support update")
+ }
+ if (self->typ == FISHER){
+ JSTHROW("Fisher Recognizer does not support update")
+ }
+
+ cv::vector<cv::Mat> images;
+ cv::vector<int> labels;
+
+
+ Handle<Value> exception = UnwrapTrainingData(args, &images, &labels);
+ if (!exception->IsUndefined()){
+ return exception;
+ }
+
+ self->rec->update(images, labels);
+
+ return scope.Close(v8::Undefined());
+}
+
Handle<Value>
FaceRecognizerWrap::PredictSync(const Arguments& args){
@@ -194,4 +235,9 @@ FaceRecognizerWrap::PredictSync(const Arguments& args){
return scope.Close(res);
}
+
+
+
+
+
#endif // End version > 2.4
View
@@ -7,21 +7,25 @@
class FaceRecognizerWrap: public node::ObjectWrap {
public:
cv::Ptr<cv::FaceRecognizer> rec;
+ int typ;
static Persistent<FunctionTemplate> constructor;
static void Init(Handle<Object> target);
static Handle<Value> New(const Arguments &args);
- FaceRecognizerWrap(cv::Ptr<cv::FaceRecognizer> f);
+ FaceRecognizerWrap(cv::Ptr<cv::FaceRecognizer> f, int type);
JSFUNC(CreateLBPH)
JSFUNC(CreateEigen)
JSFUNC(CreateFisher)
- JSFUNC(Train)
- JSFUNC(Update)
+ JSFUNC(TrainSync)
+ //JSFUNC(Train)
+ JSFUNC(UpdateSync)
+ //JSFUNC(Update)
JSFUNC(PredictSync)
+ // JSFUNC(Predict)
//static void EIO_Predict(eio_req *req);
//static int EIO_AfterPredict(eio_req *req);

0 comments on commit 8e55b38

Please sign in to comment.