-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVirtual_Keyboard.py
More file actions
113 lines (94 loc) · 4.29 KB
/
Copy pathVirtual_Keyboard.py
File metadata and controls
113 lines (94 loc) · 4.29 KB
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
103
104
105
106
107
108
109
110
111
112
113
import cv2
import mediapipe as mp
import time
keys = [
['Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P'],
['A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L'],
['Z', 'X', 'C', 'V', 'B', 'N', 'M', 'Space', '<-'], # Added backspace key
[]
]
# Additional number keys
for i in range(10):
keys[3].append(str(i))
string = ""
# Function to draw the keyboard
def draw_keyboard(frame):
"""
This function draws the virtual keyboard on the given frame.
Parameters:
frame (numpy.ndarray): The frame on which the keyboard will be drawn.
Returns:
None
"""
# Iterate over each row of keys
for i, row in enumerate(keys):
# Iterate over each key in the row
for j, key in enumerate(row):
# Calculate the top-left coordinates of the key rectangle
x = j * 60 + 10 # Column index multiplied by key width and added to initial x-coordinate
y = i * 60 + 10 # Row index multiplied by key height and added to initial y-coordinate
# Draw the key rectangle
cv2.rectangle(frame, (x, y), (x+50, y+50), (255, 0, 0), 2)
# Draw the text on the key
cv2.putText(frame, key, (x+15 if key != 'Space' else x+5, y+35), cv2.FONT_HERSHEY_SIMPLEX, 0.5 if key != 'Space' else 0.4, (255, 0, 0), 1)
def detect_key(frame, x, y):
for i, row in enumerate(keys):
for j, key in enumerate(row):
x1 = j * 60 + 10
y1 = i * 60 + 10 # Adjusted key size and position
x2 = x1 + 50 # Adjusted key size
y2 = y1 + 50 # Adjusted key size
if x1 < x < x2 and y1 < y < y2:
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
cv2.putText(frame, key, (x1+15 if key != 'Space' else x1+5, y1+35), cv2.FONT_HERSHEY_SIMPLEX, 0.5 if key != 'Space' else 0.4, (0, 255, 0), 1)
return " " if key == "Space" else key
return None
source = cv2.VideoCapture(0)
drawing = mp.solutions.drawing_utils
drawing_styles = mp.solutions.drawing_styles
hand = mp.solutions.hands
hands = hand.Hands(
static_image_mode=False,
max_num_hands=1,
min_detection_confidence=0.6
)
last_pressed_key = None
last_key_time = 0 # Track the time of the last key press
debounce_time = 0.5 # 500 milliseconds
while True:
data, frame = source.read()
frame = cv2.flip(frame, 1)
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = hands.process(rgb_frame)
draw_keyboard(frame)
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
drawing.draw_landmarks(frame, hand_landmarks, hand.HAND_CONNECTIONS,
drawing_styles.get_default_hand_landmarks_style(),
drawing_styles.get_default_hand_connections_style())
x1 = int(hand_landmarks.landmark[8].x * frame.shape[1])
y1 = int(hand_landmarks.landmark[8].y * frame.shape[0])
x2 = int(hand_landmarks.landmark[12].x * frame.shape[1])
y2 = int(hand_landmarks.landmark[12].y * frame.shape[0])
current_time = time.time()
if not (abs(x1 - x2) <= 0.12 * frame.shape[1] and abs(y1 - y2) <= 0.12 * frame.shape[0]):
detected_key = detect_key(frame, x1, y1)
if detected_key and detected_key != last_pressed_key and (current_time - last_key_time) > debounce_time:
print(f"Key Pressed: {detected_key}")
if detected_key == '<-':
if string != "":
string = string[:-1] # Delete the last character
else:
string += detected_key
last_pressed_key = detected_key
last_key_time = current_time # Update the time of the last key press
else:
last_pressed_key = None
cv2.putText(frame, string, (20, frame.shape[0] - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, (12, 7, 168), 2)
# Underline the result string
cv2.line(frame, (20, frame.shape[0] - 10), (20 + len(string) * 20, frame.shape[0] - 10), (0, 0, 255), 2)
cv2.imshow("Virtual Keyboard", frame)
if cv2.waitKey(1) & 0xFF == 27: # ESC key to exit
break
source.release()
cv2.destroyAllWindows()