/
facebox_teach.go
172 lines (164 loc) · 4.3 KB
/
facebox_teach.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
package facebox
import (
"bytes"
"io"
"mime/multipart"
"net/http"
"net/url"
"strings"
"github.com/machinebox/sdk-go/internal/mbhttp"
"github.com/pkg/errors"
)
// Teach teaches facebox the face in the io.Reader.
// The name should be the name of the person who owns the face.
// The id should be a unique identifier for the image, usually the filename.
func (c *Client) Teach(image io.Reader, id, name string) error {
fn := id
if fn == "" {
fn = "image.dat"
}
var buf bytes.Buffer
w := multipart.NewWriter(&buf)
fw, err := w.CreateFormFile("file", fn)
if err != nil {
return err
}
_, err = io.Copy(fw, image)
if err != nil {
return err
}
if err := w.WriteField("name", name); err != nil {
return err
}
if err := w.WriteField("id", id); err != nil {
return err
}
if err = w.Close(); err != nil {
return err
}
u, err := url.Parse(c.addr + "/facebox/teach")
if err != nil {
return err
}
if !u.IsAbs() {
return errors.New("box address must be absolute")
}
req, err := http.NewRequest("POST", u.String(), &buf)
if err != nil {
return err
}
req.Header.Set("Accept", "application/json; charset=utf-8")
req.Header.Set("Content-Type", w.FormDataContentType())
_, err = mbhttp.New("facebox", c.HTTPClient).DoUnmarshal(req, nil)
if err != nil {
return err
}
return nil
}
// TeachURL teaches facebox the face in the image at the specified URL.
// See Teach for more information.
func (c *Client) TeachURL(imageURL *url.URL, id, name string) error {
u, err := url.Parse(c.addr + "/facebox/teach")
if err != nil {
return err
}
if !u.IsAbs() {
return errors.New("box address must be absolute")
}
if !imageURL.IsAbs() {
return errors.New("url must be absolute")
}
form := url.Values{}
form.Set("url", imageURL.String())
form.Set("name", name)
form.Set("id", id)
req, err := http.NewRequest("POST", u.String(), strings.NewReader(form.Encode()))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Accept", "application/json; charset=utf-8")
_, err = mbhttp.New("facebox", c.HTTPClient).DoUnmarshal(req, nil)
if err != nil {
return err
}
return nil
}
// TeachFaceprint teaches facebox the face that is represented by the faceprint as a parameter.
// See Teach for more information.
func (c *Client) TeachFaceprint(faceprint, id, name string) error {
u, err := url.Parse(c.addr + "/facebox/teach")
if err != nil {
return err
}
if !u.IsAbs() {
return errors.New("box address must be absolute")
}
form := url.Values{}
form.Set("faceprint", faceprint)
form.Set("name", name)
form.Set("id", id)
req, err := http.NewRequest("POST", u.String(), strings.NewReader(form.Encode()))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Accept", "application/json; charset=utf-8")
_, err = mbhttp.New("facebox", c.HTTPClient).DoUnmarshal(req, nil)
if err != nil {
return err
}
return nil
}
// TeachBase64 teaches facebox the face in the Base64 encoded image.
// See Teach for more information.
func (c *Client) TeachBase64(data, id, name string) error {
u, err := url.Parse(c.addr + "/facebox/teach")
if err != nil {
return err
}
if !u.IsAbs() {
return errors.New("box address must be absolute")
}
form := url.Values{}
form.Set("base64", data)
form.Set("name", name)
form.Set("id", id)
req, err := http.NewRequest("POST", u.String(), strings.NewReader(form.Encode()))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Accept", "application/json; charset=utf-8")
_, err = mbhttp.New("facebox", c.HTTPClient).DoUnmarshal(req, nil)
if err != nil {
return err
}
return nil
}
// Remove makes facebox to forget a face
func (c *Client) Remove(id string) error {
if id == "" {
return errors.New("id can not be empty")
}
u, err := url.Parse(c.addr + "/facebox/teach")
if err != nil {
return err
}
if !u.IsAbs() {
return errors.New("box address must be absolute")
}
q := u.Query()
u.Path = u.Path + "/" + id
u.RawQuery = q.Encode()
req, err := http.NewRequest("DELETE", u.String(), nil)
if err != nil {
return err
}
req.Header.Set("Accept", "application/json; charset=utf-8")
_, err = mbhttp.New("facebox", c.HTTPClient).DoUnmarshal(req, nil)
if err != nil {
return err
}
return nil
}