-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
96 lines (58 loc) · 3.33 KB
/
app.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
import cv2
import mediapipe as mp
capture = cv2.VideoCapture(0)
width, height = capture.get(cv2.CAP_PROP_FRAME_WIDTH), capture.get(cv2.CAP_PROP_FRAME_HEIGHT)
mp_drawing, mp_drawing_styles, mp_pose = mp.solutions.drawing_utils, mp.solutions.drawing_styles, mp.solutions.pose
def coordinates(processed):
lshoulder = processed.pose_landmarks.landmark[mp_pose.PoseLandmark.LEFT_SHOULDER]
lshoulderx, lshouldery = lshoulder.x * width, lshoulder.y * height
rshoulder = processed.pose_landmarks.landmark[mp_pose.PoseLandmark.RIGHT_SHOULDER]
rshoulderx, rshouldery = rshoulder.x * width, rshoulder.y * height
lelbow = processed.pose_landmarks.landmark[mp_pose.PoseLandmark.LEFT_ELBOW]
lelbowx, lelbowy = lelbow.x * width, lelbow.y * height
relbow = processed.pose_landmarks.landmark[mp_pose.PoseLandmark.RIGHT_ELBOW]
relbowx, relbowy = relbow.x * width, relbow.y * height
lwrist = processed.pose_landmarks.landmark[mp_pose.PoseLandmark.LEFT_WRIST]
lwristx, lwristy = lwrist.x * width, lwrist.y * height
rwrist = processed.pose_landmarks.landmark[mp_pose.PoseLandmark.RIGHT_WRIST]
rwristx, rwristy = rwrist.x * width, rwrist.y * height
rhip = processed.pose_landmarks.landmark[mp_pose.PoseLandmark.RIGHT_HIP]
rhipx, rhipy = rhip.x * width, rhip.y * height
lhip = processed.pose_landmarks.landmark[mp_pose.PoseLandmark.LEFT_HIP]
lhipx, lhipy = lhip.x * width, lhip.y * height
rknee = processed.pose_landmarks.landmark[mp_pose.PoseLandmark.RIGHT_KNEE]
rkneex, rkneey = rknee.x * width, rknee.y * height
lknee = processed.pose_landmarks.landmark[mp_pose.PoseLandmark.LEFT_KNEE]
lkneex, lkneey = lknee.x * width, lknee.y * height
rankle = processed.pose_landmarks.landmark[mp_pose.PoseLandmark.RIGHT_ANKLE]
ranklex, rankley = rankle.x * width, rankle.y * height
lankle = processed.pose_landmarks.landmark[mp_pose.PoseLandmark.LEFT_ANKLE]
lanklex, lankley = lankle.x * width, lankle.y * height
return lshoulderx, lshouldery, rshoulderx, rshouldery, lelbowx, lelbowy, relbowx, relbowy, lwristx, lwristy, rwristx, rwristy, rhipx, rhipy, lhipx, lhipy, rkneex, rkneey, lkneex, lkneey, ranklex, rankley, lanklex, lankley
with mp_pose.Pose(min_detection_confidence = 0.8, min_tracking_confidence = 0.8) as pose:
log = open("log.txt", "w")
while capture.isOpened():
read, canvas = capture.read()
if not read:
print("Dropped a Frame")
continue
# Temporarily mark the image as not writeable to improve performance when passing to mediapipe
canvas.flags.writeable = False
canvas = cv2.cvtColor(canvas, cv2.COLOR_BGR2RGB)
processed = pose.process(canvas)
canvas.flags.writeable = True
canvas = cv2.cvtColor(canvas, cv2.COLOR_RGB2BGR)
if processed.pose_landmarks:
mp_drawing.draw_landmarks(
canvas,
processed.pose_landmarks,
mp_pose.POSE_CONNECTIONS,
landmark_drawing_spec = mp_drawing_styles.get_default_pose_landmarks_style()
)
log.write(f"{coordinates(processed)[1:-1]}\n")
else:
log.write("None, " * 23 + "None\n")
cv2.imshow("EchoPose", cv2.flip(canvas, 1)) # Flip video
if cv2.waitKey(5) & 0xFF in (27, 81, 113):
break
capture.release()