0

我正在尝试在 Python 中使用 AWS Rekognition 检测多个图像的标签。此过程需要大约 3 秒才能标记图像。有什么方法可以并行标记这些图像?

由于我已限制使用 boto3 会话,请尽可能提供代码片段。

4

1 回答 1

0

您可以做的最好的事情是,与其在本地机器上运行代码,不如在云中将其作为函数运行。使用 AWS Lambda,您可以轻松做到这一点。只需将 s3 对象上传作为触发器添加到您的 lambda ,无论何时将任何图像上传到您的 s3 存储桶,它都会触发您的 lambda 函数并且它会检测标签,然后您可以按照您想要的方式使用这些标签,您甚至可以存储它们到 dynamodb 表以供以后参考并从该表中获取。

最好的事情是,如果您同时上传多个图像,那么每个图像将被并行执行,因为 lambda 具有高度可扩展性,并且您可以同时获得所有结果。

相同的示例代码:

from __future__ import print_function

import boto3
from decimal import Decimal
import json
import urllib

print('Loading function')

rekognition = boto3.client('rekognition')


# --------------- Helper Functions to call Rekognition APIs ------------------





def detect_labels(bucket, key):
    response = rekognition.detect_labels(Image={"S3Object": {"Bucket": bucket, "Name": key}})

    # Sample code to write response to DynamoDB table 'MyTable' with 'PK' as Primary Key.
    # Note: role used for executing this Lambda function should have write access to the table.
    #table = boto3.resource('dynamodb').Table('MyTable')
    #labels = [{'Confidence': Decimal(str(label_prediction['Confidence'])), 'Name': label_prediction['Name']} for label_prediction in response['Labels']]
    #table.put_item(Item={'PK': key, 'Labels': labels})
    return response



# --------------- Main handler ------------------


def lambda_handler(event, context):

    # Get the object from the event
    bucket = event['Records'][0]['s3']['bucket']['name']
    key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key'].encode('utf8'))
    try:
        #Calls rekognition DetectLabels API to detect labels in S3 object
        response = detect_labels(bucket, key)
        print(response)

        return response
    except Exception as e:
        print(e)
        print("Error processing object {} from bucket {}. ".format(key, bucket) +
              "Make sure your object and bucket exist and your bucket is in the same region as this function.")
        raise e
于 2018-06-13T13:40:23.547 回答