-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathclient_api.go
370 lines (321 loc) · 10.6 KB
/
client_api.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package rekognition
import (
"bytes"
"errors"
"io"
"io/ioutil"
SDK "github.com/aws/aws-sdk-go/service/rekognition"
"github.com/evalphobia/aws-sdk-go-wrapper/private/pointers"
)
// DetectFacesFromLocalFile gets label info from local image file.
func (svc *Rekognition) DetectFacesFromLocalFile(filepath string) (*FaceDetailResponse, error) {
byt, err := ioutil.ReadFile(filepath)
if err != nil {
return nil, err
}
return svc.DetectFacesByBytes(byt)
}
// DetectFacesFromURL gets label info from URL.
func (svc *Rekognition) DetectFacesFromURL(url string) (*FaceDetailResponse, error) {
byt, err := svc.getImageFromURL(url)
if err != nil {
return nil, err
}
return svc.DetectFacesByBytes(byt)
}
// DetectFacesByBytes gets label info from image of bytes.
func (svc *Rekognition) DetectFacesByBytes(byt []byte) (*FaceDetailResponse, error) {
input := &SDK.DetectFacesInput{
Image: &SDK.Image{
Bytes: byt,
},
}
return svc.detectFaces(input)
}
// DetectFacesByS3Object gets label info from image of S3 object.
func (svc *Rekognition) DetectFacesByS3Object(bucket, name string, version ...string) (*FaceDetailResponse, error) {
input := &SDK.DetectFacesInput{
Image: &SDK.Image{
S3Object: createS3Object(bucket, name, version...),
},
}
return svc.detectFaces(input)
}
// detectFaces executes `detectFace` operation and gets face info.
func (svc *Rekognition) detectFaces(input *SDK.DetectFacesInput) (*FaceDetailResponse, error) {
op, err := svc.client.DetectFaces(input)
if err != nil {
svc.Errorf("error on `DetectFaces` operation; error=%s;", err.Error())
return nil, err
}
list := make([]*FaceDetail, len(op.FaceDetails))
for i, f := range op.FaceDetails {
list[i] = NewFaceDetailFromAWSFaceDetail(f)
}
result := &FaceDetailResponse{
List: list,
}
if op.OrientationCorrection != nil {
result.Orientation = *op.OrientationCorrection
}
return result, nil
}
// DetectLabelsFromLocalFile gets label info from local image file.
func (svc *Rekognition) DetectLabelsFromLocalFile(filepath string) (*LabelResponse, error) {
byt, err := ioutil.ReadFile(filepath)
if err != nil {
return nil, err
}
return svc.DetectLabelsByBytes(byt)
}
// DetectLabelsFromURL gets label info from URL.
func (svc *Rekognition) DetectLabelsFromURL(url string) (*LabelResponse, error) {
byt, err := svc.getImageFromURL(url)
if err != nil {
return nil, err
}
return svc.DetectLabelsByBytes(byt)
}
// DetectLabelsByBytes gets label info from image of bytes.
func (svc *Rekognition) DetectLabelsByBytes(byt []byte) (*LabelResponse, error) {
input := &SDK.DetectLabelsInput{
Image: &SDK.Image{
Bytes: byt,
},
}
return svc.detectLabels(input)
}
// DetectLabelsByS3Object gets label info from image of S3 object.
func (svc *Rekognition) DetectLabelsByS3Object(bucket, name string, version ...string) (*LabelResponse, error) {
input := &SDK.DetectLabelsInput{
Image: &SDK.Image{
S3Object: createS3Object(bucket, name, version...),
},
}
return svc.detectLabels(input)
}
// detectLabels executes `DetectLabels` operation and gets face info.
func (svc *Rekognition) detectLabels(input *SDK.DetectLabelsInput) (*LabelResponse, error) {
op, err := svc.client.DetectLabels(input)
if err != nil {
svc.Errorf("error on `DetectLabels` operation; error=%s;", err.Error())
return nil, err
}
list := make([]*Label, len(op.Labels))
for i, l := range op.Labels {
list[i] = NewLabelFromAWSLabel(l)
}
result := &LabelResponse{
List: list,
}
if op.OrientationCorrection != nil {
result.Orientation = *op.OrientationCorrection
}
return result, nil
}
// DetectModerationLabelsFromLocalFile gets moderation info from local image file.
func (svc *Rekognition) DetectModerationLabelsFromLocalFile(filepath string) (*ModerationLabelResponse, error) {
byt, err := ioutil.ReadFile(filepath)
if err != nil {
return nil, err
}
return svc.DetectModerationLabelsByBytes(byt)
}
// DetectModerationLabelsFromURL gets moderation info from URL.
func (svc *Rekognition) DetectModerationLabelsFromURL(url string) (*ModerationLabelResponse, error) {
byt, err := svc.getImageFromURL(url)
if err != nil {
return nil, err
}
return svc.DetectModerationLabelsByBytes(byt)
}
// DetectModerationLabelsByBytes gets moderation info from image of bytes.
func (svc *Rekognition) DetectModerationLabelsByBytes(byt []byte) (*ModerationLabelResponse, error) {
input := &SDK.DetectModerationLabelsInput{
Image: &SDK.Image{
Bytes: byt,
},
}
return svc.detectModerationLabels(input)
}
// DetectModerationLabelsByS3Object gets moderation info from image of S3 object.
func (svc *Rekognition) DetectModerationLabelsByS3Object(bucket, name string, version ...string) (*ModerationLabelResponse, error) {
input := &SDK.DetectModerationLabelsInput{
Image: &SDK.Image{
S3Object: createS3Object(bucket, name, version...),
},
}
return svc.detectModerationLabels(input)
}
// detectModerationLabels executes `DetectModerationLabels` operation and gets face info.
func (svc *Rekognition) detectModerationLabels(input *SDK.DetectModerationLabelsInput) (*ModerationLabelResponse, error) {
op, err := svc.client.DetectModerationLabels(input)
if err != nil {
svc.Errorf("error on `DetectModerationLabels` operation; error=%s;", err.Error())
return nil, err
}
list := make([]*ModerationLabel, len(op.ModerationLabels))
for i, l := range op.ModerationLabels {
list[i] = NewModerationLabelFromAWSModerationLabel(l)
}
return &ModerationLabelResponse{
List: list,
}, nil
}
// RecognizeCelebritiesFromLocalFile gets celebrities info from local image file.
func (svc *Rekognition) RecognizeCelebritiesFromLocalFile(filepath string) (*CelebrityResponse, error) {
byt, err := ioutil.ReadFile(filepath)
if err != nil {
return nil, err
}
return svc.RecognizeCelebritiesByBytes(byt)
}
// RecognizeCelebritiesFromURL gets celebrities info from URL.
func (svc *Rekognition) RecognizeCelebritiesFromURL(url string) (*CelebrityResponse, error) {
byt, err := svc.getImageFromURL(url)
if err != nil {
return nil, err
}
return svc.RecognizeCelebritiesByBytes(byt)
}
// RecognizeCelebritiesByBytes gets celebrities info from image of bytes.
func (svc *Rekognition) RecognizeCelebritiesByBytes(byt []byte) (*CelebrityResponse, error) {
input := &SDK.RecognizeCelebritiesInput{
Image: &SDK.Image{
Bytes: byt,
},
}
return svc.recognizeCelebrities(input)
}
// RecognizeCelebritiesByS3Object gets celebrities info from image of S3 object.
func (svc *Rekognition) RecognizeCelebritiesByS3Object(bucket, name string, version ...string) (*CelebrityResponse, error) {
input := &SDK.RecognizeCelebritiesInput{
Image: &SDK.Image{
S3Object: createS3Object(bucket, name, version...),
},
}
return svc.recognizeCelebrities(input)
}
// recognizeCelebrities executes `RecognizeCelebrities` operation and gets celebrities info.
func (svc *Rekognition) recognizeCelebrities(input *SDK.RecognizeCelebritiesInput) (*CelebrityResponse, error) {
op, err := svc.client.RecognizeCelebrities(input)
if err != nil {
svc.Errorf("error on `RecognizeCelebrities` operation; error=%s;", err.Error())
return &CelebrityResponse{}, err
}
list := make([]*Celebrity, len(op.CelebrityFaces))
for i, c := range op.CelebrityFaces {
var urls []string
if len(c.Urls) != 0 {
urls := make([]string, len(c.Urls))
for j, url := range c.Urls {
urls[j] = *url
}
}
list[i] = &Celebrity{
Name: *c.Name,
MatchConfidence: *c.MatchConfidence,
Urls: urls,
}
}
return &CelebrityResponse{
List: list,
}, nil
}
// GetCelebrityInfoByID gets celebrities info by id.
func (svc *Rekognition) GetCelebrityInfoByID(id string) (*CelebrityInfoResponse, error) {
return svc.getCelebrityInfo(&SDK.GetCelebrityInfoInput{
Id: pointers.String(id),
})
}
// getCelebrityInfo executes `GetCelebrityInfo` operation and gets celebrities info.
func (svc *Rekognition) getCelebrityInfo(input *SDK.GetCelebrityInfoInput) (*CelebrityInfoResponse, error) {
op, err := svc.client.GetCelebrityInfo(input)
if err != nil {
svc.Errorf("error on `GetCelebrityInfo` operation; error=%s;", err.Error())
return &CelebrityInfoResponse{}, err
}
return NewCelebrityInfoResponseFromAWSOutput(op), nil
}
// CompareFacesFromLocalFile compares faces from local image files.
func (svc *Rekognition) CompareFacesFromLocalFile(source, target string) (*CompareFaceResponse, error) {
sourceByte, err := ioutil.ReadFile(source)
if err != nil {
return nil, err
}
targetByte, err := ioutil.ReadFile(target)
if err != nil {
return nil, err
}
return svc.CompareFacesByBytes(sourceByte, targetByte)
}
// CompareFacesFromURL compares faces from URL.
func (svc *Rekognition) CompareFacesFromURL(source, target string) (*CompareFaceResponse, error) {
sourceByte, err := svc.getImageFromURL(source)
if err != nil {
return nil, err
}
targetByte, err := svc.getImageFromURL(target)
if err != nil {
return nil, err
}
return svc.CompareFacesByBytes(sourceByte, targetByte)
}
// CompareFacesByBytes compares faces from image of bytes.
func (svc *Rekognition) CompareFacesByBytes(source, target []byte) (*CompareFaceResponse, error) {
sourceImage := &SDK.Image{
Bytes: source,
}
targetImage := &SDK.Image{
Bytes: target,
}
input := &SDK.CompareFacesInput{
SourceImage: sourceImage,
TargetImage: targetImage,
}
return svc.compareFaces(input)
}
// CompareFaces executes `CompareFaces` operation and gets celebrities info.
func (svc *Rekognition) CompareFaces(input *SDK.CompareFacesInput) (*CompareFaceResponse, error) {
return svc.compareFaces(input)
}
// compareFaces executes `CompareFaces` operation and gets celebrities info.
func (svc *Rekognition) compareFaces(input *SDK.CompareFacesInput) (*CompareFaceResponse, error) {
op, err := svc.client.CompareFaces(input)
if err != nil {
svc.Errorf("error on `CompareFaces` operation; error=%s;", err.Error())
return &CompareFaceResponse{}, err
}
return NewCompareFaceResponseFromAWSOutput(op), nil
}
// getImageFromURL gets image data from url.
func (svc *Rekognition) getImageFromURL(url string) ([]byte, error) {
cli := svc.httpClient
if cli == nil {
err := errors.New("error on `getImageFromURL`; error=`svc.httpClient is nil`;")
svc.Errorf(err.Error())
return nil, err
}
resp, err := cli.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
buf := new(bytes.Buffer)
_, err = io.Copy(buf, resp.Body)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// createS3Object creates *SDK.S3Object from bucket and name.
func createS3Object(bucket, name string, version ...string) *SDK.S3Object {
s3object := &SDK.S3Object{
Bucket: pointers.String(bucket),
Name: pointers.String(name),
}
if len(version) == 1 {
s3object.Version = pointers.String(version[0])
}
return s3object
}