用树莓派制作动作监控摄像系统

Posted So istes immer

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用树莓派制作动作监控摄像系统相关的知识,希望对你有一定的参考价值。

目录

这里我用的是CSI接口的摄像头

1.启用CSI摄像头 

①摄像头提前安装好
②sudo raspi-config
进行如下配置InterFacing Options -> Camera -> Enable

2.测试

raspistill -v -o test.jpg
用raspistill这个指令可以抓拍一张图片
-v选项表示在运行摄像头时输出详细信息
-o选项用于指出输出图片的文件名(这里的文件名是 test.jpg)

raspistill --help -? 可以查看raspistill的使用帮助

要想查看我们抓拍下来的test.jpg,你可以把远程桌面调出来,也可以把图片下到本地

要想实现后者,需要两步:
①sudo apt-get install lrzsz
②sz test.jpg


 

3. 用一个python脚本实现摄像头的实时监控 

用xshell和树莓派连接好之后,安装python3:sudo apt-get install python3 

monitor.py 

# Web streaming example
# Source code from the official PiCamera package
# http://picamera.readthedocs.io/en/latest/recipes2.html#web-streaming

import io
import picamera
import logging
import socketserver
from threading import Condition
from http import server

PAGE="""\\
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<head>
<title>Raspberry Pi - Surveillance Camera</title>
</head>
<body>
<center><h1>Raspberry Pi - Surveillance Camera</h1></center>
<center><h1>你在我的眼里</h1></center>
<center><img src="stream.mjpg" width="640" height="480"></center>
</body>
</html>
"""

class StreamingOutput(object):
    def __init__(self):
        self.frame = None
        self.buffer = io.BytesIO()
        self.condition = Condition()

    def write(self, buf):
        if buf.startswith(b'\\xff\\xd8'):
            # New frame, copy the existing buffer's content and notify all
            # clients it's available
            self.buffer.truncate()
            with self.condition:
                self.frame = self.buffer.getvalue()
                self.condition.notify_all()
            self.buffer.seek(0)
        return self.buffer.write(buf)

class StreamingHandler(server.BaseHTTPRequestHandler):
    def do_GET(self):
        if self.path == '/':
            self.send_response(301)
            self.send_header('Location', '/index.html')
            self.end_headers()
        elif self.path == '/index.html':
            content = PAGE.encode('utf-8')
            self.send_response(200)
            self.send_header('Content-Type', 'text/html')
            self.send_header('Content-Length', len(content))
            self.end_headers()
            self.wfile.write(content)
        elif self.path == '/stream.mjpg':
            self.send_response(200)
            self.send_header('Age', 0)
            self.send_header('Cache-Control', 'no-cache, private')
            self.send_header('Pragma', 'no-cache')
            self.send_header('Content-Type', 'multipart/x-mixed-replace; boundary=FRAME')
            self.end_headers()
            try:
                while True:
                    with output.condition:
                        output.condition.wait()
                        frame = output.frame
                    self.wfile.write(b'--FRAME\\r\\n')
                    self.send_header('Content-Type', 'image/jpeg')
                    self.send_header('Content-Length', len(frame))
                    self.end_headers()
                    self.wfile.write(frame)
                    self.wfile.write(b'\\r\\n')
            except Exception as e:
                logging.warning(
                    'Removed streaming client %s: %s',
                    self.client_address, str(e))
        else:
            self.send_error(404)
            self.end_headers()

class StreamingServer(socketserver.ThreadingMixIn, server.HTTPServer):
    allow_reuse_address = True
    daemon_threads = True

with picamera.PiCamera(resolution='640x480', framerate=24) as camera:
    output = StreamingOutput()
    #Uncomment the next line to change your Pi's Camera rotation (in degrees)
    #camera.rotation = 90
    camera.start_recording(output, format='mjpeg')
    try:
        address = ('', 8000)
        server = StreamingServer(address, StreamingHandler)
        server.serve_forever()
    finally:
        camera.stop_recording()

可以在本地把上面的python脚本搞好,再通过rz指令传到树莓派上 

传上去之后,切换到monitor.py所在目录,执行python3 monitor.py

之后,如果你的电脑和树莓派都连的手机热点,就可以在手机浏览器中输入:http://树莓派IP:8000 ,效果如下:

以上是关于用树莓派制作动作监控摄像系统的主要内容,如果未能解决你的问题,请参考以下文章

树莓派摄像头检测人物动作

基于树莓派的家庭智能监控系统如何实现?

「玩转树莓派」搭建智能家居远程监控系统

树莓派进阶之路 (017) - 基于树莓派的专用摄像头实时监控

在树莓派上使用动作来解决网络摄像头流问题

智能家居代码构建编写简单工厂模式树莓派摄像头视频监控功能实现