-
Notifications
You must be signed in to change notification settings - Fork 0
/
control.py
88 lines (74 loc) · 3.89 KB
/
control.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
import cv2
import keyInputs as keyinput
import mediapipe as mp
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_hands = mp.solutions.hands
cap = cv2.VideoCapture(0)
with mp_hands.Hands(model_complexity=0,min_detection_confidence=0.5,min_tracking_confidence=0.5) as hands:
while cap.isOpened():
success, image = cap.read()
if not success:
print("Ignoring empty camera frame.")
continue
image.flags.writeable = False
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
results = hands.process(image)
imageHeight, imageWidth, _ = image.shape
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
co = []
tip = []
pip = []
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
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())
for point in mp_hands.HandLandmark:
if str(point) == "HandLandmark.WRIST":
normalizedLandmark = hand_landmarks.landmark[point]
pixelCoordinatesLandmark = mp_drawing._normalized_to_pixel_coordinates(normalizedLandmark.x,normalizedLandmark.y,imageWidth, imageHeight)
try:
co.append(list(pixelCoordinatesLandmark))
except:
continue
for point in mp_hands.HandLandmark:
if str(point) == "HandLandmark.MIDDLE_FINGER_PIP":
normalizedLandmark = hand_landmarks.landmark[point]
pixelCoordinatesLandmark = mp_drawing._normalized_to_pixel_coordinates(normalizedLandmark.x,normalizedLandmark.y,imageWidth, imageHeight)
try:
pip.append(list(pixelCoordinatesLandmark))
except:
continue
for point in mp_hands.HandLandmark:
if str(point) == "HandLandmark.MIDDLE_FINGER_TIP":
normalizedLandmark = hand_landmarks.landmark[point]
pixelCoordinatesLandmark = mp_drawing._normalized_to_pixel_coordinates(normalizedLandmark.x,normalizedLandmark.y,imageWidth, imageHeight)
try:
tip.append(list(pixelCoordinatesLandmark))
except:
continue
if len(co)==2 and len(tip)==2 and len(pip)==2:
if co[0][0] < co[1][0] and tip[1][1] < pip[1][1] and tip[0][1] > pip[0][1]:
print("1Accelerate")
keyinput.release_key('l')
keyinput.press_key('r')
elif co[1][0] < co[0][0] and tip[0][1] < pip[0][1] and tip[1][1] > pip[1][1]:
print("2Accelerate")
keyinput.release_key('l')
keyinput.press_key('r')
elif co[0][0] < co[1][0] and tip[0][1] < pip[0][1] and tip[1][1] > pip[1][1]:
print("1Break")
keyinput.release_key('r')
keyinput.press_key('l')
elif co[1][0] < co[0][0] and tip[1][1] < pip[1][1] and tip[0][1] > pip[0][1]:
print("2Break")
keyinput.release_key('r')
keyinput.press_key('l')
else:
print("release")
keyinput.release_key('r')
keyinput.release_key('l')
# cv2.imshow('Hands',cv2.flip(image, 1))
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()