#import tkinter as tk import sys import os, subprocess import shutil from datetime import datetime import time from PyQt5 import QtCore from PyQt5.QtWidgets import * from PyQt5.QtGui import QIcon, QPixmap from PyQt5.QtCore import * scriptPath = os.path.dirname(os.path.realpath(__file__)) inputFilePath = scriptPath + '\\files\\input' outputFilePath = scriptPath + '\\files\\output' LIB_RAW = 0 LIB_VIS = 1 def files(path): for subdir, dirs, files_list in os.walk(path): for file in files_list: yield os.path.join(subdir, file) def save_input(oldpath): # add timestamp to the filename, so it wouln't overwrite but the user still knows which file's which timestamp = str(int(time.time())) basename = os.path.basename(oldpath) try: basename.index('.') basename_no_extension = basename[:basename.index('.')] extension = basename[index_of_dot:] except ValueError: basename_no_extension = basename extension = '' filename = basename_no_extension + '_' + timestamp + extension newpath = inputFilePath + '\\' + filename shutil.copy(oldpath, newpath) return newpath class deletePopup(QMessageBox): def __init__(self, parent=None): super(deletePopup, self).__init__(parent) self.setText("Are you sure you want to delete the file?") self.setIcon(self.Warning) self.setWindowTitle("Confirm deletion") self.setStandardButtons(self.Yes | self.No) class LibraryTableButtons(QWidget): def __init__(self, file, table, type, mainWindow, parent=None): super(LibraryTableButtons,self).__init__(parent) def viewFile(): os.startfile(file) def deleteFile(): self.exPopup = deletePopup() ret = self.exPopup.exec() if ret == self.exPopup.Yes: os.remove(file) table.fillTable(type, mainWindow) def analyzeFile(): cmd = "py detect.py --source {} --view-img".format(str(file)) popen = subprocess.Popen(cmd, cwd="../yolov5/", stdout=subprocess.PIPE) popen.wait() # todo: display gif/spinning wheel mainWindow.showVisualisation(file) pass layout = QHBoxLayout() layout.setContentsMargins(0,0,0,0) layout.setSpacing(0) if type == LIB_RAW: analyze_btn = QPushButton('Analyze') analyze_btn.clicked.connect(analyzeFile) layout.addWidget(analyze_btn) view_btn = QPushButton('View') view_btn.clicked.connect(viewFile) layout.addWidget(view_btn) delete_btn = QPushButton('Delete') delete_btn.clicked.connect(deleteFile) layout.addWidget(delete_btn) self.setLayout(layout) class LibraryTable(QTableWidget): def __init__(self, type, mainWindow, singleFilePath = None, parent = None): QTableWidget.__init__(self) self.fillTable(type, mainWindow, singleFilePath) def fillTable(self, type, mainWindow, singleFilePath = None): self.setColumnCount(3) if singleFilePath != None: self.setRowCount(1) self.setHorizontalHeaderLabels(['Upload date', 'Filename', 'Options']) filePath = inputFilePath self.horizontalHeader().setSectionResizeMode(0, QHeaderView.Stretch) self.horizontalHeader().setSectionResizeMode(1, QHeaderView.Stretch) self.horizontalHeader().setSectionResizeMode(2, QHeaderView.ResizeToContents) self.setItem(0,0,QTableWidgetItem(str(datetime.fromtimestamp(os.path.getmtime(singleFilePath))))) self.setItem(0,1,QTableWidgetItem(str(os.path.basename(singleFilePath)))) self.setCellWidget(0,2,LibraryTableButtons(singleFilePath, self, type, mainWindow)) else: if type == LIB_RAW: self.setHorizontalHeaderLabels(['Upload date', 'Filename', 'Options']) filePath = inputFilePath else: self.setHorizontalHeaderLabels(['Creation date', 'Filename', 'Options']) filePath = outputFilePath self.horizontalHeader().setSectionResizeMode(0, QHeaderView.Stretch) self.horizontalHeader().setSectionResizeMode(1, QHeaderView.Stretch) self.horizontalHeader().setSectionResizeMode(2, QHeaderView.ResizeToContents) dates = [] names = [] for index, file in enumerate(files(filePath)): dates.append(QTableWidgetItem(str(datetime.fromtimestamp(os.path.getmtime(file))))) names.append(QTableWidgetItem(str(file))) self.setRowCount(len(dates)) for index, date in enumerate(dates): self.setItem(index,0,date) self.setItem(index,1,names[index]) self.setCellWidget(index,2,LibraryTableButtons(names[index].text(), self, type, mainWindow)) class formatHelp(QLabel): def __init__(self, parent=None): QLabel.__init__(self) with open(scriptPath + os.path.sep + 'static/help.txt', 'r', encoding='utf-8') as file: help_text = file.read().replace('\n', '') self.setText(help_text) self.setStyleSheet("padding-left: 20px; padding-right: 20px; padding-top: 10px; padding-bottom: 10px; font-size:32px;") self.adjustSize() class MainWindow(QMainWindow): def __init__(self): super().__init__() self.initUI() # todo: split in multiply labels for richer formatting # Show Help tab def showHelp(self): label = formatHelp() self.setCentralWidget(label) # Show raw uploaded files def showInputLibrary(self): libTable = LibraryTable(LIB_RAW, self) self.setCentralWidget(libTable) # Show visualisations def showVisualisationsLibrary(self): libTable = LibraryTable(LIB_VIS, self) self.setCentralWidget(libTable) def showUploadFile(self): dialog = QFileDialog(self) dialog.setFileMode(QFileDialog.AnyFile) dialog.setFilter(QDir.Files) if dialog.exec_(): file_path = dialog.selectedFiles()[0] # ['C:/Users/agatha/Desktop/SYI/VisionScore/win_venv/requirements.txt'] newPath = save_input(file_path) singleFileTable = LibraryTable(LIB_RAW, self, newPath) self.setCentralWidget(singleFileTable) def showVisualisation(self, path): singleFileTable = LibraryTable(LIB_VIS, self, path) self.setCentralWidget(singleFileTable) def initUI(self): self.setGeometry(0, 0, 600, 400) self.setWindowTitle('VisionScore') scriptDir = os.path.dirname(os.path.realpath(__file__)) self.setWindowIcon(QIcon(scriptDir + os.path.sep + 'static/v_logo.jpg')) # File menu menuBar = self.menuBar() fileMenu = QMenu("&File", self) menuBar.addMenu(fileMenu) # Upload file uploadAct = QAction('&Upload new file', self) uploadAct.triggered.connect(self.showUploadFile) fileMenu.addAction(uploadAct) # Exit app exitAct = QAction('&Exit', self) exitAct.setShortcut('Ctrl+Q') exitAct.setStatusTip('Exit') exitAct.triggered.connect(qApp.quit) fileMenu.addAction(exitAct) # Library menu libraryMenu = QMenu("&Library", self) menuBar.addMenu(libraryMenu) # Input files inputAct = QAction('&Input files', self) inputAct.triggered.connect(self.showInputLibrary) libraryMenu.addAction(inputAct) # Visualisations visAct = QAction('&Visualisations', self) visAct.triggered.connect(self.showVisualisationsLibrary) libraryMenu.addAction(visAct) # Help helpAct = QAction('&Help', self) helpAct.triggered.connect(self.showHelp) menuBar.addAction(helpAct) self.showMaximized() def main(): app = QApplication(sys.argv) w = MainWindow() sys.exit(app.exec_()) if __name__ == '__main__': main()