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

问题描述 投票:0回答:2

我一直在尝试使用 AWSRekognition SDK 来比较人脸。但是,Amazon 没有关于如何将他们的 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 并构建请求。

谢谢!

ios amazon-web-services swift3 amazon-rekognition
2个回答
5
投票
  1. 实例化Rekognition Client,这里我使用默认配置的客户端。

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

1
投票

斯威夫特 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)
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.