from PyQt5 import QtCore, QtGui, QtWidgets
import landmarks
import datasets

class Triple:
    def __init__(self, img, nameText, rateText):
        self.img = img
        self.nameText = nameText
        self.rateText = rateText

############################

class Ui_MainWindow(object):
    def setupNN(self):
        [self.X_samples, self.X_paths] = datasets.getSamples("samples", pixel=32)
        self.model = datasets.load_model("model.h5")
        self.landmarks = landmarks.getLandmarks()
        self.index = -1
        self.uploadedPath = ""

    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(550, 700)
        MainWindow.setMinimumSize(QtCore.QSize(550, 700))
        MainWindow.setMaximumSize(QtCore.QSize(550, 700))
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.stackedWidget = QtWidgets.QStackedWidget(self.centralwidget)
        self.stackedWidget.setEnabled(True)
        self.stackedWidget.setGeometry(QtCore.QRect(0, 0, 550, 700))
        self.stackedWidget.setMinimumSize(QtCore.QSize(0, 0))
        self.stackedWidget.setFrameShape(QtWidgets.QFrame.NoFrame)
        self.stackedWidget.setLineWidth(1)
        self.stackedWidget.setObjectName("stackedWidget")
        self.main_page = QtWidgets.QWidget()
        self.main_page.setObjectName("main_page")
        self.push_start = QtWidgets.QPushButton(self.main_page)
        self.push_start.setGeometry(QtCore.QRect(290, 550, 231, 40))
        font = QtGui.QFont()
        font.setFamily("Bahnschrift SemiBold")
        font.setPointSize(9)
        font.setBold(False)
        font.setItalic(False)
        font.setWeight(50)
        self.push_start.setFont(font)
        self.push_start.setStyleSheet("background-color: rgb(0, 170, 0);\n"
"font: \"Bahnschrift SemiBold\";\n"
"color: rgb(255, 255, 255);")
        self.push_start.setObjectName("push_start")
        self.push_random = QtWidgets.QPushButton(self.main_page)
        self.push_random.setGeometry(QtCore.QRect(30, 550, 231, 40))
        font = QtGui.QFont()
        font.setFamily("Bahnschrift SemiBold")
        font.setPointSize(9)
        font.setBold(False)
        font.setItalic(False)
        font.setWeight(50)
        self.push_random.setFont(font)
        self.push_random.setStyleSheet("background-color: rgb(255, 85, 0);\n"
"font: \"Bahnschrift SemiBold\";\n"
"color: rgb(255, 255, 255);")
        self.push_random.setObjectName("push_random")
        self.line_5 = QtWidgets.QFrame(self.main_page)
        self.line_5.setGeometry(QtCore.QRect(20, 620, 530, 3))
        self.line_5.setFrameShape(QtWidgets.QFrame.HLine)
        self.line_5.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line_5.setObjectName("line_5")
        self.logo = QtWidgets.QLabel(self.main_page)
        self.logo.setGeometry(QtCore.QRect(105, 10, 341, 201))
        self.logo.setText("")
        self.logo.setPixmap(QtGui.QPixmap("images/logo.png"))
        self.logo.setScaledContents(True)
        self.logo.setObjectName("logo")
        self.footer = QtWidgets.QLabel(self.main_page)
        self.footer.setGeometry(QtCore.QRect(25, 640, 500, 41))
        font = QtGui.QFont()
        font.setFamily("Cambria")
        font.setPointSize(11)
        font.setBold(False)
        font.setWeight(50)
        self.footer.setFont(font)
        self.footer.setStyleSheet("background-color: rgb(191, 191, 191);\n"
"color: white;")
        self.footer.setAlignment(QtCore.Qt.AlignCenter)
        self.footer.setObjectName("footer")
        self.push_image = QtWidgets.QPushButton(self.main_page)
        self.push_image.setGeometry(QtCore.QRect(30, 240, 491, 270))
        font = QtGui.QFont()
        font.setFamily("MS Shell Dlg 2")
        font.setPointSize(12)
        self.push_image.setFont(font)
        self.push_image.setStyleSheet("background-color: rgb(191, 191, 191);\n"
"color: rgb(255, 255, 255);")
        self.push_image.setObjectName("push_image")
        self.stackedWidget.addWidget(self.main_page)
        self.result_page = QtWidgets.QWidget()
        self.result_page.setObjectName("result_page")
        self.verticalLayoutWidget_2 = QtWidgets.QWidget(self.result_page)
        self.verticalLayoutWidget_2.setGeometry(QtCore.QRect(20, 140, 511, 501))
        self.verticalLayoutWidget_2.setObjectName("verticalLayoutWidget_2")
        self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.verticalLayoutWidget_2)
        self.verticalLayout_3.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout_3.setObjectName("verticalLayout_3")
        self.gridLayout = QtWidgets.QGridLayout()
        self.gridLayout.setHorizontalSpacing(20)
        self.gridLayout.setVerticalSpacing(7)
        self.gridLayout.setObjectName("gridLayout")
        self.verticalLayout_2 = QtWidgets.QVBoxLayout()
        self.verticalLayout_2.setSpacing(7)
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.name1 = QtWidgets.QLabel(self.verticalLayoutWidget_2)
        font = QtGui.QFont()
        font.setFamily("Georgia")
        font.setPointSize(10)
        font.setBold(False)
        font.setItalic(False)
        font.setUnderline(False)
        font.setWeight(50)
        font.setStrikeOut(False)
        font.setKerning(True)
        self.name1.setFont(font)
        self.name1.setStyleSheet("background-color: rgba(184, 184, 184, 100);\n"
"color: black;")
        self.name1.setAlignment(QtCore.Qt.AlignCenter)
        self.name1.setObjectName("name1")
        self.verticalLayout_2.addWidget(self.name1)
        self.rate1 = QtWidgets.QLabel(self.verticalLayoutWidget_2)
        font = QtGui.QFont()
        font.setFamily("Georgia")
        font.setPointSize(10)
        font.setBold(False)
        font.setItalic(False)
        font.setUnderline(False)
        font.setWeight(50)
        font.setStrikeOut(False)
        font.setKerning(True)
        self.rate1.setFont(font)
        self.rate1.setStyleSheet("background-color: rgba(184, 184, 184, 100);\n"
"color: black;")
        self.rate1.setAlignment(QtCore.Qt.AlignCenter)
        self.rate1.setObjectName("rate1")
        self.verticalLayout_2.addWidget(self.rate1)
        self.gridLayout.addLayout(self.verticalLayout_2, 0, 1, 1, 1)
        self.img1 = QtWidgets.QLabel(self.verticalLayoutWidget_2)
        self.img1.setStyleSheet("background-color: rgb(190, 190, 190);")
        self.img1.setText("")
        self.img1.setObjectName("img1")
        self.gridLayout.addWidget(self.img1, 0, 0, 1, 1)
        self.gridLayout.setColumnStretch(0, 2)
        self.gridLayout.setColumnStretch(1, 5)
        self.verticalLayout_3.addLayout(self.gridLayout)
        self.line = QtWidgets.QFrame(self.verticalLayoutWidget_2)
        self.line.setFrameShape(QtWidgets.QFrame.HLine)
        self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line.setObjectName("line")
        self.verticalLayout_3.addWidget(self.line)
        self.gridLayout_2 = QtWidgets.QGridLayout()
        self.gridLayout_2.setHorizontalSpacing(20)
        self.gridLayout_2.setVerticalSpacing(7)
        self.gridLayout_2.setObjectName("gridLayout_2")
        self.verticalLayout_4 = QtWidgets.QVBoxLayout()
        self.verticalLayout_4.setSpacing(7)
        self.verticalLayout_4.setObjectName("verticalLayout_4")
        self.name2 = QtWidgets.QLabel(self.verticalLayoutWidget_2)
        font = QtGui.QFont()
        font.setFamily("Georgia")
        font.setPointSize(10)
        font.setBold(False)
        font.setItalic(False)
        font.setUnderline(False)
        font.setWeight(50)
        font.setStrikeOut(False)
        font.setKerning(True)
        self.name2.setFont(font)
        self.name2.setStyleSheet("background-color: rgba(184, 184, 184, 100);\n"
"color: black;")
        self.name2.setAlignment(QtCore.Qt.AlignCenter)
        self.name2.setObjectName("name2")
        self.verticalLayout_4.addWidget(self.name2)
        self.rate2 = QtWidgets.QLabel(self.verticalLayoutWidget_2)
        font = QtGui.QFont()
        font.setFamily("Georgia")
        font.setPointSize(10)
        font.setBold(False)
        font.setItalic(False)
        font.setUnderline(False)
        font.setWeight(50)
        font.setStrikeOut(False)
        font.setKerning(True)
        self.rate2.setFont(font)
        self.rate2.setStyleSheet("background-color: rgba(184, 184, 184, 100);\n"
"color: black;")
        self.rate2.setAlignment(QtCore.Qt.AlignCenter)
        self.rate2.setObjectName("rate2")
        self.verticalLayout_4.addWidget(self.rate2)
        self.gridLayout_2.addLayout(self.verticalLayout_4, 0, 1, 1, 1)
        self.img2 = QtWidgets.QLabel(self.verticalLayoutWidget_2)
        self.img2.setStyleSheet("background-color: rgb(190, 190, 190);")
        self.img2.setText("")
        self.img2.setObjectName("img2")
        self.gridLayout_2.addWidget(self.img2, 0, 0, 1, 1)
        self.gridLayout_2.setColumnStretch(0, 2)
        self.gridLayout_2.setColumnStretch(1, 5)
        self.verticalLayout_3.addLayout(self.gridLayout_2)
        self.line_2 = QtWidgets.QFrame(self.verticalLayoutWidget_2)
        self.line_2.setFrameShape(QtWidgets.QFrame.HLine)
        self.line_2.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line_2.setObjectName("line_2")
        self.verticalLayout_3.addWidget(self.line_2)
        self.gridLayout_3 = QtWidgets.QGridLayout()
        self.gridLayout_3.setHorizontalSpacing(20)
        self.gridLayout_3.setVerticalSpacing(7)
        self.gridLayout_3.setObjectName("gridLayout_3")
        self.img3 = QtWidgets.QLabel(self.verticalLayoutWidget_2)
        self.img3.setStyleSheet("background-color: rgb(190, 190, 190);")
        self.img3.setText("")
        self.img3.setObjectName("img3")
        self.gridLayout_3.addWidget(self.img3, 1, 0, 1, 1)
        self.verticalLayout_5 = QtWidgets.QVBoxLayout()
        self.verticalLayout_5.setSpacing(7)
        self.verticalLayout_5.setObjectName("verticalLayout_5")
        self.name3 = QtWidgets.QLabel(self.verticalLayoutWidget_2)
        font = QtGui.QFont()
        font.setFamily("Georgia")
        font.setPointSize(10)
        font.setBold(False)
        font.setItalic(False)
        font.setUnderline(False)
        font.setWeight(50)
        font.setStrikeOut(False)
        font.setKerning(True)
        self.name3.setFont(font)
        self.name3.setStyleSheet("background-color: rgba(184, 184, 184, 100);\n"
"color: black;")
        self.name3.setAlignment(QtCore.Qt.AlignCenter)
        self.name3.setObjectName("name3")
        self.verticalLayout_5.addWidget(self.name3)
        self.rate3 = QtWidgets.QLabel(self.verticalLayoutWidget_2)
        font = QtGui.QFont()
        font.setFamily("Georgia")
        font.setPointSize(10)
        font.setBold(False)
        font.setItalic(False)
        font.setUnderline(False)
        font.setWeight(50)
        font.setStrikeOut(False)
        font.setKerning(True)
        self.rate3.setFont(font)
        self.rate3.setStyleSheet("background-color: rgba(184, 184, 184, 100);\n"
"color: black;")
        self.rate3.setAlignment(QtCore.Qt.AlignCenter)
        self.rate3.setObjectName("rate3")
        self.verticalLayout_5.addWidget(self.rate3)
        self.gridLayout_3.addLayout(self.verticalLayout_5, 1, 1, 1, 1)
        self.gridLayout_3.setColumnStretch(0, 2)
        self.gridLayout_3.setColumnStretch(1, 5)
        self.verticalLayout_3.addLayout(self.gridLayout_3)
        self.line_3 = QtWidgets.QFrame(self.verticalLayoutWidget_2)
        self.line_3.setFrameShape(QtWidgets.QFrame.HLine)
        self.line_3.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line_3.setObjectName("line_3")
        self.verticalLayout_3.addWidget(self.line_3)
        self.gridLayout_4 = QtWidgets.QGridLayout()
        self.gridLayout_4.setHorizontalSpacing(20)
        self.gridLayout_4.setVerticalSpacing(7)
        self.gridLayout_4.setObjectName("gridLayout_4")
        self.verticalLayout_6 = QtWidgets.QVBoxLayout()
        self.verticalLayout_6.setSpacing(7)
        self.verticalLayout_6.setObjectName("verticalLayout_6")
        self.name4 = QtWidgets.QLabel(self.verticalLayoutWidget_2)
        font = QtGui.QFont()
        font.setFamily("Georgia")
        font.setPointSize(10)
        font.setBold(False)
        font.setItalic(False)
        font.setUnderline(False)
        font.setWeight(50)
        font.setStrikeOut(False)
        font.setKerning(True)
        self.name4.setFont(font)
        self.name4.setStyleSheet("background-color: rgba(184, 184, 184, 100);\n"
"color: black;")
        self.name4.setAlignment(QtCore.Qt.AlignCenter)
        self.name4.setObjectName("name4")
        self.verticalLayout_6.addWidget(self.name4)
        self.rate4 = QtWidgets.QLabel(self.verticalLayoutWidget_2)
        font = QtGui.QFont()
        font.setFamily("Georgia")
        font.setPointSize(10)
        font.setBold(False)
        font.setItalic(False)
        font.setUnderline(False)
        font.setWeight(50)
        font.setStrikeOut(False)
        font.setKerning(True)
        self.rate4.setFont(font)
        self.rate4.setStyleSheet("background-color: rgba(184, 184, 184, 100);\n"
"color: black;")
        self.rate4.setAlignment(QtCore.Qt.AlignCenter)
        self.rate4.setObjectName("rate4")
        self.verticalLayout_6.addWidget(self.rate4)
        self.gridLayout_4.addLayout(self.verticalLayout_6, 0, 1, 1, 1)
        self.img4 = QtWidgets.QLabel(self.verticalLayoutWidget_2)
        self.img4.setStyleSheet("background-color: rgb(190, 190, 190);")
        self.img4.setText("")
        self.img4.setObjectName("img4")
        self.gridLayout_4.addWidget(self.img4, 0, 0, 1, 1)
        self.gridLayout_4.setColumnStretch(0, 2)
        self.gridLayout_4.setColumnStretch(1, 5)
        self.verticalLayout_3.addLayout(self.gridLayout_4)
        self.line_4 = QtWidgets.QFrame(self.verticalLayoutWidget_2)
        self.line_4.setFrameShape(QtWidgets.QFrame.HLine)
        self.line_4.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line_4.setObjectName("line_4")
        self.verticalLayout_3.addWidget(self.line_4)
        self.gridLayout_5 = QtWidgets.QGridLayout()
        self.gridLayout_5.setHorizontalSpacing(20)
        self.gridLayout_5.setVerticalSpacing(7)
        self.gridLayout_5.setObjectName("gridLayout_5")
        self.img5 = QtWidgets.QLabel(self.verticalLayoutWidget_2)
        self.img5.setStyleSheet("background-color: rgb(190, 190, 190);")
        self.img5.setText("")
        self.img5.setObjectName("img5")
        self.gridLayout_5.addWidget(self.img5, 0, 0, 1, 1)
        self.verticalLayout_7 = QtWidgets.QVBoxLayout()
        self.verticalLayout_7.setSpacing(7)
        self.verticalLayout_7.setObjectName("verticalLayout_7")
        self.name5 = QtWidgets.QLabel(self.verticalLayoutWidget_2)
        font = QtGui.QFont()
        font.setFamily("Georgia")
        font.setPointSize(10)
        font.setBold(False)
        font.setItalic(False)
        font.setUnderline(False)
        font.setWeight(50)
        font.setStrikeOut(False)
        font.setKerning(True)
        self.name5.setFont(font)
        self.name5.setStyleSheet("background-color: rgba(184, 184, 184, 100);\n"
"color: black;")
        self.name5.setAlignment(QtCore.Qt.AlignCenter)
        self.name5.setObjectName("name5")
        self.verticalLayout_7.addWidget(self.name5)
        self.rate5 = QtWidgets.QLabel(self.verticalLayoutWidget_2)
        font = QtGui.QFont()
        font.setFamily("Georgia")
        font.setPointSize(10)
        font.setBold(False)
        font.setItalic(False)
        font.setUnderline(False)
        font.setWeight(50)
        font.setStrikeOut(False)
        font.setKerning(True)
        self.rate5.setFont(font)
        self.rate5.setStyleSheet("background-color: rgba(184, 184, 184, 100);\n"
"color: black;")
        self.rate5.setAlignment(QtCore.Qt.AlignCenter)
        self.rate5.setObjectName("rate5")
        self.verticalLayout_7.addWidget(self.rate5)
        self.gridLayout_5.addLayout(self.verticalLayout_7, 0, 1, 1, 1)
        self.gridLayout_5.setColumnStretch(0, 2)
        self.gridLayout_5.setColumnStretch(1, 5)
        self.verticalLayout_3.addLayout(self.gridLayout_5)
        self.header = QtWidgets.QLabel(self.result_page)
        self.header.setGeometry(QtCore.QRect(20, 20, 331, 101))
        font = QtGui.QFont()
        font.setFamily("Georgia")
        font.setPointSize(11)
        font.setBold(False)
        font.setItalic(False)
        font.setUnderline(False)
        font.setWeight(50)
        self.header.setFont(font)
        self.header.setStyleSheet("background-color: rgb(255, 170, 0);\n"
"color: rgb(255, 255, 255);")
        self.header.setAlignment(QtCore.Qt.AlignCenter)
        self.header.setObjectName("header")
        self.push_back = QtWidgets.QPushButton(self.result_page)
        self.push_back.setGeometry(QtCore.QRect(20, 660, 511, 30))
        font = QtGui.QFont()
        font.setPointSize(9)
        self.push_back.setFont(font)
        self.push_back.setStyleSheet("background-color: rgb(255, 58, 58);\n"
"color: rgb(255, 255, 255);")
        self.push_back.setObjectName("push_back")
        self.orig_img = QtWidgets.QLabel(self.result_page)
        self.orig_img.setGeometry(QtCore.QRect(370, 20, 161, 101))
        self.orig_img.setStyleSheet("background-color: rgb(190, 190, 190);")
        self.orig_img.setText("")
        self.orig_img.setObjectName("orig_img")
        self.stackedWidget.addWidget(self.result_page)
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        self.stackedWidget.setCurrentIndex(1)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

        ########################################

        self.push_image.setStyleSheet("border-image: url('images/default.png')")

        self.img1.setScaledContents(True)
        self.img2.setScaledContents(True)
        self.img3.setScaledContents(True)
        self.img4.setScaledContents(True)
        self.img5.setScaledContents(True)
        self.orig_img.setScaledContents(True)

        self.triples = [Triple(self.img1, self.name1, self.rate1),
                        Triple(self.img2, self.name2, self.rate2),
                        Triple(self.img3, self.name3, self.rate3),
                        Triple(self.img4, self.name4, self.rate4),
                        Triple(self.img5, self.name5, self.rate5)]

        self.push_start.clicked.connect(self.start)
        self.push_back.clicked.connect(self.back)
        self.push_image.clicked.connect(self.uploadImage)
        self.push_random.clicked.connect(self.randomImage)

        self.stackedWidget.setCurrentIndex(0)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Landmark Recognition System"))
        self.push_start.setText(_translate("MainWindow", "START"))
        self.push_random.setText(_translate("MainWindow", "RANDOM"))
        self.footer.setText(_translate("MainWindow", "YTÜ - Graduation Project - Osman Araz & Muhammet Çeneli"))
        self.name1.setText(_translate("MainWindow", "AYASOFYA"))
        self.rate1.setText(_translate("MainWindow", "70 %"))
        self.name2.setText(_translate("MainWindow", "AYASOFYA"))
        self.rate2.setText(_translate("MainWindow", "70 %"))
        self.name3.setText(_translate("MainWindow", "AYASOFYA"))
        self.rate3.setText(_translate("MainWindow", "70 %"))
        self.name4.setText(_translate("MainWindow", "AYASOFYA"))
        self.rate4.setText(_translate("MainWindow", "70 %"))
        self.name5.setText(_translate("MainWindow", "AYASOFYA"))
        self.rate5.setText(_translate("MainWindow", "70 %"))
        self.header.setText(_translate("MainWindow", "PREDICTIONS FOR:"))
        self.push_back.setText(_translate("MainWindow", "BACK"))

    def start(self):
        if self.uploadedPath or self.index != -1:
            image = datasets.getImage(self.uploadedPath) if self.uploadedPath else self.X_samples[self.index:self.index+1]
            args, rates = datasets.neuralNetwork(self.model, image)
            self.setResults(args, rates)
            self.stackedWidget.setCurrentIndex(1)
            self.orig_img.setPixmap(QtGui.QPixmap(self.uploadedPath if self.uploadedPath else self.X_paths[self.index]))
        else:
            self.popup()

    def popup(self):
        mgsBox = QtWidgets.QMessageBox()
        mgsBox.setWindowTitle("Warning")
        mgsBox.setText("Please upload an image!")
        mgsBox.setIcon(QtWidgets.QMessageBox.Warning)
        mgsBox.exec_()

    def setResults(self, args, rates):
        for i in range(len(self.triples)):
            self.triples[i].img.setPixmap(QtGui.QPixmap(self.landmarks[args[i][0]].path))
            self.triples[i].nameText.setText(self.landmarks[args[i][0]].name)
            self.triples[i].rateText.setText(format(rates[i][0]*100, '.2f') + " %")

    def back(self):
        self.push_start.setText("START")
        self.push_image.setStyleSheet("border-image: url('images/default.png')")
        self.uploadedPath = ""
        self.index = -1
        self.stackedWidget.setCurrentIndex(0)

    def randomImage(self):
        self.index = datasets.random.randint(0, self.X_samples.shape[0]-1)
        print(self.index, " : ", self.X_paths[self.index])
        self.push_image.setStyleSheet("border-image: url('" + self.X_paths[self.index] + "')")
        self.uploadedPath = ""

    def uploadImage(self):
        path = QtWidgets.QFileDialog.getOpenFileName(None, 'Open file', 'C:/', "Image files (*.jpg *.jpeg *.png)")[0]
        
        if path:
            self.push_image.setStyleSheet("border-image: url('" + path + "')")
            self.uploadedPath = path
            self.index = -1

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