-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathemoch.py
157 lines (119 loc) · 4.58 KB
/
emoch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
"""Speech Emotion Analysis using Python."""
# GUI Imports
import sys
import platform
from PySide2 import QtCore, QtGui, QtWidgets
from PySide2.QtCore import (QCoreApplication, QPropertyAnimation, QDate, QDateTime,
QMetaObject, QObject, QPoint, QRect, QSize, QTime, QUrl, Qt, QEvent)
from PySide2.QtGui import (QBrush, QColor, QConicalGradient, QCursor, QFont, QFontDatabase,
QIcon, QKeySequence, QLinearGradient, QPalette, QPainter, QPixmap, QRadialGradient)
from PySide2.QtWidgets import *
# Importing GUI Files
from splash_screen import Ui_SplashScreen
from main_window import Ui_MainWindow
from assets import *
# Imporitng Prediction Code
from live_classifier import get_emotion
# Importing Helper Functions
from utils import get_audio_devices
from live_classifier import start_stream, stop_stream
# Other Imports
import os
# Global Variables
PROGRESS = 0
class MainWindow(QMainWindow):
"""Main Window."""
def __init__(self):
"""Setup & Control the window."""
QMainWindow.__init__(self)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
# Images
self.pixmaps = {
'angry': QPixmap(images['ANGRY']).scaled(650, 350, Qt.KeepAspectRatio, Qt.FastTransformation),
'calm': QPixmap(images['CALM']).scaled(650, 350, Qt.KeepAspectRatio, Qt.FastTransformation),
'apprehensive': QPixmap(images['APPREHENSIVE']).scaled(650, 350, Qt.KeepAspectRatio, Qt.FastTransformation),
'elated': QPixmap(images['ELATED']).scaled(650, 350, Qt.KeepAspectRatio, Qt.FastTransformation)
}
# Configuring/Adding Audio Devices
audioDevices = get_audio_devices()
for item in audioDevices[1]:
self.ui.InputSelect.addItem(item)
self.ui.InputSelect.setCurrentIndex(1)
self.ui.InputSelect.currentIndexChanged.connect(self.setaudiodevice)
# Starting Audio Stream & Classification
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.start)
self.timer.start(1)
self.show()
def setaudiodevice(self):
self.timer.stop()
self.ui.label_2.setText("Just a sec...")
stop_stream()
start_stream(self.ui.InputSelect.currentIndex())
self.timer.start(1)
def start(self):
emotion = get_emotion()
self.ui.label_2.setText(emotion[0].upper())
self.ui.Image.setPixmap(self.pixmaps[emotion[0]])
def closeEvent(self, event):
if os.path.exists("emoch.tmp"):
os.remove("emoch.tmp")
class SplashScreen(QMainWindow):
"""Splash Screen."""
def __init__(self):
"""Setup the window."""
# Initializing some required assets
QMainWindow.__init__(self)
self.ui = Ui_SplashScreen()
self.ui.setupUi(self)
try:
start_stream()
except:
try:
start_stream(0)
except:
QtCore.QTimer.singleShot(1500, lambda: self.ui.Name.setText("Error!"))
sys.exit()
# Setting Progress to 0 on startup
self._set_progress(0)
# Remove Title Bar
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
# Apply Drop Shadow
self.shadow = QGraphicsDropShadowEffect()
self.shadow.setBlurRadius(20)
self.shadow.setXOffset(0)
self.shadow.setYOffset(0)
self.shadow.setColor(QColor(0, 0, 0, 100))
self.ui.Background.setGraphicsEffect(self.shadow)
# Connecting progress bar
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self._load)
self.timer.start(20)
# Outputing the GUI to the screen
self.show()
def _load(self):
"""Update Progress."""
global PROGRESS
# Updating Progress Bar
self._set_progress(PROGRESS)
# When Progress Bar Completes
if PROGRESS > 100:
self.timer.stop()
# Show Main Window
self.main = MainWindow()
self.close()
PROGRESS += 1
def _set_progress(self, value):
"""Update progress bar in GUI."""
# Converting values to float between 0 and 1
progress = (100 - value) / 100.0
# Creating GUI Update stylesheet
new_progress_stylesheet = stylesheet.replace("{@02}", str(progress))
# Updating GUI
self.ui.Progress.setStyleSheet(new_progress_stylesheet)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = SplashScreen()
sys.exit(app.exec_())