-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
83 lines (66 loc) · 2.21 KB
/
main.py
File metadata and controls
83 lines (66 loc) · 2.21 KB
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
import pickle
import cv2
import numpy as np
import mediapipe as mp
from controller.Controller import Controller
model_path = "./models/pose_model_5.pkl"
def main():
# set the control scheme
controls = {
"crouch": ["s"],
"jump_front": ["w"],
"jump_left": ["a", "w"],
"jump_right": ["d", "w"],
"neutral": [],
"pause": ["p"],
"walk_left": ["a"],
"walk_right": ["d"],
}
mp_pose = mp.solutions.pose
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
# Instantiate controllers
try:
with open(model_path, "rb") as f:
controller = Controller(pickle.load(f), controls)
print("Successfully loaded model")
except IOError:
print("failed to open model")
vid = cv2.VideoCapture(1)
with mp_pose.Pose(
min_tracking_confidence=0.5,
min_detection_confidence=0.5,
model_complexity=1,
smooth_landmarks=True,
) as pose:
while vid.isOpened():
# read webcam image
success, image = vid.read()
# skip empty frames
if not success:
continue
# calculate pose
results = pose.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
if results != None and results.pose_landmarks != None:
row = []
for landmark in results.pose_landmarks.landmark:
row.append(landmark.x)
row.append(landmark.y)
row.append(landmark.z)
controller.do_action(row)
# draw 3D pose landmarks live
mp_drawing.draw_landmarks(
image,
results.pose_landmarks,
mp_pose.POSE_CONNECTIONS,
landmark_drawing_spec=mp_drawing_styles.get_default_pose_landmarks_style())
# draw image
cv2.imshow("MediaPipePose", cv2.flip(image, 1))
if cv2.waitKey(5) & 0xFF == ord('q'):
break
# After the loop release the cap object
vid.release()
# Destroy all the windows
cv2.destroyAllWindows()
if __name__ == "__main__":
main()