mirror of
https://github.com/peterbraden/node-opencv.git
synced 2025-12-08 19:45:55 +00:00
commit
1da9634322
@ -11,6 +11,7 @@
|
||||
"src/Contours.cc",
|
||||
"src/Point.cc",
|
||||
"src/VideoCaptureWrap.cc",
|
||||
"src/VideoWriterWrap.cc",
|
||||
"src/CamShift.cc",
|
||||
"src/HighGUI.cc",
|
||||
"src/FaceRecognizer.cc",
|
||||
|
||||
29
examples/write-video.js
Normal file
29
examples/write-video.js
Normal file
@ -0,0 +1,29 @@
|
||||
var cv = require('../lib/opencv');
|
||||
var path = require('path');
|
||||
|
||||
var vid = new cv.VideoCapture(path.join(__dirname, 'files', 'motion.mov'));
|
||||
|
||||
vid.read(function(err, mat) {
|
||||
if (err) throw err;
|
||||
|
||||
var filename = './tmp/output-'+new Date().getTime()+'.avi';
|
||||
var writer = new cv.VideoWriter(filename, 'DIVX', vid.getFPS(), mat.size(), true);
|
||||
writer.writeSync(mat);
|
||||
|
||||
var x = 0;
|
||||
var iter = function () {
|
||||
vid.read(function (err, m2) {
|
||||
x++;
|
||||
writer.writeSync(m2);
|
||||
if (x < 100) {
|
||||
iter();
|
||||
} else {
|
||||
vid.release();
|
||||
writer.release();
|
||||
}
|
||||
})
|
||||
};
|
||||
iter();
|
||||
});
|
||||
|
||||
|
||||
@ -8,6 +8,7 @@ var cv = module.exports = require('./bindings');
|
||||
|
||||
var Matrix = cv.Matrix
|
||||
, VideoCapture = cv.VideoCapture
|
||||
, VideoWriter = cv.VideoWriter
|
||||
, ImageStream
|
||||
, ImageDataStream
|
||||
, ObjectDetectionStream
|
||||
|
||||
@ -35,6 +35,7 @@ void VideoCaptureWrap::Init(Local<Object> target) {
|
||||
Nan::SetPrototypeMethod(ctor, "setPosition", SetPosition);
|
||||
Nan::SetPrototypeMethod(ctor, "getFrameAt", GetFrameAt);
|
||||
Nan::SetPrototypeMethod(ctor, "getFrameCount", GetFrameCount);
|
||||
Nan::SetPrototypeMethod(ctor, "getFPS", GetFPS);
|
||||
Nan::SetPrototypeMethod(ctor, "release", Release);
|
||||
Nan::SetPrototypeMethod(ctor, "ReadSync", ReadSync);
|
||||
Nan::SetPrototypeMethod(ctor, "grab", Grab);
|
||||
@ -114,6 +115,17 @@ NAN_METHOD(VideoCaptureWrap::GetFrameCount) {
|
||||
info.GetReturnValue().Set(Nan::New<Number>(cnt));
|
||||
}
|
||||
|
||||
|
||||
NAN_METHOD(VideoCaptureWrap::GetFPS) {
|
||||
Nan::HandleScope scope;
|
||||
VideoCaptureWrap *v = Nan::ObjectWrap::Unwrap<VideoCaptureWrap>(info.This());
|
||||
|
||||
int fps = int(v->cap.get(CV_CAP_PROP_FPS));
|
||||
|
||||
info.GetReturnValue().Set(Nan::New<Number>(fps));
|
||||
}
|
||||
|
||||
|
||||
NAN_METHOD(VideoCaptureWrap::GetHeight) {
|
||||
Nan::HandleScope scope;
|
||||
VideoCaptureWrap *v = Nan::ObjectWrap::Unwrap<VideoCaptureWrap>(info.This());
|
||||
|
||||
@ -29,6 +29,8 @@ public:
|
||||
static NAN_METHOD(SetPosition);
|
||||
static NAN_METHOD(GetFrameCount);
|
||||
|
||||
static NAN_METHOD(GetFPS);
|
||||
|
||||
static NAN_METHOD(GetFrameAt);
|
||||
|
||||
// release the stream
|
||||
|
||||
152
src/VideoWriterWrap.cc
Executable file
152
src/VideoWriterWrap.cc
Executable file
@ -0,0 +1,152 @@
|
||||
#include "VideoWriterWrap.h"
|
||||
#include "Matrix.h"
|
||||
#include "OpenCV.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
Nan::Persistent<FunctionTemplate> VideoWriterWrap::constructor;
|
||||
|
||||
struct videowriter_baton {
|
||||
|
||||
Nan::Persistent<Function> cb;
|
||||
VideoWriterWrap *vc;
|
||||
Matrix *im;
|
||||
|
||||
uv_work_t request;
|
||||
};
|
||||
|
||||
void VideoWriterWrap::Init(Local<Object> target) {
|
||||
Nan::HandleScope scope;
|
||||
|
||||
//Class
|
||||
Local<FunctionTemplate> ctor = Nan::New<FunctionTemplate>(VideoWriterWrap::New);
|
||||
constructor.Reset(ctor);
|
||||
ctor->InstanceTemplate()->SetInternalFieldCount(1);
|
||||
ctor->SetClassName(Nan::New("VideoWriter").ToLocalChecked());
|
||||
|
||||
Nan::SetPrototypeMethod(ctor, "write", Write);
|
||||
Nan::SetPrototypeMethod(ctor, "writeSync", WriteSync);
|
||||
Nan::SetPrototypeMethod(ctor, "release", Release);
|
||||
|
||||
target->Set(Nan::New("VideoWriter").ToLocalChecked(), ctor->GetFunction());
|
||||
}
|
||||
|
||||
NAN_METHOD(VideoWriterWrap::New) {
|
||||
Nan::HandleScope scope;
|
||||
|
||||
if (info.This()->InternalFieldCount() == 0)
|
||||
return Nan::ThrowTypeError("Cannot Instantiate without new");
|
||||
|
||||
VideoWriterWrap *v;
|
||||
|
||||
|
||||
// Arg 0 is the output filename
|
||||
std::string filename = std::string(*Nan::Utf8String(info[0]->ToString()));
|
||||
|
||||
// Arg 1 is the fourcc code
|
||||
const char *fourccStr = std::string(*Nan::Utf8String(info[1]->ToString())).c_str();
|
||||
int fourcc = CV_FOURCC(fourccStr[0],fourccStr[1],fourccStr[2],fourccStr[3]);
|
||||
|
||||
// Arg 2 is the output fps
|
||||
double fps = Nan::To<double>(info[2]).FromJust();
|
||||
|
||||
// Arg 3 is the image size
|
||||
cv::Size imageSize;
|
||||
if (info[3]->IsArray()) {
|
||||
Local<Object> v8sz = info[3]->ToObject();
|
||||
imageSize = cv::Size(v8sz->Get(1)->IntegerValue(), v8sz->Get(0)->IntegerValue());
|
||||
} else {
|
||||
JSTHROW_TYPE("Must pass image size");
|
||||
}
|
||||
|
||||
// Arg 4 is the color flag
|
||||
bool isColor = info[4]->BooleanValue();
|
||||
v = new VideoWriterWrap(filename, fourcc, fps, imageSize, isColor);
|
||||
|
||||
v->Wrap(info.This());
|
||||
|
||||
info.GetReturnValue().Set(info.This());
|
||||
}
|
||||
|
||||
VideoWriterWrap::VideoWriterWrap(const std::string& filename, int fourcc, double fps, cv::Size frameSize, bool isColor) {
|
||||
Nan::HandleScope scope;
|
||||
writer.open(filename, fourcc, fps, frameSize, isColor);
|
||||
|
||||
if(!writer.isOpened()) {
|
||||
Nan::ThrowError("Writer could not be opened");
|
||||
}
|
||||
}
|
||||
|
||||
NAN_METHOD(VideoWriterWrap::Release) {
|
||||
Nan::HandleScope scope;
|
||||
VideoWriterWrap *v = Nan::ObjectWrap::Unwrap<VideoWriterWrap>(info.This());
|
||||
|
||||
v->writer.release();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
class AsyncVWWorker: public Nan::AsyncWorker {
|
||||
public:
|
||||
AsyncVWWorker(Nan::Callback *callback, VideoWriterWrap *vw, Matrix *im) :
|
||||
Nan::AsyncWorker(callback),
|
||||
vw(vw),
|
||||
im(im) {
|
||||
}
|
||||
|
||||
~AsyncVWWorker() {
|
||||
}
|
||||
|
||||
// Executed inside the worker-thread.
|
||||
// It is not safe to access V8, or V8 data structures
|
||||
// here, so everything we need for input and output
|
||||
// should go on `this`.
|
||||
void Execute() {
|
||||
this->vw->writer.write(im->mat);
|
||||
}
|
||||
|
||||
// Executed when the async work is complete
|
||||
// this function will be run inside the main event loop
|
||||
// so it is safe to use V8 again
|
||||
void HandleOKCallback() {
|
||||
Nan::HandleScope scope;
|
||||
|
||||
Local<Value> argv[] = {
|
||||
Nan::Null()
|
||||
};
|
||||
|
||||
Nan::TryCatch try_catch;
|
||||
callback->Call(1, argv);
|
||||
if (try_catch.HasCaught()) {
|
||||
Nan::FatalException(try_catch);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
VideoWriterWrap *vw;
|
||||
Matrix* im;
|
||||
};
|
||||
|
||||
NAN_METHOD(VideoWriterWrap::Write) {
|
||||
Nan::HandleScope scope;
|
||||
VideoWriterWrap *v = Nan::ObjectWrap::Unwrap<VideoWriterWrap>(info.This());
|
||||
|
||||
Matrix *im = Nan::ObjectWrap::Unwrap < Matrix > (info[0]->ToObject());
|
||||
REQ_FUN_ARG(1, cb);
|
||||
|
||||
Nan::Callback *callback = new Nan::Callback(cb.As<Function>());
|
||||
Nan::AsyncQueueWorker(new AsyncVWWorker(callback, v, im));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
NAN_METHOD(VideoWriterWrap::WriteSync) {
|
||||
Nan::HandleScope scope;
|
||||
VideoWriterWrap *v = Nan::ObjectWrap::Unwrap<VideoWriterWrap>(info.This());
|
||||
Matrix *im = Nan::ObjectWrap::Unwrap < Matrix > (info[0]->ToObject());
|
||||
|
||||
v->writer.write(im->mat);
|
||||
|
||||
info.GetReturnValue().Set(Nan::Null());
|
||||
}
|
||||
|
||||
18
src/VideoWriterWrap.h
Executable file
18
src/VideoWriterWrap.h
Executable file
@ -0,0 +1,18 @@
|
||||
#include "OpenCV.h"
|
||||
|
||||
class VideoWriterWrap: public Nan::ObjectWrap {
|
||||
public:
|
||||
cv::VideoWriter writer;
|
||||
|
||||
static Nan::Persistent<FunctionTemplate> constructor;
|
||||
static void Init(Local<Object> target);
|
||||
static NAN_METHOD(New);
|
||||
|
||||
VideoWriterWrap(const std::string& filename, int fourcc, double fps, cv::Size frameSize, bool isColor=true);
|
||||
|
||||
static NAN_METHOD(Write);
|
||||
static NAN_METHOD(WriteSync);
|
||||
|
||||
// release the stream
|
||||
static NAN_METHOD(Release);
|
||||
};
|
||||
@ -4,6 +4,7 @@
|
||||
#include "Matrix.h"
|
||||
#include "CascadeClassifierWrap.h"
|
||||
#include "VideoCaptureWrap.h"
|
||||
#include "VideoWriterWrap.h"
|
||||
#include "Contours.h"
|
||||
#include "CamShift.h"
|
||||
#include "HighGUI.h"
|
||||
@ -25,6 +26,7 @@ extern "C" void init(Local<Object> target) {
|
||||
Matrix::Init(target);
|
||||
CascadeClassifierWrap::Init(target);
|
||||
VideoCaptureWrap::Init(target);
|
||||
VideoWriterWrap::Init(target);
|
||||
Contour::Init(target);
|
||||
TrackedObject::Init(target);
|
||||
NamedWindow::Init(target);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user