-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcamera.py
More file actions
55 lines (39 loc) · 1.27 KB
/
camera.py
File metadata and controls
55 lines (39 loc) · 1.27 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
import cv2
import os
import numpy as np
import onnxruntime as ort
def center_crop(frame):
h , w , _ = frame.shape
start = abs(h - w) // 2
if h > w:
return frame[start:start+w]
return frame[:,start:start+h]
def main():
#constants
index_to_letter = list('ABCDEFGHIKLMNOPQRSTUVWXY')
mean = 0.485 * 255.
std = 0.229 * 255.
#create a runnable session with exported model
fname = "signlanguage.onnx"
ort_session = ort.InferenceSession(fname)
execution_path = os.getcwd()
cap = cv2.VideoCapture(0)
while True:
#create frame by frame
ret , frame = cap.read()
#preprocess data
frame = center_crop(frame)
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
x = cv2.resize(frame,(28,28))
x = x.reshape(1,1,28,28).astype(np.float32)
y = ort_session.run(None,{'input':x})[0]
index = np.argmax(y,axis=1)
letter = index_to_letter[int(index)]
cv2.putText(frame,letter,(100,100),cv2.FONT_HERSHEY_SIMPLEX, 2.0, (0, 255, 0), thickness=2)
cv2.imshow("Sign Language Translator",frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
main()