2016-12-28 149 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