forked from aryanvij02/PushUpCounter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
counter.py
93 lines (74 loc) · 3.13 KB
/
counter.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
import cv2
import numpy as np
from pose import PoseDetector
def main():
cap = cv2.VideoCapture(0)
detector = PoseDetector()
count = 0
direction = 0
form = 0
feedback = "Fix Form"
recordedCount = 0
while cap.isOpened():
ret, img = cap.read() #640 x 480
#Determine dimensions of video - Help with creation of box in Line 43
width = cap.get(3) # float `width`
height = cap.get(4) # float `height`
# print(width, height)
img = detector.findPose(img, False)
lmList = detector.findPosition(img, False)
# print(lmList)
if len(lmList) != 0:
elbow = detector.findAngle(img, 11, 13, 15)
shoulder = detector.findAngle(img, 13, 11, 23)
hip = detector.findAngle(img, 11, 23,25)
#Percentage of success of pushup
per = np.interp(elbow, (90, 160), (0, 100))
#Bar to show Pushup progress
bar = np.interp(elbow, (90, 160), (380, 50))
#Check to ensure right form before starting the program
if elbow > 160 and shoulder > 40 and hip > 160:
form = 1
#Check for full range of motion for the pushup
if form == 1:
if per == 0:
if elbow <= 90 and hip > 160:
feedback = "Up"
if direction == 0:
count += 0.5
direction = 1
else:
feedback = "Fix Form"
if per == 100:
if elbow > 160 and shoulder > 40 and hip > 160:
feedback = "Down"
if direction == 1:
count += 0.5
direction = 0
else:
feedback = "Fix Form"
# form = 0
if count != recordedCount:
print(count)
recordedCount = count
#Draw Bar
if form == 1:
cv2.rectangle(img, (580, 50), (600, 380), (0, 255, 0), 3)
cv2.rectangle(img, (580, int(bar)), (600, 380), (0, 255, 0), cv2.FILLED)
cv2.putText(img, f'{int(per)}%', (565, 430), cv2.FONT_HERSHEY_PLAIN, 2,
(255, 0, 0), 2)
#Pushup counter
cv2.rectangle(img, (0, 380), (100, 480), (0, 255, 0), cv2.FILLED)
cv2.putText(img, str(int(count)), (25, 455), cv2.FONT_HERSHEY_PLAIN, 5,
(255, 0, 0), 5)
#Feedback
cv2.rectangle(img, (500, 0), (640, 40), (255, 255, 255), cv2.FILLED)
cv2.putText(img, feedback, (500, 40 ), cv2.FONT_HERSHEY_PLAIN, 2,
(0, 255, 0), 2)
cv2.imshow('Pushup counter', img)
if cv2.waitKey(10) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
main()