<reponame>trachpro/Emotion_recognition<filename>test.py
import numpy as np
import cv2
import imutils
import time
from keras.preprocessing.image import img_to_array
from keras.models import load_model
import numpy as np

detection_model_path = 'haarcascade_files/haarcascade_frontalface_default.xml'
emotion_model_path = 'models/_mini_XCEPTION.102-0.66.hdf5'

emotion_classifier = load_model(emotion_model_path, compile=False)
EMOTIONS = ["angry" ,"disgust","scared", "happy", "sad", "surprised", "neutral"]

face_detection = cv2.CascadeClassifier(detection_model_path)
# camera = cv2.VideoCapture("C:/Users/TuPM/Downloads/Video/emotion_video.mkv") #Using video
camera = cv2.VideoCapture(0) #using camera

cv2.namedWindow('frame')

while True:
    start_time = time.time()
    ret, frame = camera.read()

    frame = imutils.resize(frame,width=300)
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = face_detection.detectMultiScale(gray,scaleFactor=1.1,minNeighbors=5,minSize=(30,30),flags=cv2.CASCADE_SCALE_IMAGE)

    canvas = np.zeros((250, 300, 3), dtype="uint8")
    frameClone = frame.copy()
    if len(faces) > 0:
        # for (x,y,w,h) in faces:
        #     cv2.rectangle(gray, (x, y), (x+w, y+h), (0, 255, 0), 2)
        faces = sorted(faces, reverse=True, key=lambda x: (x[2] - x[0]) * (x[3] - x[1]))[0]
        # print(faces)
        (fX, fY, fW, fH) = faces

        roi = gray[fY: fY + fH, fX:fX + fW]
        roi = cv2.resize(roi, (64, 64))
        roi = roi.astype("float") / 255.0
        roi = img_to_array(roi)
        roi = np.expand_dims(roi, axis = 0)

        preds = emotion_classifier.predict(roi)[0]
        emotion_probability = np.max(preds)
        label = EMOTIONS[preds.argmax()]

        for i, (emotion, prob) in enumerate(zip(EMOTIONS, preds)):
            text = "{}: {:.2f}%".format(emotion, prob * 100)

            w = int(prob * 300)
            cv2.rectangle(canvas, (7, (i * 35) + 5),
            (w, (i * 35) + 35), (0, 0, 255), -1)
            cv2.putText(canvas, text, (10, (i * 35) + 23),
            cv2.FONT_HERSHEY_SIMPLEX, 0.45,
            (255, 255, 255), 2)
            cv2.putText(frameClone, label, (fX, fY - 10),
            cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2)
            cv2.rectangle(frameClone, (fX, fY), (fX + fW, fY + fH), (0, 0, 255), 2)

    cv2.imshow('frame', frameClone)
    cv2.imshow("Probabilities", canvas)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
    # print("FPS: ", 1.0 / (time.time() - start_time))
    # print(len(faces))
    

camera.release()
# cv2.distroyAllWindows()
import numpy as np
import cv2
import imutils
import time
from keras.preprocessing.image import img_to_array
from keras.models import load_model
import numpy as np

detection_model_path = 'haarcascade_files/haarcascade_frontalface_default.xml'
emotion_model_path = 'models/_mini_XCEPTION.102-0.66.hdf5'

emotion_classifier = load_model(emotion_model_path, compile=False)
EMOTIONS = ["angry" ,"disgust","scared", "happy", "sad", "surprised", "neutral"]

face_detection = cv2.CascadeClassifier(detection_model_path)
# camera = cv2.VideoCapture("C:/Users/TuPM/Downloads/Video/emotion_video.mkv") #Using video
camera = cv2.VideoCapture(0) #using camera

cv2.namedWindow('frame')

while True:
    start_time = time.time()
    ret, frame = camera.read()

    frame = imutils.resize(frame,width=300)
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = face_detection.detectMultiScale(gray,scaleFactor=1.1,minNeighbors=5,minSize=(30,30),flags=cv2.CASCADE_SCALE_IMAGE)

    canvas = np.zeros((250, 300, 3), dtype="uint8")
    frameClone = frame.copy()
    if len(faces) > 0:
        # for (x,y,w,h) in faces:
        #     cv2.rectangle(gray, (x, y), (x+w, y+h), (0, 255, 0), 2)
        faces = sorted(faces, reverse=True, key=lambda x: (x[2] - x[0]) * (x[3] - x[1]))[0]
        # print(faces)
        (fX, fY, fW, fH) = faces

        roi = gray[fY: fY + fH, fX:fX + fW]
        roi = cv2.resize(roi, (64, 64))
        roi = roi.astype("float") / 255.0
        roi = img_to_array(roi)
        roi = np.expand_dims(roi, axis = 0)

        preds = emotion_classifier.predict(roi)[0]
        emotion_probability = np.max(preds)
        label = EMOTIONS[preds.argmax()]

        for i, (emotion, prob) in enumerate(zip(EMOTIONS, preds)):
            text = "{}: {:.2f}%".format(emotion, prob * 100)

            w = int(prob * 300)
            cv2.rectangle(canvas, (7, (i * 35) + 5),
            (w, (i * 35) + 35), (0, 0, 255), -1)
            cv2.putText(canvas, text, (10, (i * 35) + 23),
            cv2.FONT_HERSHEY_SIMPLEX, 0.45,
            (255, 255, 255), 2)
            cv2.putText(frameClone, label, (fX, fY - 10),
            cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2)
            cv2.rectangle(frameClone, (fX, fY), (fX + fW, fY + fH), (0, 0, 255), 2)

    cv2.imshow('frame', frameClone)
    cv2.imshow("Probabilities", canvas)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
    # print("FPS: ", 1.0 / (time.time() - start_time))
    # print(len(faces))
    

camera.release()
# cv2.distroyAllWindows()
# camera = cv2.VideoCapture("C:/Users/TuPM/Downloads/Video/emotion_video.mkv") #Using video #using camera # for (x,y,w,h) in faces: #     cv2.rectangle(gray, (x, y), (x+w, y+h), (0, 255, 0), 2) # print(faces) # print("FPS: ", 1.0 / (time.time() - start_time)) # print(len(faces)) # cv2.distroyAllWindows()