-
Notifications
You must be signed in to change notification settings - Fork 9
/
detect.go
49 lines (41 loc) · 917 Bytes
/
detect.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
package facest
import (
"bytes"
"io"
"mime/multipart"
"net/http"
)
// DetectRes .
type DetectRes struct {
Count int `json:"count"`
Faces []DetectedFace `json:"faces"`
}
// DetectedFace .
type DetectedFace struct {
Rectangle Rectangle `json:"rectangle"`
}
// Detect faces within a given image.
func (c *Client) Detect(image io.Reader) (*DetectRes, error) {
var buf bytes.Buffer
w := multipart.NewWriter(&buf)
fw, err := w.CreateFormFile("image", "image.jpg")
if err != nil {
return nil, err
}
if _, err = io.Copy(fw, image); err != nil {
return nil, err
}
if err = w.Close(); err != nil {
return nil, err
}
req, err := http.NewRequest("POST", c.baseURL+"/detect", &buf)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", w.FormDataContentType())
res := DetectRes{}
if err := c.sendRequest(req, &res); err != nil {
return nil, err
}
return &res, nil
}