2016-12-28 209 views
0

假期即将到来,我想用一段时间与我得到的树莓派3。我也有一个USB摄像头(不是树莓相机),所以我想问一下:树莓派上的USB摄像头

我怎样才能得到相机的快照,以用于后续处理(什么样的处理并不重要)

满足以下条件:

1)在互联网上有一些资源描述下载应用程序,显然做我问。我对这个不感兴趣,但在实际管理的相机,我写

2)我想写(用于PIC采集和处理),可以在C程序(或程序中获得的快照类似:C++等)或Python。我打开两个

3)我不喜欢使用OpenCV的(我已经有了一个源代码,这一点,但由于个人原因我不喜欢用这个)

任何帮助非常赞赏

+0

你想控制RPI通过本地网络还是会自动喜欢某些东西(每次RPI在一段时间内打开)? – Avoid

+0

自动很好。我并不担心网络等问题,只关心如何从相机中获取图像(以便稍后进行我自己的处理) – KansaiRobot

回答

1

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

+0

谢谢!我尝试了第一种方法,它的工作原理。相机设置需要修改我认为,因为图像是蓝色但可以。唯一的一点是,它产生了一个窗口,我可以看到图像,但这个窗口无法用任何方法关闭。 (而且图像没有在其他窗口上存在,等等)想知道为什么以及如何关闭它 – KansaiRobot