【问题标题】:Multiprocessing multiple images using Rekognition in Python在 Python 中使用 Rekognition 对多个图像进行多处理
【发布时间】:2018-06-13 21:40:23
【问题描述】:

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

由于我已经限制使用 boto3 会话,如果可能,请提供代码 sn-p。

【问题讨论】:

  • 你有开始的代码吗?我们在这里帮助寻找解决方案,而不是为您编写 ii。

标签: python amazon-web-services boto3 python-multiprocessing amazon-rekognition


【解决方案1】:

您可以做的最好的事情是,与其在本地机器上运行代码,不如在云中将其作为函数运行。使用 AWS Lambda,您可以轻松做到这一点。只需将 s3 对象上传作为触发器添加到您的 lambda 中,每当任何图像上传到您的 s3 存储桶时,它都会触发您的 lambda 函数,它会 detect_labels 然后您可以按照自己的方式使用这些标签想要,您甚至可以将它们存储到 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

【讨论】:

    猜你喜欢
    • 2020-07-05
    • 1970-01-01
    • 2016-10-21
    • 1970-01-01
    • 1970-01-01
    • 2013-10-08
    • 2018-07-07
    • 2021-10-17
    • 1970-01-01
    相关资源
    最近更新 更多