使用Lambda-python对dynamoDB表进行sed识别响应 [英] sed recognition response to DynamoDB table using Lambda-python
问题描述
我正在使用Lambda来检测人脸,并希望将响应发送给Dynamotable. 这是我正在使用的代码:
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
我的问题在这一行:
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.
执行此操作: 1.在表中添加什么作为键和分区键? 2.如何将整个响应转移到表
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 *******************
我尝试了以下代码:
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表中创建新项时,分区键始终是必须的.
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.
您的JSON对象应如下所示:
Your JSON object should look like this:
{
"myPartitionKey": "myValue",
"attr1": "val1",
"attr2:" "val2"
}
编辑 :OP更新了他的问题之后,以下是一些新信息:
EDIT: After the OP updated his question, here's some new information:
对于问题1)
您确定要处理的图像是否有效?如果该文件已损坏,则Rekognition将失败并抛出该错误.
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.
对于问题2)
您无法在Python中将字符串与字典连接在一起.您的年龄和性别变量是字典,而不是字符串.因此,您需要在这些词典中访问内部属性.它们具有值"属性.我不是Python开发人员,但是您需要访问Gender对象内的Value属性.但是,年龄对象具有低"和高"作为属性.
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.
您可以在文档
希望这会有所帮助!
这篇关于使用Lambda-python对dynamoDB表进行sed识别响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!