-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest1.py
97 lines (82 loc) · 3.87 KB
/
test1.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
import cv2
import mediapipe as mp
import numpy as np
import time
# The first test - the TUG test
# Returns the total time in Seconds:
def test1():
# start the timer
start_time = time.time()
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_pose = mp.solutions.pose
# For webcam input:
cap = cv2.VideoCapture(0)
with mp_pose.Pose(
min_detection_confidence=0.5,
min_tracking_confidence=0.5) as pose:
# initialize variables
leftStart_y = 0
rightStart_y = 0
leftStart_x = 0
rightStart_x = 0
rightPrev_x = 0
leftPrev_x = 0
frame = 0
rightx_distance = 0
leftx_distance = 0
while cap.isOpened():
success, image = cap.read()
if not success:
print("Ignoring empty camera frame.")
# If loading a video, use 'break' instead of 'continue'.
continue
# To improve performance, optionally mark the image as not writeable to
# pass by reference.
image.flags.writeable = False
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image_height, image_width, _ = image.shape
results = pose.process(image)
if 'landmark' in dir(results.pose_world_landmarks):
right_shoulder = results.pose_landmarks.landmark[mp_pose.PoseLandmark.RIGHT_SHOULDER]
right_shoulder_pos = np.array([right_shoulder.x * image_width, right_shoulder.y * image_height, right_shoulder.z])
left_shoulder = results.pose_landmarks.landmark[mp_pose.PoseLandmark.LEFT_SHOULDER]
left_shoulder_pos = np.array([left_shoulder.x * image_width, left_shoulder.y * image_height, left_shoulder.z])
rightCurr_x = right_shoulder_pos[0]
rightCurr_y = right_shoulder_pos[1]
leftCurr_x = left_shoulder_pos[0]
leftCurr_y = left_shoulder_pos[1]
# Set start states
if (frame == 1):
rightStart_y = rightCurr_y
rightStart_x = rightCurr_x
leftStart_y = leftCurr_y
leftStart_x = leftCurr_x
rightPrev_x = rightStart_x
leftPrev_x = leftStart_x
# Calculate moved distance in x axis of both shoulders
rightx_distance += abs(rightCurr_x - rightPrev_x)
leftx_distance += abs(leftCurr_x - leftPrev_x)
# If the person is sitting and has moved along the x axis for enough distance, stop the timer
if (abs(rightCurr_y - rightStart_y) < image_height * 0.05) and (abs(leftCurr_y - leftStart_y) < image_height * 0.05) \
and (rightx_distance > image_width * 0.4) and (leftx_distance > image_width * 0.4):
total_time = time.time() - start_time # stop the timer, return the total time
cap.release()
return total_time
leftPrev_x = leftCurr_x
rightPrev_x = rightCurr_x
# Draw the pose annotation on the image.
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
mp_drawing.draw_landmarks(
image,
results.pose_landmarks,
mp_pose.POSE_CONNECTIONS,
landmark_drawing_spec=mp_drawing_styles.get_default_pose_landmarks_style())
frame += 1
# Flip the image horizontally for a selfie-view display.
cv2.putText(image, "time = " + str(int(time.time() - start_time)), (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 2, cv2.LINE_AA)
cv2.imshow('MediaPipe Pose', image)
if cv2.waitKey(5) & 0xFF == 27:
break
cap.release()