├── README.md ├── test.py └── temple_run_control.py /README.md: -------------------------------------------------------------------------------- 1 | # TempleRun_GestureControl -------------------------------------------------------------------------------- /test.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import mediapipe as mp 3 | import pyautogui 4 | 5 | mp_hands = mp.solutions.hands 6 | hands = mp_hands.Hands(max_num_hands=1) 7 | mp_draw = mp.solutions.drawing_utils 8 | 9 | cap = cv2.VideoCapture(0) 10 | prev_x = None 11 | gesture_cooldown = 0 12 | frame_count = 0 13 | 14 | def count_fingers(hand_landmarks): 15 | fingers = [] 16 | fingers.append(hand_landmarks.landmark[4].x < hand_landmarks.landmark[3].x) 17 | for id in [8, 12, 16, 20]: 18 | fingers.append(hand_landmarks.landmark[id].y < hand_landmarks.landmark[id - 2].y) 19 | return fingers.count(True) 20 | 21 | while True: 22 | success, frame = cap.read() 23 | frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) 24 | results = hands.process(frame_rgb) 25 | 26 | if results.multi_hand_landmarks: 27 | for hand_landmarks in results.multi_hand_landmarks: 28 | mp_draw.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS) 29 | 30 | curr_x = hand_landmarks.landmark[8].x 31 | finger_count = count_fingers(hand_landmarks) 32 | 33 | frame_count += 1 34 | if frame_count % 5 == 0 and gesture_cooldown == 0: 35 | if prev_x is not None: 36 | delta = curr_x - prev_x 37 | if delta > 0.1: 38 | print("Swipe Right →") 39 | pyautogui.press('right') 40 | gesture_cooldown = 10 41 | elif delta < -0.1: 42 | print("Swipe Left ←") 43 | pyautogui.press('left') 44 | gesture_cooldown = 10 45 | prev_x = curr_x 46 | 47 | if finger_count == 5: 48 | print("Jump ↑") 49 | pyautogui.press('up') 50 | gesture_cooldown = 10 51 | elif finger_count == 0: 52 | print("Slide ↓") 53 | pyautogui.press('down') 54 | gesture_cooldown = 10 55 | 56 | if gesture_cooldown > 0: 57 | gesture_cooldown -= 1 58 | 59 | cv2.imshow("Temple Run Hand Control", frame) 60 | if cv2.waitKey(1) & 0xFF == ord('q'): 61 | break 62 | 63 | cap.release() 64 | cv2.destroyAllWindows() 65 | -------------------------------------------------------------------------------- /temple_run_control.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import mediapipe as mp 3 | import pyautogui 4 | 5 | mp_hands = mp.solutions.hands 6 | hands = mp_hands.Hands(max_num_hands=1) 7 | mp_draw = mp.solutions.drawing_utils 8 | 9 | cap = cv2.VideoCapture(0) 10 | prev_x = None 11 | gesture_cooldown = 0 12 | frame_count = 0 13 | 14 | def count_fingers(hand_landmarks): 15 | fingers = [] 16 | 17 | 18 | fingers.append(hand_landmarks.landmark[4].y < hand_landmarks.landmark[3].y) 19 | 20 | 21 | for id in [8, 12, 16, 20]: 22 | fingers.append(hand_landmarks.landmark[id].y < hand_landmarks.landmark[id - 2].y) 23 | 24 | return fingers 25 | 26 | def is_fist(hand_landmarks): 27 | 28 | for id in [8, 12, 16, 20]: 29 | if hand_landmarks.landmark[id].y < hand_landmarks.landmark[id - 2].y: 30 | return False 31 | return True 32 | 33 | while True: 34 | success, frame = cap.read() 35 | frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) 36 | results = hands.process(frame_rgb) 37 | 38 | if results.multi_hand_landmarks: 39 | for hand_landmarks in results.multi_hand_landmarks: 40 | mp_draw.draw_landmarks(frame, hand_landmarks, mp_hands.HAND_CONNECTIONS) 41 | 42 | 43 | fingers = count_fingers(hand_landmarks) 44 | finger_count = fingers.count(True) 45 | 46 | curr_x = hand_landmarks.landmark[8].x 47 | 48 | frame_count += 1 49 | if frame_count % 5 == 0 and gesture_cooldown == 0: 50 | 51 | if prev_x is not None: 52 | delta = curr_x - prev_x 53 | if delta > 0.1: 54 | print("Swipe Right →") 55 | pyautogui.press('right') 56 | gesture_cooldown = 10 57 | elif delta < -0.1: 58 | print("Swipe Left ←") 59 | pyautogui.press('left') 60 | gesture_cooldown = 10 61 | prev_x = curr_x 62 | 63 | 64 | if finger_count == 5: 65 | print("Open Palm → Jump ↑") 66 | pyautogui.press('up') 67 | gesture_cooldown = 10 68 | 69 | 70 | elif is_fist(hand_landmarks): 71 | print("Fist → Slide ↓") 72 | pyautogui.press('down') 73 | gesture_cooldown = 10 74 | 75 | if gesture_cooldown > 0: 76 | gesture_cooldown -= 1 77 | 78 | cv2.imshow("Temple Run Hand Control", frame) 79 | if cv2.waitKey(1) & 0xFF == ord('q'): 80 | break 81 | 82 | cap.release() 83 | cv2.destroyAllWindows() 84 | --------------------------------------------------------------------------------