虹軟SDK在nodejs中的集成

==虹軟官網地址==node

http://www.arcsoft.com.cnlinux

在官網註冊帳號,而且申請人臉識別激活碼, 選擇SDK版本和運行系統(windows/linux/android/ios) ,咱們選擇windows作測試,申請類型選擇1:N ,功能模塊包括人臉檢測、人臉跟蹤、人臉識別。申請以後會獲取APP_ID 和SDK_Key,在代碼中會用到。android

==虹軟SDK人臉檢測目的==ios

主要是與face++人臉檢測作對比,看可否在face++人臉檢測以前選擇虹軟事先檢測一下。c++

==c++部分功能實現==windows

選擇 Qtcreator 4.2.1 ,新建c++ 庫。 設置Qt .pro文件函數

#不加載Qt庫
QT       -= core gui
#生成庫名字
TARGET = detect_lib
#定義生成lib
TEMPLATE = lib

DEFINES += DETECT_LIB_LIBRARY
SOURCES += detect_lib.cpp
#加載虹軟sdk頭文件
HEADERS += detect_lib.h \
    inc/amcomdef.h \
    inc/ammem.h \
    inc/arcsoft_fsdk_face_detection.h \
    inc/asvloffscreen.h \
    inc/merror.h

unix {
    target.path = /usr/lib
    INSTALLS += target
}

unix|win32: LIBS += -L$$PWD/lib/ -llibarcsoft_fsdk_face_detection

INCLUDEPATH += $$PWD/.
DEPENDPATH += $$PWD/.

上面是.pro文件,主要是一些配置信息,如生成庫名字 加載虹軟SDK 和頭文件...測試

下面是detect_lib.h文件 主要供nodejs調用的接口文件。ui

#ifndef DETECT_LIB_H
#define DETECT_LIB_H

#   ifdef __cplusplus
#   define EXTERN_NAME extern "C"
#   else
#   define EXTERN_NAME extern
#   endif

#if defined(WIN32)
#   define Q_DECL_EXPORT __declspec(dllexport)
#   define Q_DECL_IMPORT __declspec(dllexport)
#if defined(DETECT_LIB_LIBRARY)
#   define DETECT_LIBSHARED_EXPORT EXTERN_NAME Q_DECL_EXPORT
#   else
#   define DETECT_LIBSHARED_EXPORT EXTERN_NAME Q_DECL_IMPORT
#endif
#else
#   define DETECT_LIBSHARED_EXPORT EXTERN_NAME
#endif

DETECT_LIBSHARED_EXPORT int add(int a,int b);

DETECT_LIBSHARED_EXPORT int detect(unsigned char * data,int width,int height);

#endif // DETECT_LIB_H

接口add 函數 主要作測試用spa

int detect(unsigned char * data,int width,int height);

檢測人臉函數, data:rgb像素值,width:圖片寬度,height:圖片高度

detect_lib.cpp

#include <nan.h>
#include "detect_lib.h"
using namespace Nan ;
using namespace v8;


class DetectWorker : public AsyncWorker {
public:
DetectWorker(Callback *callback, unsigned char* buffer,int width,int height)
: AsyncWorker(callback), p_buffer(buffer), m_width(width),m_height(height) {m_num = 0;}
~DetectWorker() {}

//這個函數運行在工做線程,而不是v8線程,因此不能訪問v8的數據
void Execute () {

//m_num = add(12,3);
m_num = detect(p_buffer,m_width,m_height);
// m_num = 5;

}

//這個是libuv的回調函數,在這裏能夠使用v8的數據
void HandleOKCallback () {

Local<Object> bmpData = NewBuffer(m_num).ToLocalChecked();
Local<Value> argv[] = {
Nan::Null()
,Uint32::New(v8::Isolate::GetCurrent(),m_num)
};


callback->Call(2, argv);
};

private:
unsigned char * p_buffer;
int m_width;
int m_height;
int m_num;
};


NAN_METHOD(detect){
unsigned char * buffer = (unsigned char*) node::Buffer::Data(info[0]->ToObject());
int width = info[1]->Uint32Value();
int height = info[2]->Uint32Value();

Callback *callback = new Callback(info[3].As<Function>());
AsyncQueueWorker(new DetectWorker(callback, buffer,width ,height));
}

NAN_MODULE_INIT(Init)
{
Nan::Set(target,New<String>("detect").ToLocalChecked(),
GetFunction(New<FunctionTemplate>(detect)).ToLocalChecked());
}

NODE_MODULE(detect, Init)

NAN_METHOD(detect) 表示定義接口detect ,js能夠直接調用, 這裏主要是node中的buffer直接以字節的方式傳遞給c++。也是nodejs與c++交互的重要方式。

將編譯好的dll 和虹軟sdk dll 和detect_lib.h拷貝到當前目錄,而後經過node-gyp configure 和node-gyp build 生成.node

至此.node庫編譯完成,能夠使用require直接飲用該.node 如:var detect = require('./build/Release/detect.node');

相關文章
相關標籤/搜索