Skip to content

Commit 20b2057

Browse files
sinbycosxuv
authored andcommitted
Add webcam support and example
1 parent 48910bc commit 20b2057

File tree

2 files changed

+53
-8
lines changed

2 files changed

+53
-8
lines changed
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#This file allows to perform Emotion detection on frames grabbed from the webcam using OpenCV-Python
2+
3+
import cv2
4+
import sys
5+
sys.path.append('../')
6+
7+
#Choose the type of Face Expression Model
8+
from src.fermodel import FERModel
9+
10+
#Frame Number
11+
FRAME_NUM = 0
12+
13+
#Choose the type of face detector cascade you want to use
14+
cascPath = "~/EmoPy/venv/lib/python3.5/site-packages/cv2/data/haarcascade_frontalface_default.xml"
15+
faceCascade = cv2.CascadeClassifier(cascPath)
16+
#Specify the camera which you want to use. The default argument is '0'
17+
video_capture = cv2.VideoCapture(0)
18+
19+
while True:
20+
#Capture frame-by-frame
21+
ret, frame = video_capture.read()
22+
#Save the captured frame on disk
23+
file = '~/EmoPy/models/examples/image_data/image.jpg'
24+
cv2.imwrite(file, frame)
25+
#Can choose other target emotions from the emotion subset defined in fermodel.py in src directory. The function
26+
# defined as `def _check_emotion_set_is_supported(self):`
27+
target_emotions = ['calm', 'anger', 'happiness']
28+
model = FERModel(target_emotions, verbose=True)
29+
frameString = model.predict(file)
30+
#Display frame number and emotion
31+
cv2.putText(frame, 'Frame:' + str(FRAME_NUM), (10, 40), cv2.FONT_HERSHEY_COMPLEX_SMALL, 3, (0, 0, 255), 2, cv2.LINE_AA)
32+
cv2.putText(frame, frameString, (10,450), cv2.FONT_HERSHEY_COMPLEX_SMALL, 3, (0,255,0), 2, cv2.LINE_AA)
33+
cv2.imshow('Video', frame)
34+
cv2.waitKey(1)
35+
FRAME_NUM += 1
36+
#Press Esc to exit the window
37+
if cv2.waitKey(1) & 0xFF == 27:
38+
break
39+
#Closes all windows
40+
cv2.destroyAllWindows()
41+

EmoPy/src/fermodel.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from scipy import misc
44
import numpy as np
55
import json
6-
from pkg_resources import resource_filename
76

87
class 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

Comments
 (0)