-
-
Notifications
You must be signed in to change notification settings - Fork 170
/
face_test.go
233 lines (207 loc) · 5 KB
/
face_test.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
package face_test
import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"path/filepath"
"strings"
"testing"
"unsafe"
"github.com/Kagami/go-face"
)
var (
rec *face.Recognizer
idolTests = map[string]string{
"elkie.jpg": "Elkie, CLC",
"chaeyoung.jpg": "Chaeyoung, Twice",
"chaeyoung2.jpg": "Chaeyoung, Twice",
"sejeong.jpg": "Sejeong, Gugudan",
"jimin.jpg": "Jimin, AOA",
"jimin2.jpg": "Jimin, AOA",
"jimin4.jpg": "Jimin, AOA",
"meiqi.jpg": "Mei Qi, WJSN",
"chaeyeon.jpg": "Chaeyeon, DIA",
"chaeyeon3.jpg": "Chaeyeon, DIA",
"tzuyu2.jpg": "Tzuyu, Twice",
"nayoung.jpg": "Nayoung, PRISTIN",
"luda2.jpg": "Luda, WJSN",
"joy.jpg": "Joy, Red Velvet",
}
)
type Idol struct {
ID string `json:"id"`
Name string `json:"name"`
BandName string `json:"band_name"`
}
type IdolFace struct {
Descriptor string `json:"descriptor"`
IdolID string `json:"idol_id"`
}
type IdolData struct {
Idols []Idol `json:"idols"`
Faces []IdolFace `json:"faces"`
byID map[string]*Idol
}
type TrainData struct {
samples []face.Descriptor
cats []int32
labels []string
}
func getTPath(fname string) string {
return filepath.Join("testdata", "images", fname)
}
func getIdolData() (idata *IdolData, err error) {
data, err := ioutil.ReadFile(filepath.Join("testdata", "idols.json"))
if err != nil {
return
}
idata = &IdolData{}
err = json.Unmarshal(data, idata)
if err != nil {
return
}
idata.byID = make(map[string]*Idol)
for i, _ := range idata.Idols {
idol := &idata.Idols[i]
idata.byID[idol.ID] = idol
}
return
}
func str2descr(s string) face.Descriptor {
b, err := base64.StdEncoding.DecodeString(s)
if err != nil {
panic(err)
}
return *(*face.Descriptor)(unsafe.Pointer(&b[0]))
}
func getTrainData(idata *IdolData) (tdata *TrainData) {
var samples []face.Descriptor
var cats []int32
var labels []string
var catID int32
var prevIdolID string
catID = -1
for i, _ := range idata.Faces {
iface := &idata.Faces[i]
descriptor := str2descr(iface.Descriptor)
samples = append(samples, descriptor)
if iface.IdolID != prevIdolID {
catID++
labels = append(labels, iface.IdolID)
}
cats = append(cats, catID)
prevIdolID = iface.IdolID
}
tdata = &TrainData{
samples: samples,
cats: cats,
labels: labels,
}
return
}
func recognizeAndClassify(fpath string, tolerance float32) (id int, err error) {
id = -1
f, err := rec.RecognizeSingleFile(fpath)
if err != nil || f == nil {
return
}
if tolerance < 0 {
id = rec.Classify(f.Descriptor)
} else {
id = rec.ClassifyThreshold(f.Descriptor, tolerance)
}
return
}
func TestSerializationError(t *testing.T) {
_, err := face.NewRecognizer("/notexist")
switch err.(type) {
case face.SerializationError:
// skip
default:
t.Fatalf("Wrong error: %v", err)
}
}
func TestInit(t *testing.T) {
var err error
rec, err = face.NewRecognizer(filepath.Join("testdata", "models"))
if err != nil {
t.Fatalf("Can't init face recognizer: %v", err)
}
}
func TestImageLoadError(t *testing.T) {
_, err := rec.Recognize([]byte{1, 2, 3})
switch err.(type) {
case face.ImageLoadError:
// skip
default:
t.Fatalf("Wrong error: %v", err)
}
}
func TestNumFaces(t *testing.T) {
faces, err := rec.RecognizeFile(getTPath("pristin.jpg"))
if err != nil {
t.Fatalf("Can't get faces: %v", err)
}
numFaces := len(faces)
if numFaces != 10 {
t.Fatalf("Wrong number of faces: %d", numFaces)
}
}
func TestEmptyClassify(t *testing.T) {
var sample face.Descriptor
id := rec.Classify(sample)
if id >= 0 {
t.Fatalf("Shouldn't recognize but got %d category", id)
}
}
func TestIdols(t *testing.T) {
idata, err := getIdolData()
if err != nil {
t.Fatalf("Can't get idol data: %v", err)
}
tdata := getTrainData(idata)
rec.SetSamples(tdata.samples, tdata.cats)
for fname, expected := range idolTests {
t.Run(fname, func(t *testing.T) {
names := strings.Split(expected, ", ")
expectedIname := names[0]
expectedBname := names[1]
catID, err := recognizeAndClassify(getTPath(fname), -1)
if err != nil {
t.Fatalf("Can't recognize: %v", err)
}
if catID < 0 {
t.Errorf("%s: expected “%s” but not recognized", fname, expected)
return
}
idolID := tdata.labels[catID]
idol := idata.byID[idolID]
actualIname := idol.Name
actualBname := idol.BandName
if expectedIname != actualIname || expectedBname != actualBname {
actual := fmt.Sprintf("%s, %s", actualIname, actualBname)
t.Errorf("%s: expected “%s” but got “%s”", fname, expected, actual)
}
})
}
}
func TestClassifyThreshold(t *testing.T) {
id, err := recognizeAndClassify(getTPath("nana.jpg"), 0.1)
if err != nil {
t.Fatalf("Can't recognize: %v", err)
}
if id >= 0 {
t.Fatalf("Shouldn't recognize but got %d category", id)
}
id, err = recognizeAndClassify(getTPath("nana.jpg"), 0.8)
if err != nil {
t.Fatalf("Can't recognize: %v", err)
}
if id < 0 {
t.Fatalf("Should have recognized but got %d category", id)
}
}
func TestClose(t *testing.T) {
rec.Close()
}