-
Notifications
You must be signed in to change notification settings - Fork 8
/
Sensei.py
415 lines (357 loc) · 14.9 KB
/
Sensei.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# !/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import time
import sys
import pickle
import argparse
import datetime
from aboutGUI import Ui_AboutWindow
from PyInstallerUtils import pyInstallerResourcePath
# import subprocess
from cv2 import (VideoCapture, waitKey, CascadeClassifier, cvtColor,
COLOR_BGR2GRAY)
from PyQt5.QtWidgets import (QPushButton, QApplication, QProgressBar, QLabel,
QInputDialog, qApp, QAction, QMenu,
QSystemTrayIcon, QMainWindow, QDialog)
from PyQt5.QtCore import (QThread, QTimer, QRect, QPropertyAnimation)
from PyQt5.QtGui import QIcon
# CASCPATH = "/usr/local/opt/opencv3/share/OpenCV/haarcascades/haarcascade_frontalface_default.xml"
# FACECASCADE = CascadeClassifier(CASCPATH)
# Delay between checking posture in miliseconds.
MONITOR_DELAY = 2000
# Notify when user is 1.2 times closer than the calibration distance.
SENSITIVITY = 1.2
CALIBRATION_SAMPLE_RATE = 100
# Sound setting
soundOn = True
USER_ID = None
SESSION_ID = None
TERMINAL_NOTIFIER_INSTALLED = None
CASCPATH = 'face.xml'
# CASCPATH = pyInstallerResourcePath('haarcascade_eye_tree_eyeglasses.xml')
FACECASCADE = CascadeClassifier(pyInstallerResourcePath(CASCPATH))
print("path:", pyInstallerResourcePath(CASCPATH))
APP_ICON_PATH = pyInstallerResourcePath('posture.png')
def trace(frame, event, arg):
print(("%s, %s:%d" % (event, frame.f_code.co_filename, frame.f_lineno)))
return trace
def getFaces(frame):
gray = cvtColor(frame, COLOR_BGR2GRAY)
faces = FACECASCADE.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=2,
minSize=(100, 100),
# flags=cv2.cv.CV_HAAR_SCALE_IMAGE
flags=0)
if len(faces):
print("Face found: ", faces[0])
return faces
class Sensei(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
print("meta:", USER_ID, SESSION_ID)
self.history = {}
self.history[USER_ID] = {}
self.history[USER_ID][SESSION_ID] = {}
self.history[USER_ID][SESSION_ID][datetime.datetime.now().strftime(
'%Y-%m-%d_%H-%M-%S')] = "sensitivity: " + str(SENSITIVITY)
# Create the worker Thread
# TODO: See if any advantage to using thread or if timer alone works.
# TODO: Compare to workerThread example at
# https://stackoverflow.com/questions/31945125/pyqt5-qthread-passing-variables-to-main
self.capture = Capture(self)
self.timer = QTimer(self, timeout=self.calibrate)
self.mode = 0 # 0: Initial, 1: Calibrate, 2: Monitor
self.checkDependencies()
def checkDependencies(self):
global TERMINAL_NOTIFIER_INSTALLED
if 'darwin' in sys.platform and not TERMINAL_NOTIFIER_INSTALLED:
# FIXME: Add check for Brew installation and installation of
# terminal-notifier.
self.instructions.setText(
'Installing terminal-notifier dependency')
print('Installing terminal-notifier is required')
# FIXME: This line hangs.
# subprocess.call(
# ['brew', 'install', 'terminal-notifier'], stdout=subprocess.PIPE)
# TERMINAL_NOTIFIER_INSTALLED = True
self.instructions.setText('Sit upright and click \'Calibrate\'')
def aboutEvent(self, event):
dialog = QDialog()
aboutDialog = Ui_AboutWindow()
aboutDialog.setupUi(dialog)
aboutDialog.githubButton.clicked.connect(self.openGitHub)
dialog.exec_()
self.trayIcon.showMessage("Notice 🙇👊", "Keep strait posture",
QSystemTrayIcon.Information, 4000)
def closeEvent(self, event):
""" Override QWidget close event to save history on exit. """
# TODO: Replace with CSV method.
# if os.path.exists('posture.dat'):
# with open('posture.dat','rb') as saved_history:
# history =pickle.load(saved_history)
if self.history:
if hasattr(sys, "_MEIPASS"): # PyInstaller deployed
here = os.path.join(sys._MEIPASS)
else:
here = os.path.dirname(os.path.realpath(__file__))
directory = os.path.join(here, 'data', str(USER_ID))
# directory = os.path.join(here, 'data', 'test')
if not os.path.exists(directory):
os.makedirs(directory)
with open(os.path.join(directory,
str(SESSION_ID) + '.dat'), 'wb') as f:
pickle.dump(self.history, f)
qApp.quit()
def start(self):
self.timer.start()
def stop(self):
self.timer.stop()
def initUI(self):
menu = QMenu()
# Use Buddha in place of smiley face
# iconPath = pyInstallerResourcePath('exit-gray.png')
iconPath = pyInstallerResourcePath('meditate.png')
self.trayIcon = QSystemTrayIcon(self)
supported = self.trayIcon.supportsMessages()
self.trayIcon.setIcon(QIcon(iconPath))
self.trayIcon.setContextMenu(menu)
self.trayIcon.showMessage('a', 'b')
self.trayIcon.show()
self.postureIcon = QSystemTrayIcon(self)
self.postureIcon.setIcon(QIcon(pyInstallerResourcePath('posture.png')))
self.postureIcon.setContextMenu(menu)
self.postureIcon.show()
exitAction = QAction(
"&Quit Sensei", self, shortcut="Ctrl+Q", triggered=self.closeEvent)
preferencesAction = QAction(
"&Preferences...", self, triggered=self.showApp)
# preferencesAction.setStatusTip('Sensei Preferences')
aboutAction = QAction("&About Sensei", self, triggered=self.aboutEvent)
menu.addAction(aboutAction)
menu.addSeparator()
menu.addAction(preferencesAction)
menu.addSeparator()
menu.addAction(exitAction)
optionsMenu = menu.addMenu('&Options')
soundToggleAction = QAction(
"Toggle Sound", self, triggered=self.toggleSound)
optionsMenu.addAction(soundToggleAction)
# TODO: Add settings panel.
# changeSettings = QAction(QIcon('exit.png'), "&Settings", self, shortcut="Cmd+,", triggered=self.changeSettings)
# changeSettings.setStatusTip('Change Settings')
# menu.addAction(changeSettings)
self.pbar = QProgressBar(self)
self.pbar.setGeometry(30, 40, 200, 25)
self.pbarValue = 0
self.setGeometry(300, 300, 290, 150)
self.setWindowTitle('Posture Monitor')
self.startButton = QPushButton('Calibrate', self)
self.startButton.move(30, 60)
self.startButton.clicked.connect(self.calibrate)
self.stopButton = QPushButton('Stop', self)
self.stopButton.move(30, 60)
self.stopButton.clicked.connect(self.endCalibration)
self.stopButton.hide()
self.settingsButton = QPushButton('Settings', self)
self.settingsButton.move(140, 60)
self.settingsButton.clicked.connect(self.settings)
self.doneButton = QPushButton('Done', self)
self.doneButton.move(30, 60)
self.doneButton.hide()
self.doneButton.clicked.connect(self.minimize)
# TODO: Create QWidget panel for Settings with MONITOR_DELAY
# and SENSITIVITY options.
# layout = QFormLayout()
# self.le = QLineEdit()
self.instructions = QLabel(self)
self.instructions.move(40, 20)
self.instructions.setText('Sit upright and click \'Calibrate\'')
self.instructions.setGeometry(40, 20, 230, 25)
if not supported:
self.instructions.setText(
'Error: Notification is not available on your system.')
self.show()
def toggleSound(self):
global soundOn # FIXME: Replace with preferences dictionary or similar
soundOn = not soundOn
def minimize(self):
self.reset()
self.hide()
def reset(self):
pass
def showApp(self):
self.show()
self.raise_()
self.doneButton.hide()
self.startButton.show()
self.pbar.show()
self.settingsButton.show()
self.activateWindow()
def settings(self):
global MONITOR_DELAY
seconds, ok = QInputDialog.getInt(
self, "Delay Settings",
"Enter number of seconds to check posture\n(Default = 2)")
if ok:
seconds = seconds if seconds >= 1 else 0.5
MONITOR_DELAY = seconds * 1000
def endCalibration(self):
self.mode = 2 # Monitor mode
self.timer.stop()
self.stopButton.hide()
self.startButton.setText('Recalibrate') # Keep hidden.
self.instructions.setText('Sit upright and click \'Recalibrate\'')
self.instructions.hide()
self.pbar.hide()
self.settingsButton.hide()
self.history[USER_ID][SESSION_ID][datetime.datetime.now().strftime(
'%Y-%m-%d_%H-%M-%S')] = "baseline: " + str(self.upright)
self.animateClosing()
# Begin monitoring posture.
self.timer = QTimer(self, timeout=self.monitor)
self.timer.start(MONITOR_DELAY)
def animateClosing(self):
self.doneButton.show()
animation = QPropertyAnimation(self.doneButton, b"geometry")
animation.setDuration(1000)
animation.setStartValue(QRect(10, 60, 39, 20))
animation.setEndValue(QRect(120, 60, 39, 20))
animation.start()
self.animation = animation
def monitor(self):
"""
Grab the picture, find the face, and sent notification
if needed.
"""
photo = self.capture.takePhoto()
faces = getFaces(photo)
while not len(faces):
print("No faces detected.")
time.sleep(2)
photo = self.capture.takePhoto()
faces = getFaces(photo)
# Record history for later analyis.
# TODO: Make this into cvs-friendly format.
self.history[USER_ID][SESSION_ID][datetime.datetime.now().strftime(
'%Y-%m-%d_%H-%M-%S')] = faces
x, y, w, h = faces[0]
if w > self.upright * SENSITIVITY:
self.notify(
title='Sensei 🙇👊', # TODO: Add doctor emoji `👨⚕️`
subtitle='Whack!',
message='Sit up strait 🙏⛩',
appIcon=APP_ICON_PATH)
def notify(self, title, subtitle, message, sound=None, appIcon=None):
"""
Mac-only and requires `terminal-notifier` to be installed.
# TODO: Add check that terminal-notifier is installed.
# TODO: Add Linux and windows compatibility.
# TODO: Linux example:
# TODO: sudo apt-get install libnotify-bin
# TODO: from gi.repository import Notify
# TODO: Notify.init("App Name")
# TODO: Notify.Notification.new("Hi").show()
"""
# FIXME: Test following line on windows / linux.
# Doesn't work on Mac and might replace `terminal-notifier` dependency
# self.trayIcon.showMessage('Title', 'Content')
if 'darwin' in sys.platform and TERMINAL_NOTIFIER_INSTALLED: # Check if on a Mac.
t = '-title {!r}'.format(title)
s = '-subtitle {!r}'.format(subtitle)
m = '-message {!r}'.format(message)
snd = '-sound {!r}'.format(sound)
i = '-appIcon {!r}'.format(appIcon)
os.system('terminal-notifier {}'.format(' '.join([m, t, s, snd,
i])))
else:
self.trayIcon.showMessage("Notice 🙇👊", "Keep strait posture",
QSystemTrayIcon.Information, 4000)
def calibrate(self):
if self.mode == 2: # Came from 'Recalibrate'
# Set up for calibrate mode.
self.mode = 1
self.stopButton.show()
self.startButton.hide()
self.instructions.setText('Press \'stop\' when ready')
self.timer.stop()
self.timer = QTimer(self, timeout=self.calibrate)
self.timer.start(CALIBRATION_SAMPLE_RATE)
# Interpolate posture information from face.
photo = self.capture.takePhoto()
faces = getFaces(photo)
while not len(faces):
print("No faces detected.")
time.sleep(2)
photo = self.capture.takePhoto()
faces = getFaces(photo)
# TODO: Focus on user's face rather than artifacts of face detector of others
# on camera
# if len(faces) > 1:
# print(faces) # Take argmax of faces
x, y, w, h = faces[0]
self.upright = w
self.history[USER_ID][SESSION_ID][datetime.datetime.now().strftime(
'%Y-%m-%d_%H-%M-%S') + ': calibration'] = self.upright
self.history["upright_face_width"] = self.upright
if self.mode == 0: # Initial mode
self.timer.start(CALIBRATION_SAMPLE_RATE)
self.startButton.hide()
self.stopButton.show()
self.instructions.setText('Press \'stop\' when ready')
self.mode = 1 # Calibrate mode
elif self.mode == 1:
# Update posture monitor bar.
self.pbar.setValue(self.upright / 4)
time.sleep(0.05)
def openGitHub(self):
import webbrowser
webbrowser.open_new_tab('https://github.com/JustinShenk/sensei')
class Capture(QThread):
def __init__(self, window):
super(Capture, self).__init__(window)
self.window = window
self.capturing = False
self.cam = VideoCapture(0)
self.cam.set(3, 640)
self.cam.set(4, 480)
def takePhoto(self):
if not self.cam.isOpened():
self.cam.open(0)
waitKey(5)
_, frame = self.cam.read()
# cv2.imwrite('tst.png', frame)
waitKey(1)
# Optional - save image.
# cv2.imwrite('save.png', frame)
return frame
def processCLArgs():
""" Process command line arguments to work with QApplication. """
parser = argparse.ArgumentParser()
parser.add_argument(
"-d", "--debug", help="Debug mode", action="store_true")
# TODO: Add to `Session ID` to settings menu.
parser.add_argument("--session", help="Session ID", action="store")
parser.add_argument("--user", help="User ID", action="store")
parsed_args, unparsed_args = parser.parse_known_args()
return parsed_args, unparsed_args
def main():
parsed_args, unparsed_args = processCLArgs()
SESSION_ID = parsed_args.session
USER_ID = parsed_args.user
# (Debug mode) Set global debug tracing option.
if parsed_args.debug:
sys.settrace(trace)
# Check dependency.
TERMINAL_NOTIFIER_INSTALLED = True if os.path.exists(
'/usr/local/bin/terminal-notifier') else False
# QApplication expects the first argument to be the program name.
qt_args = sys.argv[:1] + unparsed_args
app = QApplication(qt_args)
sensei = Sensei()
sys.exit(app.exec_())
if __name__ == '__main__':
main()