This repository was archived by the owner on Jan 23, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtyler.py
More file actions
106 lines (74 loc) · 2.63 KB
/
tyler.py
File metadata and controls
106 lines (74 loc) · 2.63 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import threading
import numpy as np
import cv2
import mediapipe as mp
import time
mp_pose = mp.solutions.pose
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
class Controller:
def __init__(self):
self.vid = cv2.VideoCapture(0)
self.vid.set(3,400)
self.vid.set(4,400)
self.frame = None
self.landmarks = None
self.thread1 = threading.Thread(target = self.grabFrame)
self.alive = True
# Controller controls
self.jump = False
self.right = False
self.left = False
def destroy(self):
self.alive = False
self.vid.release()
def grabFrame(self):
while self.alive:
# Get image
__, self.frame = self.vid.read()
self.frame = cv2.flip(self.frame, 1)
self.frame = cv2.line(self.frame, (0, int(self.frame.shape[0] * .70)), (self.frame.shape[1], int(self.frame.shape[0] * .70)), (0, 255, 0), 1)
self.frame = cv2.line(self.frame, (0, int(self.frame.shape[0] * 0.30)), (self.frame.shape[1], int(self.frame.shape[0] * 0.30)), (255, 0, 0), 1)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Label image
with mp_pose.Pose(min_detection_confidence=0.5, model_complexity=0) as pose:
results = pose.process(cv2.cvtColor(self.frame, cv2.COLOR_BGR2RGB))
mp_drawing.draw_landmarks(
self.frame,
results.pose_landmarks,
mp_pose.POSE_CONNECTIONS,
landmark_drawing_spec=mp_drawing_styles.get_default_pose_landmarks_style())
if results.pose_landmarks != None:
# Is moving
self.landmarks = results.pose_landmarks
l_x = self.landmarks.landmark[25].x
l_y = self.landmarks.landmark[25].y
r_x = self.landmarks.landmark[26].x
r_y = self.landmarks.landmark[26].y
j_r = self.landmarks.landmark[20].y
j_l = self.landmarks.landmark[21].y
if r_x > 0.5 and (r_y < 0.70 or l_y < 0.70):
self.right = True
else:
self.right = False
if l_x < 0.5 and (l_y < 0.70 or r_y < 0.70):
self.left = True
else:
self.left = False
if j_r < 0.3 and j_l < 0.3:
self.jump = True
else:
self.jump = False
cv2.imshow('frame', self.frame)
print("Threading stopped")
def startThread(self, gameOver):
self.thread1.start()
if __name__ == "__main__":
myController = Controller()
time.sleep(3)
while True:
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cv2.imshow('frame', myController.frame)
print(myController.frame)