-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
306 lines (235 loc) · 9.8 KB
/
app.py
File metadata and controls
306 lines (235 loc) · 9.8 KB
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
from asyncio import staggered
from turtle import right
import cv2
import time
from matplotlib import container
import physiotherapyAssessment as pta
import poseDetector as pd
import postureCorrector as pc
from pygame import mixer, event
import tkinter as tk
from tkinter import ttk
from PIL import Image, ImageTk
mixer.init()
beepSound = mixer.Sound("beep.mp3")
situpSound = mixer.Sound("situp.mp3")
congoShoulder = mixer.Sound("congoShoulder.mp3")
congoNeck = mixer.Sound("congoNeck.mp3")
def show_frame(frame):
frame.tkraise()
def postureCode(page):
show_frame(page)
videoLabel = tk.Label(posturePage)
videoLabel.pack(padx=20)
# initialize the mixer
mixer.init()
# loading the model
model = pd.poseDetector(conDet=0.75, conTrack=0.75)
# selecting video source
cap = cv2.VideoCapture(0)
# cap = cv2.VideoCapture("1.mp4")
def showVideoFrame():
landmarks = model.getPosition(model.findPose(cap.read()[1]))
corrector = pc.postureCorrector(landmarks=landmarks)
# time.sleep(0.1)
cv2image = cv2.cvtColor(cap.read()[1], cv2.COLOR_BGR2RGBA)
img = Image.fromarray(cv2image)
imgtk = ImageTk.PhotoImage(image=img)
videoLabel.imgtk = imgtk
videoLabel.configure(image=imgtk)
videoLabel.after(20, showVideoFrame)
# check if the posture is correct
if not mixer.get_busy():
if corrector.checkSlump():
# print("Sit up, you are slumping!")
situpSound.play()
# read the frame
success, img = cap.read()
img = model.findPose(img)
showVideoFrame()
def shoulderCode(frame):
show_frame(frame)
# initialize the mixer
mixer.init()
videoLabel = tk.Label(shoulderPage)
videoLabel.pack(padx=20)
counterVar = tk.StringVar()
counterVar.set("Counter: "+"0")
counterLabel = tk.Label(shoulderPage, textvariable=counterVar, font=("Helvetica", 20))
counterLabel.pack()
# loading the model
model = pd.poseDetector(conDet=0.75, conTrack=0.75)
# selecting video source
cap = cv2.VideoCapture(0)
# cap = cv2.VideoCapture("1.mp4")
pt = 0
dictStage = {"stage": "None", "counter": 0}
def showVideoFrame():
landmarks = model.getPosition(model.findPose(cap.read()[1]))
# print(cap.read()[0])
cv2image = cv2.cvtColor(cap.read()[1], cv2.COLOR_BGR2RGBA)
img = Image.fromarray(cv2image)
imgtk = ImageTk.PhotoImage(image=img)
videoLabel.imgtk = imgtk
videoLabel.configure(image=imgtk)
videoLabel.after(40, showVideoFrame)
# check shoulder movement score
if not mixer.get_busy():
if dictStage["counter"] == 15:
congoShoulder.play()
counterVar.set("Congrats! You did great!")
return
if "LEFT_SHOULDER" in landmarks and "RIGHT_SHOULDER" in landmarks and "LEFT_ELBOW" in landmarks and "RIGHT_ELBOW" in landmarks:
leftAngle = model.getAngle(landmarks["LEFT_HIP"], landmarks["LEFT_SHOULDER"], landmarks["LEFT_ELBOW"])
# print("Shoulder Score: ", leftAngle)
rightAngle = 180 - model.getAngle(landmarks["RIGHT_HIP"], landmarks["RIGHT_SHOULDER"], landmarks["RIGHT_ELBOW"])
# print("Shoulder Score: ", rightAngle)
# if leftAngle and rightAngle:
# beepSound.play()
# print(int(leftAngle), int(rightAngle))
if leftAngle > 160 and rightAngle > 160:
# beepSound.play()
dictStage["stage"] = "Up"
if leftAngle < 30 and rightAngle < 30 and dictStage["stage"] == "Up":
beepSound.play()
dictStage["stage"] = "Down"
dictStage["counter"] += 1
print("Counter: ", dictStage["counter"])
counterVar.set("Counter: "+str(dictStage["counter"]))
window.update_idletasks()
showVideoFrame()
def neckCode(frame):
show_frame(frame)
# initialize the mixer
mixer.init()
videoLabel = tk.Label(neckPage)
videoLabel.pack(padx=20)
counterVar = tk.StringVar()
counterVar.set("Counter: "+"0")
counterLabel = tk.Label(neckPage, textvariable=counterVar, font=("Helvetica", 20))
counterLabel.pack()
# loading the model
model = pd.poseDetector(conDet=0.75, conTrack=0.75)
# selecting video source
cap = cv2.VideoCapture(0)
# cap = cv2.VideoCapture("1.mp4")
pt = 0
dictStage = {"stage": "None", "counter": 0}
def showVideoFrame():
landmarks = model.getPosition(model.findPose(cap.read()[1]))
corrector = pc.postureCorrector(landmarks=landmarks)
cv2image = cv2.cvtColor(cap.read()[1], cv2.COLOR_BGR2RGBA)
img = Image.fromarray(cv2image)
imgtk = ImageTk.PhotoImage(image=img)
videoLabel.imgtk = imgtk
videoLabel.configure(image=imgtk)
videoLabel.after(40, showVideoFrame)
if not mixer.get_busy():
if dictStage["counter"] == 15:
congoNeck.play()
counterVar.set("Congrats! You did great!")
# time.sleep(2)
return
try:
if not corrector.checkSlump():
# beepSound.play()
dictStage["stage"] = "Up"
elif corrector.checkSlump() and dictStage["stage"] == "Up":
beepSound.play()
dictStage["stage"] = "Down"
dictStage["counter"] += 1
print("Counter: ", dictStage["counter"])
counterVar.set("Counter: "+str(dictStage["counter"]))
window.update_idletasks()
except:
pass
# initialise loop variables
# read the frame
success, img = cap.read()
img = model.findPose(img)
showVideoFrame()
window = tk.Tk()
window.title("Physiotherapy Assessment")
window.state('zoomed')
window.rowconfigure(0, weight = 1)
window.columnconfigure(0, weight = 1)
homePage, posturePage, shoulderPage, neckPage = tk.Frame(window), tk.Frame(window), tk.Frame(window), tk.Frame(window)
for frame in (homePage, shoulderPage, neckPage, posturePage):
frame.grid(row = 0, column = 0, sticky = 'nsew')
# code for Home Page
homeTitle = ttk.Label(homePage, text = "Physiotherapy Assessment", font = ("Helvetica", 30))
homeTitle.pack(fill="both", expand=True)
shoulderAssessmentButton = ttk.Button(homePage, text = "Shoulder Assessment", command = lambda:shoulderCode(shoulderPage))
shoulderAssessmentButton.pack(pady = 50)
postureCorrectorButton = ttk.Button(homePage, text = "Posture Corrector", command = lambda:postureCode(posturePage))
postureCorrectorButton.pack(pady = 80)
neckButton = ttk.Button(homePage, text = "Neck Assessment", command = lambda:neckCode(neckPage))
neckButton.pack(pady = 110)
# code for Shoulder Page
shoulderTitle = ttk.Label(shoulderPage, text = "Shoulder Assessment", font = ("Helvetica", 50))
shoulderTitle.pack(fill="both")
# code for Posture Page
postureTitle = ttk.Label(posturePage, text = "Posture Corrector", font = ("Helvetica", 50))
postureTitle.pack(fill="both")
neckTitle = ttk.Label(neckPage, text = "Neck Assessment", font = ("Helvetica", 50))
neckTitle.pack(fill="both")
show_frame(homePage)
window.mainloop()
# # initialize the mixer
# mixer.init()
# # loading the model
# model = pd.poseDetector(conDet=0.75, conTrack=0.75)
# # selecting video source
# cap = cv2.VideoCapture(0)
# # cap = cv2.VideoCapture("1.mp4")
# # initialise loop variables
# pt = 0
# i = 0
# counter = 0
# dictStage["stage"] = None
# while cap.isopened():
# i += 1
# # read the frame
# success, img = cap.read()
# img = model.findPose(img)
# # get the landmarks
# landmarks = model.getPosition(img)
# corrector = pc.postureCorrector(landmarks=landmarks)
# # draw neck landmark
# if "LEFT_SHOULDER" in landmarks and "RIGHT_SHOULDER" in landmarks:
# nx = int((landmarks["RIGHT_SHOULDER"].x + landmarks["LEFT_SHOULDER"].x)/2)
# ny = int((landmarks["RIGHT_SHOULDER"].y + landmarks["LEFT_SHOULDER"].y)/2)
# cv2.circle(img, (nx, ny), 2, (0, 0, 255), -1)
# # # check posture every 60 frames
# # if i % 60 == 0:
# # # check if the posture is correct
# # if corrector.checkSlump():
# # print("Sit up, you are slumping!")
# # situpSound = mixer.Sound("situp.mp3")
# # situpSound.play()
# # if i % 30 == 0:
# # check shoulder movement score
# if "LEFT_SHOULDER" in landmarks and "RIGHT_SHOULDER" in landmarks and "LEFT_ELBOW" in landmarks and "RIGHT_ELBOW" in landmarks:
# shoulderAssessment = pta.shoulderAssessment(landmarks=landmarks)
# shoulderScore = shoulderAssessment.getScoreList()
# leftAngle = int(180-shoulderScore["leftShoulderScore"])
# # print("Shoulder Score: ", leftAngle)
# rightAngle = int(shoulderScore["rightShoulderScore"])
# # print("Shoulder Score: ", rightAngle)
# if leftAngle > 160 and rightAngle > 160:
# dictStage["stage"] = "Up"
# if leftAngle < 30 and rightAngle < 30 and dictStage["stage"] == "Up":
# dictStage["stage"] = "Down"
# counter += 1
# print("Counter: ", counter)
# # check fps
# ct = time.time()
# fps = 1/(ct-pt)
# pt = ct
# cv2.putText(img, str(int(fps)), (70, 50),
# cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
# cv2.imshow("Image", img)
# cv2.waitKey(1)
# # end code after 1000 frames
# if i == 1000:
# break