菜鸟教程小白 发表于 2022-12-12 12:41:42

ios - 如何使用 AWS Rekognition 在 Swift 3 中检测图像标签和人脸


                                            <p><p>所以我一直在尝试使用 AWSRekognition SDK 来检测图像中的人脸和标签。但是,Amazon 没有关于如何将他们的 SDK 与 iOS 集成的文档。他们提供的链接显示了如何使用 Rekognition(开发人员指南),其中的示例仅使用 Java,而且非常有限。</p>

<p> <a href="http://docs.aws.amazon.com/rekognition/latest/dg/what-is.html" rel="noreferrer noopener nofollow">Amazon Rekognition Developer Guide</a> </p>

<p>如果您单击他们的“iOS 文档”,它会将您带到一般的 iOS 文档页面,任何部分都没有 Rekognition 的迹象。 </p>

<p> <a href="http://docs.aws.amazon.com/mobile/sdkforios/developerguide/" rel="noreferrer noopener nofollow">AWS iOS Developer Guide</a> </p>

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

<p>我已经下载了 <code>AWSRekognition.framework</code> 和 <code>AWSCore.framework</code> 并将它们添加到我的项目中。此外,我已将它们都导入到我的 <code>AppDelegate.swift</code> 中并初始化了我的 AWS 凭证。</p>

<pre><code>let credentialsProvider = AWSCognitoCredentialsProvider(
      regionType: AWSRegionType.usEast1,
      identityPoolId: &#34;us-east-1_myPoolID&#34;)
let configuration = AWSServiceConfiguration(
      region: AWSRegionType.usEast1,
      credentialsProvider: credentialsProvider)
AWSServiceManager.default().defaultServiceConfiguration = configuration
</code></pre>

<p>我还尝试初始化 Rekognition 并构建请求:</p>

<pre><code>do {

    let rekognitionClient:AWSRekognition = AWSRekognition(forKey: &#34;Maybe a Key from AWS?&#34;)

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

} catch {
    print(&#34;Error&#34;)
}
</code></pre>

<p>非常感谢!</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>Web 上缺少 Rekognition iOS SDK 的文档,但 SDK 代码中的注释对我很有帮助。如果在 Xcode 中单击关键字时按住 <kbd>Cmd</kbd>,您应该能够在评论中找到所需的所有信息。</p>

<p>从这里您可以看到 key 指的是以前注册的客户端,您可以使用 <code>registerRekognitionWithConfiguration</code> 来完成,但是您可以使用 Karthik 提到的默认值来跳过所有这些:</p>

<pre><code>let rekognitionClient = AWSRekognition.defaultRekognition()
</code></pre>

<p>我一直在处理面部检测,所以我没有在我自己的代码中使用 <code>AWSRekognitionDetectLabelsRequest</code>,但我认为您可能出错的地方是 <code>image</code> 属性<code>AWSRekognitionDetectLabelsRequest</code> 应该是 <code>AWSRekognitionImage</code> 而不是像您传入的 <code>UIImage</code>。您可以调用 <code>UIImageJPEGRepresentation</code> 来获取来自 UIImage 的原始字节。</p>

<pre><code>let sourceImage = UIImage(named: &#34;TestImage&#34;)

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

guard let request = AWSRekognitionDetectLabelsRequest() else {
    puts(&#34;Unable to initialize AWSRekognitionDetectLabelsRequest.&#34;)
    return
}

request.image = image
request.maxLabels = 3
request.minConfidence = 90
</code></pre>

<p>如果您也像这样单独设置请求属性,调试起来也会容易得多。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何使用 AWS Rekognition 在 Swift 3 中检测图像标签和人脸,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/41348880/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/41348880/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何使用 AWS Rekognition 在 Swift 3 中检测图像标签和人脸