for motvey

parent 7f5c7888
var d=require("./buld/Release/face-detection.node");
var d=require("./build/Release/face-detection.node");
d.setWorkingDir(__dirname);
console.log('set wd');
......
......@@ -28,16 +28,21 @@
using namespace std;
using namespace cv;
using namespace ppf_match_3d;
using namespace FaceDetection;
String face_cascade_name = "";
String eyes_cascade_name = "";
cv::CascadeClassifier face_cascade;
cv::CascadeClassifier eyes_cascade;
struct facePosition {int x, y, width, height; };
using namespace FaceDetection;
std::function<void(cv::Mat)> *faceFrameReadyCallback;
int Loop(FaceDetection::Mode, string filename);
FaceDetection::Mode currentMode;
FaceDetection::Mode newMode;
struct facePosition {int x, y, width, height; };
int Loop(string filename);
void FaceDetector::Init(string wd)
{
......@@ -47,26 +52,27 @@ void FaceDetector::Init(string wd)
if( !face_cascade.load( face_cascade_name ) ){ printf("--(!)Error loading\n"); return; };
}
/*
void FaceDetector::Start(std::function<void(int)>&& faceCallback) {
this->started = true;
this->Loop([faceCallback](int i)->void{
faceCallback(i);
});
}*/
void FaceDetector::Start(std::function<void(cv::Mat)>&& faceCallback) {
currentMode=IDLE;
newMode=IDLE;
faceFrameReadyCallback=&faceCallback;
Loop("");
}
///
/// \param filename
void FaceDetector::SaveFace(string filename)
{
Loop(TRAIN, filename);
Loop(filename);
}
///
/// \param filename
int FaceDetector::CheckFace(string filename)
{
int res=Loop(RECOGNIZE, filename);
int res=Loop(filename);
return res;
}
......@@ -85,12 +91,9 @@ bool detectAndDisplay( Mat frame, facePosition &facePos ) {
cvtColor(frame, frame_gray, CV_BGR2GRAY);
equalizeHist(frame_gray, frame_gray);
//-- Detect faces
face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(180, 180));
if (faces.size() > 0) {
//facePos=faces[0];
facePos.x = faces[0].x;
facePos.y = faces[0].y;
facePos.width = faces[0].width;
......@@ -100,27 +103,36 @@ bool detectAndDisplay( Mat frame, facePosition &facePos ) {
return false;
}
int Loop(Mode currentMode, string filename)
int Loop(string filename)
{
try {
try
{
currentMode=IDLE;
newMode=IDLE;
ppf_match_3d::PPF3DDetector detector(1.0/3.0);
ppf_match_3d::PPF3DDetector detector(1.0 / 4.0,0.05, 20);
rs::context ctx;
if (ctx.get_device_count() == 0) throw std::runtime_error("No device detected. Is it plugged in?");
rs::device *device = ctx.get_device(0);
device->enable_stream(rs::stream::depth, rs::preset::best_quality);
device->enable_stream(rs::stream::color, rs::preset::best_quality);
device->enable_stream(rs::stream::infrared, rs::preset::best_quality);
device->enable_stream(rs::stream::depth, 640, 480, rs::format::z16, 30);//rs::preset::best_quality);
device->enable_stream(rs::stream::color, 640, 480, rs::format::rgb8, 30);//rs::preset::best_quality);
device->enable_stream(rs::stream::infrared, 640, 480, rs::format::y16, 30);//rs::preset::best_quality);
device->start();
int trainClock = 0;
bool looping=true;
bool looping = true;
while (looping)
{
currentMode=newMode;
while (looping) {
if (device->is_streaming())
device->wait_for_frames();
......@@ -141,9 +153,11 @@ int Loop(Mode currentMode, string filename)
/////////////////////////////////////////////////////////////////////////////////////////////
facePosition facePos;
if (trainClock % 15 == 0) {
if (trainClock % 7 == 0)
{
cv::Mat frame(color_intrin.height, color_intrin.width, CV_8UC3, (uchar *) color_image);
if (detectAndDisplay(frame, facePos)) {
if (detectAndDisplay(frame, facePos))
{
faceDetected = true;
}
}
......@@ -155,8 +169,10 @@ int Loop(Mode currentMode, string filename)
/////////////////////////////////////////////////////////////////////////////////////////////
int modelPointsCount = 0;
for (int dy = 0; dy < depth_intrin.height; ++dy) {
for (int dx = 0; dx < depth_intrin.width; ++dx) {
for (int dy = 0; dy < depth_intrin.height; ++dy)
{
for (int dx = 0; dx < depth_intrin.width; ++dx)
{
// Retrieve the 16-bit depth value and map it into a depth in meters
uint16_t depth_value = depth_image[dy * depth_intrin.width + dx];
float depth_in_meters = depth_value * scale;
......@@ -176,12 +192,13 @@ int Loop(Mode currentMode, string filename)
float a = cx - (facePos.x + facePos.width / 2);
float b = cy - (facePos.y + facePos.height / 2);
float ry = (facePos.height / 2) * 0.9f;
float rx = (facePos.width / 2) * 0.7f;
float ry = (facePos.height / 2) * 1.0f;
float rx = (facePos.width / 2) * 0.8f;
bool insideEllipse = ((a * a) / (rx * rx) + (b * b) / (ry * ry)) <= 1;
if (insideEllipse) {
if (insideEllipse)
{
protoModel.at<float>(modelPointsCount, 0) = depth_point.x;
protoModel.at<float>(modelPointsCount, 1) = depth_point.y;
protoModel.at<float>(modelPointsCount, 2) = depth_point.z;
......@@ -194,12 +211,13 @@ int Loop(Mode currentMode, string filename)
}
}
if(modelPointsCount==0)
if (modelPointsCount == 0)
continue;
cv::Mat model(modelPointsCount, 6, CV_32F);
for (int k = 0; k < modelPointsCount; k++) {
for (int k = 0; k < modelPointsCount; k++)
{
model.at<float>(k, 0) = protoModel.at<float>(k, 0);
model.at<float>(k, 1) = protoModel.at<float>(k, 1);
model.at<float>(k, 2) = protoModel.at<float>(k, 2);
......@@ -208,12 +226,16 @@ int Loop(Mode currentMode, string filename)
model.at<float>(k, 5) = protoModel.at<float>(k, 5);
}
switch (currentMode) {
case TRAIN:
//detector.trainModel(model);
(*faceFrameReadyCallback)(model);
switch (currentMode)
{
case IDLE:
printf("idle");
break;
case TRAIN:
writePLY(model, filename.c_str());
looping=false;
looping = false;
break;
case RECOGNIZE:
......@@ -224,7 +246,8 @@ int Loop(Mode currentMode, string filename)
vector<Pose3DPtr> results;
detector.match(model, results);
trainClock = 1;
if (results.size() > 0) {
if (results.size() > 0)
{
device->stop();
return results[0]->numVotes;
}
......@@ -237,12 +260,14 @@ int Loop(Mode currentMode, string filename)
return 0;
}
catch (const rs::error &e) {
catch (const rs::error &e)
{
std::cerr << "RealSense error calling " << e.get_failed_function() << "(" << e.get_failed_args() << "):\n "
<< e.what() << std::endl;
return 0;
}
catch (const std::exception &e) {
catch (const std::exception &e)
{
std::cerr << e.what() << std::endl;
return 0;
}
......
#include <sstream>
#include <vector>
#include <functional>
#include <opencv2/opencv.hpp>
namespace FaceDetection {
namespace FaceDetection
{
using namespace std;
enum Mode{
enum Mode
{
TRAIN,
RECOGNIZE
RECOGNIZE,
IDLE
};
class FaceDetector {
class FaceDetector
{
public:
void Init(string);
void SaveFace(string);
void Start(std::function<void(cv::Mat)>&&);
int CheckFace(string);
private:
Mode currentMode;
};
}
\ No newline at end of file
......@@ -34,6 +34,7 @@ namespace FaceDetection
Persistent<Function> callback;
Isolate *isolate;
string filename;
cv::Mat frame;
int result;
};
......@@ -71,6 +72,8 @@ namespace FaceDetection
Isolate *isolate = args.GetIsolate();
faceRequest *request = new faceRequest();
cb = Local<Function>::Cast(args[0]);
request->callback.Reset(isolate, cb);
request->isolate = isolate;
faceSaveAsyncToken.data = request;
......@@ -134,10 +137,58 @@ namespace FaceDetection
faceDetector->Init(foo);
}
///
/// \param args
void Start(const FunctionCallbackInfo<Value> &args)
{
Isolate *isolate = args.GetIsolate();
faceRequest *request = new faceRequest();
cb = Local<Function>::Cast(args[0]);
request->callback.Reset(isolate, cb);
request->isolate = isolate;
faceCheckAsyncToken.data = request;
uv_async_init(loop, &faceCheckAsyncToken, [](uv_async_t* req){
auto request = (faceRequest *) req->data;
auto isolate = request->isolate;
auto frame=request->frame;
HandleScope scope(isolate);
v8::Local<Array> myArray = v8::Array::New(isolate);
for(int i=0;i<frame.rows;i++)
{
Local<Array> row = Array::New(isolate);
row->Set(0,Number::New(isolate,frame.at<float>(i,0)));
row->Set(1,Number::New(isolate,frame.at<float>(i,1)));
row->Set(2,Number::New(isolate,frame.at<float>(i,2)));
myArray->Set(i,row);
}
const unsigned argc = 1;
Local<Value> argv[argc] = {myArray};
Local<Function>::New(isolate, request->callback)->
Call(isolate->GetCurrentContext()->Global(), argc, argv);
});
uv_thread_create(&texample_thread, [](void*){
faceDetector->Start([](cv::Mat mat){
printf("lol, callback\n");
uv_async_send(&faceCheckAsyncToken);
});
}, NULL);
}
void Stop(const FunctionCallbackInfo<Value> &args)
{
}
void init(Local<Object> exports, Local<Object> module)
{
NODE_SET_METHOD(exports, "saveFace", Save);
NODE_SET_METHOD(exports, "checkFace", Check);
NODE_SET_METHOD(exports, "start", Start);
NODE_SET_METHOD(exports, "stop", Stop);
NODE_SET_METHOD(exports, "setWorkingDir", SetWorkingDir);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment