-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraise.py
126 lines (95 loc) · 3.38 KB
/
raise.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
import argparse
import cv2
import numpy as np
from scipy import ndimage
from tf_pose.estimator import TfPoseEstimator
from tf_pose.networks import get_graph_path, model_wh
def str2bool(v):
return v.lower() in ("yes", "true", "t", "1")
def angle_between_points(a, b, c):
a = np.array(a)
b = np.array(b)
c = np.array(c)
ba = a - b
bc = c - b
cosine_angle = np.dot(ba, bc) / (np.linalg.norm(ba) * np.linalg.norm(bc))
angle = np.arccos(cosine_angle)
return np.degrees(angle)
font = cv2.FONT_HERSHEY_SIMPLEX
bottomLeftCornerOfText = (10, 500)
fontScale = 1.5
lineType = 2
thickness = 5
if __name__ == '__main__':
# Initial arguments
resize = '432x368' # Recommended 432x368 or 656x368 or 1312x736
resize_out_ratio = 4.0
model = 'mobilenet_thin'
vidlocation = 'squat.mp4'
tensorrt = "False"
w, h = model_wh(resize)
print(w, h)
if w > 0 and h > 0:
e = TfPoseEstimator(get_graph_path(model), target_size=(
w, h), trt_bool=str2bool(tensorrt))
else:
e = TfPoseEstimator(get_graph_path(model), target_size=(
432, 368), trt_bool=str2bool(tensorrt))
cam = cv2.VideoCapture(0)
count_of_raises = 0
raise_pos = 0
prev_raise_pos = 0
i = 0
while True:
ret_val, image = cam.read()
if ret_val == False:
print("Video file not found")
break
humans = e.inference(image, resize_to_default=(
w > 0 and h > 0), upsample_size=resize_out_ratio)
if len(humans) < 1:
print("No human detected")
continue
try:
center_1 = (int(humans[0].body_parts[1].x * w),
int(humans[0].body_parts[1].y * h)) # sternum
center_5 = (int(humans[0].body_parts[5].x * w),
int(humans[0].body_parts[5].y * h)) # left shoulder
center_6 = (int(humans[0].body_parts[6].x * w),
int(humans[0].body_parts[6].y * h)) # left elbow
raise_angle = angle_between_points(center_1, center_5, center_6)
if raise_angle >= 150:
raise_pos = 1
fontColor = (0,255,0)
else:
fontColor = (0,0,255)
raise_pos = 0
if prev_raise_pos - raise_pos == 1:
count_of_raises +=1
prev_raise_pos = raise_pos
cv2.putText(image, 'Number of raises: ' + str(count_of_raises),
(0, 0),
font,
fontScale,
fontColor,
lineType,
thickness
)
cv2.putText(image, 'Angle of shoulder: ' + str(round(raise_angle, 1)),
# (100, 200),
center_5,
font,
fontScale,
fontColor,
lineType,
thickness
)
except:
print("Incorrect camera dimensions")
image = TfPoseEstimator.draw_humans(image, humans, imgcopy=False)
cv2.imshow('tf-pose-estimation result',
cv2.resize(image, (0, 0), fx=0.5, fy=0.5))
if cv2.waitKey(1) == 'q':
break
cam.release()
cv2.destroyAllWindows()