-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
173 lines (137 loc) · 4.98 KB
/
main.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
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
import cv2
import mediapipe as mp
import numpy as np
import directkeys
import time
mp_drawing = mp.solutions.drawing_utils
thepose = mp.solutions.pose
def angle_calc(p1, p2, p3):
v1 = np.array([p1.x - p2.x, p1.y - p2.y])
v2 = np.array([p3.x - p2.x, p3.y - p2.y])
dot_product = np.dot(v1, v2)
magnitude1 = np.linalg.norm(v1)
magnitude2 = np.linalg.norm(v2)
cosine_angle = dot_product / (magnitude1 * magnitude2)
angle_radians = np.arccos(cosine_angle)
angle_degrees = np.degrees(angle_radians)
return angle_degrees
def calculate_mean(points):
if len(points) == 0:
return None
mean_point = np.mean(points, axis=0)
return mean_point
cap = cv2.VideoCapture(0)
with thepose.Pose(min_detection_confidence=0.5, min_tracking_confidence=0.5) as pose:
while cap.isOpened():
ret, frame = cap.read()
if not ret:
continue
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
image.flags.writeable = False
results = pose.process(image)
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
# Access landmarks directly using attribute access
if results.pose_landmarks:
poser_landmarks = results.pose_landmarks
# Access specific landmarks using their index
"""landmark12 = poser_landmarks.landmark[12]
landmark14 = poser_landmarks.landmark[14]
landmark16 = poser_landmarks.landmark[16]
h, w, _ = frame.shape
x12, y12 = int(landmark12.x * w), int(landmark12.y * h)
x14, y14 = int(landmark14.x * w), int(landmark14.y * h)
x16, y16 = int(landmark16.x * w), int(landmark16.y * h)
cv2.circle(image, (x12, y12), 5, (0, 255, 0), -1)
cv2.circle(image, (x14, y14), 5, (0, 255, 0), -1)
cv2.circle(image, (x16, y16), 5, (0, 255, 0), -1)"""
p12 = poser_landmarks.landmark[12]
p14 = poser_landmarks.landmark[14]
p16 = poser_landmarks.landmark[16]
angle = angle_calc(p12, p14, p16)
p24=poser_landmarks.landmark[24]
p26=poser_landmarks.landmark[26]
p28=poser_landmarks.landmark[28]
anglel=angle_calc(p24,p26,p28)
if 148<angle and angle<166:
directkeys.PressKey(0x1E)
time.sleep(0.5)
directkeys.ReleaseKey(0x1E)
time.sleep(0.5)
if 40<anglel and anglel<90:
directkeys.PressKey(0x1F)
time.sleep(0.5)
directkeys.ReleaseKey(0x1F)
time.sleep(0.5)
if cv2.waitKey(1) == ord('p'):
break
cap.release()
cv2.destroyAllWindows()import cv2
import mediapipe as mp
import numpy as np
import directkeys
import time
mp_drawing = mp.solutions.drawing_utils
thepose = mp.solutions.pose
def angle_calc(p1, p2, p3):
v1 = np.array([p1.x - p2.x, p1.y - p2.y])
v2 = np.array([p3.x - p2.x, p3.y - p2.y])
dot_product = np.dot(v1, v2)
magnitude1 = np.linalg.norm(v1)
magnitude2 = np.linalg.norm(v2)
cosine_angle = dot_product / (magnitude1 * magnitude2)
angle_radians = np.arccos(cosine_angle)
angle_degrees = np.degrees(angle_radians)
return angle_degrees
def calculate_mean(points):
if len(points) == 0:
return None
mean_point = np.mean(points, axis=0)
return mean_point
cap = cv2.VideoCapture(0)
with thepose.Pose(min_detection_confidence=0.5, min_tracking_confidence=0.5) as pose:
while cap.isOpened():
ret, frame = cap.read()
if not ret:
continue
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
image.flags.writeable = False
results = pose.process(image)
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
# Access landmarks directly using attribute access
if results.pose_landmarks:
poser_landmarks = results.pose_landmarks
# Access specific landmarks using their index
"""landmark12 = poser_landmarks.landmark[12]
landmark14 = poser_landmarks.landmark[14]
landmark16 = poser_landmarks.landmark[16]
h, w, _ = frame.shape
x12, y12 = int(landmark12.x * w), int(landmark12.y * h)
x14, y14 = int(landmark14.x * w), int(landmark14.y * h)
x16, y16 = int(landmark16.x * w), int(landmark16.y * h)
cv2.circle(image, (x12, y12), 5, (0, 255, 0), -1)
cv2.circle(image, (x14, y14), 5, (0, 255, 0), -1)
cv2.circle(image, (x16, y16), 5, (0, 255, 0), -1)"""
p12 = poser_landmarks.landmark[12]
p14 = poser_landmarks.landmark[14]
p16 = poser_landmarks.landmark[16]
angle = angle_calc(p12, p14, p16)
p24=poser_landmarks.landmark[24]
p26=poser_landmarks.landmark[26]
p28=poser_landmarks.landmark[28]
anglel=angle_calc(p24,p26,p28)
if 148<angle and angle<166:
directkeys.PressKey(0x1E)
time.sleep(0.5)
directkeys.ReleaseKey(0x1E)
time.sleep(0.5)
if 40<anglel and anglel<90:
directkeys.PressKey(0x1F)
time.sleep(0.5)
directkeys.ReleaseKey(0x1F)
time.sleep(0.5)
if cv2.waitKey(1) == ord('p'):
break
cap.release()
cv2.destroyAllWindows()