https://picamera.readthedocs.io/en/release-1.13/_modules/picamera/camera.html
https://blog.csdn.net/u012005313/article/details/70244747
https://www.cnblogs.com/uestc-mm/p/7606855.html
使用树莓派的摄像头示例
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
# initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
camera.resolution = (640, 480)
camera.framerate = 32
camera.hflip = True
camera.vflip = True
rawCapture = PiRGBArray(camera, size=(640, 480))
# allow the camera to warmup
time.sleep(0.1)
# capture frames from the camera
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
# grab the raw NumPy array representing the image, then initialize the timestamp
# and occupied/unoccupied text
image = frame.array
# show the frame
cv2.imshow("Frame", image)
key = cv2.waitKey(1) & 0xFF
# clear the stream in preparation for the next frame
rawCapture.truncate(0)
# if the `q` key was pressed, break from the loop
if key == ord("q"):
break
首先使用picamera模块当中的PiCamera方法创建返回一个camera的对象:
camera = PiCamera()
初始化camera对象当中的相关参数(可设置也可以读取):
camera.saturation 设置图像视频的饱和度
__camera.brightness = 50__设置图像的亮度(50表示白平衡的状态)
camera.shutter_speed = 6000000 相机快门速度
camera.iso = 800 ISO是衡量胶片对光线敏感程度的标准。如50 ISO, 64 ISO, 100 ISO表示在曝光感应速度上要比高数值的来得慢,高数值ISO是指超过200以上的标准,如200 ISO, 400 ISO
camera.sharpness = 0 设置图像的锐度值,默认是0,取值范围是-100~100之间
camera.framrate = 32 这里可能用的Fraction是一个分数模块来存储分数1/6,保证分数运算的精度(记得调用模块:from fractions import Fraction)
camera.hflip = Ture 是否进行水平翻转
camera.vflip = False 是否进行垂直翻转
camera.rotation = 0 是否对图像进行旋转
camera.resolution = (280,160) 设置图像的width和height
a_gain = camera.analog_gain 这个值表示摄像头传感器件到数字装换之前的模拟信号的增益,格式是Fraction的格式 一般似乎也用不上
d_gain = camera.digital_gain 这个值表示摄像头的数字增益大小 一般似乎也用不上
camera.led = False 值为False那么led为关灯的状态,True为开灯的状态
使用camera对象当中的相关函数API:
图像拍照
# 拍摄一张图片,图片的名称为test.jpg use_video_port用来指定是通过视频接口来使用还是普通的接口
camera.capture('test.jpg', use_video_port = False)
# 连续的拍摄一组视频帧,将每一帧都存储在rawCapture这个变量当中,存储的格式是bgr的格式,使用了摄像头的接口
camera.capture_continuous(rawCapture, format="bgr", use_video_port=True)
函数参数详解
从摄像头中获得一张图像,将这张图像存储在output当中.
capture(output, format=None, use_video_port=False, resize=None, splitter_port=0, bayer=False, **options)
从摄像头上获取连续的视频流,相关的参数和上面的函数相似
capture_continuous(output, format=None, use_video_port=False, resize=None, splitter_port=0, burst=False, bayer=False, **options)
参数output是一个用来保存图像数据的区域
output:为字符串string时,表示将得到的数据写入到文件名是string文件当中
output:是一个可以写入的对象时,就相当于将图像的数据追加append到对象的后面
output:是一个具有buffer protocal数据类型的数据(要保证足够大的写入空间来保存图像)
参数format是表示文件的类型,主要有下面的文件类型
jpeg- Write a JPEG file
png - Write a PNG file
gif - Write a GIF file
bmp- Write a Windows bitmap file
yuv - Write the raw image data to a file in YUV420 format
rgb - Write the raw image data to a file in 24-bit RGB format
bgr - Write the raw image data to a file in 24-bit BGR format
参数use_video_port用来表示获取图像的端口
True 表示从视频端口获取图像,这个时候为了保证视频的帧速,画质就会降低
False 表示从标准的端口获取图像,图像的质量较高,获取的时间较长
视频录像
# 开始进行录制视频的参数设置,param1是录制视频的文件名 param2是设置录制文件的格式
camera.start_recording('Video.mjpeg',format = 'mjpeg')
# 启动开始录制,等待总共录制的时间为20s 主要单位是s秒
camera.wait_recording(20)
# 停止视频的录制功能
camera.stop_recording()