This Go program demonstrates the use of AWS Rekognition to detect and compare faces in images. It utilizes the AWS SDK for Go v2 to interact with AWS Rekognition, a powerful service that enables image and video analysis. The script loads two images from the filesystem, checks each for faces, and then compares these faces to determine a similarity score.
- Face Detection: Validates that each input image contains a face.
- Face Comparison: Compares two faces to determine how similar they are.
- Go 1.x or higher
- AWS account and AWS CLI configured
- AWS IAM permissions sufficient to access AWS Rekognition
-
Install Go (if not already installed):
https://golang.org/doc/install
-
Clone the repository (or just copy the script):
git clone https://yourrepository.git cd your-repository-folder
-
Set up AWS CLI and configure your AWS credentials (if not already done):
aws configure
This will require your AWS Access Key ID, Secret Access Key, and default region.
-
Install required Go packages:
go get github.com/aws/aws-sdk-go-v2/aws go get github.com/aws/aws-sdk-go-v2/config go get github.com/aws/aws-sdk-go-v2/service/rekognition
-
Prepare your images: Place the images you want to compare in the same directory as the script, or modify the
loadImage
function to point to their paths. -
Run the script:
go run main.go
The program will load the images, check each for faces, and compare the faces if detected. The output will indicate whether faces were detected and, if so, the similarity percentage between the faces.
loadImage(filename string) []byte
: Reads an image file and converts it into a byte slice for processing.detectFace(ctx context.Context, image []byte, client *rekognition.Client) error
: Detects faces in the provided image using AWS Rekognition.compareFaces(ctx context.Context, sourceImage []byte, targetImage []byte, client *rekognition.Client) (float32, error)
: Compares two faces and returns the similarity score.
Errors are handled using a custom function ifErrorPanic(err error)
which will stop the program and display an error message if an operation fails. This approach is straightforward and suitable for demonstration purposes but may be adapted for more nuanced error handling in a production environment.