-
Notifications
You must be signed in to change notification settings - Fork 0
/
detector.py
85 lines (51 loc) · 1.85 KB
/
detector.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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import matplotlib.pyplot as plt
import cv2
import numpy as np
from IPython.display import display, Image
import ipywidgets as widgets
from fastai import *
import fastbook
from fastbook import *
from plyer import notification
from PIL import Image
# In[2]:
# fastbook.setup_book()
# In[3]:
posture_model = load_learner('posture_detector_model.pkl') # Load detector model fine-tuned on various sitting postures
# In[4]:
posture = widgets.Label()
# In[5]:
def notify(posture): # Sends system notification to report bad sitting posture
notification.notify(title = 'Posture Notification', message = 'Your current posture is BAD, Please Fix!', app_icon = 'notify-icon.ico',timeout = 10)
# In[6]:
#display(posture)
# In[8]:
def startPostureDetection():
try:
detector = cv2.VideoCapture(0) # Launch camera
except:
print("Cannot Open Device")
try:
ret, frame = detector.read() # Store current position of the computer user
count = 0
while(ret == True):
ret, frame = detector.read() # Store current position of the computer user
posture.value = posture_model.predict(frame)[0] # Predict the user's posture
if (posture.value == 'bad'):
count += 1
if count == 50:
notify(posture.value) # Only send reports of a bad posture when the user has been in that position
count = 0 # for a significant while
if not ret:
detector.release() # Clean up memory
print ("Released Video Resource")
break
except KeyboardInterrupt:
detector.release() # Clean up memory
print("Released Video Resource from KeyboardInterrupt")
pass
startPostureDetection()
# In[ ]: