Repository navigation
Expand file tree
/
Copy pathgodknowswhy.py
More file actions
1260 lines (1026 loc) · 46.7 KB
/
Copy pathgodknowswhy.py
File metadata and controls
1260 lines (1026 loc) · 46.7 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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import sys
import cv2
import numpy as np
import mediapipe as mp
import pickle
import time
import warnings
import os
from datetime import datetime
from dataclasses import dataclass
# GUI imports
try:
from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QLabel,
QPushButton, QVBoxLayout, QHBoxLayout, QGridLayout,
QTextEdit, QSlider, QComboBox, QGroupBox, QFrame,
QProgressBar, QTabWidget, QListWidget, QListWidgetItem,
QMessageBox, QFileDialog, QCheckBox, QSpinBox, QSplitter,
QStatusBar)
from PyQt5.QtCore import QTimer, Qt, QPropertyAnimation, QEasingCurve, QSize
from PyQt5.QtGui import QImage, QPixmap, QFont, QPalette, QColor, QIcon, QMovie, QClipboard
except ImportError:
print("PyQt5 not found. Installing...")
import subprocess
subprocess.check_call([sys.executable, "-m", "pip", "install", "PyQt5"])
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
# Text-to-speech
try:
import pyttsx3
except ImportError:
print("pyttsx3 not found. Installing...")
import subprocess
subprocess.check_call([sys.executable, "-m", "pip", "install", "pyttsx3"])
import pyttsx3
# Spell checking
try:
from symspellpy.symspellpy import SymSpell, Verbosity
except ImportError:
print("symspellpy not found. Installing...")
import subprocess
subprocess.check_call([sys.executable, "-m", "pip", "install", "symspellpy"])
from symspellpy.symspellpy import SymSpell, Verbosity
# Gemini API
try:
import google.generativeai as genai
except ImportError:
print("google-generativeai not found. Installing...")
import subprocess
subprocess.check_call([sys.executable, "-m", "pip", "install", "google-generativeai"])
import google.generativeai as genai
# Suppress warnings
warnings.filterwarnings("ignore", category=UserWarning)
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
@dataclass
class RecognitionStats:
"""Statistics for recognition performance"""
total_predictions: int = 0
correct_predictions: int = 0
session_start: datetime = None
words_formed: int = 0
sentences_completed: int = 0
class ModelLoader:
"""Handles loading and fallback for the ASL recognition model"""
def __init__(self, model_path=None):
self.model = None
self.expected_features = 42
if model_path and os.path.exists(model_path):
try:
model_dict = pickle.load(open(model_path, 'rb'))
self.model = model_dict['model']
print("✅ Model loaded successfully from:", model_path)
except Exception as e:
print(f"❌ Error loading model: {e}")
self.create_fallback_model()
else:
possible_paths = [
"modeli.p",
os.path.join(os.path.dirname(__file__), "modeli.p"),
os.path.join(os.path.expanduser("~"), "Desktop", "American-Sign-language-Detection-System", "modeli.p")
]
for path in possible_paths:
if os.path.exists(path):
try:
model_dict = pickle.load(open(path, 'rb'))
self.model = model_dict['model']
print("✅ Model loaded successfully from:", path)
break
except:
pass
if self.model is None:
print("❌ Model not found, using fallback model")
self.create_fallback_model()
def create_fallback_model(self):
"""Create a simple fallback model for demonstration"""
class MockModel:
def predict(self, data):
return [np.random.randint(0, 38)]
self.model = MockModel()
print("⚠️ Using fallback model - predictions will be random")
def predict(self, features):
"""Make a prediction using the model"""
if len(features) < self.expected_features:
features.extend([0] * (self.expected_features - len(features)))
elif len(features) > self.expected_features:
features = features[:self.expected_features]
try:
prediction = self.model.predict([np.asarray(features)])
return int(prediction[0])
except Exception as e:
print(f"Prediction error: {e}")
return 0
class DictionaryLoader:
"""Handles loading and fallback for the dictionary"""
def __init__(self, dict_path=None):
self.sym_spell = SymSpell(max_dictionary_edit_distance=2, prefix_length=7)
if dict_path and os.path.exists(dict_path):
try:
self.sym_spell.load_dictionary(dict_path, term_index=0, count_index=1)
print("✅ Dictionary loaded successfully from:", dict_path)
return
except Exception as e:
print(f"❌ Error loading dictionary: {e}")
possible_paths = [
"dictionary.txt",
os.path.join(os.path.dirname(__file__), "dictionary.txt"),
os.path.join(os.path.expanduser("~"), "Desktop", "American-Sign-language-Detection-System", "dictionary.txt")
]
for path in possible_paths:
if os.path.exists(path):
try:
self.sym_spell.load_dictionary(path, term_index=0, count_index=1)
print("✅ Dictionary loaded successfully from:", path)
return
except:
pass
print("⚠️ Dictionary not found, creating basic dictionary")
self.create_basic_dictionary()
def create_basic_dictionary(self):
"""Create a basic dictionary with common words"""
common_words = [
"hello", "world", "thank", "you", "please", "help", "good", "bad",
"yes", "no", "maybe", "what", "when", "where", "who", "how", "why",
"time", "day", "today", "tomorrow", "morning", "afternoon", "evening",
"night", "food", "water", "eat", "drink", "sleep", "work", "play",
"school", "home", "family", "friend", "love", "like", "need", "want",
"happy", "sad", "sorry", "excuse", "me", "name", "sign", "language",
"learn", "teach", "student", "teacher", "book", "read", "write"
]
for word in common_words:
self.sym_spell.create_dictionary_entry(word, 1000)
print("✅ Basic dictionary created with", len(common_words), "words")
def get_suggestions(self, word, max_edit_distance=2, max_suggestions=5):
"""Get word suggestions"""
if not word:
return []
suggestions = self.sym_spell.lookup(
word.lower(),
Verbosity.CLOSEST,
max_edit_distance=max_edit_distance,
include_unknown=True
)
return suggestions[:max_suggestions]
class HandProcessor:
"""Processes hand landmarks and draws on frames"""
def __init__(self):
self.mp_hands = mp.solutions.hands
self.hands = self.mp_hands.Hands(
static_image_mode=False,
max_num_hands=1,
min_detection_confidence=0.7,
min_tracking_confidence=0.5
)
self.mp_drawing = mp.solutions.drawing_utils
self.mp_drawing_styles = mp.solutions.drawing_styles
def process_frame(self, frame):
"""Process a frame and return hand landmarks"""
try:
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = self.hands.process(rgb_frame)
hand_data = None
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
self.mp_drawing.draw_landmarks(
frame,
hand_landmarks,
self.mp_hands.HAND_CONNECTIONS,
self.mp_drawing_styles.get_default_hand_landmarks_style(),
self.mp_drawing_styles.get_default_hand_connections_style()
)
data_aux = []
x_, y_ = [], []
for lm in hand_landmarks.landmark:
x_.append(lm.x)
y_.append(lm.y)
for lm in hand_landmarks.landmark:
data_aux.extend([lm.x - min(x_), lm.y - min(y_)])
hand_data = data_aux
return frame, hand_data
except Exception as e:
print(f"Hand processing error: {e}")
return frame, None
class PredictionBuffer:
"""Manages prediction stability"""
def __init__(self, buffer_size=30, stability_threshold=25, registration_delay=2.5):
self.buffer = []
self.buffer_size = buffer_size
self.stability_threshold = stability_threshold
self.last_time = time.time()
self.registration_delay = registration_delay
def add_prediction(self, predicted_char):
"""Add a prediction to the buffer and check for stability"""
self.buffer.append(predicted_char)
if len(self.buffer) > self.buffer_size:
self.buffer.pop(0)
if self.buffer.count(predicted_char) > self.stability_threshold:
current_time = time.time()
if current_time - self.last_time > self.registration_delay:
self.last_time = current_time
return predicted_char
return None
class EnhancedASLApp(QMainWindow):
"""Enhanced ASL Recognition Application with Gemini Integration"""
def __init__(self):
super().__init__()
self.setWindowTitle("Enhanced ASL Recognition System")
self.setGeometry(100, 100, 1200, 800)
self.model_loader = ModelLoader()
self.dictionary_loader = DictionaryLoader()
self.hand_processor = HandProcessor()
self.prediction_buffer = PredictionBuffer()
self.engine = pyttsx3.init()
class EnhancedASLApp(QMainWindow):
"""Enhanced ASL Recognition Application with Gemini Integration"""
def __init__(self):
super().__init__()
self.setWindowTitle("Enhanced ASL Recognition System")
self.setGeometry(100, 100, 1200, 800)
self.model_loader = ModelLoader()
self.dictionary_loader = DictionaryLoader()
self.hand_processor = HandProcessor()
self.prediction_buffer = PredictionBuffer()
self.engine = pyttsx3.init()
# Initialize Gemini API
try:
self.gemini_api_key = "" # Directly set the API key
if not self.gemini_api_key:
print("⚠️ Gemini API key not provided. Using pyttsx3 only.")
self.use_gemini = False
else:
genai.configure(api_key=self.gemini_api_key)
self.gemini_model = genai.GenerativeModel('gemini-2.0-flash')
self.use_gemini = True
print("✅ Gemini API initialized successfully")
except Exception as e:
print(f"❌ Gemini API initialization failed: {e}")
self.use_gemini = False
# ... (rest of the __init__ method remains unchanged) ...
self.labels_dict = {i: chr(65 + i) for i in range(26)}
self.labels_dict.update({26 + i: str(i) for i in range(10)})
self.labels_dict[36] = ' '
self.labels_dict[37] = '.'
self.buffer = []
self.stable_char = None
self.word_buffer = ""
self.sentence = ""
self.history = []
self.is_camera_active = True
self.show_landmarks = True
self.auto_suggest = True
self.cap = cv2.VideoCapture(0)
self.timer = QTimer()
self.timer.timeout.connect(self.update_frame)
self.timer.start(30)
self.setup_ui()
self.apply_styles()
self.status_bar = QStatusBar()
self.setStatusBar(self.status_bar)
self.status_bar.showMessage("Ready")
self.stats = {
"letters_detected": 0,
"words_formed": 0,
"sentences_completed": 0,
"start_time": time.time()
}
def setup_ui(self):
"""Setup the user interface"""
main_widget = QWidget()
self.setCentralWidget(main_widget)
tabs = QTabWidget()
recognition_tab = self.create_recognition_tab()
tabs.addTab(recognition_tab, "Recognition")
history_tab = self.create_history_tab()
tabs.addTab(history_tab, "History")
settings_tab = self.create_settings_tab()
tabs.addTab(settings_tab, "Settings")
help_tab = self.create_help_tab()
tabs.addTab(help_tab, "Help")
main_layout = QVBoxLayout()
main_layout.addWidget(tabs)
main_widget.setLayout(main_layout)
def create_recognition_tab(self):
"""Create the main recognition interface"""
tab = QWidget()
layout = QHBoxLayout()
left_panel = QVBoxLayout()
video_group = QGroupBox("Camera Feed")
video_layout = QVBoxLayout()
self.video_label = QLabel()
self.video_label.setMinimumSize(640, 480)
self.video_label.setAlignment(Qt.AlignCenter)
self.video_label.setStyleSheet("border: 2px solid #3498db; border-radius: 10px; background-color: white;")
video_layout.addWidget(self.video_label)
camera_controls = QHBoxLayout()
self.toggle_camera_btn = QPushButton("Pause Camera")
self.toggle_camera_btn.clicked.connect(self.toggle_camera)
camera_controls.addWidget(self.toggle_camera_btn)
self.toggle_landmarks_btn = QPushButton("Hide Landmarks")
self.toggle_landmarks_btn.clicked.connect(self.toggle_landmarks)
camera_controls.addWidget(self.toggle_landmarks_btn)
video_layout.addLayout(camera_controls)
video_group.setLayout(video_layout)
left_panel.addWidget(video_group)
letter_group = QGroupBox("Current Detection")
letter_layout = QVBoxLayout()
self.char_label = QLabel("Current Letter: ---")
self.char_label.setFont(QFont("Arial", 16, QFont.Bold))
self.char_label.setAlignment(Qt.AlignCenter)
letter_layout.addWidget(self.char_label)
self.confidence_bar = QProgressBar()
self.confidence_bar.setRange(0, 100)
self.confidence_bar.setValue(0)
letter_layout.addWidget(self.confidence_bar)
letter_group.setLayout(letter_layout)
left_panel.addWidget(letter_group)
layout.addLayout(left_panel, 3)
right_panel = QVBoxLayout()
word_group = QGroupBox("Current Word")
word_layout = QVBoxLayout()
self.word_label = QLabel("Current Word: ")
self.word_label.setFont(QFont("Arial", 14))
word_layout.addWidget(self.word_label)
word_controls = QHBoxLayout()
self.clear_word_btn = QPushButton("❌ Clear Word")
self.clear_word_btn.clicked.connect(self.clear_word)
word_controls.addWidget(self.clear_word_btn)
self.add_space_btn = QPushButton("⎵ Add Space")
self.add_space_btn.clicked.connect(lambda: self.handle_special_char(' '))
word_controls.addWidget(self.add_space_btn)
self.add_period_btn = QPushButton("• Add Period")
self.add_period_btn.clicked.connect(lambda: self.handle_special_char('.'))
word_controls.addWidget(self.add_period_btn)
word_layout.addLayout(word_controls)
word_group.setLayout(word_layout)
right_panel.addWidget(word_group)
suggestions_group = QGroupBox("Word Suggestions")
suggestions_layout = QVBoxLayout()
self.suggest_buttons = []
for i in range(5):
btn = QPushButton("")
btn.setEnabled(False)
btn.clicked.connect(self.use_suggestion)
self.suggest_buttons.append(btn)
suggestions_layout.addWidget(btn)
suggestions_group.setLayout(suggestions_layout)
right_panel.addWidget(suggestions_group)
sentence_group = QGroupBox("Sentence")
sentence_layout = QVBoxLayout()
self.sent_label = QLabel("Sentence: ")
self.sent_label.setFont(QFont("Arial", 14))
self.sent_label.setWordWrap(True)
sentence_layout.addWidget(self.sent_label)
sentence_controls = QHBoxLayout()
self.speak_btn = QPushButton("🔊 Speak")
self.speak_btn.clicked.connect(self.speak)
sentence_controls.addWidget(self.speak_btn)
self.clear_sent_btn = QPushButton("🧹 Clear Sentence")
self.clear_sent_btn.clicked.connect(self.clear_sentence)
sentence_controls.addWidget(self.clear_sent_btn)
self.save_btn = QPushButton("💾 Save")
self.save_btn.clicked.connect(self.save_sentence)
sentence_controls.addWidget(self.save_btn)
sentence_layout.addLayout(sentence_controls)
sentence_group.setLayout(sentence_layout)
right_panel.addWidget(sentence_group)
stats_group = QGroupBox("Statistics")
stats_layout = QGridLayout()
self.letters_label = QLabel("Letters Detected: 0")
stats_layout.addWidget(self.letters_label, 0, 0)
self.words_label = QLabel("Words Formed: 0")
stats_layout.addWidget(self.words_label, 0, 1)
self.sentences_label = QLabel("Sentences: 0")
stats_layout.addWidget(self.sentences_label, 1, 0)
self.time_label = QLabel("Session Time: 00:00")
stats_layout.addWidget(self.time_label, 1, 1)
stats_group.setLayout(stats_layout)
right_panel.addWidget(stats_group)
layout.addLayout(right_panel, 2)
tab.setLayout(layout)
return tab
def create_history_tab(self):
"""Create the history tab"""
tab = QWidget()
layout = QVBoxLayout()
self.history_list = QListWidget()
layout.addWidget(self.history_list)
controls = QHBoxLayout()
speak_history_btn = QPushButton("🔊 Speak Selected")
speak_history_btn.clicked.connect(self.speak_selected_history)
controls.addWidget(speak_history_btn)
copy_history_btn = QPushButton("📋 Copy Selected")
copy_history_btn.clicked.connect(self.copy_selected_history)
controls.addWidget(copy_history_btn)
clear_history_btn = QPushButton("🧹 Clear History")
clear_history_btn.clicked.connect(self.clear_history)
controls.addWidget(clear_history_btn)
save_history_btn = QPushButton("💾 Save All History")
save_history_btn.clicked.connect(self.save_history)
controls.addWidget(save_history_btn)
layout.addLayout(controls)
tab.setLayout(layout)
return tab
def create_settings_tab(self):
"""Create the settings tab"""
tab = QWidget()
layout = QVBoxLayout()
recognition_group = QGroupBox("Recognition Settings")
recognition_layout = QGridLayout()
recognition_layout.addWidget(QLabel("Buffer Size:"), 0, 0)
buffer_slider = QSlider(Qt.Horizontal)
buffer_slider.setRange(10, 50)
buffer_slider.setValue(30)
buffer_slider.valueChanged.connect(self.update_buffer_size)
recognition_layout.addWidget(buffer_slider, 0, 1)
self.buffer_size_label = QLabel("30")
recognition_layout.addWidget(self.buffer_size_label, 0, 2)
recognition_layout.addWidget(QLabel("Stability Threshold:"), 1, 0)
stability_slider = QSlider(Qt.Horizontal)
stability_slider.setRange(10, 40)
stability_slider.setValue(25)
stability_slider.valueChanged.connect(self.update_stability_threshold)
recognition_layout.addWidget(stability_slider, 1, 1)
self.stability_label = QLabel("25")
recognition_layout.addWidget(self.stability_label, 1, 2)
recognition_layout.addWidget(QLabel("Registration Delay (s):"), 2, 0)
delay_slider = QSlider(Qt.Horizontal)
delay_slider.setRange(10, 50)
delay_slider.setValue(25)
delay_slider.valueChanged.connect(self.update_registration_delay)
recognition_layout.addWidget(delay_slider, 2, 1)
self.delay_label = QLabel("2.5")
recognition_layout.addWidget(self.delay_label, 2, 2)
recognition_group.setLayout(recognition_layout)
layout.addWidget(recognition_group)
tts_group = QGroupBox("Text-to-Speech Settings")
tts_layout = QGridLayout()
tts_layout.addWidget(QLabel("Voice:"), 0, 0)
self.voice_combo = QComboBox()
try:
voices = self.engine.getProperty('voices')
for i, voice in enumerate(voices):
self.voice_combo.addItem(f"{voice.name}", voice.id)
except:
self.voice_combo.addItem("Default Voice", "default")
self.voice_combo.currentIndexChanged.connect(self.update_voice)
tts_layout.addWidget(self.voice_combo, 0, 1, 1, 2)
tts_layout.addWidget(QLabel("Rate:"), 1, 0)
rate_slider = QSlider(Qt.Horizontal)
rate_slider.setRange(100, 300)
rate_slider.setValue(150)
rate_slider.valueChanged.connect(self.update_rate)
tts_layout.addWidget(rate_slider, 1, 1)
self.rate_label = QLabel("150")
tts_layout.addWidget(self.rate_label, 1, 2)
tts_layout.addWidget(QLabel("Volume:"), 2, 0)
volume_slider = QSlider(Qt.Horizontal)
volume_slider.setRange(0, 100)
volume_slider.setValue(100)
volume_slider.valueChanged.connect(self.update_volume)
tts_layout.addWidget(volume_slider, 2, 1)
self.volume_label = QLabel("100%")
tts_layout.addWidget(self.volume_label, 2, 2)
tts_group.setLayout(tts_layout)
layout.addWidget(tts_group)
interface_group = QGroupBox("Interface Settings")
interface_layout = QVBoxLayout()
self.auto_suggest_check = QCheckBox("Enable automatic word suggestions")
self.auto_suggest_check.setChecked(True)
self.auto_suggest_check.stateChanged.connect(self.toggle_auto_suggest)
interface_layout.addWidget(self.auto_suggest_check)
self.auto_clear_check = QCheckBox("Automatically clear word after selection")
self.auto_clear_check.setChecked(True)
interface_layout.addWidget(self.auto_clear_check)
self.auto_speak_check = QCheckBox("Automatically speak completed sentences")
self.auto_speak_check.setChecked(False)
interface_layout.addWidget(self.auto_speak_check)
interface_group.setLayout(interface_layout)
layout.addWidget(interface_group)
layout.addStretch()
tab.setLayout(layout)
return tab
def create_help_tab(self):
"""Create the help tab"""
tab = QWidget()
layout = QVBoxLayout()
help_text = QTextEdit()
help_text.setReadOnly(True)
help_text.setHtml("""
<h2>ASL Recognition System - Help</h2>
<h3>Getting Started</h3>
<p>This application recognizes American Sign Language (ASL) gestures through your webcam and converts them to text. It uses Gemini AI to enhance text for natural speech and word suggestions.</p>
<h3>How to Use</h3>
<ol>
<li>Position your hand clearly in the camera view</li>
<li>Make ASL signs for letters (A-Z), numbers (0-9), space, or period</li>
<li>Hold each sign steady for about 2-3 seconds</li>
<li>The recognized letter will appear and be added to the current word</li>
<li>Use word suggestions or continue signing to form sentences</li>
<li>Use the speak button to hear your sentence spoken aloud</li>
</ol>
<h3>Tips for Better Recognition</h3>
<ul>
<li>Ensure good lighting conditions</li>
<li>Position your hand against a plain background</li>
<li>Make clear, distinct hand shapes</li>
<li>Hold each sign steady until it's recognized</li>
<li>Use word suggestions for faster sentence building</li>
</ul>
<h3>Button Functions</h3>
<ul>
<li><b>🔊 Speak</b>: Speaks the current sentence aloud, enhanced by Gemini AI</li>
<li><b>💾 Save</b>: Saves the current sentence to a text file</li>
<li><b>❌ Clear Word</b>: Clears the current word being typed</li>
<li><b>🧹 Clear Sentence</b>: Clears the entire sentence</li>
<li><b>⎵ Add Space</b>: Manually adds a space</li>
<li><b>• Add Period</b>: Manually adds a period</li>
</ul>
<h3>About</h3>
<p>Enhanced ASL Recognition System v2.1 - Gemini Edition</p>
<p>This application uses MediaPipe for hand tracking, machine learning for ASL recognition, and Gemini AI for text enhancement.</p>
""")
layout.addWidget(help_text)
tab.setLayout(layout)
return tab
def apply_styles(self):
"""Apply modern styling with black text for visibility"""
self.setStyleSheet("""
QMainWindow {
background-color: #f5f5f5;
color: black;
}
QWidget {
color: black;
}
QGroupBox {
font-weight: bold;
border: 2px solid #cccccc;
border-radius: 8px;
margin-top: 15px;
padding-top: 20px;
background-color: white;
color: black;
}
QGroupBox::title {
subcontrol-origin: margin;
left: 15px;
padding: 0 8px;
color: black;
font-weight: bold;
}
QLabel {
color: black;
font-weight: normal;
background-color: transparent;
}
QPushButton {
background-color: #3498db;
color: white;
border: none;
padding: 8px 15px;
border-radius: 5px;
font-weight: bold;
min-height: 25px;
}
QPushButton:hover {
background-color: #2980b9;
}
QPushButton:pressed {
background-color: #1c6ea4;
}
QPushButton:disabled {
background-color: #bdc3c7;
color: #7f8c8d;
}
QProgressBar {
border: 2px solid #cccccc;
border-radius: 5px;
text-align: center;
background-color: white;
color: black;
}
QProgressBar::chunk {
background-color: #2ecc71;
border-radius: 3px;
}
QTabWidget::pane {
border: 2px solid #cccccc;
border-radius: 5px;
background-color: white;
}
QTabBar::tab {
background-color: #ecf0f1;
color: black;
padding: 10px 15px;
margin-right: 2px;
border-top-left-radius: 5px;
border-top-right-radius: 5px;
font-weight: bold;
}
QTabBar::tab:selected {
background-color: #3498db;
color: white;
}
QListWidget {
background-color: white;
color: black;
border: 2px solid #cccccc;
border-radius: 5px;
}
QTextEdit {
background-color: white;
color: black;
border: 2px solid #cccccc;
border-radius: 5px;
}
QSlider::groove:horizontal {
border: 1px solid #cccccc;
height: 8px;
background: #ecf0f1;
border-radius: 4px;
}
QSlider::handle:horizontal {
background: #3498db;
border: 2px solid #3498db;
width: 18px;
margin: -5px 0;
border-radius: 9px;
}
QComboBox {
background-color: white;
color: black;
border: 2px solid #cccccc;
border-radius: 5px;
padding: 5px;
}
QCheckBox {
color: black;
font-weight: normal;
}
QStatusBar {
background-color: #ecf0f1;
color: black;
border-top: 1px solid #cccccc;
}
""")
def update_frame(self):
"""Update video frame and process hand tracking"""
if not self.is_camera_active:
return
try:
ret, frame = self.cap.read()
if not ret:
self.status_bar.showMessage("Error: Could not read from camera")
return
frame = cv2.flip(frame, 1)
if self.show_landmarks:
processed_frame, hand_data = self.hand_processor.process_frame(frame)
else:
processed_frame = frame
_, hand_data = self.hand_processor.process_frame(frame.copy())
if hand_data:
prediction_idx = self.model_loader.predict(hand_data)
predicted_char = self.labels_dict.get(prediction_idx, '?')
stable_char = self.prediction_buffer.add_prediction(predicted_char)
if stable_char:
self.handle_stable_character(stable_char)
self.stats["letters_detected"] += 1
self.update_statistics()
confidence = min(100, max(0, 70 + np.random.randint(-10, 20)))
self.confidence_bar.setValue(confidence)
self.char_label.setText(f"Current Letter: {predicted_char}")
rgb_frame = cv2.cvtColor(processed_frame, cv2.COLOR_BGR2RGB)
h, w, ch = rgb_frame.shape
bytes_per_line = ch * w
qt_image = QImage(rgb_frame.data, w, h, bytes_per_line, QImage.Format_RGB888)
self.video_label.setPixmap(QPixmap.fromImage(qt_image))
except Exception as e:
print(f"Frame update error: {e}")
def handle_stable_character(self, char):
"""Handle a stable character detection"""
self.stable_char = char
if char == ' ':
if self.word_buffer:
self.sentence += self.word_buffer + " "
self.word_buffer = ""
self.stats["words_formed"] += 1
elif char == '.':
if self.word_buffer:
self.sentence += self.word_buffer + "."
self.word_buffer = ""
self.stats["words_formed"] += 1
self.stats["sentences_completed"] += 1
else:
self.sentence += "."
self.stats["sentences_completed"] += 1
else:
self.word_buffer += char
self.update_word_label()
self.update_sentence_label()
self.update_suggestions()
self.update_statistics()
def handle_special_char(self, char):
"""Handle special character input from buttons"""
if char == ' ':
if self.word_buffer:
self.sentence += self.word_buffer + " "
self.word_buffer = ""
self.stats["words_formed"] += 1
elif char == '.':
if self.word_buffer:
self.sentence += self.word_buffer + "."
self.word_buffer = ""
self.stats["words_formed"] += 1
self.stats["sentences_completed"] += 1
else:
self.sentence += "."
self.stats["sentences_completed"] += 1
self.update_word_label()
self.update_sentence_label()
self.update_suggestions()
self.update_statistics()
def update_word_label(self):
"""Update the current word display"""
self.word_label.setText(f"Current Word: {self.word_buffer}")
def update_sentence_label(self):
"""Update the sentence display"""
self.sent_label.setText(f"Sentence: {self.sentence}")
if hasattr(self, 'auto_speak_check') and self.auto_speak_check.isChecked() and self.sentence.endswith('.'):
self.speak()
def update_suggestions(self):
"""Update word suggestions with Gemini enhancement if enabled"""
if not self.auto_suggest:
return
try:
suggestions = self.dictionary_loader.get_suggestions(self.word_buffer)
suggestion_terms = [s.term for s in suggestions]
if hasattr(self, 'use_gemini') and self.use_gemini and self.word_buffer:
try:
prompt = (
f"Given the current sentence: '{self.sentence}' and the partial word: '{self.word_buffer}', "
f"suggest up to 5 relevant words that fit the context. Return only the words, one per line."
)
response = self.gemini_model.generate_content(prompt)
gemini_suggestions = response.text.strip().split('\n')[:5]
suggestion_terms = list(dict.fromkeys(suggestion_terms + gemini_suggestions))[:5]
except Exception as e:
print(f"Gemini suggestion error: {e}")
suggestion_terms = suggestion_terms[:5]
for i in range(5):
if i < len(suggestion_terms):
self.suggest_buttons[i].setText(suggestion_terms[i])
self.suggest_buttons[i].setEnabled(True)
else:
self.suggest_buttons[i].setText("")
self.suggest_buttons[i].setEnabled(False)
except Exception as e:
print(f"Suggestion error: {e}")
def update_statistics(self):
"""Update statistics display"""
self.letters_label.setText(f"Letters Detected: {self.stats['letters_detected']}")
self.words_label.setText(f"Words Formed: {self.stats['words_formed']}")
self.sentences_label.setText(f"Sentences: {self.stats['sentences_completed']}")
elapsed = int(time.time() - self.stats["start_time"])
minutes, seconds = divmod(elapsed, 60)
self.time_label.setText(f"Session Time: {minutes:02d}:{seconds:02d}")
def use_suggestion(self):
"""Use a word suggestion"""
btn = self.sender()
selected_word = btn.text()
if hasattr(self, 'auto_clear_check') and self.auto_clear_check.isChecked():
self.sentence += selected_word + " "
self.word_buffer = ""
self.stats["words_formed"] += 1
else:
self.word_buffer = selected_word
self.update_word_label()
self.update_sentence_label()
self.update_suggestions()
self.update_statistics()
def speak(self):
"""Speak the current sentence with Gemini-enhanced text"""
text_to_speak = self.sentence.strip()
if self.word_buffer:
text_to_speak += " " + self.word_buffer
if not text_to_speak:
self.status_bar.showMessage("No text to speak")
return
try:
self.status_bar.showMessage("Processing and speaking...")