sed recognition response to DynamoDB table using Lambda-python

╄→гoц情女王★ 提交于 2019-12-11 10:02:30

问题


I am using Lambda to detect faces and would like to send the response to a Dynamotable. This is the code I am using:

rekognition = boto3.client('rekognition', region_name='us-east-1')
dynamodb = boto3.client('dynamodb', region_name='us-east-1')



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


def detect_faces(bucket, key):
    response = rekognition.detect_faces(Image={"S3Object": {"Bucket": bucket, 
    "Name": key}}, Attributes=['ALL'])
    TableName = 'table_test'
    for face in response['FaceDetails']:
        table_response = dynamodb.put_item(TableName=TableName, Item='{0} - {1}%')


     return response

My problem is in this line:

 for face in response['FaceDetails']:
        table_response = dynamodb.put_item(TableName=TableName, Item= {'key:{'S':'value'}, {'S':'Value')

I am able to see the result in the console. I don't want to add specific item(s) to the table- I need the whole response to be transferred to the table.

Do do this: 1. What to add as a key and partition key in the table? 2. How to transfer the whole response to the table

i have been stuck in this for three days now and can't figure out any result. Please help!

           ******************* EDIT *******************

I tried this code:

rekognition = boto3.client('rekognition', region_name='us-east-1')




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


 def detect_faces(bucket, key):
     response = rekognition.detect_faces(Image={"S3Object": {"Bucket": bucket, 
     "Name": key}}, Attributes=['ALL'])
     TableName = 'table_test'
     for face in response['FaceDetails']:
         face_id = str(uuid.uuid4())
         Age = face["AgeRange"]
         Gender = face["Gender"]
         print('Generating new DynamoDB record, with ID: ' + face_id)
         print('Input Age: ' + Age)
         print('Input Gender: ' + Gender)
         dynamodb = boto3.resource('dynamodb')
         table = dynamodb.Table(os.environ['test_table'])
         table.put_item(
         Item={
            'id' : face_id,
            'Age' : Age,
            'Gender' : Gender
         }
     )


     return response

It gave me two of errors:

1. Error processing object xxx.jpg
2. cannot concatenate 'str' and 'dict' objects

Can you pleaaaaase help!


回答1:


When you create a Table in DynamoDB, you must specify, at least, a Partition Key. Go to your DynamoDB table and grab your partition key. Once you have it, you can create a new object that contains this partition key with some value on it and the object you want to pass itself. The partition key is always a MUST upon creating a new Item in a DynamoDB table.

Your JSON object should look like this:

{
 "myPartitionKey": "myValue",
 "attr1": "val1",
 "attr2:" "val2"
}

EDIT: After the OP updated his question, here's some new information:

For problem 1)

Are you sure the image you are trying to process is a valid one? If it is a corrupted file Rekognition will fail and throw that error.

For problem 2)

You cannot concatenate a String with a Dictionary in Python. Your Age and Gender variables are dictionaries, not Strings. So you need to access an inner attribute within these dictionaries. They have a 'Value' attribute. I am not a Python developer, but you need to access the Value attribute inside your Gender object. The Age object, however, has 'Low' and 'High' as attributes.

You can see the complete list of attributes in the docs

Hope this helps!



来源:https://stackoverflow.com/questions/54824911/sed-recognition-response-to-dynamodb-table-using-lambda-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!