解析识别get_face_search结果 [英] Parsing rekognition get_face_search results

查看:67
本文介绍了解析识别get_face_search结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从get_face_search()AWS Rekognition API的结果中解析出人脸匹配项。它输出一个Persons数组,该数组中是给定人物和时间戳的另一个FaceMatches数组。我想从FaceMatches数组中获取信息,并能够遍历Face Matchs数组。

I am trying to parse out Face Matches from the results of the get_face_search() AWS Rekognition API. It outputs an array of Persons, within that array is another array of FaceMatches for a given person and timestamp. I want to take information from the FaceMatches array and be able to loop through the array of Face Matches.

我以前对单个数组做过类似的操作,并成功地循环了,但是在这里我可能缺少一些琐碎的事情。

I have done something similar before for single arrays and looped successfully, but I am missing something trivial here perhaps.

此处从API输出:

    Response:
{
  "JobStatus": "SUCCEEDED",
  "NextToken": "U5EdbZ+86xseDBfDlQ2u8QhSVzbdodDOmX/gSbwIgeO90l2BKWvJEscjUDmA6GFDCSSfpKA4",
  "VideoMetadata": {
    "Codec": "h264",
    "DurationMillis": 6761,
    "Format": "QuickTime / MOV",
    "FrameRate": 30.022184371948242,
    "FrameHeight": 568,
    "FrameWidth": 320
  },
  "Persons": [
    {
      "Timestamp": 0,
      "Person": {
        "Index": 0,
        "BoundingBox": {
          "Width": 0.987500011920929,
          "Height": 0.7764084339141846,
          "Left": 0.0031250000465661287,
          "Top": 0.2042253464460373
        },
        "Face": {
          "BoundingBox": {
            "Width": 0.6778846383094788,
            "Height": 0.3819068372249603,
            "Left": 0.10096154361963272,
            "Top": 0.2654387652873993
          },
          "Landmarks": [
            {
              "Type": "eyeLeft",
              "X": 0.33232420682907104,
              "Y": 0.4194057583808899
            },
            {
              "Type": "eyeRight",
              "X": 0.5422032475471497,
              "Y": 0.41616082191467285
            },
            {
              "Type": "nose",
              "X": 0.45633792877197266,
              "Y": 0.4843473732471466
            },
            {
              "Type": "mouthLeft",
              "X": 0.37002310156822205,
              "Y": 0.567118763923645
            },
            {
              "Type": "mouthRight",
              "X": 0.5330674052238464,
              "Y": 0.5631639361381531
            }
          ],
          "Pose": {
            "Roll": -2.2475271224975586,
            "Yaw": 4.371307373046875,
            "Pitch": 6.83940315246582
          },
          "Quality": {
            "Brightness": 40.40004348754883,
            "Sharpness": 99.95819854736328
          },
          "Confidence": 99.87971496582031
        }
      },
      "FaceMatches": [
        {
          "Similarity": 99.81229400634766,
          "Face": {
            "FaceId": "4699a1eb-9f6e-415d-8716-eef141d23433a",
            "BoundingBox": {
              "Width": 0.6262923432480737,
              "Height": 0.46972032423490747,
              "Left": 0.130435005324523403604,
              "Top": 0.13354002343240603
            },
            "ImageId": "1ac790eb-615a-111f-44aa-4017c3c315ad",
            "Confidence": 99.19400024414062
          }
        }
      ]
    },
    {
      "Timestamp": 66,
      "Person": {
        "Index": 0,
        "BoundingBox": {
          "Width": 0.981249988079071,
          "Height": 0.7764084339141846,
          "Left": 0.0062500000931322575,
          "Top": 0.2042253464460373
        }
      }
    },
    {
      "Timestamp": 133,
      "Person": {
        "Index": 0,
        "BoundingBox": {
          "Width": 0.9781249761581421,
          "Height": 0.783450722694397,
          "Left": 0.0062500000931322575,
          "Top": 0.19894365966320038
        }
      }
    },
    {
      "Timestamp": 199,
      "Person": {
        "Index": 0,
        "BoundingBox": {
          "Width": 0.981249988079071,
          "Height": 0.783450722694397,
          "Left": 0.0031250000465661287,
          "Top": 0.19894365966320038
        },
        "Face": {
          "BoundingBox": {
            "Width": 0.6706730723381042,
            "Height": 0.3778440058231354,
            "Left": 0.10817307233810425,
            "Top": 0.26679307222366333
          },
          "Landmarks": [
            {
              "Type": "eyeLeft",
              "X": 0.33244985342025757,
              "Y": 0.41591548919677734
            },
            {
              "Type": "eyeRight",
              "X": 0.5446155667304993,
              "Y": 0.41204410791397095
            },
            {
              "Type": "nose",
              "X": 0.4586191177368164,
              "Y": 0.479543000459671
            },
            {
              "Type": "mouthLeft",
              "X": 0.37614554166793823,
              "Y": 0.5639738440513611
            },
            {
              "Type": "mouthRight",
              "X": 0.5334802865982056,
              "Y": 0.5592300891876221
            }
          ],
          "Pose": {
            "Roll": -2.4899401664733887,
            "Yaw": 3.7596628665924072,
            "Pitch": 6.3544135093688965
          },
          "Quality": {
            "Brightness": 40.46360778808594,
            "Sharpness": 99.95819854736328
          },
          "Confidence": 99.89802551269531
        }
      },
      "FaceMatches": [
        {
          "Similarity": 99.80543518066406,
          "Face": {
            "FaceId": "4699a1eb-9f6e-415d-8716-eef141d9223a",
            "BoundingBox": {
              "Width": 0.626294234234737,
              "Height": 0.469234234890747,
              "Left": 0.130435002334234604,
              "Top": 0.13354023423449180603
            },
            "ImageId": "1ac790eb-615a-111f-44aa-4017c3c315ad",
            "Confidence": 99.19400024414062
          }
        }
      ]
    },
    {
      "Timestamp": 266,
      "Person": {
        "Index": 0,
        "BoundingBox": {
          "Width": 0.984375,
          "Height": 0.7852112650871277,
          "Left": 0,
          "Top": 0.19718310236930847
        }
      }
    }
  ],

我使用以下方法隔离了时间戳记(只是测试了我的方法):

I have isolated the timestamps (just testing my approach) using the following:

timestamps = [m['Timestamp'] for m in response['Persons']]
Output is this, as expected - [0, 66, 133, 199, 266]

但是,当我使用FaceMatches尝试相同的操作时,

However, when I try the same thing with FaceMatches, I get an error.

[0, 66, 133, 199, 266]
list indices must be integers or slices, not str: TypeError
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 40, in lambda_handler
    matches = [m['FaceMatches']['Face']['FaceId'] for m in response['Persons']]
  File "/var/task/lambda_function.py", line 40, in <listcomp>
    matches = [m['FaceMatches']['Face']['FaceId'] for m in response['Persons']]
TypeError: list indices must be integers or slices, not str

我最终需要做的是匹配的每张脸:

What I need to end up with is for each face that is matched:

Timestamp
FaceID
Similarity

有人可以帮我一下吗?

推荐答案

根据您的需要,您有两个FaceMatch对象在您的响应中,您可以通过以下方式提取所需的信息:

According to your needs , you have two FaceMatch objects in your response and you can extract required info in this way :

import json 

with open('newtest.json') as f:
    data = json.load(f)

length =len(data['Persons'])


for i in range(0,length):
    try:
        print(data['Persons'][i]['FaceMatches'][0]['Similarity'])
        print(data['Persons'][i]['FaceMatches'][0]['Face']['FaceId'])
        print(data['Persons'][i]['Timestamp'])
    except:
        continue

我在 data 变量中使用了您的json对象,但我忽略了时间戳,其中没有相应的面部匹配,如果您希望可以提取,则可以用相同的方式

I have taken your json object in data variable and i have ignored timestamps where there is no corresponding facematch, if you wish you can extract then the same way

这篇关于解析识别get_face_search结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆