MCPcopy Create free account
hub / github.com/PyImageSearch/imutils / PiVideoStream

Class PiVideoStream

imutils/video/pivideostream.py:7–59  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5import cv2
6
7class 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

Callers 1

__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…