-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathrecognizer.go
287 lines (206 loc) · 5.17 KB
/
recognizer.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
281
282
283
284
285
286
287
package recognizer
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"image"
"os"
goFace "github.com/Kagami/go-face"
)
// Data descriptor of the human face.
type Data struct {
Id string
Descriptor goFace.Descriptor
}
// Face holds coordinates and descriptor of the human face.
type Face struct {
Data
Rectangle image.Rectangle
}
/*
A Recognizer creates face descriptors for provided images and
classifies them into categories.
*/
type Recognizer struct {
Tolerance float32
rec *goFace.Recognizer
UseCNN bool
UseGray bool
Dataset []Data
}
/*
Init initialise a recognizer interface.
*/
func (_this *Recognizer) Init(Path string) error {
_this.Tolerance = 0.4
_this.UseCNN = false
_this.UseGray = true
_this.Dataset = make([]Data, 0)
rec, err := goFace.NewRecognizer(Path)
if err == nil {
_this.rec = rec
}
return err
}
/*
Close frees resources taken by the Recognizer. Safe to call multiple
times. Don't use Recognizer after close call.
*/
func (_this *Recognizer) Close() {
_this.rec.Close()
}
/*
AddImageToDataset add a sample image to the dataset
*/
func (_this *Recognizer) AddImageToDataset(Path string, Id string) error {
file := Path
var err error
if _this.UseGray {
file, err = _this.createTempGrayFile(file, Id)
if err != nil {
return err
}
defer os.Remove(file)
}
var faces []goFace.Face
if _this.UseCNN {
faces, err = _this.rec.RecognizeFileCNN(file)
} else {
faces, err = _this.rec.RecognizeFile(file)
}
if err != nil {
return err
}
if len(faces) == 0 {
return errors.New("Not a face on the image")
}
if len(faces) > 1 {
return errors.New("Not a single face on the image")
}
f := Data{}
f.Id = Id
f.Descriptor = faces[0].Descriptor
_this.Dataset = append(_this.Dataset, f)
return nil
}
/*
SetSamples sets known descriptors so you can classify the new ones.
*/
func (_this *Recognizer) SetSamples() {
var samples []goFace.Descriptor
var avengers []int32
for i, f := range _this.Dataset {
samples = append(samples, f.Descriptor)
avengers = append(avengers, int32(i))
}
_this.rec.SetSamples(samples, avengers)
}
/*
RecognizeSingle returns face if it's the only face on the image or nil otherwise.
Only JPEG format is currently supported.
*/
func (_this *Recognizer) RecognizeSingle(Path string) (goFace.Face, error) {
file := Path
var err error
if _this.UseGray {
file, err = _this.createTempGrayFile(file, "64ab59ac42d69274f06eadb11348969e")
if err != nil {
return goFace.Face{}, err
}
defer os.Remove(file)
}
var idFace *goFace.Face
if _this.UseCNN {
idFace, err = _this.rec.RecognizeSingleFileCNN(file)
} else {
idFace, err = _this.rec.RecognizeSingleFile(file)
}
if err != nil {
return goFace.Face{}, fmt.Errorf("Can't recognize: %v", err)
}
if idFace == nil {
return goFace.Face{}, fmt.Errorf("Not a single face on the image")
}
return *idFace, nil
}
/*
RecognizeMultiples returns all faces found on the provided image, sorted from
left to right. Empty list is returned if there are no faces, error is
returned if there was some error while decoding/processing image.
Only JPEG format is currently supported.
*/
func (_this *Recognizer) RecognizeMultiples(Path string) ([]goFace.Face, error) {
file := Path
var err error
if _this.UseGray {
file, err = _this.createTempGrayFile(file, "64ab59ac42d69274f06eadb11348969e")
if err != nil {
return nil, err
}
defer os.Remove(file)
}
var idFaces []goFace.Face
if _this.UseCNN {
idFaces, err = _this.rec.RecognizeFileCNN(file)
} else {
idFaces, err = _this.rec.RecognizeFile(file)
}
if err != nil {
return nil, fmt.Errorf("Can't recognize: %v", err)
}
return idFaces, nil
}
/*
Classify returns all faces identified in the image. Empty list is returned if no match.
*/
func (_this *Recognizer) Classify(Path string) ([]Face, error) {
face, err := _this.RecognizeSingle(Path)
if err != nil {
return nil, err
}
personID := _this.rec.ClassifyThreshold(face.Descriptor, _this.Tolerance)
if personID < 0 {
return nil, fmt.Errorf("Can't classify")
}
facesRec := make([]Face, 0)
aux := Face{Data: _this.Dataset[personID], Rectangle: face.Rectangle}
facesRec = append(facesRec, aux)
return facesRec, nil
}
/*
ClassifyMultiples returns all faces identified in the image. Empty list is returned if no match.
*/
func (_this *Recognizer) ClassifyMultiples(Path string) ([]Face, error) {
faces, err := _this.RecognizeMultiples(Path)
if err != nil {
return nil, fmt.Errorf("Can't recognize: %v", err)
}
facesRec := make([]Face, 0)
for _, f := range faces {
personID := _this.rec.ClassifyThreshold(f.Descriptor, _this.Tolerance)
if personID < 0 {
continue
}
aux := Face{Data: _this.Dataset[personID], Rectangle: f.Rectangle}
facesRec = append(facesRec, aux)
}
return facesRec, nil
}
/*
fileExists check se file exist
*/
func fileExists(FileName string) bool {
file, err := os.Stat(FileName)
return (err == nil) && !file.IsDir()
}
/*
jsonMarshal Marshal interface to array of byte
*/
func jsonMarshal(t interface{}) ([]byte, error) {
buffer := &bytes.Buffer{}
encoder := json.NewEncoder(buffer)
encoder.SetEscapeHTML(false)
err := encoder.Encode(t)
return buffer.Bytes(), err
}