-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFinalTestNew.py
33 lines (26 loc) · 894 Bytes
/
FinalTestNew.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import cv2
import numpy as np
import tensorflow as tf
from keras.preprocessing import image
import matplotlib.pyplot as plt
# Path to the saved model
MODEL_PATH = 'working/my_cnn_model.h5'
# Load the saved model
model = tf.keras.models.load_model(MODEL_PATH)
# Classes
CLASSES = ['angry', 'fear', 'happy', 'sad', 'surprise', 'neutral']
def predict_emotion(img_path):
# Load and preprocess the image
image = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
image = cv2.resize(image, (48,48), interpolation=cv2.INTER_LINEAR)
img = np.array(image)
img = img.reshape(1,48,48,1)
# Predict the class
predict_x = model.predict(img)
result = np.argmax(predict_x)
# Print and display the result
print("Predicted class:", CLASSES[result])
plt.imshow(image, cmap='gray')
plt.show()
# Example usage
predict_emotion('input/PrivateTest_33827821.jpg')