使用 AWS Rekognition 检测 PPE 并将结果存储在 DynamoDB 中
PPE Detection with AWS Rekognition and Storing the Results in DynamoDB
我正在使用 AWS Rekognition 的 PPE Detection 通过图像识别人们身上的 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"
}
]
如果我能在这方面得到一些帮助,我将不胜感激。谢谢!
请注意,您的图片中标记了多个人。如果您希望每个检测到 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"
}
]
我正在使用 AWS Rekognition 的 PPE Detection 通过图像识别人们身上的 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"
}
]
如果我能在这方面得到一些帮助,我将不胜感激。谢谢!
请注意,您的图片中标记了多个人。如果您希望每个检测到 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"
}
]