此次用到的配件:(可直接点击查看配件链接)
直接上干货,教程开始:
一:摄像头安装和启用
1.先将摄像头插入树莓派主板,注意接口方向。CSI摄像头排线蓝色面朝USB的方向。(参考下图)注意:插摄像头排线前建议关机操作防止静电对树莓派的伤害。
2.树莓派开机进入系统后,先开启树莓派摄像头功能。可在LX终端或者命令模式下输入下面命令
sudo raspi-config输入命令后进入系统设置界面选择第三项:Interface Options 按回车,在选择:Camera 按回车,选择:"yes" 按回车这样就能启用摄像头功能了。
二:设置python环境
1.打开树莓派LX终端或SSH命令模式下,输入命令跳转到/usr/bin目录下
cd /usr/bin2.我们先查询一下python当前版本,输入下面命令(我这里的是python2.7.16如下图)
python --version
3.接下来我们需要让python连接到python3,输入命令
sudo ln -sf python3 python
下图是命令解析:
4.然后我们再次查询python版本,这样就变成python3.7.3了。(如下图)
python --version
到此步骤说明环境设置完成了!
三:新建程序脚本
1.我们先输入命令新建一个py脚本,(如下)
sudo nano rpi-camera.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> <head> <title>Raspberry Pi - Surveillance Camera</title> </head> <body> <center><h1>Raspberry Pi - Surveillance Camera</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()
3.脚本程序粘贴好后,按Ctrl+X选择Y保存。 (如果感觉麻烦也可以直接下载脚本文件放在你的pi根目录下面)
脚本程序点击下载:rpi-camera.py 提取码:8888
四:打开实时监控
1.现在就到了最关键激动的时刻,直接输入下面命令即可查看监控画面。
sudo python rpi-camera.py
输入命令后,会一直停留在这个界面。 接下来我们可以用手机或者在电脑游览器上直接输入树莓派(IP地址:800):192.168.0.104:8000 来查看监控效果。
(注意:这里的电脑和手机还有树莓派必须是在同一个局域网内,树莓派IP地址后面的冒号注意大小写,正确格式:192.168.0.104:8000, 错误的格式:192.168.0.104:8000) 看出来什么区别了吗? 冒号的间距太大切换大写字母下的冒号间距比较短。
五:实时监控画面效果
至此教程结束,可以用来当生活中的一些视频查看器,或者实时监控等。剩下的就自己开发吧