在公司實驗室看到一個樹莓派原裝csi攝像頭,以前沒用過,拿出來玩一下,看看拍照效果如何。
安裝攝像頭的時候,不可帶電操作,否則容易燒壞攝像頭,安裝排線的時候,留意攝像頭和樹莓派上的卡扣,打開卡扣後再插入排線,排線插好後再把卡扣扣好,并注意排線的方向,如下如所示:
raspberry配套攝像頭.jpg
樹莓派連接配接示意圖.jpg
安裝驅動使能樹莓派的相關子產品
添加驅動程式檔案進來,在下面檔案下加入bcm2835-v4l2 這一行:
$sudo vim /etc/modules
# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.
i2c-dev
bcm2835-v4l2
修改Raspberry的啟動配置使能項
$sudo raspi-config
使能攝像頭.jpg
使能攝像頭.jpg
使能攝像頭.jpg
重新開機樹莓派
$sudo reboot
重新開機完之後,我們的基本的操作就完成了,下來來看看/dev下面是否存在攝像頭裝置,找到了我們想要的看到的裝置:video0 device
$ls -al /dev/ | grep video
crw-rw---- 1 root video 29, 0 Mar 9 07:50 fb0
crw-rw---- 1 root video 246, 0 Mar 9 07:50 vchiq
crw-rw---- 1 root video 248, 0 Mar 9 07:50 vcio
crw-rw---- 1 root video 245, 0 Mar 9 07:50 vcsm
crw-rw----+ 1 root video 81, 0 Mar 9 07:50 video0
我們使用raspistill指令來測試子產品是否可用
首先檢視raspistill幫助資訊
我們可以根據自己的需求,加上對應的參數
$raspistill --help
Runs camera for specific time, and take JPG capture at end if requested
usage: raspistill [options]
Image parameter commands
-?, --help : This help information
-w, --width : Set image width
-h, --height : Set image height
-q, --quality : Set jpeg quality <0 to 100>
-r, --raw : Add raw bayer data to jpeg metadata
-o, --output : Output filename (to write to stdout, use '-o -'). If not specified, no file is saved
使用raspitill指令捕獲一張圖檔
$raspistill -w 600 -h 600 -o sxy20cdd.jpg
使用display指令檢視圖檔
$display sxy20cdd.jpg
效果如下圖,沒自信自拍,隻能對着我的電源插座亂拍一張了,效果勉強能看得過去 。
sxy20cdd.jpg
用Python庫picamera控制樹莓派攝像頭子產品
如果提示沒有相關庫檔案,可用一下指令安裝
$apt-get install python-picamera
或者
$apt-get install python3-picamera
建立一個python檔案camera.py,在檔案中寫入以下語句。
注意檔案名不要和庫picamera名一樣,否則會有沖突。
$sudo vim camera.py
#!/usr/bin/python
#coding:utf-8
#導入相關子產品
from picamera import PiCamera
from time import sleep
def open_preview():
with PiCamera() as camera:
camera.resolution = (320, 240)
camera.start_preview()
sleep(5)
if __name__ == '__main__':
open_preview()
2.執行,檢視拍攝效果(效果圖省略)
$python camera.py
3.picamera庫有很多功能,詳情請自行查閱此庫檔案相關的資料,此處不再介紹。