2

I've been trying to use the AWSRekognition SDK in order to compare face. However, Amazon has no Documentation on how to integrate their SDK with iOS. They have links that show how to work with Recognition (Developer Guide) with examples only in Java and very limited.

I wanted to know if anyone knows how to integrate AWS Rekognition in Swift 3. How to Initialize it and make a request with an image, receiving a response with the labels.

I have AWS Signatures AccessKey, SecretKey, AWS Region, Service Name. also Body

{
  "SourceImage": {
    "S3Object": {
      "Bucket": "bucketName",
      "Name": "ios/sample.jpg"
    }
  },
  "TargetImage": {
    "S3Object": {
      "Bucket": "buketName",
      "Name": "ios/target.JPG"
    }
  }
}

how can I initialize Rekognition and build a Request.

Thanks you!

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Pranavan SP
  • 1,785
  • 1
  • 17
  • 33

2 Answers2

5
  1. Instantiate the Rekognition Client, Here I'm using the client with the default configuration.

    let rekognitionClient:AWSRekognition = AWSRekognition.default()
    

Otherwise, you can use the credentials as follows:

    let credentialsProvider = AWSCognitoCredentialsProvider(
        regionType: AWSRegionType.usEast2,
        identityPoolId: "us-east-2_myPoolID")

    let configuration = AWSServiceConfiguration(
        region: AWSRegionType.usEast2,
        credentialsProvider: credentialsProvider)

    AWSServiceManager.default().defaultServiceConfiguration = configuration
    let rekognitionClient:AWSRekognition = AWSRekognition.default()
  1. Now construct the request and set the image in it.

    let image = UIImage(named: "MyImage")
    let request = AWSRekognitionDetectLabelsRequest() 
    request.image = image
    request.maxLabels = <num_labels_needed>
    request.minConfidence = <confidence_interval_needed>
    
  2. Now to compare faces, read about the CompareFacesRequest: https://github.com/aws/aws-sdk-ios/blob/master/AWSRekognition/AWSRekognitionService.m#L288

There is a sample test in the SDK that compares two faces in ObjC but you can translate that in Swift:

https://github.com/aws/aws-sdk-ios/blob/master/AWSRekognitionUnitTests/AWSGeneralRekognitionTests.m#L60

    let key = "testCompareFaces"
    let configuration = AWSServiceConfiguration(region: AWSRegionUSEast2, credentialsProvider: nil)
    AWSRekognition.register(with: configuration, forKey: key)
    AWSRekognition(for: key).compareFaces(AWSRekognitionCompareFacesRequest()).continue(withBlock: {(_ task: AWSTask) -> Any in
        print("completed")
Karthikeyan
  • 1,411
  • 1
  • 13
  • 13
  • Thank you, Karthikeyan! but it's AWSRekognitionDetectLabelsRequest. somehow it's helpful. I have created myself to compare face. but How to change let image = UIImage(named: "MyImage") into bucket. My both images are from local. i want to setup one target image as the bucket image – Pranavan SP Oct 03 '17 at 05:36
  • Do you want to refer to the image stored in the S3 bucket instead of a local image? Have you tried downloading the image and referring to it using the S3 TransferUtility in AWS SDK for iOS? – Karthikeyan Oct 03 '17 at 18:51
  • Thank you Karthikeyan for your time. Yes. i didn't try that one but now I can able to use bucket image using "AWSRekongnitionS3Object", "AWSRekonitionImage" class. Found on AWSRekognitionModel.m file. Thank you! Please follow me on twitter @ImPrana. if you don't mind I will get in touch with you via social media. – Pranavan SP Oct 04 '17 at 05:22
  • @PranavanSp, If you are done with the implementation, please share the code it will be helpful – Arshad Shaik Mar 26 '19 at 04:04
  • I am using AWS for face match. I have one image download from server and another from local. How can I do that? I need to add both of images to S3 bucket and then use face match? Please explain me. I want to use face match in iOS 9.@PranavanSp – Dipang Feb 11 '20 at 08:39
1

Swift 5.0

let key = "testCompareFaces"
let credentialsProvider = AWSStaticCredentialsProvider(accessKey: "Add_access_key_id", secretKey:"Add_secret_access_key_id")
let configuration = AWSServiceConfiguration(region:.USEast1, credentialsProvider:credentialsProvider)
AWSServiceManager.default().defaultServiceConfiguration = configuration
    
AWSRekognition.register(with: configuration!, forKey: key)
let rekognition = AWSRekognition(forKey: key)

guard let request = AWSRekognitionCompareFacesRequest() else {
    puts("Unable to initialize AWSRekognitionDetectLabelsRequest.")
    return
}
let sourceImage = AWSRekognitionImage()
sourceImage!.bytes = sourceImage.jpegData(compressionQuality: 0.4)// Specify your source image
request.sourceImage = sourceImage

let targetImage = AWSRekognitionImage()
targetImage!.bytes = targetImage.jpegData(compressionQuality: 0.4) // Specify your target image
request.targetImage = targetImage

rekognition.compareFaces(request) { (respone, error) in
    if error == nil {
        if let response = respone {
            if let first = response.faceMatches?.first {
                print(first)
            }
        }
    } else {
        print(error?.localizedDescription)
    }
}
Gurjinder Singh
  • 9,221
  • 1
  • 66
  • 58