參考資料
Raspberry Pi提供了攝像頭模塊的接口,用戶圖像和視頻拍攝。以下內容根據官網,總結了一些基本的使用方法。
一、攝像頭模塊安裝
- 將攝像頭排線露出金屬的部分背對網孔和usb接口,即另一面的藍色塑料封皮正對網孔和usb接口
- 小心地將接線板上的黑色塑料往上拉(請務必保證電源關閉,手指干燥)
- 將排線插入,黑色塑料往下壓緊排線
- 開機,使用sudo raspi-config,選擇interfacing opinions,選擇Camera,並Enable
- Finish退出配置
- 重啟
二、使用命令控制攝像頭
如果一切順利的話,就可以使用命令控制攝像頭了
- 拍照---raspistill
拍攝一張照片,保存在桌面,命名為image.jpg,等待幾秒后,桌面就會出現jpg文件
pi@raspberrypi:~ $ raspistill -o Desktop/image.jpg
增加參數,還可以更改圖片大小
pi@raspberrypi:~ $ raspistill -o Desktop/image-small.jpg -w 640 -h 480
輸入raspistill直接回車可查詢raspistill命令其他參數
- 攝像---raspivid
最直接的參數 -t設置拍攝時間 -w -h設置分辨率寬和高
拍攝一段5000毫秒、分辨率為1024x768的視頻,保存在桌面,命名為video.h264
pi@raspberrypi:~ $ raspivid -o Desktop/video.h264 -t 5000 -w 1024 -h 768
三、使用Python程序控制攝像頭
使用Python程序控制攝像頭需要使用PiCamera庫
打開Raspberry Pi自帶的Thonny Python IDE,新建camera.py文件,文件命名不能用PiCamera.py
- 預覽攝像頭畫面(僅在接入顯示屏幕有效)
from picamera import PiCamera
from time import sleep
camera = PiCamera()
# Rotate by 180 degrees when the camera is upside-down
camera.rotation = 180
camera.start_preview(alpha=200) # Make the camera preview see-through by setting an alpha level from 0 to 255
sleep(5)
camera.stop_preview()
上述代碼實現打開攝像頭預覽5秒鍾再關閉的功能。但是如果這個程序僅在Raspberry Pi接入了顯示器才有效,SSH或是VNC訪問是無效的。
- 照相和錄像
修改上述代碼
from picamera import PiCamera
from time import sleep
camera = PiCamera()
# Rotate by 180 degrees when the camera is upside-down
camera.rotation = 180
camera.start_preview(alpha=200) # Make the camera preview see-through by setting an alpha level from 0 to 255
sleep(5)
# Take a picture and save as /home/pi/Desktop/image.jpg'
camera.capture('/home/pi/Desktop/image.jpg')
# Take 5 pictures every 5 seconds and save as /home/pi/Desktop/image0.jpg' ... image4.jpg
for i in range(5):
sleep(5)
camera.capture('/home/pi/Desktop/image%s.jpg' % i)
camera.stop_preview()
在拍照前,最好讓攝像頭sleep至少2秒,使之能夠感光
將capture()改成start_recording()和stop_recording()就可控制攝像頭拍攝錄像了
- 更多設置
PiCamera庫提供了很多關於圖像和攝像頭預覽的設置,並且兩者是分開的,即一部分只對拍攝的畫面有效,另一部分只對預覽有效- 分辨率 resolution
圖片最大是2592×1944 視頻最大是1920×1080,最小是64x64 - 幀數 framerate
- 圖像中增加文本 annotate_text
- 圖像文本大小 annotate_text_size,范圍:6到160,默認32
- 圖像文本顏色 import Color,annotate_background 和 annotate_foreground
- 攝像頭預覽亮度修改 brightness,范圍:0到100,默認50
- 攝像頭預覽對比度 contrast
- 其他參考
- 分辨率 resolution
from picamera import PiCamera, Color
from time import sleep
camera = PiCamera()
camera.rotation = 180
camera.resolution = (1024, 768)
camera.framerate = 15
camera.start_preview()
camera.brightness = 70
camera.annotate_background = Color('blue')
camera.annotate_foreground = Color('yellow')
camera.annotate_text = "Hello world!"
camera.annotate_text_size = 30
sleep(5)
camera.capture('/home/pi/Camera/pic.jpg')
camera.stop_preview()
camera.start_preview()
for i in range(5):
camera.annotate_text = "Brightness: %s" % i
camera.brightness = i*20
sleep(0.1)
camera.capture('/home/pi/Camera/brightness%s.jpg' % i)
camera.stop_preview()
camera.start_preview()
for i in range(5):
camera.annotate_text = "Contrast: %s" % i
camera.contrast = i*20
sleep(0.1)
camera.capture('/home/pi/Camera/contrast%s.jpg' % i)
camera.stop_preview()
四、基於vlc的Raspberry Pi攝像頭實時監控
$ sudo apt-get install vlc #Raspberry Pi系統自帶了
$ #-o - 輸出到stdout,-t 0不暫停立即獲取流, 640x360,25幀/s,-rot 180畫面旋轉180度(用了支架攝像頭畫面倒過來了)
$ sudo raspivid -o - -rot 180 -t 0 -w 480 -h 360 -fps 25|cvlc -vvv stream:///dev/stdin --sout '#standard{access=http,mux=ts,dst=:8090}' :demux=h264
在與Raspberry Pi在同一局域網內的其他設備上,用vlc打開網絡串流 http://Raspberry Pi的ip:8090
就播放看到攝像頭的畫面了,但是vlc的實施監控存在5s左右的延時,體驗並不是很好。