-
Notifications
You must be signed in to change notification settings - Fork 826
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
309 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include "opencv_modules.h" | ||
|
||
#ifdef HAVE_OPENCV_IMG_HASH | ||
|
||
#include "ImgHashBase.h" | ||
#include "ImgHashBaseBindings.h" | ||
|
||
|
||
void ImgHashBase::Init(v8::Local<v8::FunctionTemplate> ctor) { | ||
Nan::SetPrototypeMethod(ctor, "compare", Compare); | ||
Nan::SetPrototypeMethod(ctor, "compareAsync", CompareAsync); | ||
Nan::SetPrototypeMethod(ctor, "compute", Compute); | ||
Nan::SetPrototypeMethod(ctor, "computeAsync", ComputeAsync); | ||
}; | ||
|
||
NAN_METHOD(ImgHashBase::Compare) { | ||
FF::executeSyncBinding( | ||
std::make_shared<ImgHashBaseBindings::CompareWorker>(ImgHashBase::unwrapThis(info)->getImgHashBase()), | ||
"ImgHashBase::Compare", | ||
info | ||
); | ||
} | ||
|
||
NAN_METHOD(ImgHashBase::CompareAsync) { | ||
FF::executeAsyncBinding( | ||
std::make_shared<ImgHashBaseBindings::CompareWorker>(ImgHashBase::unwrapThis(info)->getImgHashBase()), | ||
"ImgHashBase::CompareAsync", | ||
info | ||
); | ||
} | ||
|
||
NAN_METHOD(ImgHashBase::Compute) { | ||
FF::executeSyncBinding( | ||
std::make_shared<ImgHashBaseBindings::ComputeWorker>(ImgHashBase::unwrapThis(info)->getImgHashBase()), | ||
"ImgHashBase::Compute", | ||
info | ||
); | ||
} | ||
|
||
NAN_METHOD(ImgHashBase::ComputeAsync) { | ||
FF::executeAsyncBinding( | ||
std::make_shared<ImgHashBaseBindings::ComputeWorker>(ImgHashBase::unwrapThis(info)->getImgHashBase()), | ||
"ImgHashBase::ComputeAsync", | ||
info | ||
); | ||
} | ||
|
||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#include "NativeNodeUtils.h" | ||
#include "Mat.h" | ||
#include "Point.h" | ||
#include "Rect.h" | ||
#include "macros.h" | ||
#include <iostream> | ||
#include <opencv2/img_hash.hpp> | ||
|
||
|
||
#ifndef __FF_IMGHASHBASE_H__ | ||
#define __FF_IMGHASHBASE_H__ | ||
|
||
class ImgHashBase : public FF::ObjectWrapBase<ImgHashBase>, public Nan::ObjectWrap { | ||
public: | ||
virtual cv::Ptr<cv::img_hash::ImgHashBase> getImgHashBase() = 0; | ||
|
||
static void Init(v8::Local<v8::FunctionTemplate>); | ||
|
||
static NAN_METHOD(Compare); | ||
static NAN_METHOD(CompareAsync); | ||
static NAN_METHOD(Compute); | ||
static NAN_METHOD(ComputeAsync); | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#include "ImgHashBase.h" | ||
|
||
#ifndef __FF_IMGHASHBASEBINDINGS_H_ | ||
#define __FF_IMGHASHBASEBINDINGS_H_ | ||
|
||
namespace ImgHashBaseBindings { | ||
|
||
struct CompareWorker : public CatchCvExceptionWorker { | ||
public: | ||
cv::Ptr<cv::img_hash::ImgHashBase> self; | ||
CompareWorker(cv::Ptr<cv::img_hash::ImgHashBase> self) { this->self = self; } | ||
|
||
std::vector<uchar> hashOne; | ||
std::vector<uchar> hashTwo; | ||
double returnValue; | ||
|
||
std::string executeCatchCvExceptionWorker() { | ||
returnValue = self->compare(hashOne, hashTwo); | ||
return ""; | ||
} | ||
|
||
v8::Local<v8::Value> getReturnValue() { | ||
v8::Local<v8::Value> ret = FF::DoubleConverter::wrap(returnValue); | ||
return ret; | ||
} | ||
|
||
bool unwrapRequiredArgs(Nan::NAN_METHOD_ARGS_TYPE info) { | ||
return (FF::UcharArrayConverter::arg(0, &hashOne, info) || | ||
FF::UcharArrayConverter::arg(1, &hashTwo, info)); | ||
} | ||
}; | ||
|
||
struct ComputeWorker : public CatchCvExceptionWorker { | ||
public: | ||
cv::Ptr<cv::img_hash::ImgHashBase> self; | ||
ComputeWorker(cv::Ptr<cv::img_hash::ImgHashBase> self) { this->self = self; } | ||
|
||
cv::Mat inputArr; | ||
std::vector<uchar> outputArr; | ||
|
||
std::string executeCatchCvExceptionWorker() { | ||
self->compute(inputArr, outputArr); | ||
return ""; | ||
} | ||
|
||
v8::Local<v8::Value> getReturnValue() { | ||
v8::Local<v8::Value> ret = FF::UcharArrayConverter::wrap(outputArr); | ||
return ret; | ||
} | ||
|
||
bool unwrapRequiredArgs(Nan::NAN_METHOD_ARGS_TYPE info) { | ||
return (Mat::Converter::arg(0, &inputArr, info)); | ||
} | ||
|
||
bool unwrapOptionalArgs(Nan::NAN_METHOD_ARGS_TYPE info) { | ||
return (FF::UcharArrayConverter::optArg(1, &outputArr, info)); | ||
} | ||
|
||
}; | ||
|
||
} | ||
|
||
#endif | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include "opencv_modules.h" | ||
|
||
#ifdef HAVE_OPENCV_IMG_HASH | ||
|
||
#include "PHash.h" | ||
|
||
Nan::Persistent<v8::FunctionTemplate> PHash::constructor; | ||
|
||
NAN_MODULE_INIT(PHash::Init) | ||
{ | ||
v8::Local<v8::FunctionTemplate> ctor = Nan::New<v8::FunctionTemplate>(PHash::New); | ||
v8::Local<v8::ObjectTemplate> instanceTemplate = ctor->InstanceTemplate(); | ||
|
||
ImgHashBase::Init(ctor); | ||
constructor.Reset(ctor); | ||
ctor->SetClassName(FF::newString("PHash")); | ||
instanceTemplate->SetInternalFieldCount(1); | ||
|
||
Nan::Set(target, FF::newString("PHash"), FF::getFunction(ctor)); | ||
}; | ||
|
||
NAN_METHOD(PHash::New) | ||
{ | ||
FF::TryCatch tryCatch("PHash::New"); | ||
FF_ASSERT_CONSTRUCT_CALL(); | ||
|
||
PHash *self = new PHash(); | ||
self->Wrap(info.Holder()); | ||
self->imgHashBase = cv::img_hash::PHash::create(); | ||
|
||
info.GetReturnValue().Set(info.Holder()); | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include "ImgHashBase.h" | ||
|
||
#ifndef __FF_PHASH_H__ | ||
#define __FF_PHASH_H__ | ||
|
||
class PHash : public ImgHashBase { | ||
public: | ||
cv::Ptr<cv::img_hash::ImgHashBase> imgHashBase; | ||
|
||
static NAN_MODULE_INIT(Init); | ||
static NAN_METHOD(New); | ||
|
||
static Nan::Persistent<v8::FunctionTemplate> constructor; | ||
cv::Ptr<cv::img_hash::ImgHashBase> getImgHashBase() { return imgHashBase; } | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#include "opencv_modules.h" | ||
|
||
#ifdef HAVE_OPENCV_IMG_HASH | ||
|
||
#include "img_hash.h" | ||
#include "PHash.h" | ||
|
||
NAN_MODULE_INIT(ImgHash::Init) { | ||
PHash::Init(target); | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include "NativeNodeUtils.h" | ||
#include "macros.h" | ||
#include "opencv2/img_hash.hpp" | ||
|
||
#ifndef __FF_IMGHASH_H__ | ||
#define __FF_IMGHASH_H__ | ||
|
||
class ImgHash { | ||
public: | ||
static NAN_MODULE_INIT(Init); | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Mat } from "./Mat.d"; | ||
|
||
export class ImgHashBase { | ||
compute(inputArr: Mat): string[]; | ||
computeAsync(inputArr: Mat): Promise<string[]>; | ||
compare(hashOne: string[], hashTwo: string[]): number; | ||
compareAsync(hashOne: string[], hashTwo: string[]): Promise<number>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { ImgHashBase } from "./ImgHashBase.d"; | ||
|
||
export class PHash extends ImgHashBase {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const { expect } = require('chai'); | ||
|
||
module.exports = ({ cv, utils, getTestImg }) => (ImgHash) => { | ||
|
||
const { | ||
generateAPITests, | ||
clearTmpData, | ||
getTmpDataFilePath, | ||
cvVersionLowerThan | ||
} = utils; | ||
|
||
describe('constructor', () => { | ||
it('is constructable without args', () => { | ||
expect(() => new ImgHash()).to.not.throw(); | ||
}); | ||
}); | ||
|
||
|
||
describe.only('api tests', () => { | ||
let imgHash; | ||
|
||
before(() => { | ||
imgHash = new ImgHash(); | ||
}); | ||
|
||
describe('compute', () => { | ||
const expectOutput = (res) => { | ||
expect(res).to.be.an('array'); | ||
}; | ||
|
||
generateAPITests({ | ||
getDut: () => imgHash, | ||
methodName: 'compute', | ||
methodNameSpace: 'ImgHashBase', | ||
getRequiredArgs: () => [getTestImg().bgrToGray()], | ||
expectOutput | ||
}); | ||
}); | ||
|
||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
const imgHashTestsFactory = require('./imgHashTests') | ||
|
||
module.exports = ({ cv, utils, getTestImg }) => { | ||
|
||
const { | ||
cvVersionGreaterEqual | ||
} = utils | ||
|
||
const imgHashTests = imgHashTestsFactory({ cv, utils, getTestImg }) | ||
|
||
describe('ImgHash', () => { | ||
|
||
describe('PHash', () => { | ||
imgHashTests(cv.PHash); | ||
}); | ||
|
||
}); | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters