文章目錄
- 前言
- 一、軟硬體準備
- 1、硬體準備
- 2、軟體準備
- 二、搭建流程
- 1、檢查是否安裝了picamera
- 2、使能攝像頭模塊
- 3、查詢樹莓派IP地址
- 4、創建python檔案并傳輸到樹莓派
- 5、運行python腳本
- 6、效果如下
- 總結
前言
上篇文章我們使用Smaba搭建了樹莓派的檔案共享系統,這里我們就用上這個檔案編輯傳輸的功能吧,接下來使用攝像頭搭建一個簡單的實時監控,
一、軟硬體準備
1、硬體準備
| 樹莓派攝像頭 |
|---|
| 樹莓派4B |
2、軟體準備
| VNC遠程桌面 |
|---|
二、搭建流程
1、檢查是否安裝了picamera
一般樹莓派都默認安裝了,但是為了保險起見,我們還是檢查一下
安裝參考鏈接
輸入如下指令:
python -c "import picamera"
python3 -c "import picamera"
如果沒有錯誤,那么我們可以進行下一步了,如果有如下類似情況,那你可能需要重新安裝一下
/*錯誤內容示例*/
$ python -c "import picamera"
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named picamera
$ python3 -c "import picamera"
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named 'picamera'
在樹莓派上安裝picamera最好使用系統的包管理器,這樣只需要新版本發布時,只需要apt正常的更新指令就能伴隨著更新,如果想洗掉也能洗掉掉,
安裝指令:
sudo apt-get update
sudo apt-get install python-picamera python3-picamera
升級指令:
sudo apt-get update
sudo apt-get upgrade
洗掉指令
sudo apt-get remove python-picamera python3-picamera
2、使能攝像頭模塊
在終端輸入指令,進入設定:
sudo raspi-config

選擇使能(enable)

最后完成后應該重啟一下
sudo reboot
重啟完成,輸入如下指令,如果一切正常,相機應啟動,相機的預覽應顯示在顯示屏上,在延遲 5 秒后,應在關閉相機之前捕獲影像imagei
raspistill -o image.jpg
3、查詢樹莓派IP地址
輸入指令:
ifconfig

4、創建python檔案并傳輸到樹莓派
檔案名可以隨便取一個英文名,代碼如下:
# 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) //這里8000是埠號
server = StreamingServer(address, StreamingHandler)
server.serve_forever()
finally:
camera.stop_recording()
前面我們做過了樹莓派檔案共享系統,這里我們從電腦中將創建好的檔案直接拖拽到樹莓派中隨意一個檔案夾即可,這里選擇了Videos檔案夾

5、運行python腳本
這里可以直接一條指令完成
python3 ./Videos/rpi_camera_surveillance_system.py
也可以直接進入所在檔案運行

6、效果如下

192.168.112:8000
總結
這是一個簡單好玩的專案,不用安裝其它任何依賴包,只要運行一個Python腳本就可以在局域網構建一個視頻監控系統,這里使用到了攝像頭,算是樹莓派比較基礎的一個硬體了吧,有條件可以動手試一下,在經過實際操作后,突然意識到這個沒有映射到公網上,因而應該只能在同一局域網內才能查看,果不其然,的確只能局限在局域網中使用,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/294224.html
標籤:python
