2023-01-07 16:06:57 +01:00
|
|
|
import numpy as np
|
|
|
|
import cv2
|
|
|
|
from tensorflow.keras.models import model_from_json
|
|
|
|
|
|
|
|
model_json_file = './input/model.json'
|
|
|
|
model_weights_file = './input/model_weights.h5'
|
|
|
|
with open(model_json_file, "r") as json_file:
|
|
|
|
loaded_model_json = json_file.read()
|
|
|
|
loaded_model = model_from_json(loaded_model_json)
|
|
|
|
loaded_model.load_weights(model_weights_file)
|
|
|
|
|
|
|
|
face_cascade = cv2.CascadeClassifier('./input/haarcascade_frontalface_default.xml')
|
|
|
|
img_size = 64
|
|
|
|
cap = cv2.VideoCapture(0)
|
|
|
|
import copy
|
|
|
|
|
|
|
|
while True:
|
|
|
|
|
|
|
|
ret, frame = cap.read()
|
|
|
|
img = copy.deepcopy(frame)
|
|
|
|
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
|
|
|
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
|
|
|
|
for (x,y,w,h) in faces:
|
|
|
|
fc = gray[y:y+h, x:x+w]
|
|
|
|
|
|
|
|
roi = cv2.resize(fc, (img_size,img_size))
|
|
|
|
pred = loaded_model.predict(roi[np.newaxis, :, :, np.newaxis])
|
|
|
|
text_idx=np.argmax(pred)
|
|
|
|
text_list = ['Angry', 'Disgust', 'Fear', 'Happy', 'Neutral', 'Sad', 'Surprise']
|
2023-02-01 17:24:31 +01:00
|
|
|
try:
|
|
|
|
text = text_list[text_idx]
|
|
|
|
except:
|
|
|
|
text = 'err'
|
|
|
|
cv2.putText(img, text, (x, y-5), cv2.FONT_HERSHEY_SIMPLEX, 2.5, (255, 0, 255), 2)
|
2023-01-07 16:06:57 +01:00
|
|
|
img = cv2.rectangle(img, (x,y), (x+w, y+h), (0,0,255), 2)
|
|
|
|
|
|
|
|
|
|
|
|
cv2.imshow("frame", img)
|
|
|
|
key = cv2.waitKey(1) & 0xFF
|
|
|
|
if key== ord('q'):
|
|
|
|
break
|
|
|
|
|
|
|
|
cap.release()
|
|
|
|
cv2.destroyAllWindows()
|