0

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

我之前为单个数组做过类似的事情并成功循环,但我可能在这里遗漏了一些微不足道的东西。

以下是 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
        }
      }
    }
  ],

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

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

但是,当我用 FaceMatches 尝试同样的事情时,我得到了一个错误。

[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

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

Timestamp
FaceID
Similarity

有人可以为我解释一下吗?

4

1 回答 1

0

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

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

我已将您的 json 对象放入数据变量中,并且我忽略了没有相应面部匹配的时间戳,如果您希望可以以相同的方式提取

于 2018-05-25T10:49:53.253 回答