json - 使用 AWS Rekognition 检测 PPE 并将结果存储在 DynamoDB 中
问题描述
我正在使用来自 AWS Rekognition 的 PPE 检测通过图像识别人身上的 PPE。每次我在 S3 中上传一张或几张图片时,都会向 SQS 发送一条消息,并从那里触发我的 lambda 函数以捕获图像名称。
lambda 函数调用 AWS Rekognition PPE 检测 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"
}
]
推荐阅读
- gpu - geth 在具有 2 个 GPU 的 Windows 10 上使用 Claymore 进行挖掘时声明 eth_submitHashrate
- drag-and-drop - 在 Blazor 拖放中未触发 Drop 事件
- javascript - 在滚动时显示导航栏:重新加载时不显示
- css - CSS:为什么我的工具提示定位不正确?
- android - 将项目添加到 Activity 和 Fragment 之间的列表视图
- r - 有条件地查找 xts 数据集的开始和结束时间索引
- jquery - Laravel 'popper.js is null' 当单击带有类'input-group-prepend'的引导 4 div 时
- parsing - 获取表达式在被解析文本中的位置和长度
- javascript - 用 JS 拖放创建的元素
- node.js - 如何在nodejs中每小时抓取一次数据?