class PeerApplication(QtWidgets.QMainWindow):
    def __init__(self):
        super(PeerApplication, self).__init__()
        os.system("pyuic5 -x GUI/gui.ui -o GUI/gui.py")  # compile the gui
        from GUI.gui import Ui_MainWindow  # import the newly compiled gui
        self.ui = Ui_MainWindow()  # create an instance of the GUI
        self.ui.setupUi(self)
        self.configure()
        self.show()

    # override the closeEvent method
    def closeEvent(self, event):
        print ("User has clicked the red x on the main window")
        event.accept()

    def configure(self):
        self.ui.connect.clicked.connect(self.makeConnection)
        self.ui.search.clicked.connect(self.search)
        self.ui.go.clicked.connect(self.enterCommand)
        self.ui.results.setRowCount(2)
        self.ui.results.setColumnCount(2)
        self.ui.results.setItem(0, 0, QTableWidgetItem("First Name"))
        self.ui.results.setItem(0, 1, QTableWidgetItem("Last Name"))
        self.ui.results.setItem(1, 0, QTableWidgetItem("Aaron"))
        self.ui.results.setItem(1, 1, QTableWidgetItem("Kopplin"))

    def makeConnection(self):
        print("connecting to server: " + self.ui.server.text())

    def search(self):
        print("searching server for \"" + self.ui.keyword.text() + "\"")

    def enterCommand(self):
        self.ui.commandLine.appendPlainText(">> " + self.ui.command.text())
        self.ui.command.clear()
 def __init__(self):
     super(PeerApplication, self).__init__()
     os.system("pyuic5 -x GUI/gui.ui -o GUI/gui.py")  # compile the gui
     from GUI.gui import Ui_MainWindow  # import the newly compiled gui
     self.ui = Ui_MainWindow()  # create an instance of the GUI
     self.ui.setupUi(self)
     self.configure()
     self.show()
Example #3
0
 def __init__(self, parent=None):
     super(runGUI, self).__init__(parent)
     self.ui = Ui_MainWindow()
     self.ui.setupUi(self)
     self.createConnections()
     self.trainingDir = 'data/images/ORL/ORL/'
     self.trainPat = self.trainingDir + 'ORL.pat'
     self.testingDir = 'data/images/ORL/ORL/unseen/'
     self.testPat = self.testingDir + 'unseen.pat'
     self.debug = 1
Example #4
0
 def __init__(self, parent=None):
     super(runGUI, self).__init__(parent)
     self.ui = Ui_MainWindow()
     self.ui.setupUi(self)
     self.createConnections()
     self.trainingDir = 'data/images/ORL/ORL/'
     self.trainPat = self.trainingDir + 'ORL.pat'
     self.testingDir = 'data/images/ORL/ORL/unseen/'
     self.testPat = self.testingDir + 'unseen.pat'
     self.debug = 1
Example #5
0
class runGUI(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(runGUI, self).__init__(parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.createConnections()
        self.trainingDir = 'data/images/ORL/ORL/'
        self.trainPat = self.trainingDir + 'ORL.pat'
        self.testingDir = 'data/images/ORL/ORL/unseen/'
        self.testPat = self.testingDir + 'unseen.pat'
        self.debug = 1

    def createConnections(self):
        self.ui.actionLoad_Training_Folder.triggered.connect(self.loadDataFiles)
        self.ui.actionLoad_Testing_Folder.triggered.connect(self.loadDataFiles)
        self.ui.actionTrain.triggered.connect(self.train)
        self.ui.actionRun_Test.triggered.connect(self.train)
        self.ui.actionPrepare_for_training.triggered.connect(self.prepareTraining)
        self.ui.actionPrepare_for_testing.triggered.connect(self.prepareTesting)

    def loadDataFiles(self):
        if self.sender() == self.ui.actionLoad_Training_Folder:
            Call = 'Training'
        else:
            Call = 'Testing'

        dialog = QtGui.QFileDialog()
        dialog.setDirectory('.')
        dialog.setFileMode(QtGui.QFileDialog.Directory)
        dialog.exec_()
        filenames = dialog.selectedFiles()

        if Call == 'Training':
            self.trainingDir = str(filenames[0])
            self.trainPat = self.trainingDir + '/' + self.trainingDir.rpartition('/')[-1] + '.pat'
        else:
            self.testingDir = str(filenames[0])
            self.testPat = self.testingDir + '/' + self.testingDir.rpartition('/')[-1] + '.pat'

    def prepareTraining(self):
        if self.trainingDir is None:
            return None
        self.log('Cleaning previous temporary data ...', 'main')
#        self.cleanDir(self.trainingDir)
        self.log('Starting training operations ...', 'main')
        directory = self.trainingDir
        outdir = directory + '/'
        self.log('Getting faces ...', 'query')
#        fd = faceDetector(outdir,outdir)
#        fd.run()
        self.log('OK', 'status')
        self.log('Saving patterns in the .pat file ...', 'query')
        injectData(self.trainPat, outdir, 'train')
        self.log('OK', 'status')

    def prepareTesting(self):
        if self.testingDir is None:
            return None
        self.log('Cleaning previous temporary data ...', 'main')
#        self.cleanDir(self.testingDir)
        self.log('Starting testing operations ...', 'main')
        directory = self.testingDir
        outdir = directory + '/'
        self.log('Getting faces ...', 'query')
#        fd = faceDetector(outdir,outdir)
#        fd.run()
        self.log('OK', 'status')
        self.log('Saving patterns in the .pat file ...', 'query')
        injectData(self.testPat, outdir, 'test')
        self.log('OK', 'status')

    def train(self):
        allSamples = goodSamples = 0.0
        self.log('Starting the real training...', 'main')
        if self.sender() == self.ui.actionTrain:
            procData = processData(self.trainPat)
            result = procData.readContents()
            data = inputData()
            data.addAll(result)
            self.ls = Lamstar.lamstar(15, 1)
            for iter in range(10):
                print('Iteration %s' % iter)
                for i in range(data.getCount()):
#                    print('Training data : ', i)
#                    self.arr2Image(data.getWholeArray(i),'input')
#                    raw_input('Enter')
                    self.ls.train(data.getSubWords(i), data.getOutputs(i))
                if(self.debug > 0):
                    self.log('Iteration no:' + str(iter), 'main')
                    self.log('No of nodes' + str(self.ls.getNoOfNodes()), 'main')

        elif self.sender() == self.ui.actionRun_Test:
            goodSamples = allSamples = 0
            procData = processData(self.testPat)
            result = procData.readContents()
            data = inputData()
            data.addAll(result)
            files = []
            for afile in os.listdir(self.testingDir):
                if afile[-3:] == 'jpg' and afile.find('procced') != -1:
                    files.append(afile)
            files.sort()
            print('Quering for test data')
            self.log('Quering for test data', 'main')
            print('number of items: %s' % (data.getCount()))
            for i in range(data.getCount()):
                self.log('Querying for ' + files[i].rpartition('/')[-1], 'query')
                print('Querying for ' + files[i].rpartition('/')[-1])
                self.arr2Image(data.getWholeArray(i), 'input')
                (result, BMU) = self.ls.query(data.getSubWords(i))
                arr = self.constructArr(BMU)
                self.arr2Image(arr, 'output')
                self.log(result, 'status')
                allSamples += 1
                if files[i].find(result + '-') != -1:
                    goodSamples += 1
                self.ui.resultLabel.setText(result)
            self.log('No of nodes: ' + str(self.ls.getNoOfNodes()), 'main')
            self.log('No of links: ' + str(self.ls.getNoOfLinks()), 'main')
            self.log('Accuracy = ' + str((float(goodSamples) / allSamples) * 100) + '%', 'main')
            print ('Good sample=%s all=%s' % (goodSamples, allSamples))

    def constructArr(self, BMU):
        dx = 50
        dy = 40
        arr = np.zeros((200, 150))

        BMU.sort()
        for (i, idx) in BMU:
            x = (i / 5) * 50
            y = (i % 5) * 40
            arr[y:y + dy, x:x + dx] = self.ls.getInputNode(i, idx) * 255
        return arr

    def arr2Image(self, arr, holder):
        from scipy.misc import imsave
        imsave('tmp.png', arr)
#        imshow(arr)
        if holder == 'input':
            self.ui.imageInputLabel.setPixmap(QtGui.QPixmap('tmp.png'))
        elif holder == 'output':
            self.ui.imageResultLabel.setPixmap(QtGui.QPixmap('tmp.png'))

#        self.ui.imageInputLabel.setPixmap
        self.ui.imageInputLabel.repaint()

    def cleanDir(self, inputDir):
        for afile in os.listdir(inputDir):
            if afile.find('procced') != -1:
                os.remove(inputDir + '/' + afile)

    def log(self, text, mode):
        if(mode == 'query'):
            self.ui.textBrowser.setAlignment(QtCore.Qt.AlignRight)
            self.ui.textBrowser.setTextColor(QtCore.Qt.black)
            self.ui.textBrowser.insertPlainText(text)
        elif(mode == 'status'):
            self.ui.textBrowser.insertPlainText('\t[ ')
            self.ui.textBrowser.setTextColor(QtCore.Qt.green)
            self.ui.textBrowser.insertPlainText(text)
            self.ui.textBrowser.setTextColor(QtCore.Qt.black)
            self.ui.textBrowser.insertPlainText(' ]\n')
        elif(mode == 'main'):
            self.ui.textBrowser.setAlignment(QtCore.Qt.AlignLeft)
            self.ui.textBrowser.setTextColor(QtCore.Qt.red)
            self.ui.textBrowser.insertPlainText(text + '\n')
        self.ui.textBrowser.repaint()
        self.repaint()
Example #6
0
class runGUI(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(runGUI, self).__init__(parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.createConnections()
        self.trainingDir = 'data/images/ORL/ORL/'
        self.trainPat = self.trainingDir + 'ORL.pat'
        self.testingDir = 'data/images/ORL/ORL/unseen/'
        self.testPat = self.testingDir + 'unseen.pat'
        self.debug = 1

    def createConnections(self):
        self.ui.actionLoad_Training_Folder.triggered.connect(
            self.loadDataFiles)
        self.ui.actionLoad_Testing_Folder.triggered.connect(self.loadDataFiles)
        self.ui.actionTrain.triggered.connect(self.train)
        self.ui.actionRun_Test.triggered.connect(self.train)
        self.ui.actionPrepare_for_training.triggered.connect(
            self.prepareTraining)
        self.ui.actionPrepare_for_testing.triggered.connect(
            self.prepareTesting)

    def loadDataFiles(self):
        if self.sender() == self.ui.actionLoad_Training_Folder:
            Call = 'Training'
        else:
            Call = 'Testing'

        dialog = QtGui.QFileDialog()
        dialog.setDirectory('.')
        dialog.setFileMode(QtGui.QFileDialog.Directory)
        dialog.exec_()
        filenames = dialog.selectedFiles()

        if Call == 'Training':
            self.trainingDir = str(filenames[0])
            self.trainPat = self.trainingDir + '/' + self.trainingDir.rpartition(
                '/')[-1] + '.pat'
        else:
            self.testingDir = str(filenames[0])
            self.testPat = self.testingDir + '/' + self.testingDir.rpartition(
                '/')[-1] + '.pat'

    def prepareTraining(self):
        if self.trainingDir is None:
            return None
        self.log('Cleaning previous temporary data ...', 'main')
        self.cleanDir(self.trainingDir)
        self.log('Starting training operations ...', 'main')
        directory = self.trainingDir
        outdir = directory + '/'
        self.log('Getting faces ...', 'query')
        fd = faceDetector(outdir, outdir)
        fd.run()
        self.log('OK', 'status')
        self.log('Saving patterns in the .pat file ...', 'query')
        injectData(self.trainPat, outdir, 'train')
        self.log('OK', 'status')

    def prepareTesting(self):
        if self.testingDir is None:
            return None
        self.log('Cleaning previous temporary data ...', 'main')
        self.cleanDir(self.testingDir)
        self.log('Starting testing operations ...', 'main')
        directory = self.testingDir
        outdir = directory + '/'
        self.log('Getting faces ...', 'query')
        fd = faceDetector(outdir, outdir)
        fd.run()
        self.log('OK', 'status')
        self.log('Saving patterns in the .pat file ...', 'query')
        injectData(self.testPat, outdir, 'test')
        self.log('OK', 'status')

    def train(self):
        allSamples = goodSamples = 0.0
        self.log('Starting the real training...', 'main')
        if self.sender() == self.ui.actionTrain:
            procData = processData(self.trainPat)
            result = procData.readContents()
            data = inputData()
            data.addAll(result)
            self.ls = Lamstar.lamstar(15, 1)
            for iter in range(10):
                print('Iteration %s' % iter)
                for i in range(data.getCount()):
                    print('Training data : ', i)
                    self.arr2Image(data.getWholeArray(i), 'input')
                    #raw_input('Enter')
                    self.ls.train(data.getSubWords(i), data.getOutputs(i))
                if (self.debug > 0):
                    self.log('Iteration no:' + str(iter), 'main')
                    self.log('No of nodes' + str(self.ls.getNoOfNodes()),
                             'main')

        elif self.sender() == self.ui.actionRun_Test:
            #goodSamples = allSamples = 0
            procData = processData(self.testPat)
            result = procData.readContents()
            data = inputData()
            data.addAll(result)
            files = []
            for afile in os.listdir(self.testingDir):
                if afile[-3:] == 'png' and afile.find('procced') != -1:
                    files.append(afile)
            files.sort()
            print('Quering for test data')
            self.log('Quering for test data', 'main')
            print('number of items: %s' % (data.getCount()))
            for i in range(data.getCount()):
                self.log('Querying for ' + files[i].rpartition('/')[-1],
                         'query')
                print('Querying for ' + files[i].rpartition('/')[-1])
                self.arr2Image(data.getWholeArray(i), 'input')
                (result, BMU) = self.ls.query(data.getSubWords(i))
                arr = self.constructArr(BMU)
                self.arr2Image(arr, 'output')
                self.log(result, 'status')
                allSamples += 1
                if files[i].find(result + '-') != -1:
                    goodSamples += 1
                self.ui.resultLabel.setText(result)
            self.log('No of nodes: ' + str(self.ls.getNoOfNodes()), 'main')
            self.log('No of links: ' + str(self.ls.getNoOfLinks()), 'main')
            self.log(
                'Accuracy = ' + str(
                    (float(goodSamples) / allSamples) * 100) + '%', 'main')
            print('Good sample=%s all=%s' % (goodSamples, allSamples))

    def constructArr(self, BMU):
        dx = 50
        dy = 40
        arr = np.zeros((200, 150))

        BMU.sort()
        for (i, idx) in BMU:
            x = (i / 5) * 50
            y = (i % 5) * 40
            arr[y:y + dy, x:x + dx] = self.ls.getInputNode(i, idx) * 255
        return arr

    def arr2Image(self, arr, holder):
        from scipy.misc import imsave  #, imshow
        imsave('tmp.png', arr)
        #imshow(arr)
        if holder == 'input':
            self.ui.imageInputLabel.setPixmap(QtGui.QPixmap('tmp.png'))
        elif holder == 'output':
            self.ui.imageResultLabel.setPixmap(QtGui.QPixmap('tmp.png'))

        self.ui.imageInputLabel.setPixmap
        self.ui.imageInputLabel.repaint()

    def cleanDir(self, inputDir):
        for afile in os.listdir(inputDir):
            if afile.find('procced') != -1:
                os.remove(inputDir + '/' + afile)

    def log(self, text, mode):
        if (mode == 'query'):
            self.ui.textBrowser.setAlignment(QtCore.Qt.AlignRight)
            self.ui.textBrowser.setTextColor(QtCore.Qt.black)
            self.ui.textBrowser.insertPlainText(text)
        elif (mode == 'status'):
            self.ui.textBrowser.insertPlainText('\t[ ')
            self.ui.textBrowser.setTextColor(QtCore.Qt.green)
            self.ui.textBrowser.insertPlainText(text)
            self.ui.textBrowser.setTextColor(QtCore.Qt.black)
            self.ui.textBrowser.insertPlainText(' ]\n')
        elif (mode == 'main'):
            self.ui.textBrowser.setAlignment(QtCore.Qt.AlignLeft)
            self.ui.textBrowser.setTextColor(QtCore.Qt.red)
            self.ui.textBrowser.insertPlainText(text + '\n')
        self.ui.textBrowser.repaint()
        self.repaint()
Example #7
0
class PeerApplication(QtWidgets.QMainWindow):
    def __init__(self):
        super(PeerApplication, self).__init__()
        os.system("pyuic5 -x GUI/gui.ui -o GUI/gui.py")  # compile the gui
        from GUI.gui import Ui_MainWindow  # import the newly compiled gui
        self.ui = Ui_MainWindow()  # create an instance of the GUI
        self.ui.setupUi(self)
        self.configure()
        self.peer = Peer()
        self.show()

    # override the closeEvent method
    def closeEvent(self, event):
        # print ("User has clicked the red x on the main window")
        event.accept()

    def configure(self):
        self.ui.connect.clicked.connect(self.makeConnection)
        self.ui.search.clicked.connect(self.search)
        self.ui.go.clicked.connect(self.enterCommand)
        self.ui.server.setText("127.0.0.2")
        self.ui.port.setText("3000")
        self.ui.username.setText("tom")
        self.ui.hostname.setText("hostname")
        self.ui.speed.addItem("Ethernet")
        self.ui.speed.addItem("Modem")
        self.ui.speed.addItem("T1")
        self.ui.speed.addItem("T3")

    def makeConnection(self, command=None, keyword=None):
        numPeers = self.peer.connectToServer(self.ui.server.text(),
                                             int(self.ui.port.text()),
                                             self.ui.username.text(),
                                             self.ui.speed.currentText())
        self.ui.commandLine.appendPlainText(
            ">> " + "Connected to server, Users online: " + numPeers)

        # print("connecting to server: " + self.ui.server.text())
        # if not command:
        #     command = 'connect'
        # self.peer.connect(command, self.ui.server.text(), int(self.ui.port.text()), self.ui.username.text(),
        #                   self.ui.hostname.text(), self.ui.speed.currentText(), keyword=keyword)

    def search(self):
        searchTerm = self.ui.keyword.text()
        if searchTerm:
            fileData = self.peer.searchServer(searchTerm)
            if fileData:
                tokens = fileData.split(",")
                while tokens:
                    speed = tokens.pop(0)
                    username = tokens.pop(0)
                    fileName = tokens.pop(0)
                    self.ui.results.insertRow(self.ui.results.rowCount())
                    rowIndex = self.ui.results.rowCount() - 1
                    self.ui.results.setItem(rowIndex, 0,
                                            QTableWidgetItem(speed))
                    self.ui.results.setItem(rowIndex, 1,
                                            QTableWidgetItem(username))
                    self.ui.results.setItem(rowIndex, 2,
                                            QTableWidgetItem(fileName))

        # print("searching server for \"" + self.ui.keyword.text() + "\"")
        # self.makeConnection('search', keyword=self.ui.keyword.text())
        #
        # # Clear the current rows
        # self.ui.results.setRowCount(0)
        #
        # # Display all of the files
        # for file in self.peer.files:
        #     self.ui.results.insertRow(self.ui.results.rowCount())
        #     rowIndex = self.ui.results.rowCount() - 1
        #     self.ui.results.setItem(rowIndex, 0, QTableWidgetItem(file.find('speed').text))
        #     self.ui.results.setItem(rowIndex, 1, QTableWidgetItem(file.find('hostname').text))
        #     self.ui.results.setItem(rowIndex, 2, QTableWidgetItem(file.find('filename').text))

    def enterCommand(self):
        if (self.ui.command.text()) == "listen":
            self.ui.commandLine.appendPlainText(
                ">> Listening for file requests...")
            requestedFileName = self.peer.notifyServer(self.ui.username.text())
            self.ui.commandLine.appendPlainText(">> " + "Peer requested: " +
                                                requestedFileName)

        else:
            # we are requesting a file from another peer
            request = self.ui.command.text().split(" ")  # username file
            if len(request) == 2:
                username = request[0]
                filename = request[1]
                # self.peer.requestHostPort(request[0])
                self.ui.commandLine.appendPlainText(">> Requesting " +
                                                    filename + " from " +
                                                    username)
                requestedFileText = self.peer.fetchFile(username, filename)
                self.ui.commandLine.appendPlainText(">> Received!")
                self.ui.commandLine.appendPlainText(">> " + requestedFileText)
            else:
                print("please enter <username> <filename>")