|
|
from tensorflow.keras.preprocessing.image import img_to_array, load_img |
|
|
import numpy as np |
|
|
|
|
|
|
|
|
def preprocess_image(image_path, target_size=(48, 48)): |
|
|
|
|
|
image = load_img(image_path, color_mode='grayscale', |
|
|
target_size=target_size) |
|
|
|
|
|
image = img_to_array(image) |
|
|
|
|
|
image = image / 255.0 |
|
|
|
|
|
image = np.expand_dims(image, axis=0) |
|
|
return image |
|
|
|
|
|
|
|
|
def predict_emotion(image_path, model, emotion_list): |
|
|
|
|
|
image = preprocess_image(image_path) |
|
|
|
|
|
prediction = model.predict(image) |
|
|
|
|
|
|
|
|
predicted_emotion = emotion_list[np.argmax(prediction)] |
|
|
return predicted_emotion |
|
|
|
|
|
|
|
|
image_path = 'cropped_face.jpg' |
|
|
|
|
|
|
|
|
|