face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
while(True):
ret, img = cap.read()
img = cv2.resize(img, (256, 256))
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray)
if len(faces)>0:
for (x, y, w, h) in faces:
try:
gray = cv2.resize(gray[x:x+w,y:y+h], (48,48))
except:
break
gray = np.expand_dims(gray, axis=-1)
gray = np.expand_dims(gray, axis=0)
pred = model.predict(gray)
idx = pred.argmax(axis=-1)[0]
emoj = cv2.imread(f'emojis/{emotions[idx]}.jpg')
plot_image(img, emoj)
else:
emoj = cv2.imread('NofaceDetected.jpeg')
plot_image(img, emoj)
if cv2.waitKey(10) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()