0

I'm a beginner and I have a project about measuring Emotions using the Face API Microsoft, following the web page tutorial I obtained the emotion score of 20 images - JSON file. I want the average of eight emotion's kind throughout the whole file. This JSON file contains a list with several nested dictionaries. Why the key was found by the nested dictionary and then the function brings a KeyError? So, I got this error and I cannot get the average of the emotions, for this topic I reviewed this answer(Python 3.4 - How to get the average of dictionary values?.) I appreciate your guidance

First, throughout the file, I accessed to the lists by loops and to the dictionaries by their keys; finally, I got the last values, that's mean only one image's result instead of 20 as my total file. Testing to fix that through a function to compile all the previous process of nested list & dictionaries, but I got a KeyError. I reviewed several times the code and I know the key is there, but I cannot figure out the solution.

The first line of JSON file (total 20):

<Response [200]>
Response:
[{'faceId': 'c7369e8f-c624-4404-8542-225ad7beb9f6', 'faceRectangle': {'top': 230, 'left': 16, 'width': 258, 'height': 258}, 'faceAttributes': {'emotion': {'anger': 0.001, 'contempt': 0.001, 'disgust': 0.0, 'fear': 0.0, 'happiness': 0.0, 'neutral': 0.993, 'sadness': 0.005, 'surprise': 0.0}}}]

My code:

Response= (batch_call_images(list_of_faces)) #whole JSON file

for question in Response: 
    print(question)

for question_data in question:
    print(question_data)

question_emotion= question_data['faceAttributes']
question_emotion

question_anger= question_emotion['emotion']['anger']
print(question_anger)
question_contempt= question_emotion['emotion']['contempt']
print(question_contempt)
question_disgust= question_emotion['emotion']['disgust']
print(question_disgust)
question_fear= question_emotion['emotion']['fear']
print(question_fear)
question_happiness= question_emotion['emotion']['happiness']
print(question_happiness)
question_neutral= question_emotion['emotion']['neutral']
print(question_neutral)
question_sadness= question_emotion['emotion']['sadness']
print(question_sadness)
question_surprise= question_emotion['emotion']['surprise']
print(question_surprise)

def result_emotions():
    for question in Response:
         for question_data in question:
             question_emotion=question_data['faceAttributes']['emotion']
             question_anger=question_emotion['emotion']['anger']
             question_contempt=question_emotion['emotion']['contempt']
             question_disgust= question_emotion['emotion']['disgust']
             question_fear= question_emotion['emotion']['fear']
             question_happiness= question_emotion['emotion']['happiness']
             question_neutral= question_emotion['emotion']['neutral']
             question_sadness= question_emotion['emotion']['sadness']
             question_surprise= question_emotion['emotion']['surprise']
             print(question_surprise= question_emotion['emotion']['surprise'])
result_emotions()

KeyError                                  Traceback (most recent call last)
<ipython-input-328-ac1f0aa1f86c> in <module>()
----> 1 result_emotions()

<ipython-input-327-0f09e398a9eb> in result_emotions()
  3             for question_data in question:
  4                 question_emotion=question_data['faceAttributes']['emotion'] #if adds it there are keyerror
----> 5                 question_anger=question_emotion['emotion']['anger']
      6                 question_contempt=question_emotion['emotion']['contempt']
      7                 question_disgust= question_emotion['emotion']['disgust']

KeyError: 'emotion'

I want the average of eight emotion's kind throughout the whole Json file, for example: anger : 0.20, contempt : 0.15, disgust : 0.00, and so on.

jia Jimmy
  • 1,693
  • 2
  • 18
  • 38

1 Answers1

0

I am not sure about rest of your code, but I will address just the KeyError. The response you got is text in json structure. To read it in json, you have two options depending on how you got your response in the first place.

If you used python requests module, then you can use my_response=Response.json(). Now you have the json object stored in my_response. In your case

my_response=Response.json()
for question in my_response:
    print (question)
    #This will print a lot of dictionaries depending on your response
    for question_data in question:
        print (question[question_data])
        #This will print 
        #c7369e8f-c624-4404-8542-225ad7beb9f6
        #{'top': 230, 'left': 16, 'width': 258, 'height': 258}
        #'emotion': {'anger': 0.001, 'contempt': 0.001, 'disgust': 0.0, 'fear': 0.0, 'happiness': 0.0, 'neutral': 0.993, 'sadness': 0.005, 'surprise': 0.0} from your example

Now you can use this to iterate over and continue with your code like below

for question in my_response:
    question_emotion=question['faceAttributes']
    question_anger= question_emotion['emotion']['anger']

If you did not use the python requests module and you got a json response in text, you now have to use python json module.

import json
my_response= json.loads(Response.text)
#rest of the code is same as above

But, I suspect, you will find more issues with your code from what I saw. Keep learning.

Raj006
  • 562
  • 4
  • 18