Last active
September 2, 2018 10:47
-
-
Save owulveryck/33753125afa6284cd5dbbb1bd4d1eb54 to your computer and use it in GitHub Desktop.
Rekognition test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"github.com/aws/aws-sdk-go/aws" | |
"github.com/aws/aws-sdk-go/aws/session" | |
"github.com/aws/aws-sdk-go/service/rekognition" | |
"github.com/blackjack/webcam" | |
"os" | |
) | |
type frameSizes []webcam.FrameSize | |
var ( | |
sess *session.Session | |
svc *rekognition.Rekognition | |
) | |
func init() { | |
var err error | |
sess, err = session.NewSession(&aws.Config{Region: aws.String("us-east-1")}) | |
if err != nil { | |
fmt.Println("failed to create session,", err) | |
return | |
} | |
svc = rekognition.New(sess) | |
} | |
func main() { | |
cam, err := webcam.Open("/dev/video0") | |
if err != nil { | |
panic(err.Error()) | |
} | |
defer cam.Close() | |
formatDesc := cam.GetSupportedFormats() | |
var formats []webcam.PixelFormat | |
for f := range formatDesc { | |
formats = append(formats, f) | |
} | |
format := formats[1] | |
frames := frameSizes(cam.GetSupportedFrameSizes(format)) | |
size := frames[2] | |
f, w, h, err := cam.SetImageFormat(format, uint32(size.MaxWidth), uint32(size.MaxHeight)) | |
if err != nil { | |
panic(err.Error()) | |
} else { | |
fmt.Fprintf(os.Stderr, "Resulting image format: %s (%dx%d)\n", formatDesc[f], w, h) | |
} | |
err = cam.StartStreaming() | |
if err != nil { | |
panic(err.Error()) | |
} | |
fmt.Println("Press enter to process") | |
for { | |
timeout := uint32(5) //5 seconds | |
err = cam.WaitForFrame(timeout) | |
switch err.(type) { | |
case nil: | |
case *webcam.Timeout: | |
fmt.Fprint(os.Stderr, err.Error()) | |
continue | |
default: | |
panic(err.Error()) | |
} | |
bufio.NewReader(os.Stdin).ReadBytes('\n') | |
frame, err := cam.ReadFrame() | |
if len(frame) != 0 { | |
params := &rekognition.DetectFacesInput{ | |
Image: &rekognition.Image{ // Required | |
Bytes: frame, | |
}, | |
Attributes: []*string{ | |
aws.String("ALL"), // Required | |
}, | |
} | |
resp, err := svc.DetectFaces(params) | |
if err != nil { | |
// Print the error, cast err to awserr.Error to get the Code and | |
// Message from an error. | |
fmt.Println(err.Error()) | |
return | |
} | |
// Pretty-print the response data. | |
for i, fd := range resp.FaceDetails { | |
fmt.Printf("The person %v is ", i) | |
for _, e := range fd.Emotions { | |
fmt.Printf("%v, ", *e.Type) | |
} | |
fmt.Printf("\n") | |
} | |
} else if err != nil { | |
panic(err.Error()) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment