nisharg nargund commited on
Commit
c20cbc8
·
1 Parent(s): 8b1f990

Upload 3 files

Browse files
Files changed (3) hide show
  1. README.md +2 -0
  2. keyinput.py +51 -0
  3. steering.py +125 -0
README.md ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ # CarGame
2
+ Made with opencv2
keyinput.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import ctypes
2
+
3
+ keys = {
4
+ "w":0x11,
5
+ "a":0x1E,
6
+ "s":0x1F,
7
+ "d":0x20,
8
+ }
9
+ PUL = ctypes.POINTER(ctypes.c_ulong)
10
+ class KeyBdInput(ctypes.Structure):
11
+ _fields_ = [("wVk", ctypes.c_ushort),
12
+ ("wScan", ctypes.c_ushort),
13
+ ("dwFlags", ctypes.c_ulong),
14
+ ("time", ctypes.c_ulong),
15
+ ("dwExtraInfo", PUL)]
16
+
17
+ class HardwareInput(ctypes.Structure):
18
+ _fields_ = [("uMsg", ctypes.c_ulong),
19
+ ("wParamL", ctypes.c_short),
20
+ ("wParamH", ctypes.c_ushort)]
21
+
22
+ class MouseInput(ctypes.Structure):
23
+ _fields_ = [("dx", ctypes.c_long),
24
+ ("dy", ctypes.c_long),
25
+ ("mouseData", ctypes.c_ulong),
26
+ ("dwFlags", ctypes.c_ulong),
27
+ ("time",ctypes.c_ulong),
28
+ ("dwExtraInfo", PUL)]
29
+
30
+ class Input_I(ctypes.Union):
31
+ _fields_ = [("ki", KeyBdInput),
32
+ ("mi", MouseInput),
33
+ ("hi", HardwareInput)]
34
+
35
+ class Input(ctypes.Structure):
36
+ _fields_ = [("type", ctypes.c_ulong),
37
+ ("ii", Input_I)]
38
+
39
+ def press_key(key):
40
+ extra = ctypes.c_ulong(0)
41
+ ii_ = Input_I()
42
+ ii_.ki = KeyBdInput( 0, keys[key], 0x0008, 0, ctypes.pointer(extra) )
43
+ x = Input( ctypes.c_ulong(1), ii_ )
44
+ ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))
45
+
46
+ def release_key(key):
47
+ extra = ctypes.c_ulong(0)
48
+ ii_ = Input_I()
49
+ ii_.ki = KeyBdInput( 0, keys[key], 0x0008 | 0x0002, 0, ctypes.pointer(extra) )
50
+ x = Input( ctypes.c_ulong(1), ii_ )
51
+ ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))
steering.py ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+ import keyinput
3
+ import cv2
4
+ import mediapipe as mp
5
+ mp_drawing = mp.solutions.drawing_utils
6
+ mp_drawing_styles = mp.solutions.drawing_styles
7
+ mp_hands = mp.solutions.hands
8
+ font = cv2.FONT_HERSHEY_SIMPLEX
9
+ # 0 For webcam input:
10
+ cap = cv2.VideoCapture(0)
11
+
12
+ with mp_hands.Hands(model_complexity=0,min_detection_confidence=0.5,min_tracking_confidence=0.5) as hands:
13
+ while cap.isOpened():
14
+ success, image = cap.read()
15
+ if not success:
16
+ print("Ignoring empty camera frame.")
17
+ continue
18
+
19
+ image.flags.writeable = False
20
+ image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
21
+ results = hands.process(image)
22
+ imageHeight, imageWidth, _ = image.shape
23
+
24
+ image.flags.writeable = True
25
+ image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
26
+ co=[]
27
+ if results.multi_hand_landmarks:
28
+ for hand_landmarks in results.multi_hand_landmarks:
29
+ mp_drawing.draw_landmarks(image,hand_landmarks,mp_hands.HAND_CONNECTIONS,mp_drawing_styles.get_default_hand_landmarks_style(),mp_drawing_styles.get_default_hand_connections_style())
30
+ for point in mp_hands.HandLandmark:
31
+ if str(point) == "HandLandmark.WRIST":
32
+ normalizedLandmark = hand_landmarks.landmark[point]
33
+ pixelCoordinatesLandmark = mp_drawing._normalized_to_pixel_coordinates(normalizedLandmark.x, normalizedLandmark.y, imageWidth, imageHeight)
34
+
35
+ try:
36
+ co.append(list(pixelCoordinatesLandmark))
37
+ except:
38
+ continue
39
+
40
+ if len(co) == 2:
41
+ xm, ym = (co[0][0] + co[1][0]) / 2, (co[0][1] + co[1][1]) / 2
42
+ radius = 150
43
+ try:
44
+ m=(co[1][1]-co[0][1])/(co[1][0]-co[0][0])
45
+ except:
46
+ continue
47
+ a = 1 + m ** 2
48
+ b = -2 * xm - 2 * co[0][0] * (m ** 2) + 2 * m * co[0][1] - 2 * m * ym
49
+ c = xm ** 2 + (m ** 2) * (co[0][0] ** 2) + co[0][1] ** 2 + ym ** 2 - 2 * co[0][1] * ym - 2 * co[0][1] * co[0][0] * m + 2 * m * ym * co[0][0] - 22500
50
+ xa = (-b + (b ** 2 - 4 * a * c) ** 0.5) / (2 * a)
51
+ xb = (-b - (b ** 2 - 4 * a * c) ** 0.5) / (2 * a)
52
+ ya = m * (xa - co[0][0]) + co[0][1]
53
+ yb = m * (xb - co[0][0]) + co[0][1]
54
+ if m!=0:
55
+ ap = 1 + ((-1/m) ** 2)
56
+ bp = -2 * xm - 2 * xm * ((-1/m) ** 2) + 2 * (-1/m) * ym - 2 * (-1/m) * ym
57
+ cp = xm ** 2 + ((-1/m) ** 2) * (xm ** 2) + ym ** 2 + ym ** 2 - 2 * ym * ym - 2 * ym * xm * (-1/m) + 2 * (-1/m) * ym * xm - 22500
58
+ try:
59
+ xap = (-bp + (bp ** 2 - 4 * ap * cp) ** 0.5) / (2 * ap)
60
+ xbp = (-bp - (bp ** 2 - 4 * ap * cp) ** 0.5) / (2 * ap)
61
+ yap = (-1 / m) * (xap - xm) + ym
62
+ ybp = (-1 / m) * (xbp - xm) + ym
63
+
64
+ except:
65
+ continue
66
+
67
+ cv2.circle(img=image, center=(int(xm), int(ym)), radius=radius, color=(195, 255, 62), thickness=15)
68
+
69
+ l = (int(math.sqrt((co[0][0] - co[1][0]) ** 2 * (co[0][1] - co[1][1]) ** 2)) - 150) // 2
70
+ cv2.line(image, (int(xa), int(ya)), (int(xb), int(yb)), (195, 255, 62), 20)
71
+ if co[0][0] > co[1][0] and co[0][1]>co[1][1] and co[0][1] - co[1][1] > 65:
72
+ print("Left - a")
73
+ keyinput.release_key('s')
74
+ keyinput.release_key('d')
75
+ keyinput.press_key('a')
76
+ cv2.line(image, (int(xbp), int(ybp)), (int(xm), int(ym)), (195, 255, 62), 20)
77
+
78
+
79
+ elif co[1][0] > co[0][0] and co[1][1]> co[0][1] and co[1][1] - co[0][1] > 65:
80
+ print("Left - a")
81
+ keyinput.release_key('s')
82
+ keyinput.release_key('d')
83
+ keyinput.press_key('a')
84
+ cv2.line(image, (int(xbp), int(ybp)), (int(xm), int(ym)), (195, 255, 62), 20)
85
+
86
+
87
+ elif co[0][0] > co[1][0] and co[1][1]> co[0][1] and co[1][1] - co[0][1] > 65:
88
+ print("Right - d")
89
+ keyinput.release_key('s')
90
+ keyinput.release_key('a')
91
+ keyinput.press_key('d')
92
+ cv2.line(image, (int(xap), int(yap)), (int(xm), int(ym)), (195, 255, 62), 20)
93
+
94
+ elif co[1][0] > co[0][0] and co[0][1]> co[1][1] and co[0][1] - co[1][1] > 65:
95
+ print("Right - d")
96
+ keyinput.release_key('s')
97
+ keyinput.release_key('a')
98
+ keyinput.press_key('d')
99
+ cv2.line(image, (int(xap), int(yap)), (int(xm), int(ym)), (195, 255, 62), 20)
100
+
101
+ else:
102
+ print("Straight - w")
103
+ keyinput.release_key('s')
104
+ keyinput.release_key('a')
105
+ keyinput.release_key('d')
106
+ keyinput.press_key('w')
107
+ if ybp>yap:
108
+ cv2.line(image, (int(xbp), int(ybp)), (int(xm), int(ym)), (195, 255, 62), 20)
109
+ else:
110
+ cv2.line(image, (int(xap), int(yap)), (int(xm), int(ym)), (195, 255, 62), 20)
111
+
112
+ if len(co)==1:
113
+ print("Back - s")
114
+ keyinput.release_key('a')
115
+ keyinput.release_key('d')
116
+ keyinput.release_key('w')
117
+ keyinput.press_key('s')
118
+
119
+ cv2.imshow('MediaPipe Hands', cv2.flip(image, 1))
120
+ cv2.waitKey(1)
121
+
122
+
123
+ if cv2.waitKey(5) & 0xFF == ord('q'):
124
+ break
125
+ cap.release()