2020-12-14 14:28:42 +01:00
#import tkinter as tk
import sys
2021-01-25 13:35:24 +01:00
import os , subprocess
2020-12-21 22:42:37 +01:00
import shutil
2020-12-21 17:40:37 +01:00
from datetime import datetime
2020-12-21 22:42:37 +01:00
import time
2020-12-14 15:38:18 +01:00
from PyQt5 import QtCore
2020-12-21 17:40:37 +01:00
from PyQt5 . QtWidgets import *
2021-02-01 17:30:30 +01:00
from PyQt5 . QtGui import *
2020-12-21 17:40:37 +01:00
from PyQt5 . QtCore import *
2020-12-14 15:38:18 +01:00
2020-12-21 17:40:37 +01:00
scriptPath = os . path . dirname ( os . path . realpath ( __file__ ) )
2021-01-04 12:47:36 +01:00
inputFilePath = scriptPath + ' \\ files \\ input '
outputFilePath = scriptPath + ' \\ files \\ output '
LIB_RAW = 0
LIB_VIS = 1
2020-12-14 14:28:42 +01:00
2021-01-11 10:24:05 +01:00
2021-02-01 17:30:30 +01:00
stylesheet = """
MainWindow {
background : qlineargradient ( x1 : 0 y1 : 0 , x2 : 1 y2 : 0 , stop : 0 rgba ( 255 , 218 , 113 , 1 ) , stop : 0.5 rgba ( 215 , 137 , 255 , 1 ) , stop : 1 rgba ( 105 , 126 , 255 , 1 ) ) ;
}
"""
2020-12-21 17:40:37 +01:00
def files ( path ) :
2021-01-25 13:35:24 +01:00
for subdir , dirs , files_list in os . walk ( path ) :
for file in files_list :
yield os . path . join ( subdir , file )
2020-12-21 17:40:37 +01:00
2021-01-27 14:07:25 +01:00
def delete_file ( file ) :
os . remove ( file )
2020-12-21 22:42:37 +01:00
def save_input ( oldpath ) :
2021-01-25 17:33:20 +01:00
# add timestamp to the filename, so it wouln't overwrite but the user still knows which file's which
2020-12-21 22:42:37 +01:00
timestamp = str ( int ( time . time ( ) ) )
2021-01-25 17:33:20 +01:00
basename = os . path . basename ( oldpath )
try :
2021-01-25 17:37:24 +01:00
index_of_dot = basename . index ( ' . ' )
basename_no_extension = basename [ : index_of_dot ]
2021-01-25 17:33:20 +01:00
extension = basename [ index_of_dot : ]
except ValueError :
basename_no_extension = basename
extension = ' '
filename = basename_no_extension + ' _ ' + timestamp + extension
2021-01-04 12:58:04 +01:00
newpath = inputFilePath + ' \\ ' + filename
2020-12-21 22:42:37 +01:00
shutil . copy ( oldpath , newpath )
2020-12-21 23:10:17 +01:00
return newpath
2021-01-11 10:24:05 +01:00
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 )
2020-12-21 22:42:37 +01:00
2021-01-25 17:37:24 +01:00
2021-01-25 17:35:09 +01:00
class analizePopup ( QMessageBox ) :
def __init__ ( self , parent = None ) :
super ( analizePopup , self ) . __init__ ( parent )
self . setStyleSheet ( " QLabel { min-width: 100px; max-width: 100px; qproperty-alignment: AlignCenter;} " ) ;
self . setWindowIcon ( QIcon ( scriptPath + os . path . sep + ' static/v_logo.jpg ' ) )
2021-01-25 17:37:24 +01:00
self . setText ( " Analyzing file... " )
2021-01-25 17:35:09 +01:00
self . setWindowTitle ( " File analysis " )
# create Label
self . setIconPixmap ( QPixmap ( scriptPath + os . path . sep + ' static/loading.gif ' ) )
icon_label = self . findChild ( QLabel , " qt_msgboxex_icon_label " )
movie = QMovie ( scriptPath + os . path . sep + ' static/loading.gif ' )
# avoid garbage collector
setattr ( self , ' icon_label ' , movie )
icon_label . setMovie ( movie )
movie . start ( )
self . setStandardButtons ( self . Cancel )
2020-12-21 22:42:37 +01:00
2021-01-25 17:37:24 +01:00
2020-12-21 17:40:37 +01:00
class LibraryTableButtons ( QWidget ) :
2021-01-25 17:33:20 +01:00
def __init__ ( self , file , table , type , mainWindow , parent = None ) :
2020-12-21 17:40:37 +01:00
super ( LibraryTableButtons , self ) . __init__ ( parent )
2020-12-21 18:18:47 +01:00
def viewFile ( ) :
2021-01-04 12:47:36 +01:00
os . startfile ( file )
2020-12-21 18:18:47 +01:00
2021-01-04 12:22:19 +01:00
def deleteFile ( ) :
2021-01-04 12:23:17 +01:00
self . exPopup = deletePopup ( )
2021-01-04 12:47:36 +01:00
ret = self . exPopup . exec ( )
if ret == self . exPopup . Yes :
2021-01-27 14:07:25 +01:00
delete_file ( file )
2021-01-25 17:33:20 +01:00
table . fillTable ( type , mainWindow )
2021-01-18 16:13:52 +01:00
2021-01-18 16:18:56 +01:00
def analyzeFile ( ) :
2021-01-25 17:35:09 +01:00
self . exPopup = analizePopup ( )
#cmd = "py detect.py --source {} --view-img".format(str(file))
#popen = subprocess.Popen(cmd, cwd="../yolov5/", stdout=subprocess.PIPE)
cancel = self . exPopup . exec ( )
if cancel == self . exPopup . Cancel :
#popen.terminate()
pass
#popen.wait()
self . exPopup . close ( )
# mainWindow.showVisualisation(file) <- ALWAYS shows even if you cancel the process and we don't have time to fix that now
2021-01-04 12:22:19 +01:00
2021-01-25 17:37:24 +01:00
2020-12-21 18:18:47 +01:00
layout = QHBoxLayout ( )
2020-12-21 17:40:37 +01:00
layout . setContentsMargins ( 0 , 0 , 0 , 0 )
layout . setSpacing ( 0 )
2021-01-18 16:13:52 +01:00
if type == LIB_RAW :
2021-01-18 16:18:56 +01:00
analyze_btn = QPushButton ( ' Analyze ' )
analyze_btn . clicked . connect ( analyzeFile )
layout . addWidget ( analyze_btn )
2020-12-21 17:40:37 +01:00
2020-12-21 18:18:47 +01:00
view_btn = QPushButton ( ' View ' )
view_btn . clicked . connect ( viewFile )
layout . addWidget ( view_btn )
2021-01-04 12:22:19 +01:00
delete_btn = QPushButton ( ' Delete ' )
delete_btn . clicked . connect ( deleteFile )
layout . addWidget ( delete_btn )
2020-12-21 17:40:37 +01:00
self . setLayout ( layout )
2020-12-21 21:39:56 +01:00
2020-12-21 17:40:37 +01:00
class LibraryTable ( QTableWidget ) :
2020-12-21 21:39:56 +01:00
2021-01-25 17:33:20 +01:00
def __init__ ( self , type , mainWindow , singleFilePath = None , parent = None ) :
2020-12-21 17:40:37 +01:00
QTableWidget . __init__ ( self )
2021-01-25 17:33:20 +01:00
self . fillTable ( type , mainWindow , singleFilePath )
2021-01-11 16:22:46 +01:00
2021-01-25 17:33:20 +01:00
def fillTable ( self , type , mainWindow , singleFilePath = None ) :
2020-12-21 17:40:37 +01:00
self . setColumnCount ( 3 )
2021-01-04 12:47:36 +01:00
2021-01-18 16:10:17 +01:00
if singleFilePath != None :
self . setRowCount ( 1 )
2021-01-04 12:47:36 +01:00
self . setHorizontalHeaderLabels ( [ ' Upload date ' , ' Filename ' , ' Options ' ] )
filePath = inputFilePath
2021-01-18 16:10:17 +01:00
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 ) ) ) )
2021-01-25 17:33:20 +01:00
self . setCellWidget ( 0 , 2 , LibraryTableButtons ( singleFilePath , self , type , mainWindow ) )
2021-01-18 16:10:17 +01:00
2021-01-04 12:47:36 +01:00
else :
2021-01-18 16:10:17 +01:00
if type == LIB_RAW :
self . setHorizontalHeaderLabels ( [ ' Upload date ' , ' Filename ' , ' Options ' ] )
filePath = inputFilePath
else :
self . setHorizontalHeaderLabels ( [ ' Creation date ' , ' Filename ' , ' Options ' ] )
filePath = outputFilePath
2021-01-11 16:22:46 +01:00
2021-01-18 16:10:17 +01:00
self . horizontalHeader ( ) . setSectionResizeMode ( 0 , QHeaderView . Stretch )
self . horizontalHeader ( ) . setSectionResizeMode ( 1 , QHeaderView . Stretch )
self . horizontalHeader ( ) . setSectionResizeMode ( 2 , QHeaderView . ResizeToContents )
2020-12-21 17:40:37 +01:00
2021-01-18 16:10:17 +01:00
dates = [ ]
2021-01-25 13:35:24 +01:00
names = [ ]
2021-01-18 16:10:17 +01:00
for index , file in enumerate ( files ( filePath ) ) :
2021-01-25 13:35:24 +01:00
dates . append ( QTableWidgetItem ( str ( datetime . fromtimestamp ( os . path . getmtime ( file ) ) ) ) )
2021-01-18 16:10:17 +01:00
names . append ( QTableWidgetItem ( str ( file ) ) )
2020-12-21 17:40:37 +01:00
2021-01-18 16:10:17 +01:00
self . setRowCount ( len ( dates ) )
2020-12-21 17:40:37 +01:00
2021-01-18 16:10:17 +01:00
for index , date in enumerate ( dates ) :
self . setItem ( index , 0 , date )
self . setItem ( index , 1 , names [ index ] )
2021-01-25 17:33:20 +01:00
self . setCellWidget ( index , 2 , LibraryTableButtons ( names [ index ] . text ( ) , self , type , mainWindow ) )
2020-12-21 17:40:37 +01:00
2020-12-14 14:28:42 +01:00
2021-01-11 10:24:05 +01:00
class formatHelp ( QLabel ) :
def __init__ ( self , parent = None ) :
QLabel . __init__ ( self )
2021-01-25 17:00:50 +01:00
with open ( scriptPath + os . path . sep + ' static/help.txt ' , ' r ' , encoding = ' utf-8 ' ) as file :
2021-01-18 17:00:04 +01:00
help_text = file . read ( ) . replace ( ' \n ' , ' ' )
2021-01-11 10:24:05 +01:00
self . setText ( help_text )
2021-02-01 17:35:26 +01:00
self . setStyleSheet ( " padding-left: 20px; padding-right: 20px; padding-top: 10px; padding-bottom: 10px; font-size:32px; background-color: white; " )
2021-01-11 10:24:05 +01:00
self . adjustSize ( )
2020-12-21 21:39:56 +01:00
class MainWindow ( QMainWindow ) :
2020-12-21 17:40:37 +01:00
2020-12-21 21:39:56 +01:00
def __init__ ( self ) :
super ( ) . __init__ ( )
self . initUI ( )
2021-01-11 10:24:05 +01:00
# todo: split in multiply labels for richer formatting
# Show Help tab
def showHelp ( self ) :
label = formatHelp ( )
self . setCentralWidget ( label )
2021-02-01 17:35:26 +01:00
def showHomepage ( self ) :
# Upload file button + instructions on homepage
label = QLabel ( ' Welcome to VisionScore! Use the button below to upload a file and start analyzing it. If you need further help, please click the " help " item in the menu above. ' , self )
label . setFont ( QFont ( ' Arial ' , 14 ) )
label . setStyleSheet ( ' color: white; ' )
label . adjustSize ( )
label . move ( 200 , 200 )
shadow = QGraphicsDropShadowEffect ( )
shadow . setBlurRadius ( 10 )
shadow . setOffset ( 2 )
label . setGraphicsEffect ( shadow )
button = QPushButton ( ' Upload file ' , self )
button . setToolTip ( ' This is an example button ' )
button . move ( 850 , 270 )
button . setFixedSize ( 220 , 50 )
button . clicked . connect ( self . showUploadFile )
2021-01-11 10:24:05 +01:00
2020-12-21 17:40:37 +01:00
# Show raw uploaded files
2021-01-04 12:47:36 +01:00
def showInputLibrary ( self ) :
2021-01-25 17:33:20 +01:00
libTable = LibraryTable ( LIB_RAW , self )
2021-01-11 16:24:30 +01:00
self . setCentralWidget ( libTable )
2021-01-04 12:47:36 +01:00
# Show visualisations
def showVisualisationsLibrary ( self ) :
2021-01-25 17:33:20 +01:00
libTable = LibraryTable ( LIB_VIS , self )
2021-01-11 16:24:30 +01:00
self . setCentralWidget ( libTable )
2020-12-21 21:39:56 +01:00
2020-12-21 22:42:37 +01:00
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']
2021-01-18 16:10:17 +01:00
newPath = save_input ( file_path )
2021-01-25 17:33:20 +01:00
singleFileTable = LibraryTable ( LIB_RAW , self , newPath )
2021-01-18 16:10:17 +01:00
self . setCentralWidget ( singleFileTable )
2021-01-25 17:33:20 +01:00
def showVisualisation ( self , path ) :
singleFileTable = LibraryTable ( LIB_VIS , self , path )
self . setCentralWidget ( singleFileTable )
2020-12-21 22:42:37 +01:00
2020-12-21 21:39:56 +01:00
def initUI ( self ) :
self . setGeometry ( 0 , 0 , 600 , 400 )
self . setWindowTitle ( ' VisionScore ' )
2021-01-25 17:33:20 +01:00
scriptDir = os . path . dirname ( os . path . realpath ( __file__ ) )
self . setWindowIcon ( QIcon ( scriptDir + os . path . sep + ' static/v_logo.jpg ' ) )
2021-02-01 17:35:26 +01:00
self . showHomepage ( )
2021-02-01 17:30:30 +01:00
2021-01-11 10:24:05 +01:00
# File menu
2020-12-21 21:39:56 +01:00
menuBar = self . menuBar ( )
2021-01-11 10:24:05 +01:00
fileMenu = QMenu ( " &File " , self )
menuBar . addMenu ( fileMenu )
2020-12-21 22:42:37 +01:00
# Upload file
uploadAct = QAction ( ' &Upload new file ' , self )
uploadAct . triggered . connect ( self . showUploadFile )
2021-01-11 10:24:05 +01:00
fileMenu . addAction ( uploadAct )
2020-12-21 21:39:56 +01:00
2021-01-11 10:24:05 +01:00
# Exit app
exitAct = QAction ( ' &Exit ' , self )
exitAct . setShortcut ( ' Ctrl+Q ' )
exitAct . setStatusTip ( ' Exit ' )
exitAct . triggered . connect ( qApp . quit )
fileMenu . addAction ( exitAct )
# Library menu
2021-01-04 12:47:36 +01:00
libraryMenu = QMenu ( " &Library " , self )
menuBar . addMenu ( libraryMenu )
2021-01-11 10:24:05 +01:00
# Input files
2021-01-04 12:47:36 +01:00
inputAct = QAction ( ' &Input files ' , self )
inputAct . triggered . connect ( self . showInputLibrary )
libraryMenu . addAction ( inputAct )
2021-01-11 10:24:05 +01:00
# Visualisations
2021-01-04 12:47:36 +01:00
visAct = QAction ( ' &Visualisations ' , self )
visAct . triggered . connect ( self . showVisualisationsLibrary )
libraryMenu . addAction ( visAct )
2020-12-21 21:39:56 +01:00
2021-01-11 10:24:05 +01:00
# Help
helpAct = QAction ( ' &Help ' , self )
helpAct . triggered . connect ( self . showHelp )
menuBar . addAction ( helpAct )
2020-12-21 21:39:56 +01:00
2021-01-25 15:00:58 +01:00
self . showMaximized ( )
2020-12-21 21:39:56 +01:00
def main ( ) :
app = QApplication ( sys . argv )
2021-02-01 17:30:30 +01:00
app . setStyleSheet ( stylesheet )
2020-12-21 21:39:56 +01:00
w = MainWindow ( )
2020-12-14 14:28:42 +01:00
sys . exit ( app . exec_ ( ) )
2020-12-21 21:39:56 +01:00
2020-12-14 14:28:42 +01:00
if __name__ == ' __main__ ' :
main ( )