Skip to content

Commit

Permalink
added setNeighbors test
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Bosonchenko committed Dec 5, 2017
1 parent 05eae9c commit 5268976
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 17 deletions.
24 changes: 16 additions & 8 deletions contrib/face.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}


11 changes: 11 additions & 0 deletions contrib/face.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions contrib/face.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
30 changes: 21 additions & 9 deletions contrib/face_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package contrib
import (
"gocv.io/x/gocv"
"testing"
"math"
)

func TestLBPHFaceRecognizer_Methods(t *testing.T) {
Expand All @@ -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 {
Expand All @@ -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{
Expand All @@ -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
Expand All @@ -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)
}
}

0 comments on commit 5268976

Please sign in to comment.