3
3
from scipy import misc
4
4
import numpy as np
5
5
import json
6
- from pkg_resources import resource_filename
7
6
8
7
class FERModel :
9
8
"""
@@ -57,7 +56,9 @@ def predict(self, image_file):
57
56
resized_image = cv2 .resize (gray_image , self .target_dimensions , interpolation = cv2 .INTER_LINEAR )
58
57
final_image = np .array ([np .array ([resized_image ]).reshape (list (self .target_dimensions )+ [self .channels ])])
59
58
prediction = self .model .predict (final_image )
60
- self ._print_prediction (prediction [0 ])
59
+ ### Return the dominant expression
60
+ dominant_expression = self ._print_prediction (prediction [0 ])
61
+ return dominant_expression
61
62
62
63
def _check_emotion_set_is_supported (self ):
63
64
"""
@@ -91,10 +92,11 @@ def _choose_model_from_target_emotions(self):
91
92
model_indices = [self .emotion_index_map [emotion ] for emotion in self .target_emotions ]
92
93
sorted_indices = [str (idx ) for idx in sorted (model_indices )]
93
94
model_suffix = '' .join (sorted_indices )
94
- model_file = 'models/conv_model_%s.hdf5' % model_suffix
95
- emotion_map_file = 'models/conv_emotion_map_%s.json' % model_suffix
96
- emotion_map = json .loads (open (resource_filename ('EmoPy' ,emotion_map_file )).read ())
97
- return load_model (resource_filename ('EmoPy' ,model_file )), emotion_map
95
+ #Modify the path to choose the model file and the emotion map that you want to use
96
+ model_file = '~/EmoPy/models/conv_model_%s.hdf5' % model_suffix
97
+ emotion_map_file = '~/EmoPy/models/conv_emotion_map_%s.json' % model_suffix
98
+ emotion_map = json .loads (open (emotion_map_file ).read ())
99
+ return load_model (model_file ), emotion_map
98
100
99
101
def _print_prediction (self , prediction ):
100
102
normalized_prediction = [x / sum (prediction ) for x in prediction ]
@@ -105,5 +107,7 @@ def _print_prediction(self, prediction):
105
107
if dominant_emotion_index == self .emotion_map [emotion ]:
106
108
dominant_emotion = emotion
107
109
break
108
- print ('Dominant emotion: %s' % dominant_emotion )
109
- print ()
110
+ # print('Dominant emotion: %s' % dominant_emotion)
111
+ # print()
112
+ return dominant_emotion
113
+
0 commit comments