-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathestimator.go
280 lines (248 loc) · 6.96 KB
/
estimator.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package facenet
import (
"errors"
"image"
"sync"
"github.com/llgcode/draw2d"
"github.com/bububa/facenet/core"
"github.com/bububa/facenet/imageutil"
)
// Estimator represents facenet estimator
type Estimator struct {
model *core.Net
db *Storage
font *imageutil.Font
lock *sync.RWMutex
}
// New init a new facenet Estimator
func New(opts ...Option) (*Estimator, error) {
instance := &Estimator{
lock: new(sync.RWMutex),
}
for _, opt := range opts {
if err := opt.apply(instance); err != nil {
return nil, err
}
}
return instance, nil
}
// SetModel set face net model
func (ins *Estimator) SetModel(net *core.Net) {
ins.lock.Lock()
defer ins.lock.Unlock()
ins.model = net
}
// SetDB set db
func (ins *Estimator) SetDB(db *Storage) {
ins.lock.Lock()
defer ins.lock.Unlock()
ins.db = db
}
// LoadDB load db file
func (ins *Estimator) LoadDB(fname string) error {
ins.lock.Lock()
defer ins.lock.Unlock()
if ins.db == nil {
ins.db = NewStorage(nil, nil)
}
return ins.db.Load(fname)
}
// SaveDB save db file
func (ins *Estimator) SaveDB(fname string) error {
ins.lock.RLock()
defer ins.lock.RUnlock()
if ins.db == nil {
return nil
}
return ins.db.Save(fname)
}
// SetFont set font
func (ins *Estimator) SetFont(data *draw2d.FontData, size float64) error {
ins.lock.Lock()
defer ins.lock.Unlock()
if ins.font == nil {
ins.font = new(imageutil.Font)
}
ins.font.Size = size
ins.font.Data = data
if ins.font.Cache != nil {
return ins.font.Load(ins.font.Cache)
}
return nil
}
// SetFontSize set font size
func (ins *Estimator) SetFontSize(size float64) {
ins.lock.Lock()
defer ins.lock.Unlock()
if ins.font == nil {
ins.font = new(imageutil.Font)
}
ins.font.Size = size
}
// SetFontCache set font cache
func (ins *Estimator) SetFontCache(cache draw2d.FontCache) error {
ins.lock.Lock()
defer ins.lock.Unlock()
if ins.font == nil {
ins.font = new(imageutil.Font)
}
ins.font.Cache = cache
if ins.font.Data != nil {
return ins.font.Load(ins.font.Cache)
}
return nil
}
// SetFontPath set font cache with font cache path
func (ins *Estimator) SetFontPath(cachePath string) error {
ins.lock.Lock()
defer ins.lock.Unlock()
if ins.font == nil {
ins.font = new(imageutil.Font)
}
ins.font.Cache = imageutil.NewFontCache(cachePath)
if ins.font.Data != nil {
return ins.font.Load(ins.font.Cache)
}
return nil
}
// People get people
func (ins *Estimator) People() *core.People {
if ins.db == nil {
return nil
}
return ins.db.People()
}
// PeopleSafe get people (mutlthread safe)
func (ins *Estimator) PeopleSafe() *core.People {
ins.lock.RLock()
defer ins.lock.RUnlock()
return ins.People()
}
// AddPerson add person to people
func (ins *Estimator) AddPerson(items ...*core.Person) {
if ins.db == nil {
return
}
ins.db.Add(items...)
}
// AddPersonSafe add person to people (multithread safe)
func (ins *Estimator) AddPersonSafe(items ...*core.Person) {
ins.lock.Lock()
defer ins.lock.Unlock()
ins.AddPerson(items...)
}
// DeletePerson delete a person by name
func (ins *Estimator) DeletePerson(name string) bool {
if ins.db == nil {
return false
}
return ins.db.Delete(name)
}
// DeletePersonSafe delete a person by name (multithread safe)
func (ins *Estimator) DeletePersonSafe(name string) bool {
ins.lock.Lock()
defer ins.lock.Unlock()
return ins.DeletePerson(name)
}
// Match match a person with embedding
func (ins *Estimator) Match(embedding []float32) (*core.Person, float64, error) {
if ins.db == nil {
return nil, 0, errors.New("no db inited")
}
return ins.db.Match(embedding)
}
// MatchSafe match a person with embedding (multithread safe)
func (ins *Estimator) MatchSafe(embedding []float32) (*core.Person, float64, error) {
ins.lock.RLock()
defer ins.lock.RUnlock()
return ins.Match(embedding)
}
// Predict returns embedding predicted results
func (ins *Estimator) Predict(embedding []float32) ([]*core.Person, []float64, error) {
if ins.db == nil {
return nil, nil, errors.New("no db inited")
}
return ins.db.Predict(embedding)
}
// PredictSafe returns embedding predicted results (multithread safe)
func (ins *Estimator) PredictSafe(embedding []float32) ([]*core.Person, []float64, error) {
ins.lock.RLock()
defer ins.lock.RUnlock()
return ins.Predict(embedding)
}
// ExtractFace extract face for a person from image
func (ins *Estimator) ExtractFace(person *core.Person, img image.Image, minSize int) (*core.FaceMarker, error) {
if ins.model == nil {
return nil, errors.New("model not inited")
}
face, err := ins.model.DetectSingle(img, minSize)
if err != nil {
return nil, err
}
person.Embeddings = append(person.Embeddings, &core.Person_Embedding{
Value: face.Embeddings[0],
})
return core.NewFaceMarker(face, person.GetName(), 1), nil
}
// ExtractFaceSafe extract face for a person from image (multithread safe)
func (ins *Estimator) ExtractFaceSafe(person *core.Person, img image.Image, minSize int) (*core.FaceMarker, error) {
ins.lock.RLock()
defer ins.lock.RUnlock()
return ins.ExtractFace(person, img, minSize)
}
// DetectFaces detect face markers from image
func (ins *Estimator) DetectFaces(img image.Image, minSize int) (*core.FaceMarkers, error) {
if ins.model == nil {
return nil, errors.New("model not inited")
}
faces, err := ins.model.DetectMultiple(img, minSize)
if err != nil {
return nil, err
}
markers := core.NewFaceMarkers(img)
for _, face := range faces {
person, distance, err := ins.Match(face.Embeddings[0])
marker := core.NewFaceMarker(face, person.GetName(), distance)
if err != nil {
marker.SetError(err)
}
markers.Append(*marker)
}
return markers, nil
}
// DetectFacesSafe detect face markers from image (multithread safe)
func (ins *Estimator) DetectFacesSafe(img image.Image, minSize int) (*core.FaceMarkers, error) {
ins.lock.RLock()
defer ins.lock.RUnlock()
return ins.DetectFaces(img, minSize)
}
// Train for trainging classifier
func (ins *Estimator) Train(split float64, iterations int, verbosity int) {
if ins.db == nil {
return
}
ins.db.Train(split, iterations, verbosity)
}
// TrainSafe for trainging classifier (multithread safe)
func (ins *Estimator) TrainSafe(split float64, iterations int, verbosity int) {
ins.lock.RLock()
defer ins.lock.RUnlock()
ins.Train(split, iterations, verbosity)
}
// BatchTrain for trainging classifier
func (ins *Estimator) BatchTrain(split float64, iterations int, verbosity int, batch int) {
if ins.db == nil {
return
}
ins.db.BatchTrain(split, iterations, verbosity, batch)
}
// BatchTrainSafe for trainging classifier (multithread safe)
func (ins *Estimator) BatchTrainSafe(split float64, iterations int, verbosity int, batch int) {
ins.lock.RLock()
defer ins.lock.RUnlock()
ins.BatchTrain(split, iterations, verbosity, batch)
}
// DrawMarkers draw face markers on image
func (ins *Estimator) DrawMarkers(markers *core.FaceMarkers, txtColor string, successColor string, failedColor string, strokeWidth float64, succeedOnly bool) image.Image {
return markers.Draw(ins.font, txtColor, successColor, failedColor, strokeWidth, succeedOnly)
}