# Form implementation generated from reading ui file 'pyqt.ui'
#
# Created by: PyQt6 UI code generator 6.0.3
#
# WARNING: Any manual changes made to this file will be lost when pyuic6 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5 import Qt
import cv2
from PyQt5.Qt import QImage, QPixmap, QTimer
from PyQt5.QtWidgets import QFileDialog
from PyQt5.QtCore import *
import numpy as np
import keras
import keras.backend as k
import keras.applications.inception_resnet_v2
from keras.preprocessing import image
import cv2
import datetime


# Font-----------------------------------------------------

class Ui_MainWindow(QtWidgets.QMainWindow):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1300, 750)  # +400, +250
        self.timer_camera = QtCore.QTimer()
        self.timer_camera2 = QtCore.QTimer()
        self.cap = cv2.VideoCapture()
        self.vidcap = cv2.VideoCapture()
        self.CAM_NUM = 0
        self.slot_init()
        self.model = keras.models.load_model('FMD.h5')
        self.face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
        self.is_Detecting = False

        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.centralwidget.setStyleSheet("background-color: 'white smoke';\n")
        self.horizontalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
        self.horizontalLayoutWidget.setGeometry(QtCore.QRect(900, 0, 400, 75))
        self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
        self.horizontalLayoutWidget.setStyleSheet("background-color: 'white';\n")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
        self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.about_btn = QtWidgets.QPushButton(self.horizontalLayoutWidget,
                                               clicked=lambda: self.about_us_btn_clicked(self))
        self.about_btn.setMaximumSize(QtCore.QSize(80, 20))
        self.about_btn.setStyleSheet("QPushButton{\n"
                                     "border: 100;\n"
                                     "background-color: rgb(255,69,0);\n"
                                     "color: 'white';\n"
                                     "font-family: 'Calibri';\n"
                                     "border-right: 1px solid rgb(255, 217, 198);\n"
                                     "border-bottom: 1px solid rgb(255, 217, 198);\n"
                                     "}\n"
                                     "QPushButton:hover{\n"
                                     "background-color: rgb(13, 152, 186);\n"
                                     "border-right: 1px solid rgb(255, 217, 198);\n"
                                     "border-bottom: 1px solid rgb(255, 217, 198);\n"
                                     "}\n"
                                     "")
        self.about_btn.setObjectName("about_btn")
        self.horizontalLayout.addWidget(self.about_btn)
        self.abstract_btn = QtWidgets.QPushButton(self.horizontalLayoutWidget,
                                                  clicked=lambda: self.abstract_btn_clicked(self))
        self.abstract_btn.setMaximumSize(QtCore.QSize(80, 20))
        self.abstract_btn.setStyleSheet("QPushButton{\n"
                                        "border: none;\n"
                                        "background-color: rgb(255,69,0);\n"
                                        "color: 'white';\n"
                                        "font-family: 'Calibri';\n"
                                        "border-right: 1px solid rgb(255, 217, 198);\n"
                                        "border-bottom: 1px solid rgb(255, 217, 198);\n"
                                        "}\n"
                                        "QPushButton:hover{\n"
                                        "background-color: rgb(13, 152, 186);\n"
                                        "border-right: 1px solid rgb(255, 217, 198);\n"
                                        "border-bottom: 1px solid rgb(255, 217, 198);\n"
                                        "}")
        self.abstract_btn.setObjectName("abstract_btn")
        self.horizontalLayout.addWidget(self.abstract_btn)
        self.whyMask_btn = QtWidgets.QPushButton(self.horizontalLayoutWidget,
                                                 clicked=lambda: self.why_mask_clicked(self))
        self.whyMask_btn.setMaximumSize(QtCore.QSize(80, 20))
        self.whyMask_btn.setStyleSheet("QPushButton{\n"
                                       "border: none;\n"
                                       "background-color: rgb(255,69,0);\n"
                                       "color: 'white';\n"
                                       "font-family: 'Calibri';\n"
                                       "border-right: 1px solid rgb(255, 217, 198);\n"
                                       "border-bottom: 1px solid rgb(255, 217, 198);\n"
                                       "}\n"
                                       "QPushButton:hover{\n"
                                       "background-color: rgb(13, 152, 186);\n"
                                       "border-right: 1px solid rgb(255, 217, 198);\n"
                                       "border-bottom: 1px solid rgb(255, 217, 198);\n"
                                       "}")
        self.whyMask_btn.setObjectName("whyMask_btn")
        self.horizontalLayout.addWidget(self.whyMask_btn)

        self.detection_btn = QtWidgets.QPushButton(self.centralwidget,
                                                   clicked=lambda: self.start_detect_btn_clicked(self))
        self.detection_btn.setGeometry(QtCore.QRect(520, 600, 250, 51))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.detection_btn.setFont(font)
        self.detection_btn.setStyleSheet("QPushButton{\n"
                                         "border: none;\n"
                                         "background-color: rgb(32,178,170);\n"
                                         "color: 'white';\n"
                                         "font-family: 'Calibri';\n"
                                         "}\n"
                                         "QPushButton:hover{\n"
                                         "background-color: 'orange';\n"
                                         "}\n"
                                         "")
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("picture/button/check.png"), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.Off)
        self.detection_btn.setIcon(icon)
        self.detection_btn.setIconSize(QtCore.QSize(32, 32))
        self.detection_btn.setObjectName("detection_btn")

        self.cancel_btn = QtWidgets.QPushButton(self.centralwidget, clicked=lambda: self.cancel_btn_clicked(self))
        self.cancel_btn.setGeometry(QtCore.QRect(950, 600, 250, 51))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.cancel_btn.setFont(font)
        self.cancel_btn.setStyleSheet("QPushButton{\n"
                                      "border: none;\n"
                                      "background-color: rgb(32,178,170);\n"
                                      "color: 'white';\n"
                                      "font-family: 'Calibri';\n"
                                      "}\n"
                                      "QPushButton:hover{\n"
                                      "background-color: 'orange';\n"
                                      "}")
        icon1 = QtGui.QIcon()
        icon1.addPixmap(QtGui.QPixmap("picture/button/close.png"), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.Off)
        self.cancel_btn.setIcon(icon1)
        self.cancel_btn.setIconSize(QtCore.QSize(32, 32))
        self.cancel_btn.setObjectName("cancel_btn")
        self.line = QtWidgets.QFrame(self.centralwidget)
        self.line.setGeometry(QtCore.QRect(0, 60, 1500, 16))
        self.line.setFrameShape(QtWidgets.QFrame.Shape.HLine)
        self.line.setFrameShadow(QtWidgets.QFrame.Shadow.Sunken)
        self.line.setObjectName("line")
        self.line_2 = QtWidgets.QFrame(self.centralwidget)
        self.line_2.setGeometry(QtCore.QRect(410, 70, 20, 800))
        self.line_2.setFrameShape(QtWidgets.QFrame.Shape.VLine)
        self.line_2.setFrameShadow(QtWidgets.QFrame.Shadow.Sunken)
        self.line_2.setObjectName("line_2")
        self.Face_Mask_Detection = QtWidgets.QLabel(self.centralwidget)
        self.Face_Mask_Detection.setGeometry(QtCore.QRect(0, 0, 940, 65))
        self.Face_Mask_Detection.setStyleSheet("QLabel { background-color : white; color : green; }")
        font = QtGui.QFont()
        font.setPointSize(20)
        font.setFamily('Calibri')
        self.Face_Mask_Detection.setFont(font)
        self.Face_Mask_Detection.setObjectName("Face_Mask_Detection")
        self.preview_window = QtWidgets.QFrame(self.centralwidget)
        self.preview_window.setGeometry(QtCore.QRect(10, 79, 390, 390))
        self.preview_window.setFrameShape(QtWidgets.QFrame.Shape.StyledPanel)
        self.preview_window.setFrameShadow(QtWidgets.QFrame.Shadow.Plain)
        self.preview_window.setLineWidth(7)
        self.preview_window.setStyleSheet("QLabel { background-color : white; color : green; font-family: 'Calibri';}")
        self.preview_window.setObjectName("preview_window")
        self.preview_label = QtWidgets.QLabel(self.preview_window)
        self.preview_label.setAlignment(Qt.AlignCenter)
        self.preview_label.setGeometry(QtCore.QRect(3, 5, 385, 380))

        font = QtGui.QFont()
        font.setPointSize(12)
        self.preview_label.setFont(font)
        self.preview_label.setObjectName("preview_label")
        self.detect_window = QtWidgets.QFrame(self.centralwidget)
        self.detect_window.setGeometry(QtCore.QRect(450, 80, 820, 480))
        self.detect_window.setFrameShape(QtWidgets.QFrame.Shape.StyledPanel)
        self.detect_window.setFrameShadow(QtWidgets.QFrame.Shadow.Plain)
        self.detect_window.setLineWidth(7)
        self.detect_window.setObjectName("detect_window")
        self.detect_window.setStyleSheet(
            "QLabel { background-color : black; color : lightseagreen ; font-family: 'Calibri'; }")
        self.detect_label = QtWidgets.QLabel(self.detect_window)
        self.detect_label.setAlignment(Qt.AlignCenter)
        self.detect_label.setGeometry(QtCore.QRect(3, 2, 815, 475))
        font = QtGui.QFont()
        font.setPointSize(12)
        self.detect_label.setFont(font)
        self.detect_label.setObjectName("detect_label")
        self.result_frame = QtWidgets.QFrame(self.centralwidget)
        self.result_frame.setGeometry(QtCore.QRect(450, 670, 820, 60))
        self.result_frame.setFrameShape(QtWidgets.QFrame.Shape.StyledPanel)
        self.result_frame.setFrameShadow(QtWidgets.QFrame.Shadow.Raised)
        self.result_frame.setObjectName("result_frame")
        self.label = QtWidgets.QLabel(self.result_frame)
        self.label.setGeometry(QtCore.QRect(3, 2, 815, 55))
        self.label.setObjectName("label")
        self.label.setStyleSheet(
            "QLabel { background-color : white ; color : darkgreen ; font-family: 'Calibri'; }")
        self.label.setAlignment(Qt.AlignCenter)
        self.buttom_frame = QtWidgets.QFrame(self.centralwidget)
        self.buttom_frame.setGeometry(QtCore.QRect(35, 500, 350, 200))
        self.buttom_frame.setFrameShape(QtWidgets.QFrame.Shape.StyledPanel)
        self.buttom_frame.setFrameShadow(QtWidgets.QFrame.Shadow.Raised)
        self.buttom_frame.setObjectName("buttom_frame")
        self.information_btn = QtWidgets.QPushButton(self.buttom_frame, clicked=lambda: self.info_btn_clicked(self))
        self.information_btn.setGeometry(QtCore.QRect(180, 120, 140, 51))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.information_btn.setFont(font)
        self.information_btn.setStyleSheet("QPushButton{\n"
                                           "border: none;\n"
                                           "background-color: 'green';\n"
                                           "color: 'white';\n"
                                           "font-family: 'Calibri';\n"
                                           "}\n"
                                           "QPushButton:hover{\n"
                                           "background-color: rgb(196, 255, 203);\n"
                                           "}\n"
                                           "")
        icon2 = QtGui.QIcon()
        icon2.addPixmap(QtGui.QPixmap("picture/button/pngaaa.com-828917.png"), QtGui.QIcon.Mode.Normal,
                        QtGui.QIcon.State.Off)
        self.information_btn.setIcon(icon2)
        self.information_btn.setIconSize(QtCore.QSize(32, 32))
        self.information_btn.setObjectName("information_btn")
        self.video_btn = QtWidgets.QPushButton(self.buttom_frame, clicked=lambda: self.open_video(self))
        self.video_btn.setGeometry(QtCore.QRect(5, 120, 140, 51))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.video_btn.setFont(font)
        self.video_btn.setStyleSheet("QPushButton{\n"
                                     "border: none;\n"
                                     "background-color: 'gold';\n"
                                     "font-family: 'Calibri';\n"
                                     "}\n"
                                     "QPushButton:hover{\n"
                                     "background-color: rgb(196, 255, 203);\n"
                                     "}\n"
                                     "")
        icon3 = QtGui.QIcon()
        icon3.addPixmap(QtGui.QPixmap("picture/button/playIcon.png"), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.Off)
        self.video_btn.setIcon(icon3)
        self.video_btn.setIconSize(QtCore.QSize(32, 32))
        self.video_btn.setObjectName("video_btn")
        self.camera_btn = QtWidgets.QPushButton(self.buttom_frame,
                                                clicked=lambda: self.button_open_camera_clicked(self))
        self.camera_btn.setGeometry(QtCore.QRect(5, 40, 140, 51))
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Policy.Minimum, QtWidgets.QSizePolicy.Policy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.camera_btn.sizePolicy().hasHeightForWidth())
        self.camera_btn.setSizePolicy(sizePolicy)
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setBold(False)
        font.setWeight(50)
        font.setKerning(True)
        font.setStyleStrategy(QtGui.QFont.StyleStrategy.PreferAntialias)
        self.camera_btn.setFont(font)
        self.camera_btn.setStyleSheet("QPushButton{\n"
                                      "border: none;\n"
                                      "background-color: 'gold';\n"
                                      "font-family: 'Calibri';\n"
                                      "}\n"
                                      "QPushButton:hover{\n"
                                      "background-color: rgb(196, 255, 203);\n"
                                      "}\n"
                                      "")
        icon4 = QtGui.QIcon()
        icon4.addPixmap(QtGui.QPixmap("picture/button/camera-131965017355314519_32.png"), QtGui.QIcon.Mode.Normal,
                        QtGui.QIcon.State.Off)
        self.camera_btn.setIcon(icon4)
        self.camera_btn.setIconSize(QtCore.QSize(32, 32))
        self.camera_btn.setObjectName("camera_btn")
        self.image_btn = QtWidgets.QPushButton(self.buttom_frame, clicked=lambda: self.open_image(self))
        self.image_btn.setGeometry(QtCore.QRect(180, 40, 140, 51))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.image_btn.setFont(font)
        self.image_btn.setStyleSheet("QPushButton{\n"
                                     "border: none;\n"
                                     "background-color: 'gold';\n"
                                     "font-family: 'Calibri';\n"
                                     "}\n"
                                     "QPushButton:hover{\n"
                                     "background-color: rgb(196, 255, 203);\n"
                                     "}\n"
                                     "\n"
                                     "")
        icon5 = QtGui.QIcon()
        icon5.addPixmap(QtGui.QPixmap("picture/button/imageIcon.png"), QtGui.QIcon.Mode.Normal, QtGui.QIcon.State.Off)
        self.image_btn.setIcon(icon5)
        self.image_btn.setIconSize(QtCore.QSize(32, 32))
        self.image_btn.setObjectName("image_btn")
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

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

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Face Mask Detection"))
        self.about_btn.setText(_translate("MainWindow", "About Us"))
        self.abstract_btn.setText(_translate("MainWindow", "Abstract"))
        self.whyMask_btn.setText(_translate("MainWindow", "Why Mask"))
        self.detection_btn.setText(_translate("MainWindow", "Start Detection"))
        self.cancel_btn.setText(_translate("MainWindow", "Cancel"))
        self.Face_Mask_Detection.setText(_translate("MainWindow", "    Face Mask Detection"))
        self.preview_label.setText(_translate("MainWindow", "Preview Window"))
        self.detect_label.setText(_translate("MainWindow", "Detection Display window"))
        self.label.setText(_translate("MainWindow", "Outcome Display window"))
        self.information_btn.setText(_translate("MainWindow", " Information"))
        self.video_btn.setText(_translate("MainWindow", "Video"))
        self.camera_btn.setText(_translate("MainWindow", "Camera"))
        self.image_btn.setText(_translate("MainWindow", "Image"))

    def abstract_btn_clicked(self, MainWindow):
        image = QtGui.QImage(QtGui.QImageReader("picture/abstract.png").read())
        pixmap_resized = image.smoothScaled(820, 480)
        self.detect_label.setPixmap(QtGui.QPixmap(pixmap_resized))

    def about_us_btn_clicked(self, MainWindow):
        image = QtGui.QImage(QtGui.QImageReader("picture/Aboutus.png").read())
        pixmap_resized = image.smoothScaled(820, 480)
        self.detect_label.setPixmap(QtGui.QPixmap(pixmap_resized))

    def why_mask_clicked(self, MainWindow):
        image = QtGui.QImage(QtGui.QImageReader("picture/whymask.png").read())
        pixmap_resized = image.smoothScaled(820, 480)
        self.detect_label.setPixmap(QtGui.QPixmap(pixmap_resized))

    def info_btn_clicked(self, MainWindow):
        image = QtGui.QImage(QtGui.QImageReader("picture/help.png").read())
        pixmap_resized = image.smoothScaled(820, 480)
        self.detect_label.setPixmap(QtGui.QPixmap(pixmap_resized))

    def start_detect_btn_clicked(self, MainWindow):
        self.is_Detecting = True

    def cancel_btn_clicked(self, MainWindow):
        self.is_Detecting = False

    def open_image(self, MainWindow):
        imgName, imgType = QFileDialog.getOpenFileName(self, 'Open Image', '', '*.jpg;;*.png;;All Files(*)')

        if self.is_Detecting == False and self.timer_camera2.isActive() == False:
            pic = QtGui.QPixmap(imgName).scaled(self.preview_label.width(), self.preview_label.height())
            self.preview_label.setPixmap(pic)
        elif self.is_Detecting == False and self.timer_camera2.isActive() == True:
            self.timer_camera2.stop()
            self.vidcap.release()
            self.preview_label.clear()
            pic = QtGui.QPixmap(imgName).scaled(self.preview_label.width(), self.preview_label.height())
            self.preview_label.setPixmap(pic)
        elif self.is_Detecting == True and self.timer_camera.isActive() == True:
            pic = QtGui.QPixmap(imgName).scaled(self.preview_label.width(), self.preview_label.height())
            self.preview_label.setPixmap(pic)
        elif self.is_Detecting == True and self.timer_camera2.isActive() == True:
            pic = QtGui.QPixmap(imgName).scaled(self.preview_label.width(), self.preview_label.height())
            self.preview_label.setPixmap(pic)
        else:
            pic = cv2.imread(imgName)
            show = cv2.resize(pic, (640, 480))
            show = cv2.cvtColor(show, cv2.COLOR_BGR2RGB)
            faces = self.face_cascade.detectMultiScale(show, scaleFactor=1.05, minNeighbors=10)
            withmasknum = 0
            withoutmasknum = 0
            for i in range(len(faces)):
                (x, y, w, h) = faces[i]
                crop = show[y:y + h, x:x + w]
                crop = cv2.resize(crop, (128, 128))
                crop = np.reshape(crop, [1, 128, 128, 3]) / 255.0
                mask_label = self.model.predict_classes(crop)
                if (mask_label == 0):  # print(mask_label)  0 :WithMask, 1 :'WithoutMask'
                    text = "WithMask"
                    cv2.rectangle(show, (x, y), (x + w, y + h), (23, 204, 66),
                                  1)  # the bounfding box color 255, 51, 63 red
                    cv2.putText(show, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (23, 204, 66), 1)  # font color
                    withmasknum += 1
                    self.label.setText(QtCore.QCoreApplication.translate("MainWindow", "faces have masks on"))
                elif (mask_label == 1):
                    text = "WithoutMask"
                    cv2.rectangle(show, (x, y), (x + w, y + h), (255, 51, 63),
                                  1)  # the bounfding box color 255, 51, 63 red
                    cv2.putText(show, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 51, 63), 1)
                    withoutmasknum += 1

            reslut_text = "Faces Detected = " + str(len(faces)) + ' ' + "WithMask = " + str(withmasknum) + ' ' + "WithoutMask = " + str(withoutmasknum)
            self.label.setText(QtCore.QCoreApplication.translate("MainWindow", reslut_text))
            showImage = QtGui.QImage(show.data, show.shape[1], show.shape[0], QtGui.QImage.Format_RGB888)
            self.detect_label.setPixmap(QtGui.QPixmap.fromImage(showImage))

    def open_video(self, MainWindow):
        vidName, vidType = QFileDialog.getOpenFileName(self, 'Open Video', '', '*.mp4;;All Files(*)')
        self.vidcap = cv2.VideoCapture(vidName)
        self.frameRate = self.vidcap.get(cv2.CAP_PROP_FPS)
        self.timer_camera2.start(self.frameRate)
        self.timer_camera2.timeout.connect(self.get_image_frame)

    def slot_init(self):
        self.timer_camera.timeout.connect(self.get_frame)

    def button_open_camera_clicked(self, MainWindow):
        if self.timer_camera.isActive() == False:
            flag = self.cap.open(self.CAM_NUM)
            if flag == False:
                msg = QtWidgets.QMessageBox.warning(self, 'warning', "check camera", buttons=QtWidgets.QMessageBox.Ok)
            else:
                self.timer_camera.start(30)
                self.camera_btn.setText(QtCore.QCoreApplication.translate("MainWindow", "Camera OFF"))

        else:
            self.timer_camera.stop()
            self.cap.release()
            self.detect_label.clear()
            self.camera_btn.setText(QtCore.QCoreApplication.translate("MainWindow", "Camera"))

    def get_frame(self):
        flag, self.image = self.cap.read()

        if self.is_Detecting == False:
            show = cv2.resize(self.image, (640, 480))
            show = cv2.cvtColor(show, cv2.COLOR_BGR2RGB)
            showImage = QtGui.QImage(show.data, show.shape[1], show.shape[0], QtGui.QImage.Format_RGB888)
            self.detect_label.setPixmap(QtGui.QPixmap.fromImage(showImage))
        else:
            show = cv2.resize(self.image, (640, 480))
            show = cv2.cvtColor(show, cv2.COLOR_BGR2RGB)
            faces = self.face_cascade.detectMultiScale(show, scaleFactor=1.05, minNeighbors=10)
            x = len(faces)
            withmasknum = 0
            withoutmasknum = 0
            for i in range(len(faces)):
                y = 0
                (x, y, w, h) = faces[i]
                crop = show[y:y + h, x:x + w]
                crop = cv2.resize(crop, (128, 128))
                crop = np.reshape(crop, [1, 128, 128, 3]) / 255.0
                mask_label = self.model.predict_classes(crop)
                if (mask_label == 0):  # print(mask_label)  0 :WithMask, 1 :'WithoutMask'
                    text = "WithMask"
                    cv2.rectangle(show, (x, y), (x + w, y + h), (23, 204, 66),
                                  1)  # the bounfding box color 255, 51, 63 red
                    cv2.putText(show, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (23, 204, 66), 1)  # font color
                    withmasknum += 1
                    # self.label.setText(QtCore.QCoreApplication.translate("MainWindow", "faces have masks on"))
                elif (mask_label == 1):
                    text = "WithoutMask"
                    cv2.rectangle(show, (x, y), (x + w, y + h), (255, 51, 63),
                                  1)  # the bounfding box color 255, 51, 63 red
                    cv2.putText(show, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 51, 63), 1)
                    withoutmasknum += 1
            reslut_text = "Faces Detected = " + str(len(faces)) + ' ' + "WithMask = " + str(withmasknum) + ' ' + "WithoutMask = " + str(withoutmasknum)
            self.label.setText(QtCore.QCoreApplication.translate("MainWindow", reslut_text))
            showImage = QtGui.QImage(show.data, show.shape[1], show.shape[0], QtGui.QImage.Format_RGB888)
            self.detect_label.setPixmap(QtGui.QPixmap.fromImage(showImage))

    def get_image_frame(self):
        flag, vidimage = self.vidcap.read()
        if flag != True:
            self.timer_camera2.stop()
            self.vidcap.release()
            self.preview_label.clear()
            self.detect_label.clear()
            return
        if self.is_Detecting == False:
            show = cv2.resize(vidimage, (380, 340))
            show = cv2.cvtColor(show, cv2.COLOR_BGR2RGB)
            showImage = QtGui.QImage(show.data, show.shape[1], show.shape[0], QtGui.QImage.Format_RGB888)
            self.preview_label.setPixmap(QtGui.QPixmap.fromImage(showImage))
        elif self.is_Detecting == True and self.timer_camera.isActive() == True:
            show = cv2.resize(vidimage, (380, 340))
            show = cv2.cvtColor(show, cv2.COLOR_BGR2RGB)
            showImage = QtGui.QImage(show.data, show.shape[1], show.shape[0], QtGui.QImage.Format_RGB888)
            self.preview_label.setPixmap(QtGui.QPixmap.fromImage(showImage))
        else:
            show = cv2.resize(vidimage, (640, 480))
            show = cv2.cvtColor(show, cv2.COLOR_BGR2RGB)
            faces = self.face_cascade.detectMultiScale(show, scaleFactor=1.05, minNeighbors=10)
            withmasknum = 0
            withoutmasknum = 0
            x = len(faces)
            for i in range(len(faces)):
                (x, y, w, h) = faces[i]
                crop = show[y:y + h, x:x + w]
                crop = cv2.resize(crop, (128, 128))
                crop = np.reshape(crop, [1, 128, 128, 3]) / 255.0
                mask_label = self.model.predict_classes(crop)
                if (mask_label == 0):  # print(mask_label)  0 :WithMask, 1 :'WithoutMask'
                    text = "WithMask"
                    cv2.rectangle(show, (x, y), (x + w, y + h), (23, 204, 66),
                                  1)  # the bounfding box color 255, 51, 63 red
                    cv2.putText(show, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (23, 204, 66), 1)  # font color
                    # self.label.setText(QtCore.QCoreApplication.translate("MainWindow", "faces have masks on"))
                elif (mask_label == 1):
                    text = "WithoutMask"
                    cv2.rectangle(show, (x, y), (x + w, y + h), (255, 51, 63),
                                  1)  # the bounfding box color 255, 51, 63 red
                    cv2.putText(show, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 51, 63), 1)
                    # self.label.setText(QtCore.QCoreApplication.translate("MainWindow", "faces have no masks on"))
                    withoutmasknum += 1
            reslut_text = "Faces Detected = " + str(len(faces)) + ' ' + "WithMask = " + str(withmasknum) + ' ' + "WithoutMask = " + str(withoutmasknum)
            showImage = QtGui.QImage(show.data, show.shape[1], show.shape[0], QtGui.QImage.Format_RGB888)
            self.label.setText(QtCore.QCoreApplication.translate("MainWindow", reslut_text))
            self.detect_label.setPixmap(QtGui.QPixmap.fromImage(showImage))


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec())
