How to use AWS Rekognition to Compare Face in Swift 3

不问归期 提交于 2020-02-15 10:08:08

问题


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!


回答1:


  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")


来源:https://stackoverflow.com/questions/46483447/how-to-use-aws-rekognition-to-compare-face-in-swift-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!