File: CascadeClassifierWrap.cc

package info (click to toggle)
node-opencv 6.0.0+git20180416.cfc96ba0-3
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 24,632 kB
  • sloc: xml: 476,707; cpp: 5,950; makefile: 114; sh: 59; ansic: 20
file content (157 lines) | stat: -rwxr-xr-x 4,334 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "CascadeClassifierWrap.h"
#include "OpenCV.h"
#include "Matrix.h"
#include <nan.h>

#ifdef HAVE_OPENCV_OBJDETECT

Nan::Persistent<FunctionTemplate> CascadeClassifierWrap::constructor;

void CascadeClassifierWrap::Init(Local<Object> target) {
  Nan::HandleScope scope;

  Local<FunctionTemplate> ctor = Nan::New<FunctionTemplate> (CascadeClassifierWrap::New);
  constructor.Reset(ctor);
  ctor->InstanceTemplate()->SetInternalFieldCount(1);
  ctor->SetClassName(Nan::New("CascadeClassifier").ToLocalChecked());

  // Prototype
  // Local<ObjectTemplate> proto = constructor->PrototypeTemplate();

  Nan::SetPrototypeMethod(ctor, "detectMultiScale", DetectMultiScale);

  target->Set(Nan::New("CascadeClassifier").ToLocalChecked(), ctor->GetFunction());
}

NAN_METHOD(CascadeClassifierWrap::New) {
  Nan::HandleScope scope;

  if (info.This()->InternalFieldCount() == 0) {
    Nan::ThrowTypeError("Cannot instantiate without new");
  }

  CascadeClassifierWrap *pt = new CascadeClassifierWrap(*info[0]);
  pt->Wrap(info.This());
  info.GetReturnValue().Set( info.This() );
}

CascadeClassifierWrap::CascadeClassifierWrap(v8::Value* fileName) {
  std::string filename;
  filename = std::string(*Nan::Utf8String(fileName->ToString()));

  if (!cc.load(filename.c_str())) {
    Nan::ThrowTypeError("Error loading file");
  }
}

class AsyncDetectMultiScale: public Nan::AsyncWorker {
public:
  AsyncDetectMultiScale(Nan::Callback *callback, CascadeClassifierWrap *cc,
      Matrix* im, double scale, int neighbors, int minw, int minh) :
      Nan::AsyncWorker(callback),
      cc(cc),
      im(new Matrix(im)), //copy the matrix so we aren't affected if the original is released
      scale(scale),
      neighbors(neighbors),
      minw(minw),
      minh(minh) {
  }

  ~AsyncDetectMultiScale() {
  }

  void Execute() {
    try {
      std::vector < cv::Rect > objects;

      cv::Mat gray;

      if (this->im->mat.channels() != 1) {
        cvtColor(this->im->mat, gray, CV_BGR2GRAY);
        equalizeHist(gray, gray);
      } else {
        gray = this->im->mat;
      }
      this->cc->cc.detectMultiScale(gray, objects, this->scale, this->neighbors,
          0 | CV_HAAR_SCALE_IMAGE, cv::Size(this->minw, this->minh));
      res = objects;
    } catch (cv::Exception& e) {
      SetErrorMessage(e.what());
    }
  }

  void HandleOKCallback() {
    Nan::HandleScope scope;

    delete im;
    im = NULL;

    Local < Value > argv[2];
    v8::Local < v8::Array > arr = Nan::New < v8::Array > (this->res.size());

    for (unsigned int i = 0; i < this->res.size(); i++) {
      v8::Local < v8::Object > x = Nan::New<v8::Object>();
      x->Set(Nan::New("x").ToLocalChecked(), Nan::New < Number > (this->res[i].x));
      x->Set(Nan::New("y").ToLocalChecked(), Nan::New < Number > (this->res[i].y));
      x->Set(Nan::New("width").ToLocalChecked(), Nan::New < Number > (this->res[i].width));
      x->Set(Nan::New("height").ToLocalChecked(), Nan::New < Number > (this->res[i].height));
      arr->Set(i, x);
    }

    argv[0] = Nan::Null();
    argv[1] = arr;

    Nan::TryCatch try_catch;
    callback->Call(2, argv);
    if (try_catch.HasCaught()) {
      Nan::FatalException(try_catch);
    }
  }

private:
  CascadeClassifierWrap *cc;
  Matrix* im;
  double scale;
  int neighbors;
  int minw;
  int minh;
  std::vector<cv::Rect> res;
};

NAN_METHOD(CascadeClassifierWrap::DetectMultiScale) {
  Nan::HandleScope scope;

  CascadeClassifierWrap *self = Nan::ObjectWrap::Unwrap<CascadeClassifierWrap> (info.This());

  if (info.Length() < 2) {
    Nan::ThrowTypeError("detectMultiScale takes at least 2 info");
  }

  Matrix *im = Nan::ObjectWrap::Unwrap < Matrix > (info[0]->ToObject());
  REQ_FUN_ARG(1, cb);

  double scale = 1.1;
  if (info.Length() > 2 && info[2]->IsNumber()) {
    scale = info[2]->NumberValue();
  }

  int neighbors = 2;
  if (info.Length() > 3 && info[3]->IsInt32()) {
    neighbors = info[3]->IntegerValue();
  }

  int minw = 30;
  int minh = 30;
  if (info.Length() > 5 && info[4]->IsInt32() && info[5]->IsInt32()) {
    minw = info[4]->IntegerValue();
    minh = info[5]->IntegerValue();
  }

  Nan::Callback *callback = new Nan::Callback(cb.As<Function>());

  Nan::AsyncQueueWorker( new AsyncDetectMultiScale(callback, self, im, scale,
          neighbors, minw, minh));
  return;
}

#endif