This repository has been archived by the owner on Sep 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
faceapi.go
137 lines (117 loc) · 3.04 KB
/
faceapi.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"image"
"image/jpeg"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"time"
"github.com/pkg/errors"
)
// analyze image using Face API
func analyze(cfg *config, img image.Image) (faces, error) {
var buf bytes.Buffer
err := jpeg.Encode(&buf, img, nil)
if err != nil {
return nil, errors.Wrap(err, "failed to encode jpeg")
}
req, err := http.NewRequest("POST", cfg.URI(), &buf)
if err != nil {
return nil, errors.Wrap(err, "failed to create request")
}
req.Header.Add("Content-Type", "application/octet-stream")
req.Header.Add("Ocp-Apim-Subscription-Key", cfg.SubscriptionKey)
requestDump, err := httputil.DumpRequest(req, false)
if err != nil {
log.Println(errors.Wrap(err, "httputil.DumpRequest"))
}
fmt.Fprintln(output, string(requestDump))
client := &http.Client{
Timeout: time.Second * 10,
}
resp, err := client.Do(req)
if err != nil {
return nil, errors.Wrap(err, "client.Do")
}
defer resp.Body.Close()
responseDump, err := httputil.DumpResponse(resp, true)
if err != nil {
log.Println(errors.Wrap(err, "httputil.DumpResponse"))
}
fmt.Fprintln(output, string(responseDump))
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "failed to read body")
}
if resp.StatusCode != 200 {
e := apiError{}
if err := json.Unmarshal(data, &e); err != nil {
return nil, errors.Wrap(err, "unmarshal api error failed")
}
return nil, fmt.Errorf("API %s", e.Error.Message)
}
detectedFaces := faces{}
err = json.Unmarshal(data, &detectedFaces)
return detectedFaces, err
}
// Face API V1.0 response data model
// https://westus.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236
type faces []face
type face struct {
FaceAttributes attributes `json:"faceAttributes"`
FaceID string `json:"faceId"`
FaceRectangle rectangle `json:"faceRectangle"`
}
type attributes struct {
Age float32 `json:"age"`
Gender string `json:"gender"`
Glasses string `json:"glasses"`
Emotion emotion `json:"emotion"`
}
type rectangle struct {
Height int `json:"height"`
Left int `json:"left"`
Top int `json:"top"`
Width int `json:"width"`
}
type emotion struct {
Anger float32 `json:"anger"`
Contempt float32 `json:"contempt"`
Disgust float32 `json:"disgust"`
Fear float32 `json:"fear"`
Happiness float32 `json:"happiness"`
Neutral float32 `json:"neutral"`
Sadness float32 `json:"sadness"`
Surprise float32 `json:"surprise"`
}
func (e emotion) String() string {
m := make(map[string]float32)
m["angry"] = e.Anger
m["contemptuous"] = e.Contempt
m["disgusted"] = e.Disgust
m["feared"] = e.Fear
m["happy"] = e.Happiness
m["neutral"] = e.Neutral
m["sad"] = e.Sadness
m["surprised"] = e.Surprise
var max float32
var res string
for k, v := range m {
if v > max {
res = k
max = v
}
}
return res
}
type apiError struct {
Error errorDetails `json:"error"`
}
type errorDetails struct {
Code string `json:"code"`
Message string `json:"message"`
}