从Google Cloud Vision中提取JSON数据中的特定数据

时间:2018-05-06 11:55:04

标签: python json raspberry-pi

我对Raspberry Pi和Python编码很陌生,但我成功配置了Google Cloud Vision。但是JSON转储看起来像:

{
    "responses": [
        {
            "faceAnnotations": [
                {
                    "angerLikelihood": "UNLIKELY",
                    "blurredLikelihood": "VERY_UNLIKELY",
                    "boundingPoly": {
                        "vertices": [
                            {
                                "x": 129
                            },
                            {
                                "x": 370
                            },
                            {
                                "x": 370,
                                "y": 240
                            },
                            {
                                "x": 129,
                                "y": 240
                            }
                        ]
                    },
                    "detectionConfidence": 0.99543685,
                    "fdBoundingPoly": {
                        "vertices": [
                            {
                                "x": 162,
                                "y": 24
                            },
                            {
                                "x": 337,
                                "y": 24
                            },
                            {
                                "x": 337,
                                "y": 199
                            },
                            {
                                "x": 162,
                                "y": 199
                            }
                        ]
                    },
                    "headwearLikelihood": "VERY_UNLIKELY",
                    "joyLikelihood": "VERY_UNLIKELY",
                    "landmarkingConfidence": 0.77542377,
                    "landmarks": [
                        {
                            "position": {
                                "x": 210.93373,
                                "y": 92.71409,
                                "z": -0.00025338508
                            },
                            "type": "LEFT_EYE"
                        },
                        {
                            "position": {
                                "x": 280.00177,
                                "y": 82.57283,
                                "z": 0.49017733
                            },
                            "type": "RIGHT_EYE"
                        },
                        {
                            "position": {
                                "x": 182.08047,
                                "y": 77.89372,
                                "z": 6.825161
                            },
                            "type": "LEFT_OF_LEFT_EYEBROW"
                        },
                        {
                            "position": {
                                "x": 225.82335,
                                "y": 72.88091,
                                "z": -13.963233
                            },
                            "type": "RIGHT_OF_LEFT_EYEBROW"
                        },
                        {
                            "position": {
                                "x": 260.4491,
                                "y": 66.19005,
                                "z": -13.798634
                            },
                            "type": "LEFT_OF_RIGHT_EYEBROW"
                        },
                        {
                            "position": {
                                "x": 303.87503,
                                "y": 59.69522,
                                "z": 7.8336163
                            },
                            "type": "RIGHT_OF_RIGHT_EYEBROW"
                        },
                        {
                            "position": {
                                "x": 244.57729,
                                "y": 83.701904,
                                "z": -15.022567
                            },
                            "type": "MIDPOINT_BETWEEN_EYES"
                        },
                        {
                            "position": {
                                "x": 251.58353,
                                "y": 124.68004,
                                "z": -36.52176
                            },
                            "type": "NOSE_TIP"
                        },
                        {
                            "position": {
                                "x": 255.39096,
                                "y": 151.87607,
                                "z": -19.560472
                            },
                            "type": "UPPER_LIP"
                        },
                        {
                            "position": {
                                "x": 259.96045,
                                "y": 178.62886,
                                "z": -14.095398
                            },
                            "type": "LOWER_LIP"
                        },
                        {
                            "position": {
                                "x": 232.35422,
                                "y": 167.2542,
                                "z": -1.0750997
                            },
                            "type": "MOUTH_LEFT"
                        },
                        {
                            "position": {
                                "x": 284.49316,
                                "y": 159.06075,
                                "z": -0.078973025
                            },
                            "type": "MOUTH_RIGHT"
                        },
                        {
                            "position": {
                                "x": 256.94714,
                                "y": 163.11235,
                                "z": -14.0897665
                            },
                            "type": "MOUTH_CENTER"
                        },
                        {
                            "position": {
                                "x": 274.47885,
                                "y": 125.8553,
                                "z": -7.8479633
                            },
                            "type": "NOSE_BOTTOM_RIGHT"
                        },
                        {
                            "position": {
                                "x": 231.2164,
                                "y": 132.60686,
                                "z": -8.418254
                            },
                            "type": "NOSE_BOTTOM_LEFT"
                        },
                        {
                            "position": {
                                "x": 252.96692,
                                "y": 135.81783,
                                "z": -19.805998
                            },
                            "type": "NOSE_BOTTOM_CENTER"
                        },
                        {
                            "position": {
                                "x": 208.6943,
                                "y": 86.72571,
                                "z": -4.8503814
                            },
                            "type": "LEFT_EYE_TOP_BOUNDARY"
                        },
                        {
                            "position": {
                                "x": 223.4354,
                                "y": 90.71454,
                                "z": 0.42966545
                            },
                            "type": "LEFT_EYE_RIGHT_CORNER"
                        },
                        {
                            "position": {
                                "x": 210.67189,
                                "y": 96.09362,
                                "z": -0.62435865
                            },
                            "type": "LEFT_EYE_BOTTOM_BOUNDARY"
                        },
                        {
                            "position": {
                                "x": 195.00711,
                                "y": 93.783226,
                                "z": 6.6310787
                            },
                            "type": "LEFT_EYE_LEFT_CORNER"
                        },
                        {
                            "position": {
                                "x": 208.30045,
                                "y": 91.73073,
                                "z": -1.7749802
                            },
                            "type": "LEFT_EYE_PUPIL"
                        },
                        {
                            "position": {
                                "x": 280.8329,
                                "y": 75.722244,
                                "z": -4.3266015
                            },
                            "type": "RIGHT_EYE_TOP_BOUNDARY"
                        },
                        {
                            "position": {
                                "x": 295.9134,
                                "y": 78.8241,
                                "z": 7.3644505
                            },
                            "type": "RIGHT_EYE_RIGHT_CORNER"
                        },
                        {
                            "position": {
                                "x": 281.82813,
                                "y": 85.56999,
                                "z": -0.09711724
                            },
                            "type": "RIGHT_EYE_BOTTOM_BOUNDARY"
                        },
                        {
                            "position": {
                                "x": 266.6147,
                                "y": 83.689865,
                                "z": 0.6850431
                            },
                            "type": "RIGHT_EYE_LEFT_CORNER"
                        },
                        {
                            "position": {
                                "x": 282.31485,
                                "y": 80.471725,
                                "z": -1.3341979
                            },
                            "type": "RIGHT_EYE_PUPIL"
                        },
                        {
                            "position": {
                                "x": 202.4563,
                                "y": 66.06882,
                                "z": -8.493092
                            },
                            "type": "LEFT_EYEBROW_UPPER_MIDPOINT"
                        },
                        {
                            "position": {
                                "x": 280.76108,
                                "y": 54.08935,
                                "z": -7.895889
                            },
                            "type": "RIGHT_EYEBROW_UPPER_MIDPOINT"
                        },
                        {
                            "position": {
                                "x": 168.31839,
                                "y": 134.46411,
                                "z": 89.73161
                            },
                            "type": "LEFT_EAR_TRAGION"
                        },
                        {
                            "position": {
                                "x": 332.23724,
                                "y": 109.35637,
                                "z": 90.81501
                            },
                            "type": "RIGHT_EAR_TRAGION"
                        },
                        {
                            "position": {
                                "x": 242.81676,
                                "y": 67.845825,
                                "z": -16.629877
                            },
                            "type": "FOREHEAD_GLABELLA"
                        },
                        {
                            "position": {
                                "x": 264.32065,
                                "y": 208.95119,
                                "z": -4.0186276
                            },
                            "type": "CHIN_GNATHION"
                        },
                        {
                            "position": {
                                "x": 183.4723,
                                "y": 179.30655,
                                "z": 59.87147
                            },
                            "type": "CHIN_LEFT_GONION"
                        },
                        {
                            "position": {
                                "x": 331.6927,
                                "y": 156.69931,
                                "z": 60.93835
                            },
                            "type": "CHIN_RIGHT_GONION"
                        }
                    ],
                    "panAngle": 0.41165036,
                    "rollAngle": -8.687789,
                    "sorrowLikelihood": "VERY_UNLIKELY",
                    "surpriseLikelihood": "VERY_UNLIKELY",
                    "tiltAngle": 0.2050134,
                    "underExposedLikelihood": "POSSIBLE"
                }
            ]
        }
    ]
}

是的,这是一个值得关注的眼睛。我只是想提取可能性。最好采用以下格式:

Anger likelihood is UNLIKEY
Joy likelihood is VERY_UNLIKELY
Sorrow likelihood is VERY_UNLIKELY
Suprise likelihood is VERY_UNLIKELY

Python代码可以在这里找到: https://github.com/DexterInd/GoogleVisionTutorials/blob/master/camera-vision-face.py

2 个答案:

答案 0 :(得分:0)

你可以使用字典理解。假设您在变量result中得到了响应,则以下代码将完全输出您想要的内容。

import json

likelihood = {
    attr[:len(attr) - 10].capitalize(): value
    for attr, value 
        in json.loads(result)['responses'][0]['faceAnnotations'][0].items()
    if attr.find('Likelihood') != -1
}

print(*[
    '{} likelihood is {}'.format(e, p) for e, p in likelihood.items()
], sep='\n')

请注意,如果responsesfaceAnnotations数组中只有一个项目,则此代码可以正常工作。如果还有更多,代码将只处理第一项。它也有点难看。

len(attr) - 10中,10是“可能性”一词的长度。

答案 1 :(得分:0)

或许以最无耻的方式回答我自己的问题:

    print "Anger likelihood is:",
    print(response['responses'][0]['faceAnnotations'][0]['angerLikelihood'])
    print "Joy likelihood is:",
    print(response['responses'][0]['faceAnnotations'][0]['joyLikelihood'])
    print "Sorrow likelihood is:",
    print(response['responses'][0]['faceAnnotations'][0]['sorrowLikelihood'])
    print "Surprise likelihood is:",
    print(response['responses'][0]['faceAnnotations'][0]['surpriseLikelihood'])

看起来像:

Anger likelihood is: VERY_UNLIKELY
Joy likelihood is: VERY_LIKELY
Sorrow likelihood is: VERY_UNLIKELY
Surprise likelihood is: VERY_UNLIKELY