-
Notifications
You must be signed in to change notification settings - Fork 55
/
main.py
90 lines (66 loc) · 3.08 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
# importing the libraries
import cv2
import mediapipe as mp
from controls import *
import threading
# initializing the pose estimation using mediapipe
mp_drawing = mp.solutions.drawing_utils
mp_pose = mp.solutions.pose
# reading the live web cam feed
cap = cv2.VideoCapture(0)
# setting up the mediapipe instance
with mp_pose.Pose(min_detection_confidence=0.5, min_tracking_confidence= 0.5) as pose:
while cap.isOpened():
ret, frame = cap.read()
# recoloring the image to rgb
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
image.flags.writeable = False
# making actual prediction
results = pose.process(image)
# recoloring the image back to bgr
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
# extracting the landmarks
try:
landmarks = results.pose_landmarks.landmark
left_shoulder = [landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].x,
landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value].y]
right_shoulder = [landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value].x,
landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value].y]
mid_point_of_shoulder = [ (left_shoulder[0] * 1000 + right_shoulder[0] * 1000)//2,
(left_shoulder[1] * 1000 + right_shoulder[1] * 1000)//2 ]
#print(int(mid_point_of_shoulder[1]))
left_threshold = 450
right_threshold = 575
top_threshold = 420
# # if x coordinate passes left threshold move left
if int(mid_point_of_shoulder[0]) < int(left_threshold):
t1 = threading.Thread(target=move_left)
t1.start()
#print("left")
# # if x coordinate passes right threshold move right
if int(mid_point_of_shoulder[0]) > int(right_threshold):
# for i in range(400):
# move_right()
t2 = threading.Thread(target=move_right)
t2.start()
# # if y coordinate passes aboe the top threshold move up
if int(mid_point_of_shoulder[1]) < int(top_threshold):
jump()
#print("jump")
else:
do_nothing()
except Exception as e :
print("enter exception", e)
do_nothing()
# rendering the detections
# to show the actual lines on the body
# mp_drawing.draw_landmarks(image, results.pose_landmarks, mp_pose.POSE_CONNECTIONS,
# mp_drawing.DrawingSpec(color=(245, 117, 66), thickness=2, circle_radius=2),
# mp_drawing.DrawingSpec(color=(245, 66, 230), thickness=2, circle_radius=2)
# )
cv2.imshow('Cam Feed', image)
if cv2.waitKey(10) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()