-
Notifications
You must be signed in to change notification settings - Fork 206
/
main-pc-cam.py
102 lines (84 loc) · 2.81 KB
/
main-pc-cam.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
95
96
97
98
99
100
101
102
import cv2
import mediapipe as mp
from pynput.keyboard import Controller
# Initialize MediaPipe hands with optimizations
mp_hands = mp.solutions.hands.Hands(static_image_mode=False, max_num_hands=1, min_detection_confidence=0.5, min_tracking_confidence=0.5)
keyboard = Controller()
# Open the camera
cp = cv2.VideoCapture(0)
x1, x2, y1, y2 = 0, 0, 0, 0
pressed_key = ""
frame_skip = 1 # Reduce frame skipping to 1 for smoother processing
frame_count = 0
while True:
# Capture frame from camera
ret, image = cp.read()
if not ret:
print("Failed to capture frame. Exiting...")
break
frame_count += 1
# Skip every nth frame to improve performance (reduced skipping)
if frame_count % frame_skip != 0:
continue
# Get image dimensions (without resizing)
image_height, image_width, _ = image.shape
# Flip the image horizontally for a selfie-view display
image = cv2.flip(image, 1)
# Convert the image to RGB
rgb_img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Process the image to detect hands
output_hands = mp_hands.process(rgb_img)
all_hands = output_hands.multi_hand_landmarks
# Detect keypresses based on hand position
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: # Finger tip of middle finger
x1 = x
y1 = y
if id == 0: # Wrist point
x2 = x
y2 = y
distX = x1 - x2
distY = y1 - y2
if distY > -140 and distY != 0:
keyboard.release('d')
keyboard.release('a')
keyboard.release('w')
keyboard.press('s')
print("Pressed Key: S")
elif distY < -200 and distY != 0:
keyboard.release('s')
keyboard.release('d')
keyboard.release('a')
keyboard.press('w')
print("Pressed Key: W")
elif distX < -100 and distX != 0:
keyboard.release('s')
keyboard.release('d')
keyboard.press('w')
keyboard.press('a')
print("Pressed Key: A")
elif distX > 55 and distX != 0:
keyboard.release('a')
keyboard.release('s')
keyboard.press('w')
keyboard.press('d')
print("Pressed Key: D")
else:
keyboard.release('d')
keyboard.release('a')
keyboard.release('w')
keyboard.release('s')
# Display the camera feed
cv2.imshow("Camera Feed", image)
# Check if 'q' is pressed to quit
q = cv2.waitKey(1)
if q == ord("q"):
break
# Release resources
cv2.destroyAllWindows()
cp.release()