--- a +++ b/utils/webcamvideostream.py @@ -0,0 +1,46 @@ +# import the necessary packages +from threading import Thread +import cv2 + + +class WebcamVideoStream: + def __init__(self, resolution, src=0, name="WebcamVideoStream"): + # initialize the video camera stream and read the first frame + # from the stream + assert isinstance(resolution, tuple) and len(resolution) == 2 + self.stream = cv2.VideoCapture(src) + self.stream.set(3, resolution[0]) + self.stream.set(4, resolution[1]) + (self.grabbed, self.frame) = self.stream.read() + + # initialize the thread name + self.name = name + + # 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, name=self.name, 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 + 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