# -*- coding: utf-8 -*-
"""
Created on Sat Feb  1 22:32:41 2020

@author: anjay Krishnaan H
"""
import cv2
import numpy as np
from keras.models import load_model

#loading the pre-trained model to main program
model = load_model('emotion_model.h5')
#model= model.load_weights(filename)

#Facial Expressions
Expressions={0:"Angry",1:"Disgust",2:"Fear",3:"Happy",4:"Sad",5:"Surprise",6:"Neutral"}

def make_prediction(unknown):
    unknown=cv2.resize(unknown,(48,48))
    unknown=unknown/255.0
    unknown=np.array(unknown).reshape(-1,48,48,1)
    predict=np.argmax(model.predict(unknown),axis = 1)
    return predict[0]

def face_in_video():
    face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
    cap=cv2.VideoCapture(0)
    while True:
        ret,img=cap.read()  
        gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
        faces = face_cascade.detectMultiScale(gray,1.3,5)
        
        for (x,y,w,h) in faces:
            sub_face = gray[y:y+h, x:x+w]
            cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
            res=make_prediction(sub_face)
            font = cv2.FONT_HERSHEY_SIMPLEX
            cv2.putText(img,str(Expressions[res]),(x,y-5),font,0.5,(205,200,50),1,cv2.LINE_AA)
        cv2.imshow('img',img)
        if cv2.waitKey(1) & 0xFF ==ord('q'):
            break
    cap.release()
    cv2.destroyAllWindows()
    
face_in_video()    

ffc0b888ad53fd3c7ee0eaa6aeb86edd9bf73330
<mask token>


def make_prediction(unknown):
    unknown = cv2.resize(unknown, (48, 48))
    unknown = unknown / 255.0
    unknown = np.array(unknown).reshape(-1, 48, 48, 1)
    predict = np.argmax(model.predict(unknown), axis=1)
    return predict[0]


def face_in_video():
    face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
    cap = cv2.VideoCapture(0)
    while True:
        ret, img = cap.read()
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        faces = face_cascade.detectMultiScale(gray, 1.3, 5)
        for x, y, w, h in faces:
            sub_face = gray[y:y + h, x:x + w]
            cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
            res = make_prediction(sub_face)
            font = cv2.FONT_HERSHEY_SIMPLEX
            cv2.putText(img, str(Expressions[res]), (x, y - 5), font, 0.5,
                (205, 200, 50), 1, cv2.LINE_AA)
        cv2.imshow('img', img)
        if cv2.waitKey(1) & 255 == ord('q'):
            break
    cap.release()
    cv2.destroyAllWindows()


<mask token>

<mask token>


def make_prediction(unknown):
    unknown = cv2.resize(unknown, (48, 48))
    unknown = unknown / 255.0
    unknown = np.array(unknown).reshape(-1, 48, 48, 1)
    predict = np.argmax(model.predict(unknown), axis=1)
    return predict[0]


def face_in_video():
    face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
    cap = cv2.VideoCapture(0)
    while True:
        ret, img = cap.read()
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        faces = face_cascade.detectMultiScale(gray, 1.3, 5)
        for x, y, w, h in faces:
            sub_face = gray[y:y + h, x:x + w]
            cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
            res = make_prediction(sub_face)
            font = cv2.FONT_HERSHEY_SIMPLEX
            cv2.putText(img, str(Expressions[res]), (x, y - 5), font, 0.5,
                (205, 200, 50), 1, cv2.LINE_AA)
        cv2.imshow('img', img)
        if cv2.waitKey(1) & 255 == ord('q'):
            break
    cap.release()
    cv2.destroyAllWindows()


face_in_video()

<mask token>
model = load_model('emotion_model.h5')
Expressions = {(0): 'Angry', (1): 'Disgust', (2): 'Fear', (3): 'Happy', (4):
    'Sad', (5): 'Surprise', (6): 'Neutral'}


def make_prediction(unknown):
    unknown = cv2.resize(unknown, (48, 48))
    unknown = unknown / 255.0
    unknown = np.array(unknown).reshape(-1, 48, 48, 1)
    predict = np.argmax(model.predict(unknown), axis=1)
    return predict[0]


def face_in_video():
    face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
    cap = cv2.VideoCapture(0)
    while True:
        ret, img = cap.read()
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        faces = face_cascade.detectMultiScale(gray, 1.3, 5)
        for x, y, w, h in faces:
            sub_face = gray[y:y + h, x:x + w]
            cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
            res = make_prediction(sub_face)
            font = cv2.FONT_HERSHEY_SIMPLEX
            cv2.putText(img, str(Expressions[res]), (x, y - 5), font, 0.5,
                (205, 200, 50), 1, cv2.LINE_AA)
        cv2.imshow('img', img)
        if cv2.waitKey(1) & 255 == ord('q'):
            break
    cap.release()
    cv2.destroyAllWindows()


face_in_video()

<mask token>
import cv2
import numpy as np
from keras.models import load_model
model = load_model('emotion_model.h5')
Expressions = {(0): 'Angry', (1): 'Disgust', (2): 'Fear', (3): 'Happy', (4):
    'Sad', (5): 'Surprise', (6): 'Neutral'}


def make_prediction(unknown):
    unknown = cv2.resize(unknown, (48, 48))
    unknown = unknown / 255.0
    unknown = np.array(unknown).reshape(-1, 48, 48, 1)
    predict = np.argmax(model.predict(unknown), axis=1)
    return predict[0]


def face_in_video():
    face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
    cap = cv2.VideoCapture(0)
    while True:
        ret, img = cap.read()
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        faces = face_cascade.detectMultiScale(gray, 1.3, 5)
        for x, y, w, h in faces:
            sub_face = gray[y:y + h, x:x + w]
            cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
            res = make_prediction(sub_face)
            font = cv2.FONT_HERSHEY_SIMPLEX
            cv2.putText(img, str(Expressions[res]), (x, y - 5), font, 0.5,
                (205, 200, 50), 1, cv2.LINE_AA)
        cv2.imshow('img', img)
        if cv2.waitKey(1) & 255 == ord('q'):
            break
    cap.release()
    cv2.destroyAllWindows()


face_in_video()

# -*- coding: utf-8 -*-
"""
Created on Sat Feb  1 22:32:41 2020

@author: anjay Krishnaan H
"""
import cv2
import numpy as np
from keras.models import load_model

#loading the pre-trained model to main program
model = load_model('emotion_model.h5')
#model= model.load_weights(filename)

#Facial Expressions
Expressions={0:"Angry",1:"Disgust",2:"Fear",3:"Happy",4:"Sad",5:"Surprise",6:"Neutral"}

def make_prediction(unknown):
    unknown=cv2.resize(unknown,(48,48))
    unknown=unknown/255.0
    unknown=np.array(unknown).reshape(-1,48,48,1)
    predict=np.argmax(model.predict(unknown),axis = 1)
    return predict[0]

def face_in_video():
    face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
    cap=cv2.VideoCapture(0)
    while True:
        ret,img=cap.read()  
        gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
        faces = face_cascade.detectMultiScale(gray,1.3,5)
        
        for (x,y,w,h) in faces:
            sub_face = gray[y:y+h, x:x+w]
            cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
            res=make_prediction(sub_face)
            font = cv2.FONT_HERSHEY_SIMPLEX
            cv2.putText(img,str(Expressions[res]),(x,y-5),font,0.5,(205,200,50),1,cv2.LINE_AA)
        cv2.imshow('img',img)
        if cv2.waitKey(1) & 0xFF ==ord('q'):
            break
    cap.release()
    cv2.destroyAllWindows()
    
face_in_video()    
