-
Notifications
You must be signed in to change notification settings - Fork 0
/
face.go
117 lines (95 loc) · 2.35 KB
/
face.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
package greenyfy
import (
"bytes"
"net/http"
"image"
"image/jpeg"
"appengine"
"appengine/urlfetch"
"encoding/json"
"errors"
)
func findFaces(c appengine.Context, img *image.Image) ([]Face, error) {
client := urlfetch.Client(c)
endpoint := config.FaceApiUrl + "?returnFaceLandmarks=true&returnFaceAttributes=headPose"
bodyType := "application/octet-stream"
buffer := new(bytes.Buffer)
if err := jpeg.Encode(buffer, *img, nil); err != nil {
return nil, err
}
req, err := http.NewRequest("POST", endpoint, buffer)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", bodyType)
req.Header.Add("Ocp-Apim-Subscription-Key", config.FaceApiKey)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
c.Errorf("Failed to access FaceAPI, return code: %v", resp.Status)
return nil, errors.New("Could not access API")
}
var obj []Face
dec := json.NewDecoder(resp.Body)
err = dec.Decode(&obj)
if err != nil {
return nil, err
}
return obj, nil
}
type Face struct {
Id string `json:"faceId"`
Rectangle FaceRectangle `json:"faceRectangle"`
Landmarks FaceLandmarks `json:"faceLandmarks"`
Attributes FaceAttributes
}
type FaceLandmarks struct {
PupilLeft Point
PupilRight Point
NoseTip Point
MouthLeft Point
MouthRight Point
EyebrowLeftOuter Point
EyebrowLeftInner Point
EyeLeftOuter Point
EyeLeftTop Point
EyeLeftBottom Point
EyeLeftInner Point
EyebrowRightInner Point
EyebrowRightOuter Point
EyeRightInner Point
EyeRightTop Point
EyeRightBottom Point
EyeRightOuter Point
NoseRootLeft Point
NoseRootRight Point
NoseLeftAlarTop Point
NoseRightAlarTop Point
NoseLeftAlarOutTip Point
NoseRightAlarOutTip Point
UpperLipTop Point
UpperLipBottom Point
UnderLipTop Point
UnderLipBottom Point
}
type Point struct {
X float32
Y float32
}
type FaceRectangle struct {
Top float32
Left float32
Width float32
Height float32
}
type FaceAttributes struct {
Pose HeadPose `json:"headPose"`
}
type HeadPose struct {
Pitch float32
Roll float32
Yaw float32
}