Mat*_*w G 38 python java webcam image-capture javacv
我想从我的网络摄像头捕获单个图像并将其保存到磁盘.我想用Java或Python(最好是Java)来做这件事.我想要一些适用于64位Win7和32位Linux的东西.
编辑:我使用Python 3.x,而不是2.x.
因为在其他任何地方我都看到这个问题让人们感到困惑,我将明确陈述一些事情:
EDIT2:我能够使用Python 2.7和pygame 1.9.1让Froyo的pygame示例在Linux上运行.pygame.camera.camera_list()调用不起作用,但对于示例的其余部分则没有必要.但是,我不得不调用cam.set_controls()(你可以在这里找到http://www.pygame.org/docs/ref/camera.html的文档)来提高亮度,这样我才真正看到了我捕获的图像.
此外,我需要调用cam.get_image()和pygame.image.save()方法三次,然后我认为第一对调用的图像实际上被保存了.他们似乎陷入了一个奇怪的缓冲区.基本上,我不是一次调用cam.get_image(),而是每次想要捕获图像时都要调用它三次.然后我才调用pygame.image.save().
不幸的是,如下所述,pygame.camera仅在Linux上受支持.我仍然没有Windows的解决方案.
Fro*_*oyo 68
@thebjorn给出了一个很好的答案.但如果您想要更多选项,可以试试OpenCV,SimpleCV.
使用SimpleCV:
from SimpleCV import Image, Camera
cam = Camera()
img = cam.getImage()
img.save("filename.jpg")
Run Code Online (Sandbox Code Playgroud)
使用OpenCV:
from cv2 import *
# initialize the camera
cam = VideoCapture(0) # 0 -> index of camera
s, img = cam.read()
if s: # frame captured without any errors
namedWindow("cam-test",CV_WINDOW_AUTOSIZE)
imshow("cam-test",img)
waitKey(0)
destroyWindow("cam-test")
imwrite("filename.jpg",img) #save image
Run Code Online (Sandbox Code Playgroud)
使用pygame:
import pygame
import pygame.camera
pygame.camera.init()
pygame.camera.list_camera() #Camera detected or not
cam = pygame.camera.Camera("/dev/video0",(640,480))
cam.start()
img = cam.get_image()
pygame.image.save(img,"filename.jpg")
Run Code Online (Sandbox Code Playgroud)
安装OpenCV:
install python-opencv bindings, numpy
Run Code Online (Sandbox Code Playgroud)
安装SimpleCV:
install python-opencv, pygame, numpy, scipy, simplecv
Run Code Online (Sandbox Code Playgroud)
获取最新版本的SimpleCV
安装pygame:
install pygame
Run Code Online (Sandbox Code Playgroud)
the*_*orn 17
在Windows上,可以使用pygame轻松与您的网络摄像头进行交互:
from VideoCapture import Device
cam = Device()
cam.saveSnapshot('image.jpg')
Run Code Online (Sandbox Code Playgroud)
我没有尝试在linux上使用pygame(我所有的linux boxen都是没有X的服务器),但这个链接可能会有所帮助http://www.jperla.com/blog/post/capturing-frames-from-a-webcam-在Linux的
前段时间我写了一个简单的Webcam Capture API,可用于此.该项目可在Github上获得.
示例代码:
Webcam webcam = Webcam.getDefault();
webcam.open();
try {
ImageIO.write(webcam.getImage(), "PNG", new File("test.png"));
} catch (IOException e) {
e.printStackTrace();
} finally {
webcam.close();
}
Run Code Online (Sandbox Code Playgroud)
import cv2
camera = cv2.VideoCapture(0)
while True:
return_value,image = camera.read()
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
cv2.imshow('image',gray)
if cv2.waitKey(1)& 0xFF == ord('s'):
cv2.imwrite('test.jpg',image)
break
camera.release()
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
114055 次 |
| 最近记录: |