Friday, September 25, 2020

Efficient Facial Expression Identification Using Machine Learning + Python

Efficient Facial Expression Identification
    
           Here you will learn how to find the facial expression of an human using Python and Machine learning. 
    Here we need some components like,
  • Python
  • PyCharm
  • Visual Studio libraries
  • Panda library
  • Numpy
  • PyQT5 -- For designing UI
  • and basic knowledge of scripting

    • Firstly we need to design the LOGIN PAGE for login purpose. Below is the code for home page and save this as "login_page.py"
            from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):

    #Message Box Properties
    def showMessageBox(self, title, message):
        font = QtGui.QFont()
        font.setFamily("Times New Roman")
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        msgBox = QtWidgets.QMessageBox()
        msgBox.setIcon(QtWidgets.QMessageBox.Information)
        msgBox.setWindowTitle(title)
        msgBox.setText(message)
        msgBox.setStandardButtons(QtWidgets.QMessageBox.Ok)
        msgBox.setStyleSheet("QLabel{ color: red}")
        msgBox.setFont(font)
        msgBox.exec_()

    #Login Button Function
    def logincheck(self):
        unm = self.lineEdit.text().upper()
        pwd = self.lineEdit_2.text().upper()
        if unm == "" or unm == "null" or pwd == "" or pwd == "null":
            self.showMessageBox("Details Empty", "\"Username\" and \"Password\" should not be empty")
        else:
            if unm == "ADMIN" and pwd == "ADMIN":
                from Code.Home_Page import Ui_Home
                MainWindow_LogIn.hide()
                self.window_Home = QtWidgets.QMainWindow()
                self.ui = Ui_Home()
                #Calling Home Page
                self.ui.setupUi(self.window_Home)
                self.window_Home.show()
                print("Login Successful")
            else:
                self.showMessageBox("Invalid Entry ", "Entered \"Username\" and \"Password\" are not correct")

    #Main Widnow UI Code
    def setupUi(self, MainWindow_LogIn):
        MainWindow_LogIn.setObjectName("MainWindow_LogIn")
        MainWindow_LogIn.setWindowModality(QtCore.Qt.ApplicationModal)
        MainWindow_LogIn.resize(950, 666)
        MainWindow_LogIn.setStyleSheet("background-image: url(../Images/Login_Bg_Image.jpeg);")#Window Bg Image
        MainWindow_LogIn.setWhatsThis("")
        self.centralwidget = QtWidgets.QWidget(MainWindow_LogIn)
        self.centralwidget.setObjectName("centralwidget")
        MainWindow_LogIn.setWindowIcon(QtGui.QIcon('../Images/Login.png')) #Window Icon

        #Login Button
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(200, 360, 200, 51))
        font = QtGui.QFont()
        font.setFamily("Times New Roman")
        font.setPointSize(14)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton.setFont(font)
        self.pushButton.setObjectName("pushButton")
        self.pushButton.setStyleSheet("background-image:url(../Images/Login_Button.png);")
        self.pushButton.clicked.connect(self.logincheck)

        #UserName Lable Properties
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(119, 180, 120, 40))
        font = QtGui.QFont()
        font.setFamily("Times New Roman")
        font.setPointSize(14)
        font.setBold(True)
        font.setWeight(75)
        self.label.setFont(font)
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.label.setObjectName("label")
        self.label.setStyleSheet("color: rgb(255, 255, 255);")

        #UserName Entry Field Properties
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(250, 180, 240, 40))
        self.lineEdit.setObjectName("lineEdit")
        self.lineEdit.setFont(font)
        #self.lineEdit.setText("admin")
        self.lineEdit.setStyleSheet("color: White")

        #Title Properties
        self.label_3 = QtWidgets.QLabel(self.centralwidget)
        self.label_3.setGeometry(QtCore.QRect(200, 70, 571, 41))
        font = QtGui.QFont()
        font.setFamily("Times New Roman")
        font.setPointSize(20)
        font.setBold(True)
        font.setWeight(75)
        self.label_3.setFont(font)
        self.label_3.setTextFormat(QtCore.Qt.PlainText)
        self.label_3.setAlignment(QtCore.Qt.AlignCenter)
        self.label_3.setWordWrap(False)
        self.label_3.setObjectName("label_3")
        self.label_3.setStyleSheet("color: White")

        #Password Entry Field Properties
        self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_2.setGeometry(QtCore.QRect(250, 270, 240, 40))
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.lineEdit_2.setFont(font)
        self.lineEdit_2.setEchoMode(QtWidgets.QLineEdit.Password)
        #self.lineEdit_2.setText("admin")
        self.lineEdit_2.setStyleSheet("color: White")

        #Password Label Properties
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(130, 270, 100, 40))
        font = QtGui.QFont()
        font.setFamily("Times New Roman")
        font.setPointSize(14)
        font.setBold(True)
        font.setWeight(75)
        self.label_2.setFont(font)
        self.label_2.setAlignment(QtCore.Qt.AlignCenter)
        self.label_2.setObjectName("label_2")
        self.label_2.setStyleSheet("color: White")

        MainWindow_LogIn.setCentralWidget(self.centralwidget)
        self.retranslateUi(MainWindow_LogIn)
        QtCore.QMetaObject.connectSlotsByName(MainWindow_LogIn)

    def retranslateUi(self, MainWindow_LogIn):
        _translate = QtCore.QCoreApplication.translate
        MainWindow_LogIn.setWindowTitle(_translate("MainWindow_LogIn", "Efficient Facial Expression Recognition"))
        self.label.setText(_translate("MainWindow_LogIn", "UserName"))
        self.label_3.setText(_translate("MainWindow_LogIn", "Efficient Facial Expression Recognition"))
        self.label_2.setText(_translate("MainWindow_LogIn", "Password"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow_LogIn = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow_LogIn)
    MainWindow_LogIn.show()
    sys.exit(app.exec_())

    •  Next we need to develop the HOME PAGE for selecting the input type between Image or live Camera or Camera
                
            from PyQt5 import QtCore, QtGui, QtWidgets
import sys


class Ui_Home(object):

    # Message Box Properties
    def showMessageBox(self, title, message):
        font = QtGui.QFont()
        font.setFamily("Times New Roman")
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        msgBox = QtWidgets.QMessageBox()
        msgBox.setIcon(QtWidgets.QMessageBox.Information)
        msgBox.setWindowTitle(title)
        msgBox.setText(message)
        msgBox.setStandardButtons(QtWidgets.QMessageBox.Ok)
        msgBox.setStyleSheet("QLabel{ color: red}")
        msgBox.setFont(font)
        msgBox.exec_()

    #Image Upload value set
    process = ""
    def Upload_image(self, Upload_image):
        if Upload_image:
            Ui_Home.process = "Image"
            print("Choosed Method : Using Image")

    #Using WebCamera value set
    def Using_WebCamera(self, Using_WebCamera):
        if Using_WebCamera:
            Ui_Home.process = "WebCamera"
            print("Choosed Method : Using WebCamera")

    #Process Selection
    def startprocess(self):
        #Using Image Process
        if Ui_Home.process == "Image":
            try:
                from Code.Image_Upload_Page import Ui_Upload_Image

                self.Dialog2 = QtWidgets.QDialog()
                self.ui2 = Ui_Upload_Image()
                self.ui2.setupUi(self.Dialog2)
                self.Dialog2.show()

            except Exception as e:
                print("Error=" + e.args[0])
                tb = sys.exc_info()[2]
                print(tb.tb_lineno)
                print(e)
        else:
            #Using Web Camera Process
            if Ui_Home.process == "WebCamera":
                try:
                    from Code.WebCamera_Recognise import WebCamDetection

                    self.showMessageBox("Message", "Launching WebCamera, Press 's' to Stop Detection ")
                    WebCamDetection.process()

                except Exception as e:
                    print("Error=" + e.args[0])
                    tb = sys.exc_info()[2]
                    print(tb.tb_lineno)
                    print(e)
            else:
                self.showMessageBox(" Choose Method ", "Please choose any between \"Image\" Or \"web camara\" ")

    #Home Page Properties
    def setupUi(self, MainWindow_Home):
        MainWindow_Home.setObjectName("MainWindow_Home")
        MainWindow_Home.resize(980, 666)
        MainWindow_Home.setAutoFillBackground(False)
        MainWindow_Home.setStyleSheet("background-image: url(../Images/Home_Bg_Image.jpg);")
        MainWindow_Home.setWindowIcon(QtGui.QIcon('../Images/Home.png'))

        #Ttile Properties
        font = QtGui.QFont()
        font.setFamily("Times New Roman")
        font.setPointSize(30)
        font.setBold(True)
        font.setItalic(True)
        font.setUnderline(False)
        font.setWeight(75)
        font.setStrikeOut(False)
        font.setKerning(True)
        self.centralwidget = QtWidgets.QWidget(MainWindow_Home)
        self.centralwidget.setObjectName("centralwidget")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(50, 50, 1000, 100))
        self.label.setObjectName("label")
        self.label.setFont(font)
        self.label.setStyleSheet("color : white")

        #Text Message on display properties
        font = QtGui.QFont()
        font.setFamily("Times New Roman")
        font.setPointSize(21)
        font.setBold(True)
        font.setItalic(True)
        font.setWeight(300)
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(90, 150, 800, 60))
        self.label_2.setObjectName("label_2")
        self.label_2.setFont(font)
        self.label_2.setStyleSheet("color : White")

        #Using Image Radio Button Properties
        font = QtGui.QFont()
        font.setFamily("Times New Roman")
        font.setPointSize(14)
        font.setBold(True)
        self.radioButton = QtWidgets.QRadioButton(self.centralwidget)
        self.radioButton.setGeometry(QtCore.QRect(150, 280, 250, 50))
        self.radioButton.setObjectName("radioButton")
        self.radioButton.toggled.connect(self.Upload_image)
        self.radioButton.setFont(font)
        self.radioButton.setStyleSheet("color:rgb(255,255,255)")

        #Using Web camera Radio Button Properties
        self.radioButton_2 = QtWidgets.QRadioButton(self.centralwidget)
        self.radioButton_2.setGeometry(QtCore.QRect(630, 280, 250, 50))
        self.radioButton_2.setObjectName("radioButton_2")
        self.radioButton_2.setFont(font)
        self.radioButton_2.setStyleSheet("color:white")
        self.radioButton_2.toggled.connect(self.Using_WebCamera)

        #Image Image
        self.label_3 = QtWidgets.QLabel(self.centralwidget)
        self.label_3.setGeometry(QtCore.QRect(150, 330, 250, 191))
        self.label_3.setObjectName("label_3")
        self.label_3.setStyleSheet("image: url(../Images/Using_Image.png);")

        #Webcam Image
        self.label_4 = QtWidgets.QLabel(self.centralwidget)
        self.label_4.setGeometry(QtCore.QRect(630, 330, 250, 190))
        self.label_4.setObjectName("label_4")
        self.label_4.setStyleSheet("image: url(../Images/Using_WebCam.png);")

        #Continue Button Properties
        font = QtGui.QFont()
        font.setFamily("Times New Roman")
        font.setPointSize(20)
        font.setBold(True)
        font.setWeight(300)
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(380, 560, 250, 50))
        self.pushButton.setFocusPolicy(QtCore.Qt.NoFocus)
        self.pushButton.setObjectName("pushButton")
        self.pushButton.setFont(font)
        self.pushButton.setStyleSheet("background:white;")
        self.pushButton.clicked.connect(self.startprocess)
        self.pushButton.setStyleSheet("background-image:url(../Images/Continue_Button.png);")

        #LogOut Button Properties
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(800, 615, 155, 40))
        self.pushButton_2.setObjectName("pushButton_2")
        self.pushButton_2.setFont(font)
        self.pushButton_2.setStyleSheet("color:Green")
        self.pushButton_2.setStyleSheet("background-image:url(../Images/Logout_Button.jpg);")
        self.pushButton_2.clicked.connect(MainWindow_Home.close)

        MainWindow_Home.setCentralWidget(self.centralwidget)
        self.retranslateUi(MainWindow_Home)
        QtCore.QMetaObject.connectSlotsByName(MainWindow_Home)

    def retranslateUi(self, MainWindow_Home):
        _translate = QtCore.QCoreApplication.translate
        MainWindow_Home.setWindowTitle(_translate("MainWindow_Home", "Efficient Facial Expression Recognition"))
        self.label.setText(_translate("Home", "Welcome to Facial Expression Recognition\n"))
        self.label_2.setText(_translate("Home", "Choose any one option below and click on CONTINUE"))
        self.radioButton.setText(_translate("Home", "Using Image"))
        self.radioButton_2.setText(_translate("Home", "Using WebCamera"))
        self.label_3.setText(_translate("Home", ""))
        self.label_4.setText(_translate("Home", ""))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow_Home = QtWidgets.QMainWindow()
    ui = Ui_Home()
    ui.setupUi(MainWindow_Home)
    MainWindow_Home.show()
    sys.exit(app.exec_())

        
    • Based on selection, If we choose Image as input For uploading the image we need to create one more page as "image_upload_page.py". Here we can upload the image
from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Upload_Image(object):

    # Message Box Properties
    def showMessageBox(self, title, message):
        font = QtGui.QFont()
        font.setFamily("Times New Roman")
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        msgBox = QtWidgets.QMessageBox()
        msgBox.setIcon(QtWidgets.QMessageBox.Information)
        msgBox.setWindowTitle(title)
        msgBox.setText(message)
        msgBox.setStandardButtons(QtWidgets.QMessageBox.Ok)
        msgBox.setStyleSheet("QLabel{ color: red}")
        msgBox.setFont(font)
        msgBox.exec_()

    #Upload Image properties
    def uploadimage(self):
        try:
            fileName, _ = QtWidgets.QFileDialog.getOpenFileName(None, "Select File","..\Testing_Images","Image Files(*.*)") #Image Files(*.*)
            #print(fileName)
            self.lineEdit.setText(fileName)

        except Exception as e:
            print("Error=" + e.args[0])
            tb = sys.exc_info()[2]
            print(tb.tb_lineno)
            print(e)

    #Using Web Camera Properties
    def startrecognise(self):
        try:
            fname = self.lineEdit.text()
            if fname == "":
                self.showMessageBox("Insufficient Data","Please upload the \"image\" then click on Recognise")
            else:
                from Code.Image_Recognise import ImageDetection
                ImageDetection.process(fname)

        except Exception as e:
            print(e.args[0])
            tb = sys.exc_info()[2]
            print(tb.tb_lineno)
            print(e)

    #Upload Image UI Properties
    def setupUi(self, Upload_Image):
        Upload_Image.setObjectName("Upload_Image")
        Upload_Image.resize(550, 280)
        Upload_Image.setStyleSheet("background-image: url(../Images/Upload_Bg_Image.jpeg);")
        Upload_Image.setWindowIcon(QtGui.QIcon('../Images/Upload.png'))

        #Display Message to user properties
        self.label = QtWidgets.QLabel(Upload_Image)
        self.label.setGeometry(QtCore.QRect(60, 30, 451, 31))
        font = QtGui.QFont()
        font.setFamily("Times New Roman")
        font.setPointSize(14)
        font.setBold(True)
        font.setWeight(75)
        self.label.setFont(font)
        self.label.setObjectName("label")
        self.label.setStyleSheet("color: rgb(255, 255, 255);")

        #Select image box properties
        self.lineEdit = QtWidgets.QLineEdit(Upload_Image)
        self.lineEdit.setGeometry(QtCore.QRect(70, 100, 270, 40))
        self.lineEdit.setReadOnly(True)
        self.lineEdit.setObjectName("lineEdit")
        font = QtGui.QFont()
        font.setFamily("Times New Roman")
        font.setPointSize(9)
        font.setBold(True)
        font.setWeight(75)
        self.lineEdit.setFont(font)
        self.lineEdit.setStyleSheet("color: rgb(255, 255, 255);")


        #Upload button properties
        self.pushButton = QtWidgets.QPushButton(Upload_Image)
        self.pushButton.setGeometry(QtCore.QRect(340, 100, 120, 40))
        font = QtGui.QFont()
        font.setFamily("Times New Roman")
        font.setPointSize(11)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton.setFont(font)
        self.pushButton.setStyleSheet("background-image:url(../Images/Upload_Image.jpg);")
        self.pushButton.setText("")
        self.pushButton.setObjectName("pushButton")
        self.pushButton.clicked.connect(self.uploadimage)

        #Recognise Button Properties
        self.pushButton_2 = QtWidgets.QPushButton(Upload_Image)
        self.pushButton_2.setGeometry(QtCore.QRect(150, 170, 150, 40))
        font = QtGui.QFont()
        font.setFamily("Times New Roman")
        font.setPointSize(11)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_2.setFont(font)
        self.pushButton_2.setObjectName("pushButton_2")
        self.pushButton_2.clicked.connect(self.startrecognise)
        self.pushButton_2.setStyleSheet("background-image:url(../Images/Recognise_Button.png);")

        self.retranslateUi(Upload_Image)
        QtCore.QMetaObject.connectSlotsByName(Upload_Image)

    def retranslateUi(self, Upload_Image):
        _translate = QtCore.QCoreApplication.translate
        Upload_Image.setWindowTitle(_translate("Upload_Image", "Upload Image"))
        self.label.setText(_translate("Upload_Image", " Upload image below and click on Recognise "))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Upload_Image = QtWidgets.QWidget()
    ui = Ui_Upload_Image()
    ui.setupUi(Upload_Image)
    Upload_Image.show()
    sys.exit(app.exec_())
    
    • Once the image is uploaded we need to click on recognize. For recognition purpose we need to write the code using Neural Networks and HAAR AdaBoost methods.
from statistics import mode
import os
import cv2
from keras.models import load_model
import numpy as np
from utils.datasets import get_labels
from utils.inference import detect_faces
from utils.inference import draw_text
from utils.inference import draw_bounding_box
from utils.inference import apply_offsets
from utils.inference import load_detection_model
from utils.preprocessor import preprocess_input
from PIL import Image


class ImageDetection:

    @staticmethod
    def process(image):
        print("Started Recognising")
        os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
        # parameters for loading data and images
        detection_model_path = '../Models/Detection_Model/haarcascade_frontalface_default.xml'
        emotion_model_path = '../Models/Emotion_Models/fer2013_mini_XCEPTION.110-0.65.hdf5' #fer2013_mini_XCEPTION.102-0.66.hdf5
        emotion_labels = get_labels('fer2013')

        # hyper-parameters for bounding boxes shape
        frame_window = 10
        emotion_offsets = (20, 40)

        # loading models
        face_detection = load_detection_model(detection_model_path)
        emotion_classifier = load_model(emotion_model_path, compile=False)

        # getting input model shapes for inference
        emotion_target_size = emotion_classifier.input_shape[1:3]

        # starting lists for calculating modes
        emotion_window = []

        # starting video streaming
        cv2.namedWindow('window_frame')
        #video_capture = cv2.VideoCapture(0)
        # Load an color image in grayscale
        img = cv2.imread(image)

        bgr_image = img
        gray_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2GRAY)
        rgb_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2RGB)
        faces = detect_faces(face_detection, gray_image)

        for face_coordinates in faces:

            x1, x2, y1, y2 = apply_offsets(face_coordinates, emotion_offsets)
            gray_face = gray_image[y1:y2, x1:x2]
            try:
                gray_face = cv2.resize(gray_face, (emotion_target_size))
            except:
                continue

            gray_face = preprocess_input(gray_face, True)
            gray_face = np.expand_dims(gray_face, 0)
            gray_face = np.expand_dims(gray_face, -1)
            emotion_prediction = emotion_classifier.predict(gray_face)
            emotion_probability = np.max(emotion_prediction)
            emotion_label_arg = np.argmax(emotion_prediction)
            emotion_text = emotion_labels[emotion_label_arg]
            emotion_window.append(emotion_text)
            if len(emotion_window) > frame_window:
                emotion_window.pop(0)
            try:
                emotion_mode = mode(emotion_window)
            except:
                continue

            if emotion_text == 'angry':
                color = emotion_probability * np.asarray((255, 0, 0))
            elif emotion_text == 'sad':
                color = emotion_probability * np.asarray((0, 0, 255))
            elif emotion_text == 'happy':
                color = emotion_probability * np.asarray((255, 255, 0))
            elif emotion_text == 'surprise':
                color = emotion_probability * np.asarray((0, 255, 255))
            elif emotion_text == 'fear':
                color = emotion_probability * np.asarray((255, 255, 255))
            else:
                color = emotion_probability * np.asarray((0, 255, 0))

            color = color.astype(int)
            color = color.tolist()

            draw_bounding_box(face_coordinates, rgb_image, color)
            draw_text(face_coordinates, rgb_image, emotion_mode,
                      color, 0, -45, 1, 1)
        bgr_image = cv2.cvtColor(rgb_image, cv2.COLOR_RGB2BGR)
        cv2.imwrite("../Images/Temp/output.jpg", bgr_image)
        # os.system("powershell -c out.jpg")
        img = Image.open("../Images/Temp/output.jpg")
        img.show()

    • If we select the video in Home Page, it should launch the camera and start recognizing the expression dynamically

from statistics import mode
import os
import cv2
from keras.models import load_model
import numpy as np
import sys
from utils.datasets import get_labels
from utils.inference import detect_faces
from utils.inference import draw_text
from utils.inference import draw_bounding_box
from utils.inference import apply_offsets
from utils.inference import load_detection_model
from utils.preprocessor import preprocess_input

class WebCamDetection:

    @staticmethod
    def process():
        try:

            os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
            # parameters for loading data and images
            detection_model_path = '../Models/Detection_Model/haarcascade_frontalface_default.xml'
            emotion_model_path = '../Models/Emotion_Models/fer2013_mini_XCEPTION.102-0.66.hdf5'
            emotion_labels = get_labels('fer2013')

            # hyper-parameters for bounding boxes shape
            frame_window = 10
            emotion_offsets = (20, 40)

            # loading models
            face_detection = load_detection_model(detection_model_path)
            emotion_classifier = load_model(emotion_model_path, compile=False)

            # getting input model shapes for inference
            emotion_target_size = emotion_classifier.input_shape[1:3]

            # starting lists for calculating modes
            emotion_window = []

            # starting video streaming
            cv2.namedWindow('window_frame')
            video_capture = cv2.VideoCapture(0)
            while True:
                bgr_image = video_capture.read()[1]
                gray_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2GRAY)
                rgb_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2RGB)
                faces = detect_faces(face_detection, gray_image)

                for face_coordinates in faces:

                    x1, x2, y1, y2 = apply_offsets(face_coordinates, emotion_offsets)
                    gray_face = gray_image[y1:y2, x1:x2]
                    try:
                        gray_face = cv2.resize(gray_face, (emotion_target_size))
                    except:
                        continue

                    gray_face = preprocess_input(gray_face, True)
                    gray_face = np.expand_dims(gray_face, 0)
                    gray_face = np.expand_dims(gray_face, -1)
                    emotion_prediction = emotion_classifier.predict(gray_face)
                    emotion_probability = np.max(emotion_prediction)
                    emotion_label_arg = np.argmax(emotion_prediction)
                    emotion_text = emotion_labels[emotion_label_arg]
                    emotion_window.append(emotion_text)

                    if len(emotion_window) > frame_window:
                        emotion_window.pop(0)
                    try:
                        emotion_mode = mode(emotion_window)
                    except:
                        continue

                    if emotion_text == 'angry':
                        color = emotion_probability * np.asarray((255, 0, 0))
                    elif emotion_text == 'sad':
                        color = emotion_probability * np.asarray((0, 0, 255))
                    elif emotion_text == 'happy':
                        color = emotion_probability * np.asarray((255, 255, 0))
                    elif emotion_text == 'surprise':
                        color = emotion_probability * np.asarray((0, 255, 255))
                    else:
                        color = emotion_probability * np.asarray((0, 255, 0))

                    color = color.astype(int)
                    color = color.tolist()

                    draw_bounding_box(face_coordinates, rgb_image, color)
                    draw_text(face_coordinates, rgb_image, emotion_mode,
                              color, 0, -45, 1, 1)

                bgr_image = cv2.cvtColor(rgb_image, cv2.COLOR_RGB2BGR)
                cv2.imshow('window_frame', bgr_image)

                if cv2.waitKey(1) & 0xFF == ord('s'):
                    break
        except Exception as e:
            print("Errors=" + e.args[0])
            tb = sys.exc_info()[2]
            print(tb.tb_lineno)
            print(e)


if __name__=="__main__":
    WebCamDetection.process()


  • Here we will use Two files as dataset files for Haar Adaboost method. (Please comment your mail id here if u need these files).
  • For video and full execution explain please watch