This repository has been archived by the owner on Mar 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
351 lines (304 loc) · 11.8 KB
/
main.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
# -*- coding: utf-8 -*-
import kivy
kivy.require('1.9.0')
from kivy.config import Config
Config.set('graphics','width','700')
Config.set('graphics','height','500')
Config.set('graphics','resizable',False)
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition
from kivy.clock import Clock
from kivy.properties import ObjectProperty
from kivy.core.window import Window
from kivy.lang import Builder
from functools import partial
import thread
import cv2
import os
import openface
import dlib
import sys
import numpy as np
import time
from scipy import misc
from PIL import Image
import time
import notify
Window.clearcolor = (1,1,1,1)
Builder.load_file('gui.kv')
class Settings(Screen):
timeButton = ObjectProperty(None)
instantlyButton = ObjectProperty(None)
intervalButton = ObjectProperty(None)
instantlyText = ObjectProperty(None)
dropDown = ObjectProperty(None)
instantlyTextBg = ObjectProperty(None)
continueButton = ObjectProperty(None)
sidewaysButton = ObjectProperty(None)
notificationInterval = 5
next = 'takePhoto'
def __init__(self, **kwargs):
super(Settings, self).__init__(**kwargs)
self.timeButton.bind(on_release=self.dropDown.open)
def setTime(self, time):
self.notificationInterval = int(time)
self.timeButton.text = time
def on_touch_down(self, touch):
if self.continueButton.collide_point(*touch.pos):
self.manager.current = self.next
self.next = 'trackPosture'
return True
elif self.instantlyButton.collide_point(*touch.pos):
color = self.instantlyTextBg.color
color[3] = 1
self.instantlyTextBg.color = color
color = self.instantlyText.color
color[3] = 1
self.instantlyText.color = color
self.timeButton.disabled = True
elif self.intervalButton.collide_point(*touch.pos):
color = self.instantlyTextBg.color
color[3] = 0
self.instantlyTextBg.color = color
color = self.instantlyText.color
color[3] = 0
self.instantlyText.color = color
self.timeButton.disabled = False
return super(Settings,self).on_touch_down(touch)
def on_leave(self):
global notificationInterval, checkSidewaysMovement
notificationInterval = [self.notificationInterval, 1][self.instantlyButton.state == 'down']
checkSidewaysMovement = self.sidewaysButton.state == 'down'
class TakePhoto(Screen):
image = ObjectProperty(None)
button = ObjectProperty(None)
buttonColors = [(0.9,0.9,0.9,1), (0.9,0.9,0.9,0.5)]
picture = None
takingPictures = False
events = []
faceRec = None
bBox = None
def __init__(self, **kwargs):
super(TakePhoto,self).__init__(**kwargs)
self.faceRec = FaceRecognition()
def update(self, dt):
if self.picture == None:
return
boundingBox = self.faceRec.getBoundingBox(self.picture)
self.button.disabled = not boundingBox
self.button.background_color = self.buttonColors[not boundingBox]
img = self.faceRec.drawBoundingBox(self.picture, boundingBox, (255,0,0))
for i in range(len(img)):
img[i] = img[i][::-1]
outimg = Image.fromarray(img, "RGB")
outimg.save("1.png","PNG")
self.image.reload()
self.image.color = (1,1,1,1)
def on_touch_down(self, touch):
if self.button.collide_point(*touch.pos):
thread.start_new_thread(self.setRef,())
return True
return super(TakePhoto,self).on_touch_down(touch)
def on_enter(self):
self.takingPictures = True
thread.start_new_thread(self.takePictures,())
self.events.append(Clock.schedule_interval(self.update,0.1))
def on_pre_leave(self):
self.takingPictures = False
for event in self.events:
event.cancel()
self.events = []
def takePictures(self):
vc = cv2.VideoCapture(-1)
while self.takingPictures:
rval, img = vc.read()
if not rval:
continue
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
self.picture = img
rval, img = vc.read()
img = np.zeros((len(img),len(img[0]),3),dtype=np.uint8)
outimg = Image.fromarray(img, "RGB")
outimg.save("1.png","PNG")
def setRef(self):
global refBBox
refBBox = None
while not refBBox:
refBBox = self.faceRec.getBoundingBox(self.picture)
self.manager.current = 'trackPosture'
class PostureTracking(Screen):
image = ObjectProperty(None)
settingButton = ObjectProperty(None)
photoButton = ObjectProperty(None)
refBBox = None
notificationInterval = 0
checkSidewaysMovement = 0
events = []
faceRec = None
colors = [(1,0,0,1),(0,1,0,1)]
gray = (0.5,0.5,0.5,1)
multiplier = 1
badPositionCount = 0
badPictureCount = 0
homeIntervals = {1:10, 5:60, 10:60, 15:60}
takingPictures = True
def on_enter(self):
global refBBox, notificationInterval, checkSidewaysMovement
self.refBBox = refBBox
self.notificationInterval = notificationInterval
self.checkSidewaysMovement = checkSidewaysMovement
self.faceRec = FaceRecognition()
self.badPictureCount = 0
self.badPositionCount = 0
self.events.append(Clock.schedule_interval(self.update,self.homeIntervals[self.notificationInterval]))
self.takingPictures = True
self.count = 0
def on_pre_leave(self):
for event in self.events:
event.cancel()
self.events = []
self.image.source = "textures/good.png"
self.takingPictures = False
def on_touch_down(self, touch):
if self.settingButton.collide_point(*touch.pos):
self.manager.current = 'settings'
return True
elif self.photoButton.collide_point(*touch.pos):
self.manager.current = 'takePhoto'
return True
return super(PostureTracking,self).on_touch_down(touch)
def update(self, dt):
self.events.append(Clock.schedule_interval(self.takePicture, 1))
count = 0
def takePicture(self, dt):
self.count+=1
vc = cv2.VideoCapture(-1)
rval, img = vc.read()
if not rval:
notify.send("Posture Tracker", "Webcam is not working")
return
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
bBox = self.faceRec.getBoundingBox(img)
diff = self.faceRec.compareFacePosition(bBox, self.refBBox)
if bBox: #face found
self.badPictureCount = 0
self.badPositionCount += 1
#leaned backwards
if diff[0] < -30 or diff[2] > 20:
self.image.source = "textures/leanedback.png"
#leaned forwards
elif diff[0] > 40:
self.image.source = "textures/crouch.png"
#leaning to the side, only checked when checkSidewaysMovements is true
elif self.checkSidewaysMovement and abs(diff[1]) > 50:
self.image.source = "textures/side.png"
#higher than normal position
elif diff[2] < -20:
self.image.source = "textures/high.png"
#good posture
else:
self.image.source = "textures/good.png"
self.badPositionCount = 0
self.multiplier = 1
#face not found on 10th time
elif self.count == 10:
self.badPictureCount += 1
self.image.source = "textures/notfound.png"
if self.badPictureCount > 2:
notify.send("Posture Tracker", "I can't see you! Are you still there? Brighter lighting might be necessary")
elif self.badPositionCount == self.notificationInterval * self.multiplier:
notify.send("Posture Tracker", "You have been sitting badly for " + str(self.badPositionCount) + " minutes")
elif self.badPositionCount > self.notificationInterval * self.multiplier:
notify.send("Posture Tracker", "It seems you're still sitting badly. Or maybe you should take a new reference picture?")
self.multiplier += 1
if bBox or self.count == 10:
self.image.reload()
self.count = 0
return False
class FaceRecognition():
boundingBox = None
align = openface.AlignDlib("/home/pessi/webryhtikamera/openface/models/dlib/shape_predictor_68_face_landmarks.dat")
def drawBoundingBox(self, img, boundingBox, color):
if not boundingBox:
return img
width = len(img[0])
height = len(img)
left = max(0,boundingBox.left())
right = min(width-1,boundingBox.right())
top = max(0,boundingBox.top())
bottom = min(height-1,boundingBox.bottom())
square = []
for y in range(max(0,top-1), min(bottom+1, width)):
if left > 0:
square.append([y,left-1])
if right < width - 1:
square.append([y,right+1])
square.append([y,left+1])
square.append([y,right-1])
square.append([y,left])
square.append([y,right])
for x in range(max(0,left-1), min(right+1, width)):
if top > 0:
square.append([top-1,x])
if bottom < height - 1:
square.append([bottom+1,x])
square.append([top+1,x])
square.append([bottom-1,x])
square.append([top,x])
square.append([bottom,x])
for pixel in square:
img[pixel[0]][pixel[1]] = color
return img
def setRefPicture(self):
img = self.takePicture()
self.boundingBox = self.getBoundingBox(img)
img = self.drawBoundingBox(img, self.boundingBox, (255,0,0))
self.saveImg(img)
if self.boundingBox != None:
return False
def takeComparisonPicture(self):
if not self.boundingBox:
self.setRefPicture()
return "Face not found"
img = self.takePicture()
boundingBox = self.getBoundingBox(img)
img = self.drawBoundingBox(img, self.boundingBox, (255,0,0))
img = self.drawBoundingBox(img, boundingBox, (255,255,255))
self.saveImg(img)
if not boundingBox or not self.boundingBox:
return "Face not found"
diff = self.compareFacePosition(boundingBox, self.boundingBox)
return "Relative difference in size: " + str(diff[0]) + \
"%\nRelative difference in location: " + str(diff[1]) + "%, " + str(diff[2]) + "%"
def compareFacePosition(self, bBox1, bBox2):
if not bBox1 or not bBox2:
return None
diff_size = int(((bBox1.area()-bBox2.area())*1.0/bBox2.area())*100)
diff_x = int(((bBox1.center().x-bBox2.center().x)*1.0/bBox2.width())*100)
diff_y = int(((bBox1.center().y-bBox2.center().y)*1.0/bBox2.height())*100)
return [diff_size, diff_x, diff_y]
def getBoundingBox(self, img):
boundingBox = self.align.getLargestFaceBoundingBox(img)
return boundingBox
def takePicture(self):
vc = cv2.VideoCapture(-1)
rval, img = vc.read()
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
return img
def saveImg(self, img):
outimg = Image.fromarray(img, "RGB")
outimg.save("1.png","PNG")
sm = ScreenManager(transition=NoTransition())
sm.add_widget(Settings(name='settings'))
sm.add_widget(TakePhoto(name='takePhoto'))
sm.add_widget(PostureTracking(name='trackPosture'))
refBBox = None
notificationInterval = 5 * 60
checkSidewaysMovement = False
class PostureTrackerApp(App):
def build(self):
notify.init("Posture Tracker")
return sm
PostureTrackerApp().run()