本文整理汇总了Python中detector.Detector.detect_motion方法的典型用法代码示例。如果您正苦于以下问题:Python Detector.detect_motion方法的具体用法?Python Detector.detect_motion怎么用?Python Detector.detect_motion使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类detector.Detector
的用法示例。
在下文中一共展示了Detector.detect_motion方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Watcher
# 需要导入模块: from detector import Detector [as 别名]
# 或者: from detector.Detector import detect_motion [as 别名]
class Watcher(object):
def __init__(self,
camera_id=None,
show_display=False,
width=FRAME_WIDTH,
height=FRAME_HEIGHT,
extra_time=EXTRA_TIME,
video_directory=VIDEO_DIR,
video_prefix=VIDEO_PREFIX,
do_compress=DO_COMPRESS):
self.camera = Camera(camera_id, prop_set={width: width, height: height})
self.total_pixels = height * width
self.detector = Detector()
self.display = None
if show_display:
self.display = Display((width, height))
self.extra_time_delta = timedelta(seconds=extra_time)
self.video_prefix = os.path.join(video_directory, video_prefix)
self.do_compress = do_compress
def get_frame(self):
"""
Get a frame with the camera, transform to greyscale and convert to Numpy
array.
"""
now = datetime.now()
image = self.camera.getImage().toGray()
return VideoFrame(image=image, timestamp=now)
def main_loop(self):
frame_pre = self.get_frame()
frame = self.get_frame()
frame_post = self.get_frame()
video_buffer = []
record_until = datetime.now()
while True:
detect = self.detector.detect_motion(frame_pre, frame, frame_post)
if detect['has_motion']:
record_until = frame.timestamp + self.extra_time_delta
if self.display:
detect['difference_image'].save(self.display)
if frame.timestamp < record_until:
video_buffer.append(frame)
else:
if video_buffer:
buffer_fname = self.save_video(video_buffer)
if self.do_compress and buffer_fname:
proc = Process(target=self.compress_video,
args=(buffer_fname,))
proc.start()
video_buffer = []
frame_pre = frame
frame = frame_post
frame_post = self.get_frame()
def save_video(self, video_buffer):
"""Use avconv to process the video buffer."""
buffer_fname = None
if not video_buffer:
return None
start = video_buffer[0].timestamp
end = video_buffer[-1].timestamp
delta = (end - start).seconds
if delta:
fps = len(video_buffer) / delta
print len(video_buffer), "frames in", delta, "seconds:", fps
timestamp = video_buffer[0].timestamp.isoformat()
timestamp = timestamp[:timestamp.rfind('.')]
buffer_fname = '%s%s.avi' % (self.video_prefix, timestamp)
video_streamer = VideoStream(fps=fps,
filename=buffer_fname,
framefill=False)
map(video_streamer.writeFrame, [frame.image for frame in video_buffer])
print "Saved video:", buffer_fname
return buffer_fname
def compress_video(self, input_fname):
output_fname = input_fname + '.mp4'
params = '-i %s -c:a copy -c:v libx264 -crf 23 -s:v 640x480 %s' % (
input_fname, output_fname)
call('avconv ' + params, shell=True)
os.remove(input_fname)