-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
49 lines (38 loc) · 1.73 KB
/
main.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from tensorflow.keras.models import load_model
import tensorflow as tf
import cv2
import numpy as np
from tensorflow.keras.preprocessing.image import img_to_array
print("Num GPUs Available: ", len(tf.config.list_physical_devices('GPU')))
# model_path = 'model_final.h5'
model_path = 'model_final_extended.h5'
face_classifier = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
classifier = load_model(model_path)
emotion_labels = ['Angry', 'Disgust', 'Fear', 'Happy', 'Neutral', 'Sad', 'Surprise']
cap = cv2.VideoCapture(0)
while True:
_, frame = cap.read()
labels = []
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_classifier.detectMultiScale(gray, 1.32, 5)
img_size = 48 if model_path == 'model_final.h5' else 128
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + h), (224, 224, 224), thickness=3)
roi_gray = gray[y:y + h, x:x + w]
roi_gray = cv2.resize(roi_gray, (img_size, img_size), interpolation=cv2.INTER_AREA)
if np.sum([roi_gray]) != 0:
roi = roi_gray.astype('float')
roi = img_to_array(roi)
roi = np.expand_dims(roi, axis=0)
prediction = classifier.predict(roi)[0]
label = emotion_labels[prediction.argmax()]
label_position = (x, y)
cv2.putText(frame, label, label_position, cv2.FONT_HERSHEY_SIMPLEX, 1, (51, 51, 255), thickness=3)
else:
cv2.putText(frame, 'No Faces', (30, 80), cv2.FONT_HERSHEY_SIMPLEX, 1, (51, 51, 255), thickness=3)
cv2.imshow('Facial Expressions Detector', frame)
# To close the program, press q
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()