当前位置: 首页>>代码示例>>Python>>正文


Python Detector.detect_motion方法代码示例

本文整理汇总了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)
开发者ID:emilioramirez,项目名称:sleepwalker,代码行数:90,代码来源:watcher.py


注:本文中的detector.Detector.detect_motion方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。