-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamevirtual.py
94 lines (75 loc) · 2.51 KB
/
gamevirtual.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
import cv2
import mediapipe as mp
from pynput.keyboard import Controller
import time
mp_hands = mp.solutions.hands.Hands()
keyboard = Controller()
url = 'https://192.168.116.66:8080/' # Corrected the port number
cp = cv2.VideoCapture(0)
x1, x2, y1, y2 = 0, 0, 0, 0
frame_rate = 30
prev = 0
while True:
time_elapsed = time.time() - prev
if time_elapsed > 1.0 / frame_rate:
prev = time.time()
ret, image = cp.read()
if not ret:
break
image_height, image_width, image_depth = image.shape
image = cv2.flip(image, 1)
rgb_img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
output_hands = mp_hands.process(rgb_img)
all_hands = output_hands.multi_hand_landmarks
if all_hands:
hand = all_hands[0]
one_hand_landmark = hand.landmark
for id, lm in enumerate(one_hand_landmark):
x = int(lm.x * image_width)
y = int(lm.y * image_height)
if id == 12:
x1 = x
y1 = y
if id == 0:
x2 = x
y2 = y
distX = x1 - x2
distY = y1 - y2
if distY > -140 and distY != 0:
# press S
keyboard.release('d')
keyboard.release('a')
keyboard.release('w')
keyboard.press('s')
print("S")
if distY < -200 and distY != 0:
keyboard.release('s')
keyboard.release('d')
keyboard.release('a')
keyboard.press('w')
print("W")
if distX < -100 and distX != 0:
keyboard.release('s')
keyboard.release('d')
# keyboard.press('w')
keyboard.press('a')
print('A')
if distX > 55 and distX != 0:
keyboard.release('a')
keyboard.release('s')
# keyboard.press('w')
keyboard.press('d')
print('D')
else:
print('none')
keyboard.release('d')
keyboard.release('a')
keyboard.release('w')
keyboard.release('s')
# if image is not None:
# cv2.imshow("Frame", image)
q = cv2.waitKey(1)
if q == ord("q"):
break
cv2.destroyAllWindows()
cp.release()