


之前在 google play 上找到某個 app,可以將舊手機轉為網路監視器, 雖然方便好用,但是監視鏡頭的控制權在別人手上,沒辦法知道有沒有資安問題。另一方面更進階的調控會需要付費。因此研究後打算使用 Raspberry pi 4 + 相機模組 + motioneye 這套 open source 來搭建,我們就來一起看看如何製作吧!
目錄
準備硬體元件
這次製作需要準備的材料為:
啟用相機模組
在 Raspbian 下預設相機模組功能是關閉的,必須到 raspi-config 中啟用
Interface Options -> P1 Camera -> Yes (enable)









安裝 Docker
由於使用 docker 的方式架設 motioneye,必須在 Raspbian 中安裝 docker
curl -fsSL get.docker.com | sudo bash
以及安裝 docker-compose,因為 Raspbian 目前還是 32 bit,指令集是 armhf,必須自己從源碼安裝
# install pip3 and python dev
sudo apt install -y python3-pip python3-dev libffi-dev libevent-dev
sudo pip3 install setuptools
# install docker-compose
sudo pip3 install docker-compose
啟動 motioneye
motioneye 其實是另一個開發者將 motion 包上了他設計的前端控制頁面而成,所以骨子裡還是 motion,很多設定參數都是一樣的。同時還有 mobile app 可以安裝(也是叫 motioneye)從手機上遠端遙控很方便!
用以下這份 docker-compose 檔案來起 motioneye
version: '2.3'
services:
motioneye:
image: ccrisan/motioneye:0.42.1-armhf
container_name: motioneye
restart: always
mem_limit: 3G
logging:
driver: "json-file"
options:
max-size: "1k"
max-file: "3"
ports:
- "8765:8765"
- "8081:8081"
- "7999:7999"
devices:
- "/dev/video0:/dev/video0"
networks:
- motioneye-network
volumes:
- ./config:/etc/motioneye
- ./record:/var/lib/motioneye
- /etc/localtime:/etc/localtime:ro
networks:
motioneye-network:
name: motioneye-network
driver: bridge
其中參數解釋一下:
port 部分
- 8765 是進入 motioneye 所使用的 port
- 8081 是監控視訊串流使用的 port
- 7999 是 motion api 的 port,可以用此操控他
device 部分
因為需要使用相機硬體,所以必須將 /dev/video0 接入 container 裡。
volume 部分
- config 是用來存放設定參數,外置後重啟 container 就不會消失
- record 是 motion 錄影後影片檔案存放的路徑
進入 motioneye web 控制頁面
先找到 raspberry pi 的 ip,假設是 192.168.1.11,使用瀏覽器輸入 http://192.168.1.11:8765,就會看到登入畫面,預設帳號是 admin,密碼留空即可。



進入後,需要給定相機來源,點選畫面中央提示新增一個。Camera type 選擇 Local V4L2 Camera,Camera 會自動帶入 /dev/video0(截圖時因為已經加入相機,所以才顯示 no cameras)



沒問題的話馬上就會看到目前相機拍攝到的畫面,左邊也有相當多的選項可以自行設定



設定 Motion Detection
既然是網路監控系統,當然希望他能夠自行偵測畫面變化後啟動錄影!
左邊設定選單往下滑找到 Motion Detection 並開啟,可以自行調整參數玩玩看。



主要幾個參數介紹一下:
Motion Gap
這是指每錄完一段影片後,中間的不反應期。比如說一隻狗走入畫面內,motion 偵測到並啟動錄影。接著狗走離開畫面,motion 停止錄影。假設 motion gap 設定為 30 sec,此時停止錄影後的三十秒內,就算狗在畫面裡面跳舞,motion 也不會再次啟動錄影。但其實對我來說兩個事件都應該要錄到,所以我將它設定為一秒。
Captured Before
指錄影時,事件發生起始點前多少格數也要錄製。比如說在下午 1點 30 分 30 秒狗走入畫面,此時 motion 偵測到動靜開始錄影,但因為 Captrued Befored 設定為 30 frames,假設我們是每秒 30 張,最後看到的影片錄製起始點就會是 1點 30分 30 秒 – 1 秒 = 1點 30分 29秒開始。
Captured After
指錄影時,在動態事件結束後,持續錄影多少格數。如同先前,狗如果在 1點 30分 40秒離開畫面,我們設定為 30 frames,他就會錄製到 1點 30分 41秒。
設定 File Storage
記憶卡空間有限,motioneye 提供將影片上傳至雲端的功能(目前裡面有提供 FTP、Google drive、Google photo、Dropbox)。這邊以 Google drive 為例,設定 upload service 為 google drive,Location 為上傳檔案到 google drive 的資料夾名稱,這邊以 motion 為名。接著點擊 obtain key 跳轉估狗登入頁面,使用你的 google 帳號登入後,可以點擊 Test Service 確認是否連結成功。



之後如果有錄影,motioneye 就會自動幫你上傳到對應 google drive 路徑,並依照日期分類。



設定 檔案存活時間
既然我們都設定讓他自動上傳影片,Rasberry 中的影片就可以讓他自動刪除,避免儲存空間不夠。
找到 Movies 設定欄位,調整 Preserve Movies 的時間,我是改成 For One Day,只要一天後就會自動刪除。這欄位裡面也有一些影片錄製的參數可以自行調整看看!



裝機測試
因為只是簡單土炮一個,所以外殼就直接拿紙盒手工挖洞,外加電火布固定,效仿目前智慧裝置使用膠水固定的概念。









實測在室溫 16 度的情況下,裸機被動散熱跑 motioneye CPU 溫度大約在 50 度附近。



裝入土炮機殼後,溫度直接來到 60 度。這要是到夏天完全無法,只好拿出散熱風扇,也是簡單用電火布固定在外部。






此時溫度來到 30 度附近,已經可以長期運作使用了!
一個網路監控系統就完成了!但其實還可以客製化啟動動態偵測時機,傳送事件訊息,以及透過 vpn 保護,這部分在下一篇文章中分享吧!如果覺得我文章內容對你有幫助的話,請在文章後面幫我按 5 個讚!讓我知道大家都喜歡什麼內容哦!
延伸閱讀:
使用 Face Recognition 套件快速建立自己的人臉辨識系統!
如何優化 Face Recognition 套件的人臉辨識?
如何用 LINE Notify 做免費的 bot 訊息推播?