Friday, September 25, 2020

Multiple Face Detection Using Machine Learning + Python

Multiple Face Detection

           

             Here We will learn about how to identify the multiple faces in on image using different different algorithms and compare which algorithm is giving good results. 

            And at the end will generate the graph to compare the results

  •     Here we need some components like,
    • Python
    • PyCharm
    • Visual Studio libraries
    • Panda library
    • Numpy
    • PyQT5 -- For designing UI
    • and basic knowledge of scripting   


  • Here first we need to develop the Login page for authentication purpose, Below is the code.
from PyQt5 import QtCore, QtGui, QtWidgets
from Code.Home_Page import Ui_MainWindow_Home


class Ui_MainWindow_Login(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 validation Process
    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":
                self.window_Home = QtWidgets.QMainWindow()
                self.ui = Ui_MainWindow_Home()
                #Calling Home Page
                self.ui.setupUi(self.window_Home)
                self.window_Home.show()
                MainWindow_Login.hide()
            else:
                self.showMessageBox("Invalid Entry ", "Entered \"Username\" and \"Password\" are not correct")

    #login Main Window Properties
    def setupUi(self, MainWindow_Login):
        MainWindow_Login.setObjectName("MainWindow_Login")
        MainWindow_Login.resize(800, 600)
        MainWindow_Login.setStyleSheet("background-image: url(../Images/Login_Bg_Image.jpeg);")#Window Bg Image
        MainWindow_Login.setWindowIcon(QtGui.QIcon('../Images/Login.png'))

        self.centralwidget = QtWidgets.QWidget(MainWindow_Login)
        self.centralwidget.setObjectName("centralwidget")

        #Title Properties
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(220, 50, 301, 51))
        font = QtGui.QFont()
        font.setFamily("Times New Roman")
        font.setPointSize(16)
        font.setBold(True)
        font.setWeight(75)
        self.label.setFont(font)
        self.label.setObjectName("label")
        self.label.setStyleSheet("color: Yellow")

        #Username Entry Field Properties
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(220, 160, 191, 41))
        self.lineEdit.setFont(font)
        self.lineEdit.setObjectName("lineEdit")
        self.lineEdit.setStyleSheet("color: White")

        #Passowrd Entry Field Properties
        self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_2.setGeometry(QtCore.QRect(220, 240, 191, 41))
        self.lineEdit_2.setFont(font)
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.lineEdit_2.setEchoMode(QtWidgets.QLineEdit.Password)
        self.lineEdit_2.setStyleSheet("color: White")

        #Username Text properties
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(90, 160, 121, 41))
        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.setObjectName("label_2")
        self.label_2.setStyleSheet("color: rgb(255, 255, 255);")

        #Passowrd Text Properties
        self.label_3 = QtWidgets.QLabel(self.centralwidget)
        self.label_3.setGeometry(QtCore.QRect(90, 240, 120, 41))
        font = QtGui.QFont()
        font.setFamily("Times New Roman")
        font.setPointSize(14)
        font.setBold(True)
        font.setWeight(75)
        self.label_3.setFont(font)
        self.label_3.setObjectName("label_3")
        self.label_3.setStyleSheet("color: White")

        #Login Button Properties
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(170, 320, 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.setStyleSheet("background-image:url(../Images/Login_Button.png);")
        self.pushButton.setObjectName("pushButton")
        self.pushButton.clicked.connect(self.logincheck)

        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", "Multiple Face Detection"))
        self.label.setText(_translate("MainWindow_Login", " Multiple Face Detection "))
        self.label_2.setText(_translate("MainWindow_Login", " UserName "))
        self.label_3.setText(_translate("MainWindow_Login", " Password"))


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


  • Next we need to develop the Home page for selecting the type of algorithm to use, below is the code
    from PyQt5 import QtCore, QtGui, QtWidgets
import sys
from Code.Haar_AdaBoost import haarBoost
from Code.LBP_AdaBoost import lbpBoost
from Code.Neural_Network import face_detect_GFNN
from Code.Graph import barChart,lineChart


class Ui_MainWindow_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_()

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

    Selected = 0
    process = ""
    #Choosing Haar_Adaboost Process
    def Haar_AdaBoost(self, select):
        if select:
            Ui_MainWindow_Home.process = "Haar-AdaBoost"
            print("process "+Ui_MainWindow_Home.process)

    #Choosing LBP_Adaboost Process
    def LBP_AdaBoost(self, select):
        if select:
            Ui_MainWindow_Home.process = "LBP-AdaBoost"
            print("process " + Ui_MainWindow_Home.process)

    #Choosing Neral_network Process
    def Neural_Network(self, select):
        if select:
            Ui_MainWindow_Home.process = "Neural-Network"
            print("process " + Ui_MainWindow_Home.process)

    #Detection Process
    def face_detect(self):
        try:
            img = self.lineEdit.text()
            alg = Ui_MainWindow_Home.process

            if img == "" and alg == "":
                self.showMessageBox("Details Empty"," Please upload the \"image\" and select the \"method\" then click on detect")

            else:
                if img == "":
                    self.showMessageBox(" Message ", " Please upload the \"image\" then click on detect")
                else:
                    if (alg == "Haar-AdaBoost"):
                        self.hfaces=0
                        self.ht=0
                        faces, dt = haarBoost(img)
                        self.hfaces = faces
                        self.ht = dt
                        Ui_MainWindow_Home.Selected = Ui_MainWindow_Home.Selected + 1
                    elif (alg == "LBP-AdaBoost"):
                        self.lfaces =0
                        self.lt=0
                        faces, dt = lbpBoost(img)
                        self.lfaces = faces
                        self.lt = dt
                        Ui_MainWindow_Home.Selected = Ui_MainWindow_Home.Selected + 1
                    elif (alg == "Neural-Network"):
                        self.nfaces=0
                        self.nt=0
                        faces, dt = face_detect_GFNN(img)
                        self.nfaces = faces
                        self.nt = dt
                        Ui_MainWindow_Home.Selected = Ui_MainWindow_Home.Selected + 1
                    else:
                        self.showMessageBox(" Invalid ", " Please Choose the method")

            if (int(self.hfaces)>0) and (int(self.lfaces)>0 and int(self.nfaces)>0):
                self.pushButton_3.show()

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

    #Taking Number Faces identified vlues for each
    def barlist(self):
        barlist = []
        barlist.clear()
        barlist.append(int(self.hfaces))
        barlist.append(int(self.lfaces))
        barlist.append(int(self.nfaces))
        barChart(barlist)

    #Taking Time taken details for the each
    def htlist1(self):
        htlist = []
        htlist.clear()
        htlist.append(float(self.ht))
        htlist.append(float(self.lt))
        htlist.append(float(self.nt))
        lineChart(htlist)

    #Plotting the graph
    def graph(self):
        self.barlist()
        self.htlist1()

    # Home pge UI Properties
    def setupUi(self, MainWindow_Home):
        MainWindow_Home.setObjectName("MainWindow_Home")
        MainWindow_Home.resize(1000, 666)
        MainWindow_Home.setStyleSheet("background-image: url(../Images/Home_Bg_Image.jpg);")
        MainWindow_Home.setWindowIcon(QtGui.QIcon('../Images/Home.png'))
        self.centralwidget = QtWidgets.QWidget(MainWindow_Home)
        self.centralwidget.setStyleSheet("")
        self.centralwidget.setObjectName("centralwidget")

        #Title Properties
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(130, 60, 750, 51))
        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.label.setFont(font)
        self.label.setAlignment(QtCore.Qt.AlignJustify|QtCore.Qt.AlignVCenter)
        self.label.setObjectName("label")
        self.label.setStyleSheet("color : white")

        #User Message Properties
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(160, 160, 660, 50))
        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.setObjectName("label_2")
        self.label_2.setStyleSheet("color : white")

        #Choosing Image entry Field properties
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(190, 230, 500, 50))
        font = QtGui.QFont()
        font.setFamily("Times New Roman")
        font.setPointSize(9)
        font.setBold(True)
        font.setWeight(75)
        self.lineEdit.setFont(font)
        self.lineEdit.setReadOnly(True)
        self.lineEdit.setObjectName("lineEdit")
        self.lineEdit.setStyleSheet("color: rgb(255, 255, 255);")

        #Detect button properties
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(180, 450, 201, 40))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton.setFont(font)
        self.pushButton.setStyleSheet("color : white")
        self.pushButton.setObjectName("pushButton")
        #self.pushButton.setStyleSheet("background-image:url(../Images/detect.jpg);")
        self.pushButton.clicked.connect(self.face_detect)

        #Upload Image Button Properties
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(690, 230, 120, 50))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_2.setFont(font)
        self.pushButton_2.setObjectName("pushButton_2")
        self.pushButton_2.clicked.connect(self.Upload_Image)
        self.pushButton_2.setStyleSheet("background-image:url(../Images/Upload_Image.jpg);")

        #Haar Adaboost Radio Button Properties
        self.radioButton = QtWidgets.QRadioButton(self.centralwidget)
        self.radioButton.setGeometry(QtCore.QRect(90, 320, 240, 34))
        font = QtGui.QFont()
        font.setFamily("Times New Roman")
        font.setPointSize(16)
        font.setBold(True)
        font.setWeight(75)
        self.radioButton.setFont(font)
        self.radioButton.setObjectName("radioButton")
        self.radioButton.toggled.connect(self.Haar_AdaBoost)
        self.radioButton.setStyleSheet("color : white")

        #Neural Network Radio Button Properties
        self.radioButton_1 = QtWidgets.QRadioButton(self.centralwidget)
        self.radioButton_1.setGeometry(QtCore.QRect(630, 320, 220, 34))
        self.radioButton_1.setFont(font)
        self.radioButton_1.setObjectName("radioButton_4")
        self.radioButton_1.toggled.connect(self.Neural_Network)
        self.radioButton_1.setStyleSheet("color : white")

        #LBP Adaboost Radio Button Properties
        self.radioButton_2 = QtWidgets.QRadioButton(self.centralwidget)
        self.radioButton_2.setGeometry(QtCore.QRect(370, 320, 220, 34))
        self.radioButton_2.setFont(font)
        self.radioButton_2.setObjectName("radioButton_5")   
        self.radioButton_2.toggled.connect(self.LBP_AdaBoost)
        self.radioButton_2.setStyleSheet("color : white")

        #Compare Button Properties
        self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_3.setGeometry(QtCore.QRect(550, 450, 201, 40))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_3.setFont(font)
        self.pushButton_3.setObjectName("pushButton_3")
        self.pushButton_3.hide()
        self.pushButton_3.clicked.connect(self.graph)
        self.pushButton_3.setStyleSheet("color : white")

        #Logout Button Properties
        self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_4.setGeometry(QtCore.QRect(820, 610, 160, 40))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton_4.setFont(font)
        self.pushButton_4.setObjectName("pushButton_4")
        self.pushButton_4.setStyleSheet("color : white")
        self.pushButton_4.setStyleSheet("background-image:url(../Images/Logout_Button.jpg);")
        self.pushButton_4.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", "Multiple Face Detection "))
        self.label.setText(_translate("MainWindow_Home", "Welcome to Multiple Face Detection "))
        self.label_2.setText(_translate("MainWindow_Home", " Upload Image below, select the method and then click on Detect"))
        self.pushButton.setText(_translate("MainWindow_Home", "Detect"))
        #self.pushButton_2.setText(_translate("MainWindow_Home", "Upload"))

        self.radioButton.setText(_translate("MainWindow_Home", "HAAR - AdaBoost"))
        self.radioButton_1.setText(_translate("MainWindow_Home", "Neural Network"))
        self.radioButton_2.setText(_translate("MainWindow_Home", "LBP - AdaBoost"))

        self.pushButton_3.setText(_translate("MainWindow_Home", "Compare"))
        #self.pushButton_4.setText(_translate("MainWindow_Home", "LogOut"))


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


  • Here we are using Three methods, Haar adaboost, LBP Adaboost and Neural Networks
  • We need to write the codes for all three
Haar adaboost.py
import numpy as np
import cv2

import time


def detect_faces(f_cascade, colored_img, scaleFactor=1.1):
    img_copy = np.copy(colored_img)
    # convert the test image to gray image as opencv face detector expects gray images
    gray = cv2.cvtColor(img_copy, cv2.COLOR_BGR2GRAY)

    #t1 = time.time()
    # let's detect multiscale (some images may be closer to camera than others) images
    faces = f_cascade.detectMultiScale(gray, scaleFactor=scaleFactor);
   # t2 = time.time()
    #dt1 = t2 - t1
    #print("dt=",dt1)
    # print the number of faces found
    print("Haar_AdaBoost")
    print('Faces found: ', len(faces))

    # go over list of faces and draw them as rectangles on original colored img
    for (x, y, w, h) in faces:
        cv2.rectangle(img_copy, (x, y), (x + w, y + h), (0, 255, 0), 2)

    return img_copy,len(faces)


def haarBoost(img):
    image = cv2.imread(img)
    haar_face_cascade = cv2.CascadeClassifier('../Cascade_Files/haarcascade_frontalface_alt.xml')
    t1 = time.time()
    # call our function to detect faces
    faces_detected_img,number_of_faces= detect_faces(haar_face_cascade, image)
    t2 = time.time()
    dt1 = t2 - t1
    print("Detection Time:",dt1)
    print("-----------------------------------------")
    # conver image to RGB and show image
   # plt.imshow()
    cv2.imshow('Haar AdaBoost', faces_detected_img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    return number_of_faces,dt1



#haarBoost('face3.jpg')


LBP_Adaboost.py

import numpy as np
import cv2

import time


def detect_faces(f_cascade, colored_img, scaleFactor=1.1):
    img_copy = np.copy(colored_img)
    # convert the test image to gray image as opencv face detector expects gray images
    gray = cv2.cvtColor(img_copy, cv2.COLOR_BGR2GRAY)
   # t1 = time.time()
    # let's detect multiscale (some images may be closer to camera than others) images
    faces = f_cascade.detectMultiScale(gray, scaleFactor=scaleFactor);
    #t2 = time.time()
    #dt1 = t2 - t1
    ##print("dt2=", dt1)
    # print the number of faces found
    print("LBP AdaBoost")
    print('Faces found: ', len(faces))

    # go over list of faces and draw them as rectangles on original colored img
    for (x, y, w, h) in faces:
        cv2.rectangle(img_copy, (x, y), (x + w, y + h), (0, 255, 0), 2)

    return img_copy,len(faces)


def lbpBoost(img):
    image = cv2.imread(img)
    lbp_face_cascade=cv2.CascadeClassifier('../Cascade_Files/lbpcascade_frontalface.xml')
    t1 = time.time()
    # call our function to detect faces
    faces_detected_img,number_of_faces = detect_faces(lbp_face_cascade, image)
    t2 = time.time()
    dt1 = t2 - t1
    print("Detecting time: ",dt1)
    print("-----------------------------------------")
    # conver image to RGB and show image
   # plt.imshow()
    cv2.imshow('LBP AdaBoost', faces_detected_img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    return number_of_faces, dt1

#lbpBoost('face3.jpg')

Neural Network.py

import  face_recognition
import cv2
from PIL import Image
import time

def face_detect_GFNN(img):
    inputimage = cv2.imread(img)
    image = face_recognition.load_image_file(img)
    t1 = time.time()
    face_locations = face_recognition.face_locations(image)
    t2 = time.time()
    dt1 = t2 - t1
    number_of_faces=format(len(face_locations))

    print("Neural Network")
    print("found {} faces.".format(len(face_locations)))
    print("Detection time=",dt1)
    # i=0
    for face_location in face_locations:
        top, right, bottom, left = face_location
        face_image = image[top:bottom, left:right]
        cv2.rectangle(inputimage, (left, top), (right, bottom), (0, 0, 255), 2)

    cv2.imshow('GF_NN', inputimage)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    return number_of_faces, dt1

#face_detect_GFNN('face4.jpg')
 

  • For developing the graph based on result captured we need to write the code using Python
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt1
import sys

def barChart(rlist):
    height=rlist
    bars = ('Haar_Adaoost', 'LBP_AdaBoost', 'Neural_Network')
    y_pos = np.arange(len(bars))
    plt.bar(y_pos, height, color=['red', 'green', 'blue'])
    plt.xticks(y_pos, bars)
    plt.xlabel('Algorithms')
    plt.ylabel('Number of Faces')
    plt.title('Prediction Accuracy Analysis')
    plt.show()


def lineChart(list):
    try:
        alg = ['Haar_Adaoost', 'LBP_AdaBoost', 'Neural_Network']
        plt1.plot(alg, list, color='red')
        plt1.xlabel('Algorithms')
        plt1.ylabel('Seconds')
        plt1.title('Prediction Time Analysis')
        plt1.show()


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

#barChart()
#lineChart()