Pygame不会在Mac上加载我的相机?

2024-03-28 15:29:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图访问我的网络摄像头在Pygame上拍照并保存,但每次我运行代码:

import pygame.camera

import pygame.image

import sys

pygame.camera.init()

cameras = pygame.camera.list_cameras()

print ("Using camera %s ..." % cameras[0])

webcam = pygame.camera.Camera(cameras[0])

webcam.start()

# grab first frame

img = webcam.get_image()

WIDTH = img.get_width()

HEIGHT = img.get_height()

screen = pygame.display.set_mode( ( WIDTH, HEIGHT ) )

pygame.display.set_caption("pyGame Camera View")

while True :

 for e in pygame.event.get() :

     if e.type == pygame.QUIT :

         sys.exit()

 # draw frame

 screen.blit(img, (0,0))

 pygame.display.flip()

 # grab next frame    

 img = webcam.get_image()

我把这个闲置着:

^{pr2}$

我有没有做错什么,或者可以在Python上安装另一个模块,它可以通过设备的内部或外部网络摄像头拍摄照片,并将其存储或发送到需要的地方?在

谢谢你


Tags: imageimport网络imggetdisplaysysframe
2条回答

您可以使用OpenCV,这是一个健壮的图像获取和操作工具。在

OpenCV接受不同的语言,python就是其中之一,因此您只需导入模块并创建一个函数,该函数获取图片并以变量形式返回图像(或者您可以将图片存储在文件中,稍后再读取)。在

看看这个链接,了解如何使用opencv2(和python2.7)获取图像或视频

https://codeplasma.com/2012/11/02/getting-webcam-images-with-python-and-opencv/

或者,如果您想使用openCV的最新3.0测试版:

http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_gui/py_video_display/py_video_display.html

编辑:

要在Mac上安装OpenCV,请访问以下链接:

http://docs.opencv.org/2.4/doc/tutorials/introduction/ios_install/ios_install.html#ios-installation

PD:你也可以在Mac上安装Linux并加入开源社区:)

根据pygame.cameradocumentation

Pygame currently supports only Linux and v4l2 cameras.

既然你用的是Mac而不是Linux,这是行不通的。对于将来,阅读您正在使用的库和模块的文档是非常有帮助的,因为这样的常见问题通常会得到解决。在

相关问题 更多 >