Browse files

WIP on face recognition

  • Loading branch information...
1 parent b17fcec commit 64f933f2a9e60392061c4cf68317f7daf2b1a675 @peterbraden committed Mar 14, 2013
Showing with 71 additions and 10 deletions.
  1. +6 −0 smoke/smoketest.js
  2. +55 −5 src/FaceRecognizer.cc
  3. +8 −5 src/FaceRecognizer.h
  4. +2 −0 src/OpenCV.h
View
6 smoke/smoketest.js
@@ -3,6 +3,12 @@ var cv = require('../lib/opencv')
var facerec = new cv.FaceRecognizer();
+facerec.train([
+ [0, '/Users/peterbraden/Downloads/orl_faces/s1/1.pgm']
+ , [0, '/Users/peterbraden/Downloads/orl_faces/s1/2.pgm']
+])
+
+
/*
var win = new cv.NamedWindow("foo");
View
60 src/FaceRecognizer.cc
@@ -20,6 +20,7 @@ FaceRecognizerWrap::Init(Handle<Object> target) {
constructor->SetClassName(String::NewSymbol("FaceRecognizer"));
NODE_SET_PROTOTYPE_METHOD(constructor, "train", Train);
+ NODE_SET_PROTOTYPE_METHOD(constructor, "predictSync", PredictSync);
target->Set(String::NewSymbol("FaceRecognizer"), constructor->GetFunction());
@@ -30,23 +31,72 @@ FaceRecognizerWrap::New(const Arguments &args) {
HandleScope scope;
if (args.This()->InternalFieldCount() == 0)
- return v8::ThrowException(v8::Exception::TypeError(v8::String::New("Cannot Instantiate without new")));
-
+ JSTHROW_TYPE("Cannot Instantiate without new")
// By default initialize eigenface
- cv::FaceRecognizer *f = cv::createEigenFaceRecognizer();
+ cv::Ptr<cv::FaceRecognizer> f = cv::createEigenFaceRecognizer();
FaceRecognizerWrap *pt = new FaceRecognizerWrap(f);
pt->Wrap(args.This());
return args.This();
}
-FaceRecognizerWrap::FaceRecognizerWrap(cv::FaceRecognizer* f){
+FaceRecognizerWrap::FaceRecognizerWrap(cv::Ptr<cv::FaceRecognizer> f){
+ rec = f;
}
Handle<Value>
FaceRecognizerWrap::Train(const Arguments& args){
- SETUP_FUNCTION(Matrix)
+ SETUP_FUNCTION(FaceRecognizerWrap)
+
+ 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")
+ }
+
+ // Iterate through [[label, image], ...] etc, and add matrix / label to vectors
+ const Local<Array> tuples = v8::Array::Cast(*args[0]);
+ const uint32_t length = tuples->Length();
+ for (uint32_t i=0 ; i<length ; ++i){
+ const Local<Value> val = tuples->Get(i);
+
+ if (!val->IsArray()){
+ JSTHROW("train takes a list of [label, image] tuples")
+ }
+
+ Local<Array> valarr = v8::Array::Cast(*val);
+
+ if (valarr->Length() != 2 || !valarr->Get(0)->IsInt32()){
+ JSTHROW("train takes a list of [label, image] tuples")
+ }
+
+ int label = valarr->Get(0)->Uint32Value();
+ cv::Mat im;
+ if (valarr->Get(1)->IsString()){
+ std::string filename = std::string(*v8::String::AsciiValue(valarr->Get(1)->ToString()));
+ im = cv::imread(filename);
+ } else {
+ Matrix *img = ObjectWrap::Unwrap<Matrix>(valarr->Get(1)->ToObject());
+ im = img->mat;
+ }
+ labels.push_back(label);
+ images.push_back(im);
+ }
+
+ self->rec->train(images, labels);
+
+ return scope.Close(v8::Undefined());
+}
+
+Handle<Value>
+FaceRecognizerWrap::PredictSync(const Arguments& args){
+ SETUP_FUNCTION(FaceRecognizerWrap)
+
+
}
+
+
#endif // End version > 2.4
View
13 src/FaceRecognizer.h
@@ -6,21 +6,24 @@
class FaceRecognizerWrap: public node::ObjectWrap {
public:
- cv::FaceRecognizer* rec;
+ cv::Ptr<cv::FaceRecognizer> rec;
static Persistent<FunctionTemplate> constructor;
static void Init(Handle<Object> target);
static Handle<Value> New(const Arguments &args);
- FaceRecognizerWrap(cv::FaceRecognizer* f);
+ FaceRecognizerWrap(cv::Ptr<cv::FaceRecognizer> f);
JSFUNC(Train)
JSFUNC(Update)
- JSFUNC(Predict)
- static void EIO_Predict(eio_req *req);
- static int EIO_AfterPredict(eio_req *req);
+ JSFUNC(PredictSync)
+ //static void EIO_Predict(eio_req *req);
+ //static int EIO_AfterPredict(eio_req *req);
+
+ // TODO Save
+ // TODO Load
};
View
2 src/OpenCV.h
@@ -30,6 +30,8 @@ using namespace node;
#define JSTHROW_TYPE(ERR) \
return v8::ThrowException(v8::Exception::TypeError(v8::String::New(ERR)));
+#define JSTHROW(ERR) \
+ return v8::ThrowException(v8::Exception::Error(v8::String::New(ERR)));
class OpenCV: public node::ObjectWrap{
public:

0 comments on commit 64f933f

Please sign in to comment.