forked from awsdocs/aws-doc-sdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect_faces.go
113 lines (88 loc) · 2.37 KB
/
detect_faces.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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package main
import (
"flag"
"fmt"
"path/filepath"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/rekognition"
)
var svc *rekognition.Rekognition
var bucket = flag.String("bucket", "", "The name of the bucket")
var photo = flag.String("photo", "", "The path to the photo file (JPEG, JPG, PNG)")
var checks = true
func init() {
//https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/sessions.html
flag.StringVar(bucket, "b", "", "The name of the bucket")
flag.StringVar(photo, "p", "", "The path to the photo file (JPEG, JPG, PNG)")
flag.Parse()
if *bucket == "" || *photo == "" {
checks = false
flag.PrintDefaults()
fmt.Println("You must supply a bucket name (-b BUCKET) and photo file (-p PHOTO)")
return
}
fileExtension := filepath.Ext(*photo)
validExtension := map[string]bool{
".png": true,
".jpg": true,
".jpeg": true,
}
if !validExtension[fileExtension] {
checks = false
fmt.Println("Rekognition only supports jpeg, jpg or png")
return
}
//Access keys are read from ~/.aws/credentials
sess, err := session.NewSession(&aws.Config{
Region: aws.String("ap-south-1"),
})
if err != nil {
checks = false
fmt.Println("Error while creating session,", err)
return
}
svc = rekognition.New(sess)
_ = svc
}
func main() {
if checks == false {
return
}
params := &rekognition.DetectFacesInput{
Image: &rekognition.Image{
S3Object: &rekognition.S3Object{
Bucket: bucket,
Name: photo,
},
},
Attributes: []*string{
aws.String("ALL"),
},
}
resp, err := svc.DetectFaces(params)
if err != nil {
fmt.Println(err.Error())
return
}
if len(resp.FaceDetails) == 0 {
fmt.Println("No faces detected in the image !")
return
}
for idx, fdetails := range resp.FaceDetails {
fmt.Printf("Person #%d : \n", idx+1)
fmt.Printf("Position : %v %v \n", *fdetails.BoundingBox.Left, *fdetails.BoundingBox.Top)
if fdetails.AgeRange != nil {
fmt.Printf("Age (Low) : %d \n", *fdetails.AgeRange.Low)
fmt.Printf("Age (High) : %d \n", *fdetails.AgeRange.High)
}
if fdetails.Emotions != nil {
fmt.Printf("Emotion : %v\n", *fdetails.Emotions[0].Type)
}
if fdetails.Gender != nil {
fmt.Printf("Gender : %v\n\n", *fdetails.Gender.Value)
}
}
}