Skip to content
This repository was archived by the owner on Oct 18, 2023. It is now read-only.

Commit c79f0bd

Browse files
Merge pull request justadudewhohacks#291 from plarsson/master
Fixed return of hog groupRectangles and added getDecisionFunction to svm
2 parents 8ed4b3b + 303e0f8 commit c79f0bd

File tree

7 files changed

+38
-8
lines changed

7 files changed

+38
-8
lines changed

cc/modules/machinelearning/SVM.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ NAN_MODULE_INIT(SVM::Init) {
2626
Nan::SetPrototypeMethod(ctor, "predict", Predict);
2727
Nan::SetPrototypeMethod(ctor, "getSupportVectors", GetSupportVectors);
2828
Nan::SetPrototypeMethod(ctor, "getUncompressedSupportVectors", GetUncompressedSupportVectors);
29+
Nan::SetPrototypeMethod(ctor, "getDecisionFunction", GetDecisionFunction);
2930
Nan::SetPrototypeMethod(ctor, "calcError", CalcError);
3031
Nan::SetPrototypeMethod(ctor, "save", Save);
3132
Nan::SetPrototypeMethod(ctor, "load", Load);
@@ -139,6 +140,25 @@ NAN_METHOD(SVM::GetUncompressedSupportVectors) {
139140
#endif
140141
}
141142

143+
NAN_METHOD(SVM::GetDecisionFunction) {
144+
FF_METHOD_CONTEXT("SVM::GetDecisionFunction");
145+
146+
if (!info[0]->IsNumber()) {
147+
FF_THROW("expected arg 0 to be a Int");
148+
}
149+
150+
FF_OBJ alpha = FF_NEW_INSTANCE(Mat::constructor);
151+
FF_OBJ svidx = FF_NEW_INSTANCE(Mat::constructor);
152+
FF_ARG_INT(0, int i);
153+
double rho = FF_UNWRAP(info.This(), SVM)->svm->getDecisionFunction(i, FF_UNWRAP_MAT_AND_GET(alpha), FF_UNWRAP_MAT_AND_GET(svidx));
154+
155+
FF_OBJ ret = FF_NEW_OBJ();
156+
Nan::Set(ret, FF_NEW_STRING("rho"), Nan::New((double)rho));
157+
Nan::Set(ret, FF_NEW_STRING("alpha"), alpha);
158+
Nan::Set(ret, FF_NEW_STRING("svidx"), svidx);
159+
FF_RETURN(ret);
160+
}
161+
142162
NAN_METHOD(SVM::CalcError) {
143163
FF_METHOD_CONTEXT("SVM::CalcError");
144164
FF_ARG_INSTANCE(0, cv::Ptr<cv::ml::TrainData> trainData, TrainData::constructor, FF_UNWRAP_TRAINDATA_AND_GET);

cc/modules/machinelearning/SVM.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class SVM: public Nan::ObjectWrap {
4444
static NAN_METHOD(Predict);
4545
static NAN_METHOD(GetSupportVectors);
4646
static NAN_METHOD(GetUncompressedSupportVectors);
47+
static NAN_METHOD(GetDecisionFunction);
4748
static NAN_METHOD(CalcError);
4849
static NAN_METHOD(Save);
4950
static NAN_METHOD(Load);

cc/modules/objdetect/HOGDescriptorBindings.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,7 @@ namespace HOGDescriptorBindings {
356356
}
357357

358358
v8::Local<v8::Value> getReturnValue() {
359-
v8::Local<v8::Object> ret = Nan::New<v8::Object>();
360-
return ret;
359+
return ObjectArrayConverter<Rect, cv::Rect2d, cv::Rect>::wrap(rectList);
361360
}
362361

363362
bool unwrapRequiredArgs(Nan::NAN_METHOD_ARGS_TYPE info) {

lib/typings/HOGDescriptor.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ export class HOGDescriptor {
3333
detectROIAsync(img: Mat, locations: Point2[], hitThreshold?: number, winStride?: Size, padding?: Size): Promise<{ foundLocations: Point2[], confidences: number[] }>;
3434
getDaimlerPeopleDetector(): number[];
3535
getDefaultPeopleDetector(): number[];
36-
groupRectangles(rectList: Rect[], weights: number[], groupThreshold: number, eps: number): void;
37-
groupRectanglesAsync(rectList: Rect[], weights: number[], groupThreshold: number, eps: number): Promise<void>;
36+
groupRectangles(rectList: Rect[], weights: number[], groupThreshold: number, eps: number): Rect[];
37+
groupRectanglesAsync(rectList: Rect[], weights: number[], groupThreshold: number, eps: number): Promise<Rect[]>;
3838
load(path: string): void;
3939
save(path: string): void;
4040
setSVMDetector(detector: number[]): void;

lib/typings/SVM.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export class SVM {
1717
constructor(params: { c?: number, coef0?: number, degree?: number, gamma?: number, nu?: number, p?: number, kernelType?: number, classWeights?: Mat });
1818
calcError(trainData: TrainData, test: boolean): { error: number, responses: Mat };
1919
getSupportVectors(): Mat;
20+
getDecisionFunction(): { rho: number, alpha: Mat, svidx: Mat };
2021
load(file: string): void;
2122
predict(sample: number[], flags?: number): number;
2223
predict(samples: Mat, flags?: number): number[];

test/tests/modules/machinelearning/SVMTests.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,15 @@ module.exports = () => {
190190
});
191191
});
192192

193+
describe('getDecisionFunction', () => {
194+
it('should return decision function', () => {
195+
const ret = svm.getDecisionFunction(0);
196+
expect(ret).to.have.property('rho').to.be.a('number');
197+
expect(ret).to.have.property('alpha').to.be.instanceOf(cv.Mat);
198+
expect(ret).to.have.property('svidx').to.be.instanceOf(cv.Mat);
199+
});
200+
});
201+
193202
describe('calcError', () => {
194203
it.skip('calcError', () => {});
195204
});

test/tests/modules/objdetect/HOGDescriptorTests.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -412,14 +412,14 @@ module.exports = () => {
412412
});
413413

414414
describe('groupRectangles', () => {
415-
const expectOutput = () => {
416-
// expect to not throw
415+
const expectOutput = (result) => {
416+
expect(result).to.be.an('array');
417+
result.forEach(rect => expect(rect).instanceOf(cv.Rect));
417418
};
418-
419419
const rectList = [new cv.Rect(0, 0, 10, 10), new cv.Rect(0, 0, 20, 20)];
420420
const weights = [0.5, 1.0];
421421
const groupThreshold = 1;
422-
const eps = 0.5;
422+
const eps = 2.0;
423423

424424
generateAPITests({
425425
getDut: () => getTestHOG(),

0 commit comments

Comments
 (0)