“模板:Raspberry Pi Camera User Manual”的版本间的差异
(创建页面,内容为“== 使用说明 == === 硬件连接 === 把摄像头的排线插入到网口和 HDMI 口之间的排线插槽,银色亮面朝向 HDMI 口。需要先把树莓 派…”) |
(→使用说明) |
||
第46行: | 第46行: | ||
=== 参考资料 === | === 参考资料 === | ||
* [http://www.raspberrypi.org/camera 树莓派摄像头教程] | * [http://www.raspberrypi.org/camera 树莓派摄像头教程] | ||
+ | |||
+ | == Python 代码控制摄像头 == | ||
+ | * 可借助picamera库,使python支持摄像头的控制 | ||
+ | |||
+ | === 预览 === | ||
+ | * 打开python IDE,如Thonny Python IDE,或打开记事本 | ||
+ | [[File:thonny-app-menu.png|400px]] | ||
+ | * 新建名字为camera.py的python文件,并保存到桌面 | ||
+ | * 输入以下代码 | ||
+ | <source lang="python"> | ||
+ | from picamera import PiCamera | ||
+ | from time import sleep | ||
+ | |||
+ | camera = PiCamera() | ||
+ | |||
+ | camera.start_preview() | ||
+ | sleep(5) | ||
+ | camera.stop_preview() | ||
+ | </source> | ||
+ | * 进入终端,执行 | ||
+ | <source lang="python"> | ||
+ | python3 Desktop/camera.py | ||
+ | </source> | ||
+ | 桌面将弹出预览框,实时取景并预览 | ||
+ | <source lang="python"> | ||
+ | 需要注意的是,预览框仅在直接连接屏幕时有效,而是使用VNC等软件远程连接树莓派桌面,则不会弹出该预览框 | ||
+ | </source> | ||
+ | 如果预览画面是上下颠倒的,则可以通过以下代码旋转180度 | ||
+ | <source lang="python"> | ||
+ | camera = PiCamera() | ||
+ | camera.rotation = 180 | ||
+ | </source> | ||
+ | * 预览画面可旋转90度、180度、270度 | ||
+ | * 如果要重置画面,可将画面旋转0度 | ||
+ | |||
+ | ==== 设置预览框的透明度 ==== | ||
+ | <source lang="python"> | ||
+ | camera.start_preview(alpha=100) | ||
+ | </source> | ||
+ | alpha的值范围是0-255 | ||
+ | |||
+ | === 拍照 === | ||
+ | * 在预览代码的中间插入camera.capture()命令 | ||
+ | <source lang="python"> | ||
+ | camera.start_preview() | ||
+ | sleep(5) | ||
+ | camera.capture('/home/pi/Desktop/image.jpg') | ||
+ | camera.stop_preview() | ||
+ | </source> | ||
+ | 请注意:必须要执行代码sleep(5),以便给摄像头的传感器留下一定时间调整亮度和白平衡 | ||
+ | |||
+ | === 录像 === | ||
+ | 移除 camera.captue(),并加入camera.start_recording()和camera.stop_recording() | ||
+ | <source lang="python"> | ||
+ | camera.start_preview() | ||
+ | camera.start_recording('/home/pi/Desktop/video.mp4') | ||
+ | sleep(5) | ||
+ | camera.stop_recording() | ||
+ | camera.stop_preview() | ||
+ | </source> | ||
+ | 以上代码执行后,将在桌面生成video.mp4的视频,长度为5秒 | ||
+ | |||
+ | === 更多参数 === | ||
+ | * picamera可以给图片添加很多参数 | ||
+ | |||
+ | ==== 设置分辨率 ==== | ||
+ | * 默认情况下,图片的分辨率等于显示器的分辨率,但最大的图像尺寸不超过2592*1944像素,视频尺寸不超过1920*1080像素;图像和视频的最小尺寸则为64*64像素 | ||
+ | * 可使用以下代码设置图像的分辨率 | ||
+ | <source lang="python"> | ||
+ | camera.resolution = (2592, 1944) | ||
+ | camera.framerate = 15 | ||
+ | camera.start_preview() | ||
+ | sleep(5) | ||
+ | camera.capture('/home/pi/Desktop/max.jpg') | ||
+ | camera.stop_preview() | ||
+ | </source> | ||
+ | * 请注意,相机的帧数需设置为≤15帧,以支持最大分辨率 | ||
+ | |||
+ | ==== 给图片加文字 ==== | ||
+ | '''添加文字''' | ||
+ | <source lang="python"> | ||
+ | camera.start_preview() | ||
+ | camera.annotate_text = "Hello world!" | ||
+ | sleep(5) | ||
+ | camera.capture('/home/pi/Desktop/text.jpg') | ||
+ | camera.stop_preview() | ||
+ | </source> | ||
+ | '''文字大小''' | ||
+ | <source lang="python"> | ||
+ | camera.annotate_text_size = 50 | ||
+ | </source> | ||
+ | annotate_text_size的取值范围为6-160,默认值32 | ||
+ | |||
+ | '''文字颜色''' | ||
+ | <source lang="python"> | ||
+ | camera.start_preview() | ||
+ | camera.annotate_background = Color('blue') | ||
+ | camera.annotate_foreground = Color('yellow') | ||
+ | camera.annotate_text = " Hello world " | ||
+ | sleep(5) | ||
+ | camera.stop_preview() | ||
+ | </source> | ||
+ | '''修改图片的参数''' | ||
+ | * 亮度 | ||
+ | <source lang="python"> | ||
+ | camera.start_preview() | ||
+ | camera.brightness = 70 | ||
+ | sleep(5) | ||
+ | camera.capture('/home/pi/Desktop/bright.jpg') | ||
+ | camera.stop_preview() | ||
+ | </source> | ||
+ | brightness的取值范围为0-100,默认值50 | ||
+ | * 对比度 | ||
+ | <source lang="python"> | ||
+ | camera.contrast = 50 | ||
+ | </source> | ||
+ | contrast的取值范围为0-100,默认值50 | ||
+ | * 风格 | ||
+ | 可通过camera.image_effect,给图片添加特效 | ||
+ | |||
+ | 有效值: | ||
+ | <pre> | ||
+ | none(默认) | ||
+ | negative | ||
+ | solarize | ||
+ | sketch | ||
+ | denoise | ||
+ | emboss | ||
+ | oilpaint | ||
+ | hatch | ||
+ | gpen | ||
+ | pastel | ||
+ | watercolor | ||
+ | film | ||
+ | blur | ||
+ | saturation | ||
+ | colorswap | ||
+ | washedout | ||
+ | posterise | ||
+ | colorpoint | ||
+ | colorbalance | ||
+ | cartoon | ||
+ | deinterlace1 | ||
+ | deinterlace2 | ||
+ | </pre> | ||
+ | 例如 | ||
+ | <source lang="python"> | ||
+ | camera.start_preview() | ||
+ | camera.image_effect = 'colorswap' | ||
+ | sleep(5) | ||
+ | camera.capture('/home/pi/Desktop/colorswap.jpg') | ||
+ | camera.stop_preview() | ||
+ | </source> | ||
+ | |||
+ | * 模式 | ||
+ | 可使用camera.exposure_mode,设置拍照模式(如夜晚、运动、雪地等模式) | ||
+ | |||
+ | 有效值: | ||
+ | <pre> | ||
+ | off | ||
+ | auto(默认) | ||
+ | night | ||
+ | nightpreview | ||
+ | backlight | ||
+ | spotlight | ||
+ | sports | ||
+ | snow | ||
+ | beach | ||
+ | verylong | ||
+ | fixedfps | ||
+ | antishake | ||
+ | fireworks | ||
+ | </pre> | ||
+ | 例如 | ||
+ | <source lang="python"> | ||
+ | camera.start_preview() | ||
+ | camera.exposure_mode = 'beach' | ||
+ | sleep(5) | ||
+ | camera.capture('/home/pi/Desktop/beach.jpg') | ||
+ | camera.stop_preview() | ||
+ | </source> | ||
+ | * 白平衡 | ||
+ | 可使用camera.awb_mode设置白平衡 | ||
+ | |||
+ | 有效值: | ||
+ | <pre> | ||
+ | off | ||
+ | auto(默认) | ||
+ | sunlight | ||
+ | cloudy | ||
+ | shade | ||
+ | tungsten | ||
+ | fluorescent | ||
+ | incandescent | ||
+ | flash | ||
+ | horizon | ||
+ | </pre> | ||
+ | 例如设置白平衡为“白天” | ||
+ | <source lang="python"> | ||
+ | camera.start_preview() | ||
+ | camera.awb_mode = 'sunlight' | ||
+ | sleep(5) | ||
+ | camera.capture('/home/pi/Desktop/sunlight.jpg') | ||
+ | camera.stop_preview() | ||
+ | </source> |
2020年12月4日 (五) 11:34的版本
目录
使用说明
硬件连接
把摄像头的排线插入到网口和 HDMI 口之间的排线插槽,银色亮面朝向 HDMI 口。需要先把树莓 派板载的排线插槽的扣子拨开,才能插入排线。排线需要紧密插入排线插槽,同时应注意避免排 线折弯。排线插入之后,需要把插槽的扣子重新扣上。
启用摄像头
1. 进入 Raspbian 系统终端,执行以下语句获取系统更新:
apt-get update apt-get upgrade
2. 使用 raspi-config 配置摄像头。执行:
sudo raspi-config
依次选择:
Interfacing Options --> Camera --> Would you like the camera interface to be enabled? 选择 <Yes>
3. 重启系统:提示 Would you like to reboot now? 选择 Yes
或者执行这个命令重启:
sudo reboot
配置并且连接完摄像头,只要给树莓派上电即可使用拍照和摄像功能。
拍照
终端执行以下语句即可拍照:
raspistill -o image.jpg
此处 image.jpg 是照片文件名。
摄像
终端执行以下语句即可摄像:
raspivid -o video.h264 -t 10000
其中 -t 10000 表示录制 10 秒,用户各根据自己的需要进行调整。
参考资料
Python 代码控制摄像头
- 可借助picamera库,使python支持摄像头的控制
预览
- 打开python IDE,如Thonny Python IDE,或打开记事本
- 新建名字为camera.py的python文件,并保存到桌面
- 输入以下代码
from picamera import PiCamera
from time import sleep
camera = PiCamera()
camera.start_preview()
sleep(5)
camera.stop_preview()
- 进入终端,执行
python3 Desktop/camera.py
桌面将弹出预览框,实时取景并预览
需要注意的是,预览框仅在直接连接屏幕时有效,而是使用VNC等软件远程连接树莓派桌面,则不会弹出该预览框
如果预览画面是上下颠倒的,则可以通过以下代码旋转180度
camera = PiCamera()
camera.rotation = 180
- 预览画面可旋转90度、180度、270度
- 如果要重置画面,可将画面旋转0度
设置预览框的透明度
camera.start_preview(alpha=100)
alpha的值范围是0-255
拍照
- 在预览代码的中间插入camera.capture()命令
camera.start_preview()
sleep(5)
camera.capture('/home/pi/Desktop/image.jpg')
camera.stop_preview()
请注意:必须要执行代码sleep(5),以便给摄像头的传感器留下一定时间调整亮度和白平衡
录像
移除 camera.captue(),并加入camera.start_recording()和camera.stop_recording()
camera.start_preview()
camera.start_recording('/home/pi/Desktop/video.mp4')
sleep(5)
camera.stop_recording()
camera.stop_preview()
以上代码执行后,将在桌面生成video.mp4的视频,长度为5秒
更多参数
- picamera可以给图片添加很多参数
设置分辨率
- 默认情况下,图片的分辨率等于显示器的分辨率,但最大的图像尺寸不超过2592*1944像素,视频尺寸不超过1920*1080像素;图像和视频的最小尺寸则为64*64像素
- 可使用以下代码设置图像的分辨率
camera.resolution = (2592, 1944)
camera.framerate = 15
camera.start_preview()
sleep(5)
camera.capture('/home/pi/Desktop/max.jpg')
camera.stop_preview()
- 请注意,相机的帧数需设置为≤15帧,以支持最大分辨率
给图片加文字
添加文字
camera.start_preview()
camera.annotate_text = "Hello world!"
sleep(5)
camera.capture('/home/pi/Desktop/text.jpg')
camera.stop_preview()
文字大小
camera.annotate_text_size = 50
annotate_text_size的取值范围为6-160,默认值32
文字颜色
camera.start_preview()
camera.annotate_background = Color('blue')
camera.annotate_foreground = Color('yellow')
camera.annotate_text = " Hello world "
sleep(5)
camera.stop_preview()
修改图片的参数
- 亮度
camera.start_preview()
camera.brightness = 70
sleep(5)
camera.capture('/home/pi/Desktop/bright.jpg')
camera.stop_preview()
brightness的取值范围为0-100,默认值50
- 对比度
camera.contrast = 50
contrast的取值范围为0-100,默认值50
- 风格
可通过camera.image_effect,给图片添加特效
有效值:
none(默认) negative solarize sketch denoise emboss oilpaint hatch gpen pastel watercolor film blur saturation colorswap washedout posterise colorpoint colorbalance cartoon deinterlace1 deinterlace2
例如
camera.start_preview()
camera.image_effect = 'colorswap'
sleep(5)
camera.capture('/home/pi/Desktop/colorswap.jpg')
camera.stop_preview()
- 模式
可使用camera.exposure_mode,设置拍照模式(如夜晚、运动、雪地等模式)
有效值:
off auto(默认) night nightpreview backlight spotlight sports snow beach verylong fixedfps antishake fireworks
例如
camera.start_preview()
camera.exposure_mode = 'beach'
sleep(5)
camera.capture('/home/pi/Desktop/beach.jpg')
camera.stop_preview()
- 白平衡
可使用camera.awb_mode设置白平衡
有效值:
off auto(默认) sunlight cloudy shade tungsten fluorescent incandescent flash horizon
例如设置白平衡为“白天”
camera.start_preview()
camera.awb_mode = 'sunlight'
sleep(5)
camera.capture('/home/pi/Desktop/sunlight.jpg')
camera.stop_preview()