-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmt_video_feed.py
157 lines (120 loc) · 4.13 KB
/
mt_video_feed.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
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
# import the necessary packages
import datetime
from threading import Thread
import cv2
import numpy as np
import time
import copy
class FPS:
def __init__(self):
# store the start time, end time, and total number of frames
# that were examined between the start and end intervals
self._start = None
self._end = None
self._numFrames = 0
def start(self):
# start the timer
self._start = datetime.datetime.now()
return self
def stop(self):
# stop the timer
self._end = datetime.datetime.now()
def update(self):
# increment the total number of frames examined during the
# start and end intervals
self._numFrames += 1
def elapsed(self):
# return the total number of seconds between the start and
# end interval
return (self._end - self._start).total_seconds()
def fps(self):
# compute the (approximate) frames per second
return self._numFrames / self.elapsed()
class WebcamVideoStream:
def __init__(self, src=0):
# initialize the video camera stream and read the first frame
# from the stream
self.stream = cv2.VideoCapture(src)
(self.grabbed, self.frame) = self.stream.read()
# initialize the variable used to indicate if the thread should
# be stopped
self.stopped = False
def start(self):
# start the thread to read frames from the video stream
Thread(target=self.update, args=()).start()
return self
def update(self):
# keep looping infinitely until the thread is stopped
while True:
# if the thread indicator variable is set, stop the thread
if self.stopped:
return
# otherwise, read the next frame from the stream
(self.grabbed, self.frame) = self.stream.read()
def read(self):
# return the frame most recently read
return self.frame
def stop(self):
# indicate that the thread should be stopped
self.stopped = True
class VideoFeed(object):
def __init__(self, model):
self.stream = WebcamVideoStream(src = 0).start()
self.model = model
# ROI properties
self.x0, self.y0 = 50, 120
self.x1, self.y1 = 850, 120
self.width = 375
self.dataColor = (255, 0, 0)
self.classes = 'NONE ONE TWO THREE FOUR FIVE'.split()
self.img_size = 64
# Right ROI
self.roi1 = None
self.prediction1 = "NONE"
Thread(target = self.fingerPrediction, args = (1,)).start()
# Left ROI
self.roi2 = None
self.prediction2 = "NONE"
Thread(target = self.fingerPrediction, args = (2,)).start()
# Fonts
self.font = cv2.FONT_HERSHEY_SIMPLEX
self.fx0, self.fy0 = 169, 15
self.fx1, self.fy1 = 950, 15
self.fh = 45
def __del__(self):
self.stream.stop()
def binaryMask(self, img):
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = cv2.GaussianBlur(img, (7,7), 3)
img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
return img
def fingerPrediction(self, which_roi):
while True:
if which_roi == 1:
roi = self.roi1
elif which_roi == 2:
roi = self.roi2
if not roi is None:
roi = self.binaryMask(roi)
roi = cv2.resize(roi, (self.img_size, self.img_size))
img = np.float32(roi) / 255.
img = np.expand_dims(img, axis = 0)
img = np.expand_dims(img, axis = -1)
if which_roi == 1:
self.prediction1 = self.classes[np.argmax(self.model.predict(img)[0])]
elif which_roi == 2:
self.prediction2 = self.classes[np.argmax(self.model.predict(img)[0])]
roi = None
def get_frame(self):
frame = self.stream.read()
frame = cv2.flip(frame, 1)
cv2.rectangle(frame, (self.x0,self.y0), (self.x0+self.width-1,self.y0+self.width-1), self.dataColor, 1)
cv2.rectangle(frame, (self.x1,self.y1), (self.x1+self.width-1,self.y1+self.width-1), self.dataColor, 1)
self.roi1 = frame[self.y0:self.y0+self.width,self.x0:self.x0+self.width]
self.roi2 = frame[self.y1:self.y1+self.width,self.x1:self.x1+self.width]
#cv2.putText(frame, "Left: {0}".format(self.prediction1), (self.fx0, self.fy0+2*self.fh), self.font, 1.0, (0,0,0), 3, 1)
#cv2.putText(frame, "Right: {0}".format(self.prediction2), (self.fx1, self.fy1+2*self.fh), self.font, 1.0, (0,0,0), 3, 1)
_, jpeg = cv2.imencode(".jpg", frame)
return frame,jpeg.tobytes()
def get_prediction(self):
return self.prediction1, self.prediction2