-
Notifications
You must be signed in to change notification settings - Fork 0
/
gesture_use.py
276 lines (214 loc) · 10.4 KB
/
gesture_use.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import cv2
import mediapipe as mp
import numpy as np
import tkinter as tk
from tkinter import Listbox
from gesture_recognition import load_gesture_model, recognize_gesture
from config import max_num_hands, alphabet_gesture, create_mapping_text, overlay_mapping_text
# Initialize MediaPipe hands model
mp_hands = mp.solutions.hands
mp_drawing = mp.solutions.drawing_utils
hands = mp_hands.Hands(
max_num_hands=max_num_hands,
min_detection_confidence=0.5,
min_tracking_confidence=0.5
)
# Load gesture recognition model
knn = load_gesture_model()
# Initialize variables
sentence = ""
last_character = ""
current_character = ""
gesture_count = 0
gesture_threshold = 3
completed_sentences = [] # Array to store completed sentences
def input_shortcut():
global completed_sentences
def save_shortcut():
shortcut = shortcut_entry.get()
completed_sentences.append(shortcut)
update_listbox()
shortcut_entry.delete(0, tk.END)
def update_listbox():
listbox.delete(0, tk.END)
for i, sentence in enumerate(completed_sentences):
listbox.insert(tk.END, f"{i + 1}: {sentence}")
def done():
root.destroy() # Close the tkinter window
root = tk.Tk()
root.title("Input Shortcuts")
root.geometry("400x300")
tk.Label(root, text="Enter the shortcut:").pack(pady=10)
shortcut_entry = tk.Entry(root)
shortcut_entry.pack(pady=5)
save_button = tk.Button(root, text="Save Shortcut", command=save_shortcut)
save_button.pack(pady=10)
done_button = tk.Button(root, text="Done", command=done)
done_button.pack(pady=10)
listbox = Listbox(root, width=50)
listbox.pack(pady=10)
update_listbox()
root.mainloop()
def recognize_gestures(add_to_sentences=True):
global sentence, last_character, current_character, gesture_count
mapping_text = create_mapping_text()
cap = cv2.VideoCapture(0)
while cap.isOpened():
ret, img = cap.read()
if not ret:
continue
img = cv2.flip(img, 1)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
result = hands.process(img)
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
# Expand the image to the right to display the completed sentences
expanded_img = np.zeros((img.shape[0], img.shape[1] + 300, 3), dtype=np.uint8)
expanded_img[:, :img.shape[1]] = img
# Initialize hand_results
hand_results = []
if result.multi_hand_landmarks is not None:
for res in result.multi_hand_landmarks:
joint = np.zeros((21, 3))
for j, lm in enumerate(res.landmark):
joint[j] = [lm.x, lm.y, lm.z]
gesture_result = recognize_gesture(knn, joint)
if gesture_result:
hand_results.append(gesture_result)
# Draw landmarks on the original image
mp_drawing.draw_landmarks(img, res, mp_hands.HAND_CONNECTIONS)
if len(hand_results) == 1:
text = hand_results[0]
temp = int(text)
if temp in alphabet_gesture:
text = alphabet_gesture[temp]
if text != current_character:
gesture_count = 0 # Reset the gesture count if a different character is detected
else:
gesture_count += 1 # Increment the gesture count if the same character is detected
if gesture_count >= gesture_threshold:
if text != last_character:
sentence += text # Add recognized letter to sentence
last_character = text
gesture_count = 0 # Reset the gesture count after adding the character
current_character = text
else:
text = "?"
(w, h), _ = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, fontScale=2, thickness=3)
x, y = 10, 390
cv2.rectangle(img, (x, y - h - 10), (x + w + 20, y + 10), (100, 100, 100), -1) # Rectangle position adjusted
cv2.putText(img, text=text, org=(x + 10, y - 5), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=2, color=(255, 255, 255), thickness=3) # Text position adjusted
elif len(hand_results) == 2:
combined_text = hand_results[1] + hand_results[0]
temp = int(combined_text)
if temp in alphabet_gesture:
text = alphabet_gesture[temp]
if text != current_character:
gesture_count = 0 # Reset the gesture count if a different character is detected
else:
gesture_count += 1 # Increment the gesture count if the same character is detected
if gesture_count >= gesture_threshold:
if text != last_character:
sentence += text # Add recognized letter to sentence
last_character = text
gesture_count = 0 # Reset the gesture count after adding the character
current_character = text
else:
text = "?"
# Copy the processed original image into the expanded image
expanded_img[:, :img.shape[1]] = img
# Display the current sentence with a background rectangle
(w, h), _ = cv2.getTextSize(sentence, cv2.FONT_HERSHEY_SIMPLEX, fontScale=2, thickness=3)
x, y = 10, 450 # Adjust the y-coordinate as needed
if len(sentence) > 0:
cv2.rectangle(expanded_img, (x - 10, y - h - 10), (x + w + 10, y + 10), (100, 100, 100), -1) # Rectangle adjusted to fit the sentence
cv2.putText(expanded_img, sentence, (x, y), cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 255, 255), 3, cv2.LINE_AA)
# Display the completed sentences on the right side of the screen
for i, completed_sentence in enumerate(completed_sentences):
cv2.putText(expanded_img, str(i) + " : " + completed_sentence, (img.shape[1] + 10, 30 * (i + 1)), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA)
overlay_mapping_text(expanded_img, mapping_text)
# Check for completion gesture (e.g., all fingers touching)
if '0' in hand_results and sentence != '' and sentence not in completed_sentences:
if add_to_sentences:
# Add the completed sentence to the array
completed_sentences.append(sentence)
print("Completed Sentence: ", sentence)
# Clear the sentence
sentence = ""
last_character = ""
current_character = ""
gesture_count = 0
cv2.imshow('Alphabet Recognition', expanded_img)
if cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
return completed_sentences
def use_gestures():
global sentence, last_character, current_character, gesture_count
cap = cv2.VideoCapture(0)
while cap.isOpened():
ret, img = cap.read()
if not ret:
continue
img = cv2.flip(img, 1)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
result = hands.process(img)
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
# Expand the image to the right to display the completed sentences
expanded_img = np.zeros((img.shape[0], img.shape[1] + 300, 3), dtype=np.uint8)
expanded_img[:, :img.shape[1]] = img
# Initialize hand_results
hand_results = []
if result.multi_hand_landmarks is not None:
for res in result.multi_hand_landmarks:
joint = np.zeros((21, 3))
for j, lm in enumerate(res.landmark):
joint[j] = [lm.x, lm.y, lm.z]
gesture_result = recognize_gesture(knn, joint)
if gesture_result:
hand_results.append(gesture_result)
# Draw landmarks on the original image
mp_drawing.draw_landmarks(img, res, mp_hands.HAND_CONNECTIONS)
if len(hand_results) == 1:
text = hand_results[0]
temp = int(text)
if temp < len(completed_sentences):
text = completed_sentences[temp]
else:
text = "?"
(w, h), _ = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, fontScale=2, thickness=3)
x, y = 10, 30
cv2.rectangle(img, (x + 50, y - h + 50), (x + w + 50, y + 50), (100, 100, 100), -1)
cv2.putText(img, text=text, org=(x + 50, y + 50), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=2, color=(255, 255, 255), thickness=3)
elif len(hand_results) == 2:
combined_text = hand_results[1] + hand_results[0]
temp = int(combined_text)
if temp < len(completed_sentences):
text = completed_sentences[temp]
else:
text = "?"
(w, h), _ = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, fontScale=2, thickness=3)
x, y = 10, 30
cv2.rectangle(img, (x + 50, y - h + 50), (x + w + 50, y + 50), (100, 100, 100), -1)
cv2.putText(img, text=text, org=(x + 50, y + 50), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=2, color=(255, 255, 255), thickness=3)
# Copy the processed original image into the expanded image
expanded_img[:, :img.shape[1]] = img
# Display the current sentence
cv2.putText(expanded_img, sentence, (10, 100), cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 255, 255), 3, cv2.LINE_AA)
# Display the completed sentences on the right side of the screen
for i, completed_sentence in enumerate(completed_sentences):
cv2.putText(expanded_img, str(i) + " : " + completed_sentence, (img.shape[1] + 10, 30 * (i + 1)), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA)
# Clear the sentence
sentence = ""
last_character = ""
current_character = ""
gesture_count = 0
cv2.imshow('Alphabet Recognition', expanded_img)
if cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
def print_completed_sentences():
print("All completed sentences:")
for s in completed_sentences:
print(s)