備份自:http://blog.rainy.im/2015/05/26/webcam-on-raspberry-pi/
用樹(shù)莓派加上相機(jī)模塊,可以實(shí)現(xiàn)實(shí)時(shí)監(jiān)控的功能肋演,再將時(shí)時(shí)視頻流導(dǎo)入到 Web 界面中仔引,就變成簡(jiǎn)單的網(wǎng)絡(luò)攝像頭(Webcam)咱筛。Web 端用 Nodejs + WebSocket胚嘲,既可以進(jìn)行實(shí)時(shí)播放,同時(shí)又方便進(jìn)功能的擴(kuò)展强胰。
![raspberry pi 2b](http://7xiijd.com1.z0.glb.clouddn.com/raspberry-pi-2.png)
Raspberry PI 2B 在硬件上比第一代有很大提升舱沧,多了兩個(gè) USB 接口,存儲(chǔ)換成了 Micro SD偶洋。攝像模塊 支持1&2代熟吏,通過(guò) HDMI 旁邊的 CSI CONNECTOR 接口接入。
下面介紹 Webcam 的搭建過(guò)程玄窝。
1. 安裝 Raspbian 操作系統(tǒng)
官方下載 系統(tǒng)鏡像牵寺,Mac 系統(tǒng)下刻制系統(tǒng)鏡像到 SD 卡中的步驟如下:
# 1.
diskutil list
# 查看 SD 卡所屬文件
#/dev/disk2
# #: TYPE NAME SIZE IDENTIFIER
# 0: FDisk_partition_scheme *7.9 GB disk2
# 1: DOS_FAT_32 NO NAME 7.9 GB disk2s1
# 2.
diskutil unmountDisk /dev/disk2
#Unmount of all volumes on disk2 was successful
# 3.
sudo dd bs=1m if=2015-05-05-raspbian-wheezy.img of=/dev/disk2
# 3125+0 records in
# 3125+0 records out
# 3276800000 bytes transferred in 1562.948501 secs (2096550 bytes/sec)
# 4.
sudo diskutil eject /dev/disk2
# 這一步可能會(huì)需要相當(dāng)長(zhǎng)的時(shí)間
# Disk /dev/disk2 ejected
刻制完成后可以將 SD 卡插入 PI 的卡槽中,通電即可開(kāi)機(jī)(第一次還是需要用 HDMI 接顯示器恩脂,操作更方便一些)帽氓。
2. 系統(tǒng)配置
第一次進(jìn)入 raspbian 系統(tǒng)需要進(jìn)行相關(guān)系統(tǒng)配置sudo raspi-config
,包括選擇使用全部 SD 卡作為系統(tǒng)存儲(chǔ)俩块、開(kāi)啟 Camera module 等選項(xiàng):
![raspi_config](http://7xiijd.com1.z0.glb.clouddn.com/raspi_config.png)
插入 USB WIFI 后黎休,配置 WIFI 連接,參考 SETTING WIFI UP VIA THE COMMAND LINE玉凯。最后势腮,更新一下源(推薦 mirrors.zju.edu.cn :D
,或者阿里云):
sudo nano /etc/apt/sourses.list
# deb http://mirrors.zju.edu.cn/raspbian/raspbian/ wheezy main contrib non-free rpi
# deb-src http://mirrors.zju.edu.cn/raspbian/raspbian/ wheezy main contrib non-free rpi
sudo apt-get update
3. Camera module 操作指令
raspbian 中系統(tǒng)自帶對(duì)攝像頭的操作指令漫仆,分別是raspistill, raspivid & raspistillyuv
(這里有官方詳細(xì)文檔)捎拯,其中 raspivid
是獲取視頻流的指令。
# 捕捉 5s 視頻并輸出到 video.h264歹啼,視頻尺寸為 320x240
raspivid -t 5000 -w 320 -h 240 -o video.h264
# 實(shí)時(shí)監(jiān)控并輸出到 stdout
raspivid -t 0 -w 320 -h 240 -o -
獲取實(shí)時(shí)監(jiān)控之后的視頻流輸出之后玄渗,可以通過(guò) ffmpeg 轉(zhuǎn)化視頻格式座菠,輸出到監(jiān)聽(tīng)服務(wù)器狸眼。
4. 安裝 Nodejs/ffmpeg
# 1. install nodejs
wget http://node-arm.herokuapp.com/node_latest_armhf.deb
sudo dpkg -i node_latest_armhf.deb
node -v
# 2. install ffmpeg
## libx264 support
git clone git://git.videolan.org/x264
cd x264
./configure --host=arm-unknown-linux-gnueabi --enable-static --disable-opencl
make
sudo make install
## ffmpeg
git clone git://git.ffmpeg.org/ffmpeg
cd ffmpeg
sudo ./configure --arch=armel --target-os=linux --enable-gpl --enable-libx264 --enable-nonfree
make # 這一步可能相當(dāng)相當(dāng)漫長(zhǎng),在我這里跑了有一個(gè)多小時(shí)
sudo make install
5. Webcam
通過(guò) ffmpeg 轉(zhuǎn)換視頻格式:
raspivid -t 0 -w 320 -h 240 -o - | ffmpeg -i - -s 320x240 -f mpeg1video \
-b 800k -r 30 http://127.0.0.1:8082/yourpassword
同時(shí)創(chuàng)建 Node server 監(jiān)聽(tīng) http://127.0.0.1:8082
(參考:jsmpeg):
git clone https://github.com/phoboslab/jsmpeg.git webcam
cd webcam && node stream-server.js yourpassword
# Listening for MPEG Stream on http://127.0.0.1:8082/<secret>/<width>/<height>
# Awaiting WebSocket connections on ws://127.0.0.1:8084/
然后打開(kāi)stream-example.html
就可以看到實(shí)時(shí)監(jiān)控畫(huà)面了浴滴,如果是遠(yuǎn)程調(diào)試需要稍作更改:
# edit stream-example.html
var client = new WebSocket( 'ws://RASPI_LOCAL_IP:8084/' );
var player = new jsmpeg(client, {canvas:canvas});
# @/path/to/webcam
python -m SimpleHTTPServer 8080
# in your browser
http://RASPI_LOCAL_IP:8080
參考