2

我一直在尝试使用 AWSRekognition SDK 来比较面部。但是,亚马逊没有关于如何将其 SDK 与 iOS 集成的文档。他们的链接显示了如何使用 Recognition(开发人员指南)以及仅在 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 并构建请求。

谢谢!

4

2 回答 2

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

    let rekognitionClient:AWSRekognition = AWSRekognition.default()
    

否则,您可以按如下方式使用凭据:

    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

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")
于 2017-09-29T17:27:58.870 回答
0

斯威夫特 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)
    }
}
于 2020-09-04T13:03:15.497 回答