Add logic for available modules

With OpenCV >3.1 and 2.4.13, OpenCV can now be installed without specific modules. This makes it so that `node-opencv` will still be able to compile.
This commit is contained in:
Dan Schultzer 2016-10-08 18:17:18 -07:00
parent 52333d460f
commit 95b0596245
20 changed files with 182 additions and 81 deletions

View File

@ -11,7 +11,7 @@
*/ */
#ifndef NODE_OPENCV_MATRIX_H #ifndef NODE_OPENCV_MATRIX_H
#define NODE_OPENCV_MATRIX_H #define NODE_OPENCV_MATRIX_H
#include <opencv/cv.h> #include <opencv2/opencv.hpp>
#include <node_object_wrap.h> #include <node_object_wrap.h>
namespace node_opencv { namespace node_opencv {

View File

@ -7,30 +7,34 @@ var Stream = require('stream').Stream
var cv = module.exports = require('./bindings'); var cv = module.exports = require('./bindings');
var Matrix = cv.Matrix var Matrix = cv.Matrix
, VideoCapture = cv.VideoCapture
, VideoWriter = cv.VideoWriter
, ImageStream , ImageStream
, ImageDataStream , ImageDataStream
, ObjectDetectionStream , ObjectDetectionStream
, VideoStream; , VideoStream;
Matrix.prototype.detectObject = function(classifier, opts, cb){ if (cv.CascadeClassifier) {
var face_cascade; Matrix.prototype.detectObject = function(classifier, opts, cb){
opts = opts || {}; var face_cascade;
cv._detectObjectClassifiers = cv._detectObjectClassifiers || {}; opts = opts || {};
cv._detectObjectClassifiers = cv._detectObjectClassifiers || {};
if (!(face_cascade = cv._detectObjectClassifiers[classifier])){ if (!(face_cascade = cv._detectObjectClassifiers[classifier])){
face_cascade = new cv.CascadeClassifier(classifier); face_cascade = new cv.CascadeClassifier(classifier);
cv._detectObjectClassifiers[classifier] = face_cascade; cv._detectObjectClassifiers[classifier] = face_cascade;
}
face_cascade.detectMultiScale(this, cb, opts.scale, opts.neighbors
, opts.min && opts.min[0], opts.min && opts.min[1]);
}
} else {
cv.Matrix.prototype.detectObject = function() {
throw new Error("You need to install OpenCV with OBJDETECT module");
} }
face_cascade.detectMultiScale(this, cb, opts.scale, opts.neighbors
, opts.min && opts.min[0], opts.min && opts.min[1]);
} }
Matrix.prototype.inspect = function(){ cv.Matrix.prototype.inspect = function(){
var size = (this.size()||[]).join('x'); var size = (this.size()||[]).join('x');
return "[ Matrix " + size + " ]"; return "[ Matrix " + size + " ]";
} }
@ -189,69 +193,70 @@ ImageDataStream.prototype.end = function(b){
}); });
} }
if (cv.CascadeClassifier) {
ObjectDetectionStream = cv.ObjectDetectionStream = function(cascade, opts){ ObjectDetectionStream = cv.ObjectDetectionStream = function(cascade, opts){
this.classifier = new cv.CascadeClassifier(cascade); this.classifier = new cv.CascadeClassifier(cascade);
this.opts = opts || {}; this.opts = opts || {};
this.readable = true; this.readable = true;
this.writable = true; this.writable = true;
}
util.inherits(ObjectDetectionStream, Stream);
ObjectDetectionStream.prototype.write = function(m){
var self = this;
this.classifier.detectMultiScale(m, function(err, objs){
if (err) return self.emit('error', err);
self.emit('data', objs, m);
} }
, this.opts.scale util.inherits(ObjectDetectionStream, Stream);
, this.opts.neighbors
, this.opts.min && this.opts.min[0]
, this.opts.min && this.opts.min[1]);
}
ObjectDetectionStream.prototype.write = function(m){
VideoStream = cv.VideoStream = function(src){ var self = this;
if (!(src instanceof VideoCapture)) src = new VideoCapture(src); this.classifier.detectMultiScale(m, function(err, objs){
this.video = src;
this.readable = true;
this.paused = false;
}
util.inherits(VideoStream, Stream);
VideoStream.prototype.read = function(){
var self = this;
var frame = function(){
self.video.read(function(err, mat){
if (err) return self.emit('error', err); if (err) return self.emit('error', err);
self.emit('data', mat); self.emit('data', objs, m);
if (!self.paused) process.nextTick(frame); }
}) , this.opts.scale
, this.opts.neighbors
, this.opts.min && this.opts.min[0]
, this.opts.min && this.opts.min[1]);
}
}
if (cv.VideoCapture) {
var VideoCapture = cv.VideoCapture
VideoStream = cv.VideoStream = function(src){
if (!(src instanceof VideoCapture)) src = new VideoCapture(src);
this.video = src;
this.readable = true;
this.paused = false;
}
util.inherits(VideoStream, Stream);
VideoStream.prototype.read = function(){
var self = this;
var frame = function(){
self.video.read(function(err, mat){
if (err) return self.emit('error', err);
self.emit('data', mat);
if (!self.paused) process.nextTick(frame);
})
}
frame();
} }
frame(); VideoStream.prototype.pause = function(){
this.paused = true;
}
VideoStream.prototype.resume = function(){
this.paused = false;
this.read();
}
VideoCapture.prototype.toStream = function(){
return new VideoStream(this);
}
} }
VideoStream.prototype.pause = function(){
this.paused = true;
}
VideoStream.prototype.resume = function(){
this.paused = false;
this.read();
}
VideoCapture.prototype.toStream = function(){
return new VideoStream(this);
}
// Provide cascade data for faces etc. // Provide cascade data for faces etc.
var CASCADES = { var CASCADES = {
FACE_CASCADE: 'haarcascade_frontalface_alt.xml' FACE_CASCADE: 'haarcascade_frontalface_alt.xml'

View File

@ -3,6 +3,8 @@
#include <iostream> #include <iostream>
#include <nan.h> #include <nan.h>
#ifdef HAVE_OPENCV_VIDEO
#if CV_MAJOR_VERSION >= 3 #if CV_MAJOR_VERSION >= 3
#warning TODO: port me to OpenCV 3 #warning TODO: port me to OpenCV 3
#endif #endif
@ -129,3 +131,4 @@ BackgroundSubtractorWrap::BackgroundSubtractorWrap(
#endif #endif
#endif

View File

@ -1,5 +1,7 @@
#include "OpenCV.h" #include "OpenCV.h"
#ifdef HAVE_OPENCV_VIDEO
#if ((CV_MAJOR_VERSION == 2) && (CV_MINOR_VERSION >=4)) #if ((CV_MAJOR_VERSION == 2) && (CV_MINOR_VERSION >=4))
#include <opencv2/video/background_segm.hpp> #include <opencv2/video/background_segm.hpp>
@ -19,3 +21,5 @@ public:
}; };
#endif #endif
#endif

View File

@ -1,6 +1,8 @@
#include "Calib3D.h" #include "Calib3D.h"
#include "Matrix.h" #include "Matrix.h"
#ifdef HAVE_OPENCV_CALIB3D
inline Local<Object> matrixFromMat(cv::Mat &input) { inline Local<Object> matrixFromMat(cv::Mat &input) {
Local<Object> matrixWrap = Local<Object> matrixWrap =
Nan::NewInstance(Nan::GetFunction(Nan::New(Matrix::constructor)).ToLocalChecked()).ToLocalChecked(); Nan::NewInstance(Nan::GetFunction(Nan::New(Matrix::constructor)).ToLocalChecked()).ToLocalChecked();
@ -564,3 +566,5 @@ NAN_METHOD(Calib3D::ReprojectImageTo3D) {
return; return;
} }
} }
#endif

View File

@ -3,6 +3,8 @@
#include "OpenCV.h" #include "OpenCV.h"
#ifdef HAVE_OPENCV_CALIB3D
#if CV_MAJOR_VERSION >= 3 #if CV_MAJOR_VERSION >= 3
#include <opencv2/calib3d.hpp> #include <opencv2/calib3d.hpp>
#endif #endif
@ -25,3 +27,4 @@ public:
}; };
#endif #endif
#endif

View File

@ -2,6 +2,8 @@
#include "OpenCV.h" #include "OpenCV.h"
#include "Matrix.h" #include "Matrix.h"
#ifdef HAVE_OPENCV_VIDEO
#if CV_MAJOR_VERSION >= 3 #if CV_MAJOR_VERSION >= 3
#include <opencv2/video/tracking.hpp> #include <opencv2/video/tracking.hpp>
#endif #endif
@ -176,3 +178,5 @@ NAN_METHOD(TrackedObject::Track) {
info.GetReturnValue().Set(arr); info.GetReturnValue().Set(arr);
} }
#endif

View File

@ -1,5 +1,6 @@
#include "OpenCV.h" #include "OpenCV.h"
#ifdef HAVE_OPENCV_VIDEO
class TrackedObject: public Nan::ObjectWrap { class TrackedObject: public Nan::ObjectWrap {
public: public:
@ -20,3 +21,5 @@ public:
JSFUNC(Track); JSFUNC(Track);
}; };
#endif

View File

@ -3,6 +3,8 @@
#include "Matrix.h" #include "Matrix.h"
#include <nan.h> #include <nan.h>
#ifdef HAVE_OPENCV_OBJDETECT
Nan::Persistent<FunctionTemplate> CascadeClassifierWrap::constructor; Nan::Persistent<FunctionTemplate> CascadeClassifierWrap::constructor;
void CascadeClassifierWrap::Init(Local<Object> target) { void CascadeClassifierWrap::Init(Local<Object> target) {
@ -54,7 +56,7 @@ public:
minw(minw), minw(minw),
minh(minh) { minh(minh) {
} }
~AsyncDetectMultiScale() { ~AsyncDetectMultiScale() {
} }
@ -149,3 +151,5 @@ NAN_METHOD(CascadeClassifierWrap::DetectMultiScale) {
neighbors, minw, minh)); neighbors, minw, minh));
return; return;
} }
#endif

View File

@ -1,6 +1,9 @@
#include "OpenCV.h" #include "OpenCV.h"
#ifdef HAVE_OPENCV_OBJDETECT
#if CV_MAJOR_VERSION >= 3 #if CV_MAJOR_VERSION >= 3
#include <opencv2/objdetect.hpp> #include <opencv2/objdetect.hpp>
#endif #endif
class CascadeClassifierWrap: public Nan::ObjectWrap { class CascadeClassifierWrap: public Nan::ObjectWrap {
@ -20,3 +23,5 @@ public:
static void EIO_DetectMultiScale(uv_work_t *req); static void EIO_DetectMultiScale(uv_work_t *req);
static int EIO_AfterDetectMultiScale(uv_work_t *req); static int EIO_AfterDetectMultiScale(uv_work_t *req);
}; };
#endif

View File

@ -6,6 +6,8 @@
#include <nan.h> #include <nan.h>
#include <stdio.h> #include <stdio.h>
#ifdef HAVE_OPENCV_FEATURES2D
void Features::Init(Local<Object> target) { void Features::Init(Local<Object> target) {
Nan::HandleScope scope; Nan::HandleScope scope;
@ -112,3 +114,4 @@ NAN_METHOD(Features::Similarity) {
} }
#endif #endif
#endif

View File

@ -2,6 +2,8 @@
#if ((CV_MAJOR_VERSION == 2) && (CV_MINOR_VERSION >=4)) #if ((CV_MAJOR_VERSION == 2) && (CV_MINOR_VERSION >=4))
#ifdef HAVE_OPENCV_FEATURES2D
#include <opencv2/core/core.hpp> #include <opencv2/core/core.hpp>
#include <opencv2/features2d/features2d.hpp> #include <opencv2/features2d/features2d.hpp>
@ -14,3 +16,5 @@ public:
}; };
#endif #endif
#endif

View File

@ -2,6 +2,8 @@
#include "OpenCV.h" #include "OpenCV.h"
#include "Matrix.h" #include "Matrix.h"
#ifdef HAVE_OPENCV_HIGHGUI
Nan::Persistent<FunctionTemplate> NamedWindow::constructor; Nan::Persistent<FunctionTemplate> NamedWindow::constructor;
void NamedWindow::Init(Local<Object> target) { void NamedWindow::Init(Local<Object> target) {
@ -82,3 +84,5 @@ NAN_METHOD(NamedWindow::BlockingWaitKey) {
info.GetReturnValue().Set(Nan::New<Number>(res)); info.GetReturnValue().Set(Nan::New<Number>(res));
} }
#endif

View File

@ -1,5 +1,7 @@
#include "OpenCV.h" #include "OpenCV.h"
#ifdef HAVE_OPENCV_HIGHGUI
class NamedWindow: public Nan::ObjectWrap { class NamedWindow: public Nan::ObjectWrap {
public: public:
std::string winname; std::string winname;
@ -17,3 +19,5 @@ public:
; ;
}; };
#endif

View File

@ -1,6 +1,8 @@
#include "ImgProc.h" #include "ImgProc.h"
#include "Matrix.h" #include "Matrix.h"
#ifdef HAVE_OPENCV_IMGPROC
void ImgProc::Init(Local<Object> target) { void ImgProc::Init(Local<Object> target) {
Nan::Persistent<Object> inner; Nan::Persistent<Object> inner;
Local<Object> obj = Nan::New<Object>(); Local<Object> obj = Nan::New<Object>();
@ -233,3 +235,5 @@ NAN_METHOD(ImgProc::GetStructuringElement) {
return; return;
} }
} }
#endif

View File

@ -3,6 +3,8 @@
#include "OpenCV.h" #include "OpenCV.h"
#ifdef HAVE_OPENCV_IMGPROC
/** /**
* Implementation of imgproc.hpp functions * Implementation of imgproc.hpp functions
*/ */
@ -17,3 +19,5 @@ public:
}; };
#endif #endif
#endif

View File

@ -10,22 +10,41 @@
#endif #endif
#include <opencv2/core/version.hpp>
#if !((((CV_MAJOR_VERSION <= 2) && (CV_MINOR_VERSION <= 4)) && (CV_MINOR_VERSION < 13)) || ((CV_MAJOR_VERSION >= 3) && (CV_MINOR_VERSION < 1)))
#define INCLUDE_AVAILABLE_MODULES_ONLY
#endif
#include <v8.h> #include <v8.h>
#include <node.h> #include <node.h>
#include <node_object_wrap.h> #include <node_object_wrap.h>
#include <node_version.h> #include <node_version.h>
#include <node_buffer.h> #include <node_buffer.h>
#include <opencv/cv.h> #include <opencv2/opencv.hpp>
#if ((CV_MAJOR_VERSION <= 2) && (CV_MINOR_VERSION <= 4) && (CV_MINOR_VERSION < 10))
#include <opencv/highgui.h> #include <opencv/highgui.h>
#if CV_MAJOR_VERSION >= 3 #else
#include <opencv2/highgui.hpp> #include <opencv2/imgcodecs/imgcodecs_c.h>
#include <opencv2/imgproc.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/opencv_modules.hpp>
#endif #endif
#if ((CV_MAJOR_VERSION == 2) && (CV_MINOR_VERSION >=4) && (CV_SUBMINOR_VERSION>=4))
#define HAVE_OPENCV_FACE #ifndef INCLUDE_AVAILABLE_MODULES_ONLY
#define HAVE_OPENCV_CALIB3D
#define HAVE_OPENCV_FEATURES2D
#define HAVE_OPENCV_FLANN
#define HAVE_OPENCV_HIGHGUI
// #define HAVE_OPENCV_IMGCODECS
#define HAVE_OPENCV_IMGPROC
#define HAVE_OPENCV_ML
#define HAVE_OPENCV_OBJDETECT
#define HAVE_OPENCV_PHOTO
#define HAVE_OPENCV_SHAPE
#define HAVE_OPENCV_STITCHING
#define HAVE_OPENCV_SUPERRES
#define HAVE_OPENCV_VIDEO
#define HAVE_OPENCV_VIDEOIO
#define HAVE_OPENCV_VIDEOSTAB
#define HAVE_OPENCV_VIZ
#endif #endif
#include <string.h> #include <string.h>

View File

@ -4,6 +4,8 @@
#include <iostream> #include <iostream>
#ifdef HAVE_OPENCV_VIDEOIO
Nan::Persistent<FunctionTemplate> VideoCaptureWrap::constructor; Nan::Persistent<FunctionTemplate> VideoCaptureWrap::constructor;
struct videocapture_baton { struct videocapture_baton {
@ -311,3 +313,5 @@ NAN_METHOD(VideoCaptureWrap::Retrieve) {
return; return;
} }
#endif

View File

@ -1,5 +1,7 @@
#include "OpenCV.h" #include "OpenCV.h"
#ifdef HAVE_OPENCV_VIDEOIO
class VideoCaptureWrap: public Nan::ObjectWrap { class VideoCaptureWrap: public Nan::ObjectWrap {
public: public:
cv::VideoCapture cap; cv::VideoCapture cap;
@ -36,3 +38,5 @@ public:
// release the stream // release the stream
static NAN_METHOD(Release); static NAN_METHOD(Release);
}; };
#endif

View File

@ -24,23 +24,39 @@ extern "C" void init(Local<Object> target) {
Point::Init(target); Point::Init(target);
Matrix::Init(target); Matrix::Init(target);
#ifdef HAVE_OPENCV_OBJDETECT
CascadeClassifierWrap::Init(target); CascadeClassifierWrap::Init(target);
#endif
#ifdef HAVE_OPENCV_VIDEOIO
VideoCaptureWrap::Init(target); VideoCaptureWrap::Init(target);
VideoWriterWrap::Init(target); VideoWriterWrap::Init(target);
#endif
Contour::Init(target); Contour::Init(target);
#ifdef HAVE_OPENCV_VIDEO
TrackedObject::Init(target); TrackedObject::Init(target);
#endif
#ifdef HAVE_OPENCV_HIGHGUI
NamedWindow::Init(target); NamedWindow::Init(target);
#endif
Constants::Init(target); Constants::Init(target);
#ifdef HAVE_OPENCV_CALIB3D
Calib3D::Init(target); Calib3D::Init(target);
#endif
#ifdef HAVE_OPENCV_IMGPROC
ImgProc::Init(target); ImgProc::Init(target);
Histogram::Init(target); Histogram::Init(target);
#endif
#if CV_MAJOR_VERSION < 3 #if CV_MAJOR_VERSION < 3
StereoBM::Init(target); StereoBM::Init(target);
StereoSGBM::Init(target); StereoSGBM::Init(target);
StereoGC::Init(target); StereoGC::Init(target);
#if CV_MAJOR_VERSION == 2 && CV_MINOR_VERSION >=4 #if CV_MAJOR_VERSION == 2 && CV_MINOR_VERSION >=4
BackgroundSubtractorWrap::Init(target); #ifdef HAVE_OPENCV_VIDEO
Features::Init(target); BackgroundSubtractorWrap::Init(target);
#endif
#ifdef HAVE_OPENCV_FEATURES2D
Features::Init(target);
#endif
LDAWrap::Init(target); LDAWrap::Init(target);
#endif #endif
#endif #endif