![](/img/trans.png)
[英]Using python for put_item in dynamodb table with Lambda function
[英]sed recognition response to DynamoDB table using Lambda-python
我正在使用Lambda來檢測人臉,並希望將響應發送給Dynamotable。 這是我正在使用的代碼:
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
我的問題在這一行:
for face in response['FaceDetails']:
table_response = dynamodb.put_item(TableName=TableName, Item= {'key:{'S':'value'}, {'S':'Value')
我可以在控制台中看到結果。 我不想將特定項目添加到表格中-我需要將整個響應轉移到表格中。
請執行以下操作:1.在表中添加什么作為鍵和分區鍵? 2.如何將整個響應轉移到表格
我已經被困在這里三天了,無法找到任何結果。 請幫忙!
******************* EDIT *******************
我嘗試了這段代碼:
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
它給了我兩個錯誤:
1. Error processing object xxx.jpg
2. cannot concatenate 'str' and 'dict' objects
您能幫忙嗎?
在DynamoDB中創建表時,必須至少指定一個分區鍵。 轉到您的DynamoDB表並獲取您的分區鍵。 擁有它之后,您可以創建一個新對象,該對象包含此分區鍵,上面帶有一些值以及您要傳遞的對象。 在DynamoDB表中創建新Item時,分區鍵始終是MUST。
您的JSON對象應如下所示:
{
"myPartitionKey": "myValue",
"attr1": "val1",
"attr2:" "val2"
}
編輯 :OP更新了他的問題后,這里有一些新信息:
對於問題1)
您確定要處理的圖像是否有效? 如果文件損壞,則Rekognition將失敗並拋出該錯誤。
對於問題2)
您無法在Python中將字符串與字典連接在一起。 您的年齡和性別變量是字典,而不是字符串。 因此,您需要在這些詞典中訪問內部屬性。 它們具有“值”屬性。 我不是Python開發人員,但是您需要訪問Gender對象內的Value屬性。 但是,年齡對象具有“低”和“高”作為屬性。
您可以在文檔中查看屬性的完整列表
希望這可以幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.