-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplease
More file actions
86 lines (73 loc) · 2.06 KB
/
please
File metadata and controls
86 lines (73 loc) · 2.06 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
import numpy as np
import cv2
from tensorflow.keras.models import load_model
model = load_model('best_model_v4.h5') # Load your trained model
target_size = (200, 200) # Example target size, should match your model's input size
num_to_label = {
0: 'A',
1: 'B',
2: 'C',
3: 'D',
4: 'E',
5: 'F',
6: 'G',
7: 'H',
8: 'I',
9: 'J',
10: 'K',
11: 'L',
12: 'M',
13: 'N',
14: 'O',
15: 'P',
16: 'Q',
17: 'R',
18: 'S',
19: 'T',
20: 'U',
21: 'V',
22: 'W',
23: 'X',
24: 'Y',
25: 'Z',
26: 'del',
27: 'nothing',
28: 'space'
}
def preprocess_frame(frame, target_size):
frame = crop_to_square(frame) # Crop the frame to a square
frame = cv2.resize(frame, target_size)
frame = frame / 255.0 # Normalize to [0, 1] range
frame = frame.astype('float32')
frame = np.expand_dims(frame, axis=0) # Add batch dimension
return frame
def crop_to_square(frame):
h, w, _ = frame.shape
if h > w:
diff = (h - w) // 2
frame = frame[diff:diff + w, :]
else:
diff = (w - h) // 2
frame = frame[:, diff:diff + h]
return frame
cap = cv2.VideoCapture(0) # Start capturing video from the first webcam
while True:
ret, frame = cap.read()
if not ret:
break
# Crop the frame to a square before any processing
square_frame = crop_to_square(frame)
# Preprocess the frame
processed_frame = preprocess_frame(frame, target_size)
prediction = model.predict(processed_frame)
# Decode the prediction (depends on your model's output format)
predicted_label = np.argmax(prediction)
# Map the predicted label to the corresponding letter
predicted_label = num_to_label[predicted_label]
# Display the label on the square frame
cv2.putText(square_frame, str(predicted_label), (30, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 0, 0), 2, cv2.LINE_AA)
cv2.imshow('Live ASL Recognition', square_frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()