33from scipy import misc
44import numpy as np
55import json
6- from pkg_resources import resource_filename
76
87class FERModel :
98 """
@@ -57,7 +56,9 @@ def predict(self, image_file):
5756 resized_image = cv2 .resize (gray_image , self .target_dimensions , interpolation = cv2 .INTER_LINEAR )
5857 final_image = np .array ([np .array ([resized_image ]).reshape (list (self .target_dimensions )+ [self .channels ])])
5958 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
6162
6263 def _check_emotion_set_is_supported (self ):
6364 """
@@ -91,10 +92,11 @@ def _choose_model_from_target_emotions(self):
9192 model_indices = [self .emotion_index_map [emotion ] for emotion in self .target_emotions ]
9293 sorted_indices = [str (idx ) for idx in sorted (model_indices )]
9394 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
98100
99101 def _print_prediction (self , prediction ):
100102 normalized_prediction = [x / sum (prediction ) for x in prediction ]
@@ -105,5 +107,7 @@ def _print_prediction(self, prediction):
105107 if dominant_emotion_index == self .emotion_map [emotion ]:
106108 dominant_emotion = emotion
107109 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