From c34e89f31798cee01b159559cd42e9d567fde2ba Mon Sep 17 00:00:00 2001 From: Julien Deswaef Date: Sun, 24 Feb 2019 12:00:16 -0500 Subject: [PATCH] Modifies Webcam example to bypass recording images to disk --- EmoPy/examples/fermodel_example_webcam.py | 6 ++---- EmoPy/src/fermodel.py | 14 +++++++++++--- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/EmoPy/examples/fermodel_example_webcam.py b/EmoPy/examples/fermodel_example_webcam.py index 7e78f36..450c2d1 100644 --- a/EmoPy/examples/fermodel_example_webcam.py +++ b/EmoPy/examples/fermodel_example_webcam.py @@ -24,11 +24,9 @@ while True: #Capture frame-by-frame ret, frame = video_capture.read() - #Save the captured frame on disk - file = 'image_data/image.jpg' - cv2.imwrite(file, frame) - frameString = model.predict(file) + #Passing the frame to the predictor + frameString = model.predict_from_ndarray(frame) #Display emotion retval, baseline = cv2.getTextSize(frameString, fontFace, fontScale, thickness) diff --git a/EmoPy/src/fermodel.py b/EmoPy/src/fermodel.py index e44dc91..6ca2a6e 100644 --- a/EmoPy/src/fermodel.py +++ b/EmoPy/src/fermodel.py @@ -51,9 +51,17 @@ def predict(self, image_file): :param images: image file (jpg or png format) """ image = misc.imread(image_file) - gray_image = image - if len(image.shape) > 2: - gray_image = cv2.cvtColor(image, code=cv2.COLOR_BGR2GRAY) + self.predict_from_ndarray(image) + + def predict_from_ndarray(self, image_array): + """ + Predicts discrete emotion for given image. + + :param image_array: a n dimensional array representing an image + """ + gray_image = image_array + if len(image_array.shape) > 2: + gray_image = cv2.cvtColor(image_array, code=cv2.COLOR_BGR2GRAY) resized_image = cv2.resize(gray_image, self.target_dimensions, interpolation=cv2.INTER_LINEAR) final_image = np.array([np.array([resized_image]).reshape(list(self.target_dimensions)+[self.channels])]) prediction = self.model.predict(final_image)