前言
在GitHub上發現一個好玩的項目,不用安裝其它任何依賴包,只要運行一個Python腳本就可以在局域網構建一個視頻監控系統。果斷試了一下,確實挺好玩的,現在分享給大家。

搭建方法
1. 硬件準備
首先你得有一個樹莓派CSI接口的攝像頭和一個裝好系統的樹莓派。 如何安裝系統請參考我之前的教程如何給樹莓派寫入鏡像


如果你之前已經使用過攝像頭接口,直接看跳到下一步。如果還沒有的用過的話,樹莓派攝像頭接口可能還沒有打開,這是樹莓派打開攝像頭接口的方法: (1) 命令行輸入: sudo raspi-config (2) 選中 InterfacingOptions,按 Enter進入子菜單

(3) 選中 Camera,按 Enter打開攝像頭接口,確認后即可

2. 接入攝像頭
給樹莓派接上攝像頭很容易。確保樹莓派已經關機的情況下,用指甲輕輕打開CSI接口卡扣,將連接帶有金屬接觸面的方向對準CSI接口黑色的一面,再扣緊卡扣就可以了。

3. 找到樹莓派的IP地址
在樹莓派終端輸入命令 ipconfig,wlan0即為樹莓派的ip地址,這里是 192.168.123.126

4. 下載腳本
GitHub項目地址:(
https://github.com/RuiSantosdotme/Random-Nerd-Tutorials/blob/master/Projects/rpicamerasurveillance_system.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>小雨的家 Xiaoyu's Home</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'xffxd8'):
# 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'--FRAMErn')
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'rn')
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()
5. 寫入腳本
將上面的代碼保存一個名為 rpi.py的Python文件,通過smb傳輸的方式,將代碼上傳至樹莓派任意用戶文件夾中,這里我們可以上傳到樹莓派 ~/swift文件夾下。
注意:如果你還不會使用smb服務,請查看我的上一篇文章樹莓派安裝Samba服務
6. 代碼執行
python3 ~/swift/rpi.python
此時監控系統以及開始工作了。局域網內的任何設備,打開瀏覽器地址欄輸入 192.168.123.126:8000,(冒號前的IP地址換成你自己樹莓派的ip)即可打開網絡監控系統。視頻響應的延時取決于你的樹莓派和路由器的性能。
7.實際效果

最后:
由于樹莓派沒有開機按鍵,每次開機都要手動上電(拔插電源接口)于是我把樹莓派電源接在可以手機控制開關的智能插座上,這樣就能隨時通過手機來控制樹莓派開機了。另外我還用手機包裝盒給這個樹莓派監控系統做了一個外殼,這樣就可以“偽裝”起來,手機上再安裝一個可以ssh的App,隨時執行視頻監控腳本。這樣我就可以在家里任何地方,隨時用手機查看監控了。

