【问题标题】:USB camera on a raspberry pi树莓派上的 USB 摄像头
【发布时间】:2017-05-12 12:49:20
【问题描述】:

假期快到了,我想花点时间用我得到的 Raspberry Pi 3。我还有一个 USB 摄像头(不是覆盆子摄像头)所以我想问一下:

如何获取相机的快照用于后续处理(什么处理不重要)

满足以下条件:

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

2) 我想编写的程序(用于图片采集和处理)可以是 C(或类似:C++ 等)或 Python。我对两者都持开放态度

3) 我不想使用 OpenCV(我已经得到了源代码,但出于个人原因我不想使用它)

非常感谢任何帮助

【问题讨论】:

  • 您想通过本地网络控制 RPI 还是想要一些自动的东西(每次 RPI 在间隔内开启)?
  • 自动就好了。我不太担心网络之类的东西,只担心如何从相机中获取图像(以便以后自己处理)

标签: video camera raspberry-pi3


【解决方案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路:
这是需要这个库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) + ")"

安装

对于这个库,您需要安装 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

【讨论】:

  • 谢谢!我尝试了第一种方法,它有效。我认为需要修改相机设置,因为图像偏蓝但还可以。唯一的问题是它生成了一个窗口,我可以在其中看到图像,但该窗口无法以任何方式关闭。 (并且图像在它上面的其他窗口中无法幸存,等等)想知道为什么以及如何关闭它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-07
  • 1970-01-01
  • 2022-11-14
  • 2020-11-05
  • 2013-01-01
相关资源
最近更新 更多