Extracting and making the results from JSON response pretty from Google Cloud Vision

google
cloud-vision

#1

Hi all,

I am quite new to Raspberry Pi and Python coding but I successful in configuring Google Cloud Vision. However with the JSON data looking like:

{
    "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"
                }
            ]
        }
    ]
}

Yes, it’s an eyesore to look at. I am only wanting to extract the likelihood. Preferably in this format:
Anger likelihood is VERY_LIKELY
Joy likelihood is VERY_UNLIKELY
Sorrow likelihood is VERY_UNLIKELY
Suprise likelihood is VERY_UNLIKELY

Python code can be found here:


#2

JSON can be easily parsed in Python3 using the JSON library to make it look pretty. See this library for some commands and examples.