-
Notifications
You must be signed in to change notification settings - Fork 0
/
realtime.py
88 lines (65 loc) · 2.02 KB
/
realtime.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
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
'''
USAGE:
python cam_test.py
'''
import torch
import joblib
import torch.nn as nn
import numpy as np
import cv2
import torch.nn.functional as F
import time
import cnn_models
# load label binarizer
lb = joblib.load('C:\\Users\\SARAH\\Desktop\\Multivariate Stats\\ASL\\output\\lb.pkl')
model = cnn_models.CustomCNN()
model.load_state_dict(torch.load('C:\\Users\\SARAH\\Desktop\\Multivariate Stats\\ASL\\output\\model.pth'))
print(model)
print('Model loaded')
# In[ ]:
def hand_area(img):
hand = img[100:324, 100:324]
hand = cv2.resize(hand, (224,224))
return hand
# In[ ]:
cap = cv2.VideoCapture(0)
if (cap.isOpened() == False):
print('Error while trying to open camera. Plese check again...')
# get the frame width and height
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))
# define codec and create VideoWriter object
out = cv2.VideoWriter('ASL/output/asl.mp4', cv2.VideoWriter_fourcc(*'mp4v'), 30, (frame_width,frame_height))
# In[ ]:
while(cap.isOpened()):
# capture each frame of the video
ret, frame = cap.read()
# get the hand area on the video capture screen
cv2.rectangle(frame, (100, 100), (324, 324), (20, 34, 255), 2)
hand = hand_area(frame)
image = hand
image = np.transpose(image, (2, 0, 1)).astype(np.float32)
image = torch.tensor(image, dtype=torch.float)
image = image.unsqueeze(0)
outputs = model(image)
_, preds = torch.max(outputs.data, 1)
cv2.putText(frame, lb.classes_[preds], (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2)
#plt.imshow(frame, cmap = 'gray', interpolation = 'bicubic')
#plt.xticks([]), plt.yticks([]) # to hide tick values on X and Y axis
#plt.show()
cv2.imshow('image', frame)
#exit()
#out.write(frame)
# press `q` to exit
if cv2.waitKey(27) & 0xFF == ord('q'):
break
# release VideoCapture()
cap.release()
# close all frames and video windows
cv2.destroyAllWindows()