简体   繁体   English

如何在Swift 3中使用AWS Rekognition检测图像标签和面部

[英]How to use AWS Rekognition to detect Image Labels and Faces in Swift 3

So I've been trying to use the AWSRekognition SDK in order to detect faces and labels in images. 因此,我一直在尝试使用AWSRekognition SDK来检测图像中的面部和标签。 However, Amazon has no Documentation on how to integrate their SDK with iOS. 但是,Amazon没有有关如何将其SDK与iOS集成的文档。 They have links that show how to work with Rekognition (Developer Guide) with examples only in Java and very limited. 它们的链接显示了如何使用Rekognition(开发人员指南),并且仅以Java为例提供了示例,而且非常有限。

Amazon Rekognition Developer Guide Amazon Rekognition开发人员指南

If you click on their "iOS Documentation", it takes you to the general iOS documentation page, with no signs of Rekognition in any section. 如果单击他们的“ iOS文档”,它将带您到常规的iOS文档页面,任何部分都没有识别的迹象。

AWS iOS Developer Guide AWS iOS开发人员指南

I wanted to know if anyone knows how to integrate AWS Rekognition in Swift 3 . 我想知道是否有人知道如何在Swift 3中集成AWS Rekognition。 How to Initialize it and make a request with an image, receiving a response with the labels. 如何初始化它并使用图像发出请求,并接收带有标签的响应。

I already downloaded the AWSRekognition.framework and the AWSCore.framework and added them into my project. 我已经下载了AWSRekognition.frameworkAWSCore.framework并将它们添加到我的项目中。 Also I have imported both of them in my AppDelegate.swift and initialized my AWS Credentials. 另外,我已经将它们都导入了AppDelegate.swift并初始化了我的AWS凭证。

let credentialsProvider = AWSCognitoCredentialsProvider(
        regionType: AWSRegionType.usEast1,
        identityPoolId: "us-east-1_myPoolID")
let configuration = AWSServiceConfiguration(
        region: AWSRegionType.usEast1,
        credentialsProvider: credentialsProvider)
AWSServiceManager.default().defaultServiceConfiguration = configuration

Also I've tried to initialize Rekognition and build a Request: 我也尝试初始化Rekognition并建立一个Request:

do {

    let rekognitionClient:AWSRekognition = AWSRekognition(forKey: "Maybe a Key from AWS?")

    let request: AWSRekognitionDetectLabelsRequest = try AWSRekognitionDetectLabelsRequest(dictionary: ["image": UIImage(named:"TestImage")!, "maxLabels":3, "minConfidence":90], error: (print("error")))
    rekognitionClient.detectLabels(request) { (response:AWSRekognitionDetectLabelsResponse?, error:Error?) in
        if error == nil {
            print(response!)
        }
    }

} catch {
    print("Error")
}

Thanks a lot! 非常感谢!

The documentation on the web for the Rekognition iOS SDK is lacking but the comments in the SDK code were pretty helpful for me. Web上缺少Rekognition iOS SDK的文档,但是SDK代码中的注释对我很有帮助。 If you hold Cmd while clicking on a keyword in Xcode you should be able to find all the info you need in the comments. 如果您在单击Xco​​de中的关键字时按住Cmd,则应该能够在注释中找到所需的所有信息。

From this you can see that the Key is referring to a previously registered client which you can do with registerRekognitionWithConfiguration , but you can skip all that by using the default as Karthik mentioned: 从中可以看到Key指向以前注册的客户端,可以使用registerRekognitionWithConfiguration ,但是可以通过使用Karthik提到的默认设置来跳过所有操作:

let rekognitionClient = AWSRekognition.defaultRekognition()

I have been working with face detection so I haven't used AWSRekognitionDetectLabelsRequest in my own code, but I think where you might be going wrong is that the image property of AWSRekognitionDetectLabelsRequest should be an AWSRekognitionImage and not a UIImage like you are passing in. You can call UIImageJPEGRepresentation to get the raw bytes from a UIImage. 我一直在进行人脸检测,因此我没有在自己的代码中使用AWSRekognitionDetectLabelsRequest ,但我认为您可能会出错的地方是AWSRekognitionDetectLabelsRequestimage属性应该是AWSRekognitionImage而不是像传入的UIImage 。可以调用UIImageJPEGRepresentation来从UIImage获取原始字节。

let sourceImage = UIImage(named: "TestImage")

let image = AWSRekognitionImage()
image!.bytes = UIImageJPEGRepresentation(sourceImage!, 0.7)

guard let request = AWSRekognitionDetectLabelsRequest() else {
    puts("Unable to initialize AWSRekognitionDetectLabelsRequest.")
    return
}

request.image = image
request.maxLabels = 3
request.minConfidence = 90

It should also be a lot easier to debug if you set the request properties individually like this too. 如果您也这样单独设置请求属性,调试起来也应该容易得多。

let credentialsProvider = AWSCognitoCredentialsProvider(regionType:.EUWest1,
                                                            identityPoolId:"please enter pool id")
    let configuration = AWSServiceConfiguration(region:.EUWest1, credentialsProvider:credentialsProvider)
    AWSServiceManager.default().defaultServiceConfiguration = configuration
    let rekognitionClient = AWSRekognition.default()
    let image = AWSRekognitionImage()
    image!.bytes = UIImageJPEGRepresentation(sourceImages, 0.7)
    guard let request = AWSRekognitionDetectLabelsRequest()
        else {
            puts("Unable to initialize AWSRekognitionDetectLabelsRequest.")
            return
    }
    request.image = image
    request.maxLabels = 5
    request.minConfidence = 90

    rekognitionClient.detectLabels(request) { (response:AWSRekognitionDetectLabelsResponse?, error:Error?) in
        if error == nil {
            print("response ",response)



        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM