Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update PiVideoStream to let its method 'read' always return newly updated frame #231

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions imutils/video/pivideostream.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from picamera import PiCamera
from threading import Thread
import cv2
import time

class PiVideoStream:
def __init__(self, resolution=(320, 240), framerate=32, **kwargs):
Expand All @@ -22,9 +23,10 @@ def __init__(self, resolution=(320, 240), framerate=32, **kwargs):
self.stream = self.camera.capture_continuous(self.rawCapture,
format="bgr", use_video_port=True)

# initialize the frame and the variable used to indicate
# if the thread should be stopped
# initialize the frame and the checker that checks is it updated
# and the variable used to indicate if the thread should be stopped
self.frame = None
self.is_updated = False
self.stopped = False

def start(self):
Expand All @@ -38,9 +40,10 @@ def update(self):
# keep looping infinitely until the thread is stopped
for f in self.stream:
# grab the frame from the stream and clear the stream in
# preparation for the next frame
# preparation for the next frame and update the update checker
self.frame = f.array
self.rawCapture.truncate(0)
self.is_updated = True

# if the thread indicator variable is set, stop the thread
# and resource camera resources
Expand All @@ -51,9 +54,17 @@ def update(self):
return

def read(self):
# return the frame most recently read
# block until there is newly updated frame
while not self.is_updated:
if self.stopped:
return None
time.sleep(0.001)
continue
self.is_updated = False
# return the newly updated frame
return self.frame

def stop(self):
# indicate that the thread should be stopped
self.stopped = True