From 5268976872c1fae8fe889f7c82699dc1616b3cdf Mon Sep 17 00:00:00 2001 From: Andrey Bosonchenko Date: Tue, 5 Dec 2017 14:32:09 +0200 Subject: [PATCH] added setNeighbors test --- contrib/face.cpp | 24 ++++++++++++++++-------- contrib/face.go | 11 +++++++++++ contrib/face.h | 1 + contrib/face_test.go | 30 +++++++++++++++++++++--------- 4 files changed, 49 insertions(+), 17 deletions(-) diff --git a/contrib/face.cpp b/contrib/face.cpp index e3a9db0a..a8e53f7f 100644 --- a/contrib/face.cpp +++ b/contrib/face.cpp @@ -38,8 +38,6 @@ void LBPHFaceRecognizer_Update(LBPHFaceRecognizer fr, Mats mats, IntVector label int LBPHFaceRecognizer_Predict(LBPHFaceRecognizer fr, Mat sample) { int label; - double confidence; - label = (*fr)->predict(*sample); return label; @@ -71,20 +69,30 @@ void LBPHFaceRecognizer_SetRadius(LBPHFaceRecognizer fr, int radius) { } void LBPHFaceRecognizer_SetNeighbors(LBPHFaceRecognizer fr, int neighbors) { - (*fr)->setNeighbors(neighbors); + (*fr)->setNeighbors(neighbors); - return; + return; +} + + +int LBPHFaceRecognizer_GetNeighbors(LBPHFaceRecognizer fr) { + int n; + + n = (*fr)->getNeighbors(); + + return n; } void LBPHFaceRecognizer_SaveFile(LBPHFaceRecognizer fr, const char* filename) { - (*fr)->write(filename); + (*fr)->write(filename); - return; + return; } void LBPHFaceRecognizer_LoadFile(LBPHFaceRecognizer fr, const char* filename) { - (*fr)->read(filename); + (*fr)->read(filename); - return; + return; } + diff --git a/contrib/face.go b/contrib/face.go index 017d1cb1..823f55ae 100644 --- a/contrib/face.go +++ b/contrib/face.go @@ -112,11 +112,22 @@ func (fr *LBPHFaceRecognizer) SetThreshold(threshold float32) { // set Neighbors // // see https://docs.opencv.org/3.3.1/df/d25/classcv_1_1face_1_1LBPHFaceRecognizer.html#ab225f7bf353ce8697a506eda10124a92 +// wrong neighbors can raise opencv exception! // func (fr *LBPHFaceRecognizer) SetNeighbors(neighbors int) { C.LBPHFaceRecognizer_SetNeighbors(fr.p, (C.int)(neighbors)) } +// get Neighbors +// +// see https://docs.opencv.org/3.3.1/df/d25/classcv_1_1face_1_1LBPHFaceRecognizer.html#a50a3e2ca6e8464166e153c9df84b0a77 +// +func (fr *LBPHFaceRecognizer) GetNeighbors() int { + n := C.LBPHFaceRecognizer_GetNeighbors(fr.p) + + return int(n) +} + // set Radius // // see https://docs.opencv.org/3.3.1/df/d25/classcv_1_1face_1_1LBPHFaceRecognizer.html#a62d94c75cade902fd3b487b1ef9883fc diff --git a/contrib/face.h b/contrib/face.h index c8bec22d..f512c8c3 100644 --- a/contrib/face.h +++ b/contrib/face.h @@ -31,6 +31,7 @@ void LBPHFaceRecognizer_SetRadius(LBPHFaceRecognizer fr, int radius); void LBPHFaceRecognizer_SetNeighbors(LBPHFaceRecognizer fr, int neighbors); void LBPHFaceRecognizer_SaveFile(LBPHFaceRecognizer fr, const char* filename); void LBPHFaceRecognizer_LoadFile(LBPHFaceRecognizer fr, const char* filename); +int LBPHFaceRecognizer_GetNeighbors(LBPHFaceRecognizer fr); #ifdef __cplusplus } diff --git a/contrib/face_test.go b/contrib/face_test.go index 7ea360a4..a6adc6b6 100644 --- a/contrib/face_test.go +++ b/contrib/face_test.go @@ -3,6 +3,7 @@ package contrib import ( "gocv.io/x/gocv" "testing" + "math" ) func TestLBPHFaceRecognizer_Methods(t *testing.T) { @@ -27,7 +28,7 @@ func TestLBPHFaceRecognizer_Methods(t *testing.T) { sample := gocv.IMRead("./att_faces/s2/5.pgm", gocv.IMReadGrayScale) label := model.Predict(sample) if label != 2 { - t.Errorf("Invalid simple predict! label: %s", label) + t.Errorf("Invalid simple predict! label: %d", label) } resp := model.PredictExtendedResponse(sample) if resp.Label != 2 { @@ -38,20 +39,31 @@ func TestLBPHFaceRecognizer_Methods(t *testing.T) { model.SetThreshold(0.0) label = model.Predict(sample) if label != -1 { - t.Errorf("Invalid set wrong threshold! label: %s", label) + t.Errorf("Invalid set wrong threshold! label: %d", label) } + //// set good threshold + model.SetThreshold(math.MaxFloat32) // set wrong radius - model.SetThreshold(123.0) model.SetRadius(0) label = model.Predict(sample) - if label != -1 { - t.Errorf("Invalid set wrong radius! label: %s", label) + if label == 2 { + t.Errorf("Invalid set wrong radius! label: %d", label) + } + + neighbors := model.GetNeighbors() + if neighbors == 0 { + t.Errorf("Invalid get neighbors! n: %d", neighbors) } - // add new data model.SetRadius(1) - model.SetThreshold(123.0) + model.SetNeighbors(8) + label = model.Predict(sample) + if label != 2 { + t.Errorf("Invalid set neighbors! label: %d", label) + } + + // add new data sample = gocv.IMRead("./att_faces/s3/10.pgm", gocv.IMReadGrayScale) newLabels := []int{3, 3, 3, 3, 3, 3} newImages := []gocv.Mat{ @@ -65,7 +77,7 @@ func TestLBPHFaceRecognizer_Methods(t *testing.T) { model.Update(newImages, newLabels) label = model.Predict(sample) if label != 3 { - t.Errorf("Invalid new data update: %s", label) + t.Errorf("Invalid new data update: %d", label) } // test save and load @@ -75,6 +87,6 @@ func TestLBPHFaceRecognizer_Methods(t *testing.T) { modelNew.LoadFile(fName) label = modelNew.Predict(sample) if label != 3 { - t.Errorf("Invalid loaded data: %s", label) + t.Errorf("Invalid loaded data: %d", label) } }