-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwebcam_inference.py
82 lines (60 loc) · 2.17 KB
/
webcam_inference.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
from collections import deque, Counter
import cv2
from fastai.vision.all import *
print('Loading our Inference model...')
# load our inference model
inf_model = load_learner('model/sign_language.pkl')
print('Model Loaded')
# define a deque to get rolling average of predictions
# I go with the last 10 predictions
rolling_predictions = deque([], maxlen=10)
# get the most common item in the deque
def most_common(D):
data = Counter(D)
return data.most_common(1)[0][0]
def hand_area(img):
# specify where hand should go
hand = img[50:324, 50:324]
# the images in the model were trainind on 200x200 pixels
hand = cv2.resize(hand, (200,200))
return hand
# capture video on the webcam
cap = cv2.VideoCapture(0)
# get the dimensions on the frame
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))
# define codec and create our VideoWriter to save the video
# fourcc = cv2.VideoWriter_fourcc(*'mp4v')
# out = cv2.VideoWriter('output/sign-language.mp4', fourcc, 12, (frame_width, frame_height))
# read video
while True:
# capture each frame of the video
_, frame = cap.read()
# flip frame to feel more 'natural' to webcam
frame = cv2.flip(frame, flipCode = 1)
# draw a blue rectangle where to place hand
cv2.rectangle(frame, (50, 50), (324, 324), (255, 0, 0), 2)
# get the image
inference_image = hand_area(frame)
# get the current prediction on the hand
pred = inf_model.predict(inference_image)
# append the current prediction to our rolling predictions
rolling_predictions.append(pred[0])
# our prediction is going to be the most common letter
# in our rolling predictions
prediction_output = f'The predicted letter is {most_common(rolling_predictions)}'
# show predicted text
cv2.putText(frame, prediction_output, (10, 350), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 0, 0), 2)
# show the frame
cv2.imshow('frame', frame)
# save the frames to out file
# out.write(frame)
# press `q` to exit
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# release VideoCapture()
cap.release()
# release out file
# out.release()
# close all frames and video windows
cv2.destroyAllWindows()