Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

facemark support #1123

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ count.out
.idea/
contrib/data.yaml
contrib/testOilPainting.png
lbfmodel.yaml
23 changes: 23 additions & 0 deletions contrib/facemarkLBF.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "facemarkLBF.h"

LBPHFaceMark CreateLBPHFaceMark() {
return new cv::Ptr<cv::face::Facemark>(cv::face::FacemarkLBF::create());
}

void LBPHFaceMark_LoadModel(LBPHFaceMark fm, const char* model) {
(*fm)->loadModel(model);
}

bool LBPHFaceMark_Fit(LBPHFaceMark fm, Mat frame, struct Rects faces, Points2fVector landmarks) {
std::vector<cv::Rect> _faces;

for (int i = 0; i < faces.length; ++i) {
_faces.push_back(cv::Rect(
faces.rects[i].x,
faces.rects[i].y,
faces.rects[i].width,
faces.rects[i].height
));
}
return (*fm)->fit(*frame, _faces, *landmarks);
}
57 changes: 57 additions & 0 deletions contrib/facemarkLBF.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package contrib

/*
#include <stdlib.h>
#include "facemarkLBF.h"
#include "../core.h"
*/
import "C"
import (
"gocv.io/x/gocv"
"image"
"unsafe"
)

type LBPHFaceMark struct {
p C.LBPHFaceMark
}

func NewLBPHFaceMark() *LBPHFaceMark {
return &LBPHFaceMark{p: C.CreateLBPHFaceMark()}
}

func (mark *LBPHFaceMark) LoadModel(model string) {
cName := C.CString(model)
defer C.free(unsafe.Pointer(cName))
C.LBPHFaceMark_LoadModel(mark.p, cName)
}

// LBPHFaceMark_Fit(LBPHFaceMark fm, Mat frame, struct Rects faces, Points2fVector landmarks)
func (mark *LBPHFaceMark) Fit(img gocv.Mat, faceBox []image.Rectangle) (bool, [][]gocv.Point2f) {

bboxesRectArr := []C.struct_Rect{}
for _, v := range faceBox {
bbox := C.struct_Rect{
x: C.int(v.Min.X),
y: C.int(v.Min.Y),
width: C.int(v.Size().X),
height: C.int(v.Size().Y),
}
bboxesRectArr = append(bboxesRectArr, bbox)
}

bboxesRects := C.Rects{
rects: (*C.Rect)(&bboxesRectArr[0]),
length: C.int(len(faceBox)),
}

points2fVector := gocv.NewPoints2fVector()
result := bool(C.LBPHFaceMark_Fit(
mark.p,
C.Mat(img.Ptr()),
bboxesRects,
C.Points2fVector(points2fVector.P()),
))

return result, points2fVector.ToPoints()
}
32 changes: 32 additions & 0 deletions contrib/facemarkLBF.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef _OPENCV3_FACE_H_
#define _OPENCV3_FACE_H_

#ifdef __cplusplus
#include <opencv2/opencv.hpp>
#include <opencv2/face.hpp>

extern "C" {
#endif

#include "../core.h"

#ifdef __cplusplus
typedef cv::Ptr<cv::face::Facemark>* LBPHFaceMark;
#else
typedef void* LBPHFaceMark;
#endif

struct PredictResponse {
int label;
double confidence;
};

LBPHFaceMark CreateLBPHFaceMark();
void LBPHFaceMark_LoadModel(LBPHFaceMark fm, const char* model); //Points2fVector
bool LBPHFaceMark_Fit(LBPHFaceMark fm, Mat frame, struct Rects faces, Points2fVector landmarks);

#ifdef __cplusplus
}
#endif

#endif //_OPENCV3_FACE_H_
21 changes: 21 additions & 0 deletions contrib/facemarkLBF_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package contrib

import (
"log"
"testing"
)

func TestNewLBPHFaceMark(t *testing.T) {
mark := NewLBPHFaceMark()
log.Println(mark)
}

func TestLBPHFaceMark_LoadModel(t *testing.T) {
mark := NewLBPHFaceMark()
log.Println(mark)
mark.LoadModel("/Users/wushaojie/Documents/project/golang/go-opencv/lbfmodel.yaml")
log.Println("success")
}

func TestLBPHFaceMark_Fit(t *testing.T) {
}
Loading