方式N1:
这应该适用于Python和你不需要安装任何额外的,但一定要更新,并通过升级apt-get的:
#!/usr/bin/python
import os
import pygame, sys
from pygame.locals import *
import pygame.camera
width = 640
height = 480
#initialise pygame
pygame.init()
pygame.camera.init()
cam = pygame.camera.Camera("/dev/video0",(width,height))
cam.start()
#setup window
windowSurfaceObj = pygame.display.set_mode((width,height),1,16)
pygame.display.set_caption('Camera')
#take a picture
image = cam.get_image()
cam.stop()
#display the picture
catSurfaceObj = image
windowSurfaceObj.blit(catSurfaceObj,(0,0))
pygame.display.update()
#save picture
pygame.image.save(windowSurfaceObj,'picture.jpg')
这工作,它不是那么快,干净,但工程。使用pygame是捕获它的经典方法之一。
方式N2:
这里是需要这样的lib v4l2capture另一种方式,你应该使用这样的:
import Image
import select
import v4l2capture
# Open the video device.
video = v4l2capture.Video_device("/dev/video0")
# Suggest an image size to the device. The device may choose and
# return another size if it doesn't support the suggested one.
size_x, size_y = video.set_format(1280, 1024)
# Create a buffer to store image data in. This must be done before
# calling 'start' if v4l2capture is compiled with libv4l2. Otherwise
# raises IOError.
video.create_buffers(1)
# Send the buffer to the device. Some devices require this to be done
# before calling 'start'.
video.queue_all_buffers()
# Start the device. This lights the LED if it's a camera that has one.
video.start()
# Wait for the device to fill the buffer.
select.select((video,),(),())
# The rest is easy :-)
image_data = video.read()
video.close()
image = Image.fromstring("RGB", (size_x, size_y), image_data)
image.save("image.jpg")
print "Saved image.jpg (Size: " + str(size_x) + " x " + str(size_y) + ")"
安装
对于这个LIB您需要安装libv4l,如sudo apt-get install libv4l
。 v4l2capture默认需要libv4l。您可以在不使用libv4l的情况下编译v4l2capture ,但可以减少对YUYV输入 和RGB输出的图像格式支持。
python-v4l2capture使用distutils。
编译:sudo ./setup.py build
构建 并安装:sudo ./setup.py install
道N3:
我个人的方法是通过node.js的服务器,让自动和网络能力。这是我的工作的例子:initalazie_server
但是,这是没有摄像头,您应该添加:
var camera = require('v4l2camera');
var cam = new camera.Camera("/dev/video0");
cam.start();
cam.capture(function (success) {
var frame = cam.frameRaw();
fs.createWriteStream("/home/pi/result.jpg").end(Buffer(frame));
});
如何安装node.js的解释是在这里:Instalation of node
你想控制RPI通过本地网络还是会自动喜欢某些东西(每次RPI在一段时间内打开)? – Avoid
自动很好。我并不担心网络等问题,只关心如何从相机中获取图像(以便稍后进行我自己的处理) – KansaiRobot