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.