| 5 | import cv2 |
| 6 | |
| 7 | class PiVideoStream: |
| 8 | def __init__(self, resolution=(320, 240), framerate=32, **kwargs): |
| 9 | # initialize the camera |
| 10 | self.camera = PiCamera() |
| 11 | |
| 12 | # set camera parameters |
| 13 | self.camera.resolution = resolution |
| 14 | self.camera.framerate = framerate |
| 15 | |
| 16 | # set optional camera parameters (refer to PiCamera docs) |
| 17 | for (arg, value) in kwargs.items(): |
| 18 | setattr(self.camera, arg, value) |
| 19 | |
| 20 | # initialize the stream |
| 21 | self.rawCapture = PiRGBArray(self.camera, size=resolution) |
| 22 | self.stream = self.camera.capture_continuous(self.rawCapture, |
| 23 | format="bgr", use_video_port=True) |
| 24 | |
| 25 | # initialize the frame and the variable used to indicate |
| 26 | # if the thread should be stopped |
| 27 | self.frame = None |
| 28 | self.stopped = False |
| 29 | |
| 30 | def start(self): |
| 31 | # start the thread to read frames from the video stream |
| 32 | t = Thread(target=self.update, args=()) |
| 33 | t.daemon = True |
| 34 | t.start() |
| 35 | return self |
| 36 | |
| 37 | def update(self): |
| 38 | # keep looping infinitely until the thread is stopped |
| 39 | for f in self.stream: |
| 40 | # grab the frame from the stream and clear the stream in |
| 41 | # preparation for the next frame |
| 42 | self.frame = f.array |
| 43 | self.rawCapture.truncate(0) |
| 44 | |
| 45 | # if the thread indicator variable is set, stop the thread |
| 46 | # and resource camera resources |
| 47 | if self.stopped: |
| 48 | self.stream.close() |
| 49 | self.rawCapture.close() |
| 50 | self.camera.close() |
| 51 | return |
| 52 | |
| 53 | def read(self): |
| 54 | # return the frame most recently read |
| 55 | return self.frame |
| 56 | |
| 57 | def stop(self): |
| 58 | # indicate that the thread should be stopped |
| 59 | self.stopped = True |
no outgoing calls
no test coverage detected
searching dependent graphs…