Skip to content

Commit

Permalink
BasicFaceRecognizer + EigenFaceRecognizer + FisherFaceRecognizer (#1213)
Browse files Browse the repository at this point in the history
contrib/face: BasicFaceRecognizer + EigenFaceRecognizer + FisherFaceRecognizer
  • Loading branch information
diegohce authored Sep 3, 2024
1 parent 8fe0d8b commit 3b57c20
Show file tree
Hide file tree
Showing 6 changed files with 548 additions and 3 deletions.
108 changes: 108 additions & 0 deletions contrib/face.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,88 @@ void FaceRecognizer_LoadFile(FaceRecognizer fr, const char* filename) {
return;
}

void BasicFaceRecognizer_Train(BasicFaceRecognizer fr, Mats mats, IntVector labels_in){
std::vector<int> labels;

for (int i = 0, *v = labels_in.val; i < labels_in.length; ++v, ++i) {
labels.push_back(*v);
}

std::vector<cv::Mat> images;

for (int i = 0; i < mats.length; ++i) {
images.push_back(*mats.mats[i]);
}

(*fr)->train(images, labels);

return;
}

void BasicFaceRecognizer_Update(BasicFaceRecognizer fr, Mats mats, IntVector labels_in){
std::vector<int> labels;

for (int i = 0, *v = labels_in.val; i < labels_in.length; ++v, ++i) {
labels.push_back(*v);
}

std::vector<cv::Mat> images;

for (int i = 0; i < mats.length; ++i) {
images.push_back(*mats.mats[i]);
}
(*fr)->update(images, labels);
}

Mat BasicFaceRecognizer_getEigenValues(BasicFaceRecognizer fr){
return new cv::Mat((*fr)->getEigenValues());
}

Mat BasicFaceRecognizer_getEigenVectors(BasicFaceRecognizer fr){
return new cv::Mat((*fr)->getEigenVectors());
}

Mat BasicFaceRecognizer_getLabels(BasicFaceRecognizer fr){
return new cv::Mat((*fr)->getLabels());
}

Mat BasicFaceRecognizer_getMean(BasicFaceRecognizer fr){
return new cv::Mat((*fr)->getMean());
}

int BasicFaceRecognizer_getNumComponents(BasicFaceRecognizer fr) {
return (*fr)->getNumComponents();
}

Mats BasicFaceRecognizer_getProjections(BasicFaceRecognizer fr) {
Mats mats;

std::vector<cv::Mat> vec = (*fr)->getProjections();

mats.length = (int)vec.size();
mats.mats = new Mat[vec.size()];

for(size_t i = 0; i < vec.size(); i++) {
mats.mats[i] = new cv::Mat(vec[i]);
}
return mats;
}

void BasicFaceRecognizer_setNumComponents(BasicFaceRecognizer fr, int val){
(*fr)->setNumComponents(val);
}

void BasicFaceRecognizer_SaveFile(BasicFaceRecognizer fr, const char* filename){
(*fr)->write(filename);
}

void BasicFaceRecognizer_LoadFile(BasicFaceRecognizer fr, const char* filename){
(*fr)->read(filename);
}




LBPHFaceRecognizer CreateLBPHFaceRecognizer() {
return new cv::Ptr<cv::face::LBPHFaceRecognizer>(cv::face::LBPHFaceRecognizer::create());
}
Expand Down Expand Up @@ -121,4 +203,30 @@ int LBPHFaceRecognizer_GetGridY(LBPHFaceRecognizer fr) {

void LBPHFaceRecognizer_Close(LBPHFaceRecognizer fr) {
delete fr;
}


FisherFaceRecognizer FisherFaceRecognizer_Create(void) {
return new cv::Ptr<cv::face::FisherFaceRecognizer>(cv::face::FisherFaceRecognizer::create());
}

FisherFaceRecognizer FisherFaceRecognizer_CreateWithParams(int num_components, float threshold) {
return new cv::Ptr<cv::face::FisherFaceRecognizer>(cv::face::FisherFaceRecognizer::create(num_components, threshold));
}

void FisherFaceRecognizer_Close(FisherFaceRecognizer fr) {
delete fr;
}


EigenFaceRecognizer EigenFaceRecognizer_Create(void) {
return new cv::Ptr<cv::face::EigenFaceRecognizer>(cv::face::EigenFaceRecognizer::create());
}

EigenFaceRecognizer EigenFaceRecognizer_CreateWithParams(int num_components, float threshold) {
return new cv::Ptr<cv::face::EigenFaceRecognizer>(cv::face::EigenFaceRecognizer::create(num_components, threshold));
}

void EigenFaceRecognizer_Close(EigenFaceRecognizer fr) {
delete fr;
}
170 changes: 170 additions & 0 deletions contrib/face.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ type PredictResponse struct {
}

var _ FaceRecognizer = (*LBPHFaceRecognizer)(nil)
var _ FaceRecognizer = (*FisherFaceRecognizer)(nil)
var _ BasicFaceRecognizer = (*FisherFaceRecognizer)(nil)
var _ FaceRecognizer = (*EigenFaceRecognizer)(nil)
var _ BasicFaceRecognizer = (*EigenFaceRecognizer)(nil)

// LBPHFaceRecognizer is a wrapper for the OpenCV Local Binary Patterns
// Histograms face recognizer.
Expand Down Expand Up @@ -190,3 +194,169 @@ func (fr *LBPHFaceRecognizer) Close() error {
fr.p = nil
return nil
}

type FisherFaceRecognizer struct {
p C.FisherFaceRecognizer
}

func NewFisherFaceRecognizer() *FisherFaceRecognizer {
return &FisherFaceRecognizer{p: C.FisherFaceRecognizer_Create()}
}

func NewFisherFaceRecognizerWithParams(numComponents int, threshold float32) *FisherFaceRecognizer {
return &FisherFaceRecognizer{p: C.FisherFaceRecognizer_CreateWithParams(C.int(numComponents), C.float(threshold))}
}

func (fr *FisherFaceRecognizer) Empty() bool {
b := faceRecognizer_Empty(C.FaceRecognizer(fr.p))
return bool(b)
}

func (fr *FisherFaceRecognizer) GetEigenValues() gocv.Mat {
return basicFaceRecognizer_GetEigenValues(C.BasicFaceRecognizer(fr.p))
}

func (fr *FisherFaceRecognizer) GetEigenVectors() gocv.Mat {
return basicFaceRecognizer_GetEigenVectors(C.BasicFaceRecognizer(fr.p))
}

func (fr *FisherFaceRecognizer) GetLabels() gocv.Mat {
return basicFaceRecognizer_GetLabels(C.BasicFaceRecognizer(fr.p))
}

func (fr *FisherFaceRecognizer) GetMean() gocv.Mat {
return basicFaceRecognizer_GetMean(C.BasicFaceRecognizer(fr.p))
}

func (fr *FisherFaceRecognizer) GetNumComponents() int {
return basicFaceRecognizer_GetNumComponents(C.BasicFaceRecognizer(fr.p))
}

func (fr *FisherFaceRecognizer) SetNumComponents(val int) {
basicFaceRecognizer_SetNumComponents(C.BasicFaceRecognizer(fr.p), val)
}

func (fr *FisherFaceRecognizer) GetProjections() []gocv.Mat {
return basicFaceRecognizer_GetProjections(C.BasicFaceRecognizer(fr.p))
}

func (fr *FisherFaceRecognizer) GetThreshold() float32 {
return faceRecognizer_GetThreshold(C.FaceRecognizer(fr.p))
}

func (fr *FisherFaceRecognizer) SetThreshold(threshold float32) {
faceRecognizer_SetThreshold(C.FaceRecognizer(fr.p), threshold)
}

func (fr *FisherFaceRecognizer) LoadFile(filename string) {
basicFaceRecognizer_LoadFile(C.BasicFaceRecognizer(fr.p), filename)
}

func (fr *FisherFaceRecognizer) SaveFile(filename string) {
basicFaceRecognizer_SaveFile(C.BasicFaceRecognizer(fr.p), filename)
}

func (fr *FisherFaceRecognizer) Predict(sample gocv.Mat) int {
return faceRecognizer_Predict(C.FaceRecognizer(fr.p), sample)
}

func (fr *FisherFaceRecognizer) PredictExtendedResponse(sample gocv.Mat) PredictResponse {
return faceRecognizer_PredictExtendedResponse(C.FaceRecognizer(fr.p), sample)
}

func (fr *FisherFaceRecognizer) Train(images []gocv.Mat, labels []int) {
basicFaceRecognizer_Train(C.BasicFaceRecognizer(fr.p), images, labels)
}
func (fr *FisherFaceRecognizer) Update(newImages []gocv.Mat, newLabels []int) {
faceRecognizer_Train(C.FaceRecognizer(fr.p), newImages, newLabels)

}

func (fr *FisherFaceRecognizer) Close() error {
C.FisherFaceRecognizer_Close(fr.p)
fr.p = nil
return nil
}

type EigenFaceRecognizer struct {
p C.EigenFaceRecognizer
}

func NewEigenFaceRecognizer() *EigenFaceRecognizer {
return &EigenFaceRecognizer{p: C.EigenFaceRecognizer_Create()}
}

func NewEigenFaceRecognizerWithParams(numComponents int, threshold float32) *EigenFaceRecognizer {
return &EigenFaceRecognizer{p: C.EigenFaceRecognizer_CreateWithParams(C.int(numComponents), C.float(threshold))}
}

func (fr *EigenFaceRecognizer) Empty() bool {
b := faceRecognizer_Empty(C.FaceRecognizer(fr.p))
return bool(b)
}

func (fr *EigenFaceRecognizer) GetEigenValues() gocv.Mat {
return basicFaceRecognizer_GetEigenValues(C.BasicFaceRecognizer(fr.p))
}

func (fr *EigenFaceRecognizer) GetEigenVectors() gocv.Mat {
return basicFaceRecognizer_GetEigenVectors(C.BasicFaceRecognizer(fr.p))
}

func (fr *EigenFaceRecognizer) GetLabels() gocv.Mat {
return basicFaceRecognizer_GetLabels(C.BasicFaceRecognizer(fr.p))
}

func (fr *EigenFaceRecognizer) GetMean() gocv.Mat {
return basicFaceRecognizer_GetMean(C.BasicFaceRecognizer(fr.p))
}

func (fr *EigenFaceRecognizer) GetNumComponents() int {
return basicFaceRecognizer_GetNumComponents(C.BasicFaceRecognizer(fr.p))
}

func (fr *EigenFaceRecognizer) SetNumComponents(val int) {
basicFaceRecognizer_SetNumComponents(C.BasicFaceRecognizer(fr.p), val)
}

func (fr *EigenFaceRecognizer) GetProjections() []gocv.Mat {
return basicFaceRecognizer_GetProjections(C.BasicFaceRecognizer(fr.p))
}

func (fr *EigenFaceRecognizer) GetThreshold() float32 {
return faceRecognizer_GetThreshold(C.FaceRecognizer(fr.p))
}

func (fr *EigenFaceRecognizer) SetThreshold(threshold float32) {
faceRecognizer_SetThreshold(C.FaceRecognizer(fr.p), threshold)
}

func (fr *EigenFaceRecognizer) LoadFile(filename string) {
basicFaceRecognizer_LoadFile(C.BasicFaceRecognizer(fr.p), filename)
}

func (fr *EigenFaceRecognizer) SaveFile(filename string) {
basicFaceRecognizer_SaveFile(C.BasicFaceRecognizer(fr.p), filename)
}

func (fr *EigenFaceRecognizer) Predict(sample gocv.Mat) int {
return faceRecognizer_Predict(C.FaceRecognizer(fr.p), sample)
}

func (fr *EigenFaceRecognizer) PredictExtendedResponse(sample gocv.Mat) PredictResponse {
return faceRecognizer_PredictExtendedResponse(C.FaceRecognizer(fr.p), sample)
}

func (fr *EigenFaceRecognizer) Train(images []gocv.Mat, labels []int) {
basicFaceRecognizer_Train(C.BasicFaceRecognizer(fr.p), images, labels)
}

func (fr *EigenFaceRecognizer) Update(newImages []gocv.Mat, newLabels []int) {
basicFaceRecognizer_Train(C.BasicFaceRecognizer(fr.p), newImages, newLabels)
}

func (fr *EigenFaceRecognizer) Close() error {
C.EigenFaceRecognizer_Close(fr.p)
fr.p = nil
return nil
}
29 changes: 26 additions & 3 deletions contrib/face.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@ extern "C" {
#include "../core.h"

#ifdef __cplusplus
typedef cv::Ptr<cv::face::LBPHFaceRecognizer>* LBPHFaceRecognizer;
typedef cv::Ptr<cv::face::FaceRecognizer>* FaceRecognizer;
typedef cv::Ptr<cv::face::BasicFaceRecognizer>* BasicFaceRecognizer;
typedef cv::Ptr<cv::face::LBPHFaceRecognizer>* LBPHFaceRecognizer;
typedef cv::Ptr<cv::face::EigenFaceRecognizer>* EigenFaceRecognizer;
typedef cv::Ptr<cv::face::FisherFaceRecognizer>* FisherFaceRecognizer;
#else
typedef void* LBPHFaceRecognizer;
typedef void* FaceRecognizer;
typedef void* BasicFaceRecognizer;
typedef void* LBPHFaceRecognizer;
typedef void* EigenFaceRecognizer;
typedef void* FisherFaceRecognizer;
#endif

struct PredictResponse {
Expand All @@ -34,8 +40,19 @@ void FaceRecognizer_SaveFile(FaceRecognizer fr, const char* filename);
void FaceRecognizer_LoadFile(FaceRecognizer fr, const char* filename);


void BasicFaceRecognizer_Train(BasicFaceRecognizer fr, Mats images, IntVector labels);
void BasicFaceRecognizer_Update(BasicFaceRecognizer fr, Mats images, IntVector labels);
Mat BasicFaceRecognizer_getEigenValues(BasicFaceRecognizer fr);
Mat BasicFaceRecognizer_getEigenVectors(BasicFaceRecognizer fr);
Mat BasicFaceRecognizer_getLabels(BasicFaceRecognizer fr);
Mat BasicFaceRecognizer_getMean(BasicFaceRecognizer fr);
int BasicFaceRecognizer_getNumComponents(BasicFaceRecognizer fr);
Mats BasicFaceRecognizer_getProjections(BasicFaceRecognizer fr);
void BasicFaceRecognizer_setNumComponents(BasicFaceRecognizer fr, int val);
void BasicFaceRecognizer_SaveFile(BasicFaceRecognizer fr, const char* filename);
void BasicFaceRecognizer_LoadFile(BasicFaceRecognizer fr, const char* filename);

LBPHFaceRecognizer CreateLBPHFaceRecognizer();
LBPHFaceRecognizer CreateLBPHFaceRecognizer(void);
void LBPHFaceRecognizer_SetRadius(LBPHFaceRecognizer fr, int radius);
void LBPHFaceRecognizer_SetNeighbors(LBPHFaceRecognizer fr, int neighbors);
int LBPHFaceRecognizer_GetNeighbors(LBPHFaceRecognizer fr);
Expand All @@ -46,7 +63,13 @@ int LBPHFaceRecognizer_GetGridY(LBPHFaceRecognizer fr);
void LBPHFaceRecognizer_Close(LBPHFaceRecognizer fr);


FisherFaceRecognizer FisherFaceRecognizer_Create(void);
FisherFaceRecognizer FisherFaceRecognizer_CreateWithParams(int num_components, float threshold);
void FisherFaceRecognizer_Close(FisherFaceRecognizer fr);

EigenFaceRecognizer EigenFaceRecognizer_Create(void);
EigenFaceRecognizer EigenFaceRecognizer_CreateWithParams(int num_components, float threshold);
void EigenFaceRecognizer_Close(EigenFaceRecognizer fr);

#ifdef __cplusplus
}
Expand Down
Loading

0 comments on commit 3b57c20

Please sign in to comment.