jcberquist / aws-cfml

Lucee/ColdFusion library for interacting with AWS API's
MIT License
72 stars 53 forks source link

Add support for CompareFaces resource in Rekognition service #38

Closed ivanubi closed 4 years ago

ivanubi commented 4 years ago

Feature

This pull request adds support for CompareFaces resource in AWS Rekognition service (reference: https://docs.aws.amazon.com/rekognition/latest/dg/API_CompareFaces.html). It adds the compareFaces method to the rekognition.cfc service.

New code

This is the new code added to services/rekognition.cfc:

/**
* Returns an array of face matches ordered by similarity score in descending order
* https://docs.aws.amazon.com/rekognition/latest/dg/API_CompareFaces.html
* @SourceImage a struct with a "Bytes" key containing Base64-encoded binary data or an "S3Object" struct containing a "Bucket", "Key", and optional "Version" - https://docs.aws.amazon.com/rekognition/latest/dg/API_Image.html
* @TargetImage a struct with a "Bytes" key containing Base64-encoded binary data or an "S3Object" struct containing a "Bucket", "Key", and optional "Version" - https://docs.aws.amazon.com/rekognition/latest/dg/API_Image.html
* @SimilarityThreshold a numeric minimum level of confidence that a match must meet to be included. Valid Range: Minimum value of 0. Maximum value of 100.
* @QualityFilter a string filter that specifies a quality bar for how much filtering is done to identify faces. Valid Values: NONE | AUTO | LOW | MEDIUM | HIGH
*/
public any function compareFaces(
    required struct SourceImage,
    required struct TargetImage,
    numeric SimilarityThreshold,
    string QualityFilter
) {
    var requestSettings = api.resolveRequestSettings( argumentCollection = arguments );
    var args = { 
        'SourceImage': arguments.SourceImage,
        'TargetImage': arguments.TargetImage
    };
    if ( !isNull( arguments.SimilarityThreshold ) ) args[ 'SimilarityThreshold' ] = arguments.SimilarityThreshold;
    if ( !isNull( arguments.QualityFilter ) ) args[ 'QualityFilter' ] = arguments.QualityFilter;

    return apiCall( requestSettings, 'CompareFaces', args);
}

Use example:

aws = new path.to.awscfml.aws(
    awsKey = 'YOUR_PUBLIC_KEY',
    awsSecretKey = 'YOUR_PRIVATE_KEY',
    defaultRegion = 'us-east-1'
);

sourceImageBinary = fileReadBinary(expandPath('/face_1.jpg'));
sourceImageBase64 = binaryEncode(sourceImageBinary, 'base64');

targetImageBinary = fileReadBinary(expandPath('/face_2.jpg'));
targetImageBase64 = binaryEncode(targetImageBinary, 'base64');

response = aws.rekognition.compareFaces(
    SourceImage = {'Bytes': sourceImageBase64},
    TargetImage = {'Bytes': targetImageBase64},
    SimilarityThreshold = 70
);
WriteDump(response.data);
jcberquist commented 4 years ago

Thanks!