我正在使用AWS Rekognition的PPE检测来通过图像识别人们的PPE。每次我在S3中上传一张或几张图片时,都会向SQS发送一条消息,并从那里触发我的lambda函数来捕获图像名称。
lambda函数调用AWS Rekognition PPE Detection API,从从SQS捕获的图像名称中扫描S3中的图像。
我收到了AWS Rekognition PPE的响应,现在我想将响应的一部分存储在DynamoDB中。以下是来自DynamoDB的响应示例:
{
"ProtectiveEquipmentModelVersion": "1.0",
"Persons": [
{
"BodyParts": [
{
"Name": "FACE",
"Confidence": 99.85384368896484,
"EquipmentDetections": [
{
"BoundingBox": {
"Width": 0.12469039857387543,
"Height": 0.20445917546749115,
"Left": 0.5978690981864929,
"Top": 0.18556605279445648
},
"Confidence": 95.17121887207031,
"Type": "FACE_COVER",
"CoversBodyPart": {
"Confidence": 98.84524536132812,
"Value": true
}
}
]
},
{
"Name": "LEFT_HAND",
"Confidence": 98.26607513427734,
"EquipmentDetections": [
{
"BoundingBox": {
"Width": 0.13546951115131378,
"Height": 0.18359044194221497,
"Left": 0.47036099433898926,
"Top": 0.5242195725440979
},
"Confidence": 77.47138214111328,
"Type": "HAND_COVER",
"CoversBodyPart": {
"Confidence": 97.84107208251953,
"Value": true
}
}
]
},
{
"Name": "HEAD",
"Confidence": 99.99432373046875,
"EquipmentDetections": []
}
],
"BoundingBox": {
"Width": 0.5233333110809326,
"Height": 0.9821428656578064,
"Left": 0.3733333349227905,
"Top": 0.01785714365541935
},
"Confidence": 99.49939727783203,
"Id": 0
},
{
"BodyParts": [
{
"Name": "LEFT_HAND",
"Confidence": 93.59660339355469,
"EquipmentDetections": []
}
],
"BoundingBox": {
"Width": 0.46666666865348816,
"Height": 0.9226190447807312,
"Left": 0.0033333334140479565,
"Top": 0.0535714291036129
},
"Confidence": 98.97230529785156,
"Id": 1
},
{
"BodyParts": [
{
"Name": "FACE",
"Confidence": 77.40711212158203,
"EquipmentDetections": []
},
{
"Name": "HEAD",
"Confidence": 97.54975891113281,
"EquipmentDetections": []
}
],
"BoundingBox": {
"Width": 0.08666666597127914,
"Height": 0.1726190447807312,
"Left": 0.5633333325386047,
"Top": 0.761904776096344
},
"Confidence": 94.70215606689453,
"Id": 2
}
],
"Summary": {
"PersonsWithRequiredEquipment": [],
"PersonsWithoutRequiredEquipment": [
0,
2
],
"PersonsIndeterminate": [
1
]
}
}从上面来看,检测到的设备是: FACE_COVER和HAND_COVER。每次我尝试保存每个图像的数据时,我只能看到FACE_COVER或HAND_COVER,但不能同时看到两者。
到目前为止,我的代码如下:
import json
import boto3
from decimal import Decimal
def lambda_handler(event, context):
s3_client = boto3.client('s3')
client = boto3.client('rekognition')
for msg in event["Records"]:
msg_payload = json.loads(msg["body"])
if "Records" in msg_payload:
bucket = msg_payload["Records"][0]["s3"]["bucket"]["name"]
image = msg_payload["Records"][0]["s3"]["object"]["key"].replace("+", " ")
response = client.detect_protective_equipment(Image={'S3Object':{'Bucket':bucket,'Name':image}},SummarizationAttributes={'MinConfidence':80, 'RequiredEquipmentTypes':['FACE_COVER', 'HEAD_COVER']})
for person in response["Persons"]:
bp = person["BodyParts"]
for ed in bp:
name = ed["Name"]
ppe = ed["EquipmentDetections"]
for type in ppe:
types = type["Type"]
confidence = str(type["Confidence"])
covers_body = type["CoversBodyPart"]["Value"]
data = {
"Details":
[
{
"Body Part": ed["Name"],
"Confidence": str(type["Confidence"]),
"Cover Type": type["Type"],
"Covers Body Part": type["CoversBodyPart"]["Value"]
},
],
"Image_Name": image
}
table = boto3.resource('dynamodb').Table("PPE_Detection")
table.put_item(Item={'Image_Name': image, 'Labels': data})下面是我希望将每个图像的数据存储在DynamoDB中的方式。
[
{
"Details": [
{
"Body Part": "FACE",
"Confidence": 99.59647361,
"Cover Type": "FACE_COVER",
"Covers Body Part": true
},
{
"Body Part": "HEAD",
"Confidence": 92.464736,
"Cover Type": "HEAD_COVER",
"Covers Body Part": true
}
],
"Image_Name": "image1.jpg"
}
]如果我能在这方面得到一些帮助,我将不胜感激。谢谢!
发布于 2021-03-20 23:21:21
请注意,在您的图像中有多个人被标记。如果您希望检测到PPE的每个人的详细信息在您的结果列表中是一个不同的项目,那么您可以使用类似以下内容:
results = []
for person in response["Persons"]:
bp = person["BodyParts"]
result = { 'Image_Name': image, 'Details': [] }
for ed in bp:
name = ed["Name"]
ppe = ed["EquipmentDetections"]
for ppe_type in ppe:
types = ppe_type["Type"]
confidence = ppe_type["Confidence"]
covers_body = ppe_type["CoversBodyPart"]["Value"]
person_details = {
"Body Part": name,
"Confidence": confidence,
"Cover Type": types,
"Covers Body Part": covers_body
}
result['Details'].append(person_details)
if len(result['Details']) > 0:
results.append(result)这将产生以下结果:
[
{
"Details": [
{
"Body Part": "FACE",
"Confidence": 95.17121887207031,
"Cover Type": "FACE_COVER",
"Covers Body Part": true
},
{
"Body Part": "LEFT_HAND",
"Confidence": 77.47138214111328,
"Cover Type": "HAND_COVER",
"Covers Body Part": true
}
],
"Image_Name": "image1.jpg"
}
]https://stackoverflow.com/questions/66713765
复制相似问题