forked from hybridgroup/gocv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathface.go
177 lines (155 loc) · 5.48 KB
/
face.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package contrib
/*
#include <stdlib.h>
#include "face.h"
*/
import "C"
import (
"unsafe"
"gocv.io/x/gocv"
)
// PredictResponse represents a predicted label and associated confidence.
type PredictResponse struct {
Label int32 `json:"label"`
Confidence float32 `json:"confidence"`
}
// LBPHFaceRecognizer is a wrapper for the OpenCV Local Binary Patterns
// Histograms face recognizer.
type LBPHFaceRecognizer struct {
p C.LBPHFaceRecognizer
}
// NewLBPHFaceRecognizer creates a new LBPH Recognizer model.
//
// For further information, see:
// https://docs.opencv.org/master/df/d25/classcv_1_1face_1_1LBPHFaceRecognizer.html
//
func NewLBPHFaceRecognizer() *LBPHFaceRecognizer {
return &LBPHFaceRecognizer{p: C.CreateLBPHFaceRecognizer()}
}
// Train loaded model with images and their labels
//
// see https://docs.opencv.org/master/dd/d65/classcv_1_1face_1_1FaceRecognizer.html#ac8680c2aa9649ad3f55e27761165c0d6
//
func (fr *LBPHFaceRecognizer) Train(images []gocv.Mat, labels []int) {
cparams := []C.int{}
for _, v := range labels {
cparams = append(cparams, C.int(v))
}
labelsVector := C.struct_IntVector{}
labelsVector.val = (*C.int)(&cparams[0])
labelsVector.length = (C.int)(len(cparams))
cMatArray := make([]C.Mat, len(images))
for i, r := range images {
cMatArray[i] = (C.Mat)(r.Ptr())
}
matsVector := C.struct_Mats{
mats: (*C.Mat)(&cMatArray[0]),
length: C.int(len(images)),
}
C.LBPHFaceRecognizer_Train(fr.p, matsVector, labelsVector)
}
// Update updates the existing trained model with new images and labels.
//
// For further information, see:
// https://docs.opencv.org/master/dd/d65/classcv_1_1face_1_1FaceRecognizer.html#a8a4e73ea878dcd0c235d0487189d25f3
//
func (fr *LBPHFaceRecognizer) Update(newImages []gocv.Mat, newLabels []int) {
cparams := []C.int{}
for _, v := range newLabels {
cparams = append(cparams, C.int(v))
}
labelsVector := C.struct_IntVector{}
labelsVector.val = (*C.int)(&cparams[0])
labelsVector.length = (C.int)(len(cparams))
cMatArray := make([]C.Mat, len(newImages))
for i, r := range newImages {
cMatArray[i] = (C.Mat)(r.Ptr())
}
matsVector := C.struct_Mats{
mats: (*C.Mat)(&cMatArray[0]),
length: C.int(len(newImages)),
}
C.LBPHFaceRecognizer_Update(fr.p, matsVector, labelsVector)
}
// Predict predicts a label for a given input image. It returns the label for
// correctly predicted image or -1 if not found.
//
// For further information, see:
// https://docs.opencv.org/master/dd/d65/classcv_1_1face_1_1FaceRecognizer.html#aa2d2f02faffab1bf01317ae6502fb631
//
func (fr *LBPHFaceRecognizer) Predict(sample gocv.Mat) int {
label := C.LBPHFaceRecognizer_Predict(fr.p, (C.Mat)(sample.Ptr()))
return int(label)
}
// PredictExtendedResponse returns a label and associated confidence (e.g.
// distance) for a given input image. It is the extended version of
// `Predict()`.
//
// For further information, see:
// https://docs.opencv.org/master/dd/d65/classcv_1_1face_1_1FaceRecognizer.html#ab0d593e53ebd9a0f350c989fcac7f251
//
func (fr *LBPHFaceRecognizer) PredictExtendedResponse(sample gocv.Mat) PredictResponse {
respp := C.LBPHFaceRecognizer_PredictExtended(fr.p, (C.Mat)(sample.Ptr()))
resp := PredictResponse{
Label: int32(respp.label),
Confidence: float32(respp.confidence),
}
return resp
}
// SetThreshold sets the threshold value of the model, i.e. the threshold
// applied in the prediction.
//
// For further information, see:
// https://docs.opencv.org/master/dd/d65/classcv_1_1face_1_1FaceRecognizer.html#a3182081e5f8023e658ad8ab96656dd63
//
func (fr *LBPHFaceRecognizer) SetThreshold(threshold float32) {
C.LBPHFaceRecognizer_SetThreshold(fr.p, (C.double)(threshold))
}
// SetNeighbors sets the neighbors value of the model, i.e. the number of
// sample points to build a Circular Local Binary Pattern from. Note that wrong
// neighbors can raise OpenCV exception!
//
// For further information, see:
// https://docs.opencv.org/master/df/d25/classcv_1_1face_1_1LBPHFaceRecognizer.html#ab225f7bf353ce8697a506eda10124a92
//
func (fr *LBPHFaceRecognizer) SetNeighbors(neighbors int) {
C.LBPHFaceRecognizer_SetNeighbors(fr.p, (C.int)(neighbors))
}
// GetNeighbors returns the neighbors value of the model.
//
// For further information, see:
// https://docs.opencv.org/master/df/d25/classcv_1_1face_1_1LBPHFaceRecognizer.html#a50a3e2ca6e8464166e153c9df84b0a77
//
func (fr *LBPHFaceRecognizer) GetNeighbors() int {
n := C.LBPHFaceRecognizer_GetNeighbors(fr.p)
return int(n)
}
// SetRadius sets the radius used for building the Circular Local Binary
// Pattern.
//
// For further information, see:
// https://docs.opencv.org/master/df/d25/classcv_1_1face_1_1LBPHFaceRecognizer.html#a62d94c75cade902fd3b487b1ef9883fc
//
func (fr *LBPHFaceRecognizer) SetRadius(radius int) {
C.LBPHFaceRecognizer_SetRadius(fr.p, (C.int)(radius))
}
// SaveFile saves the trained model data to file.
//
// For further information, see:
// https://docs.opencv.org/master/dd/d65/classcv_1_1face_1_1FaceRecognizer.html#a2adf2d555550194244b05c91fefcb4d6
//
func (fr *LBPHFaceRecognizer) SaveFile(fname string) {
cName := C.CString(fname)
defer C.free(unsafe.Pointer(cName))
C.LBPHFaceRecognizer_SaveFile(fr.p, cName)
}
// LoadFile loads a trained model data from file.
//
// For further information, see:
// https://docs.opencv.org/master/dd/d65/classcv_1_1face_1_1FaceRecognizer.html#acc42e5b04595dba71f0777c7179af8c3
//
func (fr *LBPHFaceRecognizer) LoadFile(fname string) {
cName := C.CString(fname)
defer C.free(unsafe.Pointer(cName))
C.LBPHFaceRecognizer_LoadFile(fr.p, cName)
}