在覆盆子pi的USB照相机

时间:2016-12-28 09:02:05

标签: video camera raspberry-pi3

假期即将来临,我想用一段时间来使用Raspberry Pi 3。我还有一台USB摄像头(不是覆盆子摄像头),所以我想问一下:

如何获取相机的快照以用于后续处理(哪些处理并不重要)

具备以下条件:

1)互联网上有一些资源描述下载应用程序,显然是按照我的要求进行的。我对此并不感兴趣,但实际管理相机以在我编写的程序中获取快照

2)我想写的程序(用于pic获取和处理)可以是C(或类似的:C ++等)或Python。我愿意接受

3)我不想使用OpenCV(我已经有了这个源代码,但由于个人原因我不想使用它)

任何帮助非常感谢

1 个答案:

答案 0 :(得分:2)

方式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。你可以编译v4l2capture   没有libv4l,但是减少了对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

相关问题