-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunner.py
More file actions
160 lines (124 loc) · 4.78 KB
/
Runner.py
File metadata and controls
160 lines (124 loc) · 4.78 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
"""
Real-time ASL Inference Script
"""
import cv2
import time
import numpy as np
from ultralytics import YOLO
# Configuration
MODEL_PATH = "models/best_asl_27.pt"
CONFIDENCE_THRESHOLD = 0.6
DEBOUNCE_TIME = 1.5
CLASS_NAMES = [
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'backspace'
]
class SentenceBuilder:
def __init__(self):
self.text = ""
self.last_gesture = None
self.last_time = 0
def add_character(self, char, current_time):
if char == self.last_gesture and (current_time - self.last_time) < DEBOUNCE_TIME:
return False
if char == 'backspace':
if self.text:
self.text = self.text[:-1]
print(f"[BACKSPACE] → '{self.text}'")
else:
self.text += char
print(f"[+{char}] → '{self.text}'")
self.last_gesture = char
self.last_time = current_time
return True
def get_text(self):
return self.text if self.text else "[Empty]"
def clear(self):
self.text = ""
print("[CLEARED]")
def main():
print("\n" + "="*60)
print("ASL REAL-TIME DETECTION")
print("="*60)
print(f"\nLoading model: {MODEL_PATH}")
try:
model = YOLO(MODEL_PATH)
print("✓ Model loaded")
except Exception as e:
print(f"❌ Error: {e}")
print("\nRun: python train_model.py first")
return
print("\nInitializing webcam...")
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("❌ Cannot access webcam!")
return
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
print("✓ Webcam ready")
builder = SentenceBuilder()
print("\n" + "="*60)
print("CONTROLS: Q=Quit | C=Clear | S=Stats")
print("="*60 + "\n")
fps_time = time.time()
fps_counter = 0
fps = 0
while True:
ret, frame = cap.read()
if not ret:
break
frame = cv2.flip(frame, 1)
current_time = time.time()
results = model(frame, conf=CONFIDENCE_THRESHOLD, verbose=False)
for result in results:
boxes = result.boxes
for box in boxes:
x1, y1, x2, y2 = map(int, box.xyxy[0])
confidence = float(box.conf[0])
class_id = int(box.cls[0])
class_name = CLASS_NAMES[class_id]
color = (0, 165, 255) if class_name == 'backspace' else (0, 255, 0)
cv2.rectangle(frame, (x1, y1), (x2, y2), color, 3)
label = f"{class_name} {confidence:.2f}"
label_size, _ = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.7, 2)
cv2.rectangle(frame, (x1, y1 - label_size[1] - 10),
(x1 + label_size[0] + 10, y1), color, -1)
cv2.putText(frame, label, (x1 + 5, y1 - 5),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2)
if confidence >= CONFIDENCE_THRESHOLD:
builder.add_character(class_name, current_time)
fps_counter += 1
if current_time - fps_time > 1:
fps = fps_counter
fps_counter = 0
fps_time = current_time
panel_height = 180
panel = np.zeros((panel_height, frame.shape[1], 3), dtype=np.uint8)
panel[:] = (40, 40, 40)
cv2.putText(panel, "ASL Sentence Builder", (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (100, 200, 255), 2)
text_display = builder.get_text()
if len(text_display) > 50:
text_display = "..." + text_display[-47:]
cv2.rectangle(panel, (10, 45), (frame.shape[1] - 10, 100), (60, 60, 60), -1)
cv2.putText(panel, text_display, (20, 80),
cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255, 255, 255), 2)
cv2.putText(panel, "Q: Quit | C: Clear", (10, 130),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (150, 150, 150), 1)
cv2.putText(frame, f"FPS: {fps}", (10, 35),
cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 255, 0), 2)
combined = np.vstack([frame, panel])
cv2.imshow("ASL Detection", combined)
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
break
elif key == ord('c'):
builder.clear()
cap.release()
cv2.destroyAllWindows()
print("\n" + "="*60)
print(f"Final text: {builder.text if builder.text else '[Empty]'}")
print("="*60 + "\n")
if __name__ == "__main__":
main()