-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.py
402 lines (327 loc) · 11.4 KB
/
App.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
#!/usr/bin/env python3
from kivy.graphics import Color, Rotate, Rectangle
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.core.window import Window
from kivy.uix.image import Image
from kivy.clock import Clock
from kivy.graphics.texture import Texture
from kivy.lang import Builder
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.popup import Popup
from win10toast import ToastNotifier
from datetime import datetime
import webbrowser
import cv2
import requests
# Create both screens. Please note the root.manager.current: this is how
# you can control the ScreenManager from kv. Each screen has by default a
# property manager that gives you the instance of the ScreenManager used.
Builder.load_string("""
<MenuScreen>:
canvas.before:
Color:
rgba: 0.3281, 0.3789, 1, 1
Rectangle:
pos: self.pos
size: self.size
FloatLayout:
size: (420, 420)
pos: (280, 0)
canvas.before:
Color:
rgba: 1, 1, 1, 1
Rectangle:
# self here refers to the widget i.e BoxLayout
pos: self.pos
size: self.size
Image:
source: 'logo.png'
pos: (-350, 0)
size: (50, 50)
Image:
source: 'welcome.png'
pos: (-100, 260)
size: (15, 15)
Label:
text: 'Log in'
color: (0.4023, 0.4023, 0.4023, 1)
font_size: 30
halign: 'left'
text_size: root.width, 30
pos_hint: {'x':0.3, 'y':0.3}
Label:
text: 'Email'
color: (0.6016, 0.6016, 0.6016, 1)
font_size: 18
halign: 'left'
text_size: root.width-20, 20
pos_hint: {'x':0.11, 'y':0.18}
TextInput:
id: identification
size_hint: (0.5, 0.1)
multiline:False
font_size: 18
pos_hint: {'x':0.12, 'y':0.55}
background_color: (1,1,1,1)
Label:
text: 'Password'
color: (0.6016, 0.6016, 0.6016, 1)
halign: 'left'
font_size: 18
text_size: root.width-20, 20
pos_hint: {'x':0.11, 'y':-0.03}
TextInput:
id: password
multiline:False
size_hint: (0.5, 0.1)
password:True
font_size: 18
pos_hint: {'x':0.12, 'y':0.33}
background_color: (1,1,1,1)
Button:
id: loginBtn
text: 'login'
pos_hint: {'x':0.25, 'y':0.05}
size_hint: (0.2, 0.1)
<SettingsScreen>:
canvas.before:
Color:
rgba: 1, 1, 1, 1
Rectangle:
pos: self.pos
size: self.size
FloatLayout:
Image:
id: imageView
source: '<random>.jpg'
allow_stretch: True
size: (960, 480)
pos: (0, 0)
ImageButton:
id: submit
source: 'camera.png'
size_hint: (.15, .15)
pos: (415, 20)
Button:
text: 'Back'
on_press:
root.terminateCam()
root.manager.current = 'running'
size_hint: (.075, .05)
<RunningScreen>:
canvas.before:
Color:
rgba: 0.3281, 0.3789, 1, 1
Rectangle:
pos: self.pos
size: self.size
FloatLayout:
RotateImageButton:
source: 'running2.png'
on_press:
root.terminateCam()
root.manager.current = 'settings'
pos_hint: {'x':0.35, 'y':0.58}
size_hint: (0.3, 0.3)
canvas.before:
PushMatrix
canvas.after:
PopMatrix
Image:
source: 'logo.png'
pos: (0, -0.5)
size: (50, 50)
Image:
id: runningText
source: 'runningText.png'
pos_hint: {'x':0.35, 'y':0.16}
size_hint: (0.3, 0.3)
ImageButton:
id: reportBtn
source: 'goToReport.png'
pos_hint: {'x':0.4, 'y':0}
size_hint: (0.2, 0.2)
<P>:
Label:
id: contents
text: "Nothing"
size_hint: 0.6, 0.2
pos_hint: {"x":0.2, "top":1}
Button:
id: outPopup
text: "Back"
size_hint: 0.8, 0.2
pos_hint: {"x":0.1, "y":0.1}
""")
class ImageButton(ButtonBehavior, Image):
def on_press(self):
print('pressed')
class P(FloatLayout):
def __init__(self, contents, **kwargs):
super(P, self).__init__(**kwargs)
self.ids.contents.text = contents
class RotateImageButton(ButtonBehavior, Image):
def __init__(self, **kwargs):
super(RotateImageButton, self).__init__(**kwargs)
self.angle = 0
self.evn = Clock.schedule_interval(self.update, 0.3)
def on_press(self):
print('pressed')
def update(self, dt):
with self.canvas.before:
Rotate(origin=(490, 424.6), angle=self.angle)
self.angle += dt * 1
# Declare both screens
class MenuScreen(Screen):
def __init__(self, **kwargs):
super(MenuScreen, self).__init__(**kwargs)
self.ids.loginBtn.on_press = self.login
toaster = ToastNotifier()
toaster.show_toast("Posture Notification", "Welcome to POSE!",
icon_path="./wrongPosture.ico",
duration=0)
def on_enter(self, *args):
print("Start")
def login(self):
payload = {"username": self.ids.identification.text, "password": self.ids.password.text}
response = requests.post(url + '/userLogin', json=payload)
global token
try:
token = response.json()['access_token']
print(token)
sm.transition.direction = 'right'
sm.current = 'running'
except Exception as e:
print(e)
show = P(contents="Wrong ID or Password") # Create a new instance of the P class
self.popupWindow = Popup(title="Popup Window", content=show, size_hint=(None, None), size=(200, 200))
print(self.popupWindow)
show.ids.outPopup.on_press = self.popupWindow.dismiss
self.show_popup()
def show_popup(self):
self.popupWindow.open() # show the popup
def exit(self):
exit(0)
class ReportScreen(Screen):
def __init__(self, **kwargs):
super(ReportScreen, self).__init__(**kwargs)
def on_enter(self, *args):
print("running")
class RunningScreen(Screen):
def __init__(self, **kwargs):
super(RunningScreen, self).__init__(**kwargs)
self.ids.reportBtn.on_press = self.goToReport
def on_enter(self, *args):
print("running")
self.count = 1
self.capture = cv2.VideoCapture(0) # init the camera
self.cam = Clock.schedule_interval(self.send, 1.0)
self.result = Clock.schedule_interval(self.recv, 30.0)
pass
def send(self, dt):
try:
grabbed, frame = self.capture.read() # grab the current frame
now = datetime.now()
frame = cv2.resize(frame, (640, 480)) # resize the frame
cv2.imshow('frame', frame)
encoded, buffer = cv2.imencode('.jpg', frame)
img_bytes = buffer.tobytes()
files = {'image': img_bytes}
r = requests.post(
url + '/upload/' + now.strftime("%d-%m-%Y-%H:%M:%S") + "_" + str(self.count) + '_' + '.jpg',
files=files,
headers={'Authorization': 'Bearer ' + token})
self.count += 1
except Exception as e:
print(e)
def recv(self, dt):
try:
self.ids.runningText.source = "waitingText.png"
res = (requests.get(url + '/parse', headers={'Authorization': 'Bearer ' + token}).json())
if res['MFP'] != '0':
toaster = ToastNotifier()
toaster.show_toast("Posture Notification", "Wrong Posture detected!",
icon_path="./wrongPosture.ico",
duration=0)
self.ids.runningText.source = "runningText.png"
except Exception as e:
print(e)
def terminateCam(self):
print("Terminate")
cv2.destroyAllWindows()
self.cam.cancel()
self.capture.release()
def goToReport(self):
targetUrl = url.split(':')[1]+":8020/admin/"+token
print(targetUrl)
webbrowser.get('chrome').open('http:'+targetUrl)
def callback(self):
print('The button is being pressed {}'.format(1))
self.show_popup()
class SettingsScreen(Screen):
def __init__(self, **kwargs):
super(SettingsScreen, self).__init__(**kwargs)
print("SetInit")
print(self.ids)
self.img1 = Image()
self.ids.imageView.texture = self.img1.texture
self.ids.submit.on_press = self.callback
show = P(contents="Successfully Set") # Create a new instance of the P class
self.popupWindow = Popup(title="Popup Window", content=show, size_hint=(None, None), size=(200, 200))
print(self.popupWindow)
show.ids.outPopup.on_press = self.popupWindow.dismiss
def on_enter(self, *args):
self.capture = cv2.VideoCapture(0)
cv2.namedWindow("CV2 Image")
self.cam = Clock.schedule_interval(self.update, 1.0 / 33.0)
pass
def update(self, dt):
# display image from cam in opencv window
ret, frame = self.capture.read()
self.image = frame
overlay = cv2.resize(cv2.imread('human_line.png'), (frame.shape[1], frame.shape[0]))
added_image = cv2.addWeighted(frame, 1, overlay, 1, 0)
cv2.imshow("CV2 Image", frame)
# convert it to texture
buf1 = cv2.flip(added_image, 0)
buf = buf1.tostring()
texture1 = Texture.create(size=(frame.shape[1], frame.shape[0]), colorfmt='bgr')
texture1.blit_buffer(buf, colorfmt='bgr', bufferfmt='ubyte')
# display image from the texture
self.ids.imageView.texture = texture1
def terminateCam(self):
print("Terminate")
cv2.destroyAllWindows()
self.cam.cancel()
self.capture.release()
def callback(self):
print('The button is being pressed {}'.format(1))
cv2.imwrite('image.jpg', self.image)
encoded, buffer = cv2.imencode('.jpg', self.image)
img_bytes = buffer.tobytes()
files = {'image': img_bytes}
r = requests.post(
url + '/initialSet',
files=files,
headers={'Authorization': 'Bearer ' + token})
self.show_popup()
print(r.json())
def show_popup(self):
self.popupWindow.open() # show the popup
# Create the screen manager
sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(SettingsScreen(name='settings'))
sm.add_widget(RunningScreen(name='running'))
# Configuration
url = 'http://52.69.0.213:8000'
chrome_path="C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"
webbrowser.register('chrome', None,webbrowser.BackgroundBrowser(chrome_path),1)
class TestApp(App):
def build(self):
return sm
if __name__ == '__main__':
Window.size = (980, 580)
TestApp().run()