如何使用AWS Rekognition在Swift 3中比较Face

时间:2017-09-29 07:02:37

标签: ios amazon-web-services swift3 amazon-rekognition

我一直在尝试使用AWSRekognition SDK来比较面部。但是,亚马逊没有关于如何将SDK与iOS集成的文档。他们有链接,显示如何使用识别(Developer Guide),仅使用Java中的示例,并且非常有限。

我想知道是否有人知道如何在Swift 3中集成AWS Rekognition。如何初始化它并使用图像发出请求,接收带有标签的响应。

我有AWS签名AccessKey,SecretKey,AWS区域,服务名称。还有身体

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

如何初始化Rekognition并构建请求。

谢谢!

2 个答案:

答案 0 :(得分:4)

  1. 实例化Rekognition客户端,我在这里使用具有默认配置的客户端。

    let rekognitionClient:AWSRekognition = AWSRekognition.default()
    
  2. 否则,您可以使用凭据,如下所示:

        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. 现在构建请求并在其中设置图像。

      let image = UIImage(named: "MyImage")
      let request = AWSRekognitionDetectLabelsRequest() 
      request.image = image
      request.maxLabels = <num_labels_needed>
      request.minConfidence = <confidence_interval_needed>
      
    2. 现在比较一下面孔,了解CompareFacesRequest:https://github.com/aws/aws-sdk-ios/blob/master/AWSRekognition/AWSRekognitionService.m#L288

    3. SDK中有一个示例测试,用于比较ObjC中的两个面,但您可以在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")
      

答案 1 :(得分:0)

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