Python:访问不带OpenCV的相机

2024-04-20 13:10:29 发布

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

同志们

我想用Python从笔记本电脑摄像头中捕捉图像。目前所有迹象都指向OpenCV。问题是OpenCV是一个安装的噩梦,每次在新系统上重新安装代码时,它都会再次出现。

有没有一种更轻量级的方法来捕获Python中的相机数据?我在找一些东西,大意是:

$ pip install something
$ python
>>> import something
>>> im = something.get_camera_image()

我在Mac上,但任何平台上的解决方案都是受欢迎的。


Tags: installpip数据方法代码图像系统opencv
3条回答

我在使用pygame之前已经做过了。但我好像找不到我用的剧本。。。似乎有一个教程here使用本机相机模块,不依赖OpenCV。

试试这个:

import pygame
import pygame.camera
from pygame.locals import *

pygame.init()
pygame.camera.init()

cam = pygame.camera.Camera("/path/to/camera",(640,480))
cam.start()

image = cam.get_image()

如果您不知道相机的路径,您还可以获得所有可用的列表,其中应包括您的笔记本电脑摄像头:

camlist = pygame.camera.list_cameras()
    if camlist:
        cam = pygame.camera.Camera(camlist[0],(640,480))

您可以使用pyavfcamgithub。首先,您需要安装xcode和cython。这是官方演示。

import pyavfcam

# Open the default video source
cam = pyavfcam.AVFCam(sinks='image')
cam.snap_picture('test.jpg')

print "Saved test.jpg (Size: " + str(cam.shape[0]) + " x " + str(cam.shape[1]) + ")"

作为the documentation states,目前pygame.camera只支持linux和v4l2摄像头,因此这在macOS上不起作用。而在Linux上,它工作得很好。

相关问题 更多 >