方式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