-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemotionJSONDataParser.py
72 lines (52 loc) · 2.14 KB
/
emotionJSONDataParser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# Coded by William A. Rodriguez, MIT '18
# for UROP Project with Dr. Ognjen (Oggi) Rudovic
# ast is a module that allows to parse strings directy into
# python objects. Here's it used to parse a list of dictionaries
import ast
# Place this python file in the same folder that the emotionJSON.txt is in
fileName = "emotionJSON.txt"
file_object = open(fileName, 'r')
jsonString = file_object.read()
file_object.close()
# Each face dictionary is a dictionary of dictionaries
listOfFaceDictionaries = ast.literal_eval(jsonString)
def updateFaceData():
file_object = open(fileName, 'r')
jsonString = file_object.read()
file_object.close()
istOfFaceDictionaries = ast.literal_eval(jsonString)
def getAverageEmotion(emotion):
sumOfEmotionValues = 0
for face in listOfFaceDictionaries:
emotionLevel = float(face["emotions"][emotion])
sumOfEmotionValues +=emotionLevel
return sumOfEmotionValues/numberOfDetectedFaces
while True:
# Average data points
updateFaceData()
## Emotions
numberOfDetectedFaces = len(listOfFaceDictionaries)
averageAnger = getAverageEmotion("anger")
averageContempt = getAverageEmotion("contempt")
averageDisgust = getAverageEmotion("disgust")
averageEngagement = getAverageEmotion("engagement")
averageFear = getAverageEmotion("fear")
averageJoy = getAverageEmotion("joy")
averageSadness = getAverageEmotion("sadness")
averageSurprise = getAverageEmotion("surprise")
averageValence = getAverageEmotion("valence")
# The Affectiva SDK offers a rich breadth of emotion data points
# Keys to each face dictionary include:
# 'orientation', 'appearance', 'emotions', 'faceQuality', 'expressions', 'emojis', 'faceId'
# See the emotionJSON.txt file for more insight regarding the breadth of available data
print ("averageAnger",averageAnger )
print ("averageContempt",averageContempt)
print ("averageDisgust",averageDisgust)
print ("averageEngagement",averageEngagement)
print ("averageFear",averageFear)
print ("averageJoy",averageJoy)
print ("averageSadness",averageSadness)
print ("averageSurprise",averageSurprise)
print ("averageValence",averageValence)
print(listOfFaceDictionaries[0]["emotions"].keys())
print("\n")