天天看点

csi python 摄像头 树莓派_Raspberry Pi 3B+ 连接csi摄像头实验

在公司实验室看到一个树莓派原装csi摄像头,以前没用过,拿出来玩一下,看看拍照效果如何。

安装摄像头的时候,不可带电操作,否则容易烧坏摄像头,安装排线的时候,留意摄像头和树莓派上的卡扣,打开卡扣后再插入排线,排线插好后再把卡扣扣好,并注意排线的方向,如下如所示:

csi python 摄像头 树莓派_Raspberry Pi 3B+ 连接csi摄像头实验

raspberry配套摄像头.jpg

csi python 摄像头 树莓派_Raspberry Pi 3B+ 连接csi摄像头实验

树莓派连接示意图.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

csi python 摄像头 树莓派_Raspberry Pi 3B+ 连接csi摄像头实验

使能摄像头.jpg

csi python 摄像头 树莓派_Raspberry Pi 3B+ 连接csi摄像头实验

使能摄像头.jpg

csi python 摄像头 树莓派_Raspberry Pi 3B+ 连接csi摄像头实验

使能摄像头.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

效果如下图,没自信自拍,只能对着我的电源插座乱拍一张了,效果勉强能看得过去 。

csi python 摄像头 树莓派_Raspberry Pi 3B+ 连接csi摄像头实验

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库有很多功能,详情请自行查阅此库文件相关的资料,此处不再介绍。