-
Notifications
You must be signed in to change notification settings - Fork 2
/
FaceAndEyeDetectorStream.py
55 lines (45 loc) · 1.84 KB
/
FaceAndEyeDetectorStream.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
# import the necessary packages
from threading import Thread
from WebcamVideoStream import WebcamVideoStream
from features import extract_image_features
import cv2
class FaceAndEyeDetectorStream:
def __init__(self, src=0):
# initialize the video camera stream and read the first frame
# from the stream
self.webcam_stream = WebcamVideoStream(src).start()
frame, frame_time = self.webcam_stream.read()
self.frame_time = frame_time
if frame is not None:
(self.img, self.faces, self.face_features) = extract_image_features(frame)
# 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
t = Thread(target=self.update, args=())
t.daemon = True
t.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
# print('updating')
if self.stopped:
# print('returning')
return
# otherwise, read the next frame from the stream
frame, frame_time = self.webcam_stream.read()
self.frame_time = frame_time
if frame is not None:
(self.img, self.faces, self.face_features) = extract_image_features(frame)
# print('the faces', self.faces)
# print('updated', self.grabbed)
def read(self):
# return the frame most recently read
return (self.img, self.faces, self.face_features, self.frame_time)
def stop(self):
# indicate that the thread should be stopped
self.webcam_stream.stop()
self.stopped = True