pygame.camera
pygame module for camera use
Module init
Get the backends supported on this system
Surface colorspace conversion
returns a list of available cameras
load a camera

PYGAME目前本机支持Linux(V4L2)和Windows(MSMF)摄像头,并通过集成的OpenCV后端提供更广泛的平台支持。

New in pygame 2.0.2: Windows本机摄像头支持

New in pygame 2.0.3: 新的OpenCV后端

试验性!:该API可能会在以后的pyGame版本中更改或消失。如果您使用它,您的代码很可能会在下一个pyGame版本中崩溃。

拜耳要 RGB 功能基于:

Sonix SN9C101 based webcam basic I/F routines
Copyright (C) 2004 Takafumi Mizuno <taka-qce@ls-a.jp>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.

PYGAME 1.9.0中的新功能。

pygame.camera.init()
Module init
init(backend = None) -> None

此功能启动摄像头模块,选择它可以为您的系统找到的最佳网络摄像头后端。这并不能保证成功,甚至可能尝试导入第三方模块,如 OpenCV 。如果你想覆盖它的后端选择,你可以调用把你想要的后端的名称传递给这个函数。有关后端的更多信息,请参阅 get_backends()

Changed in pygame 2.0.3: 显式选择后端的选项

pygame.camera.get_backends()
Get the backends supported on this system
get_backends() -> [str]

此函数按优先顺序返回它认为有可能在您的系统上运行的每个后端。

PigGame.Camera后端:

Backend           OS        Description
---------------------------------------------------------------------------------
_camera (MSMF)    Windows   Builtin, works on Windows 8+ Python3
_camera (V4L2)    Linux     Builtin
OpenCV            Any       Uses `opencv-python` module, can't enumerate cameras
OpenCV-Mac        Mac       Same as OpenCV, but has camera enumeration
VideoCapture      Windows   Uses abandoned `VideoCapture` module, can't enumerate
                            cameras, may be removed in the future

后端之间有两个主要区别。

_Camera后端是内置到pyGame本身的,不需要第三方导入。所有其他后端都会这样做。对于OpenCV和VideoCapture后端,需要在系统上安装这些模块。

另一个很大的不同是“相机枚举”。一些后端无法列出摄像头名称,甚至无法列出系统上的摄像头数量。在这些情况下, list_cameras() 将返回类似于 [0] 。如果您知道系统上有多个摄像头,则这些后端端口将通过一个“摄像头索引号”,如果您将其用作 device 参数。

New in pygame 2.0.3.

pygame.camera.colorspace()
Surface colorspace conversion
colorspace(Surface, format, DestSurface = None) -> Surface

允许从“RGB”转换为“HSV”或“YUV”的目标色彩空间。源曲面和目标曲面的大小和像素深度必须相同。这对于处理能力有限的设备上的计算机视觉非常有用。捕捉尽可能小的图像, transform.scale() 它甚至更小,然后将色彩空间转换为 YUVHSV 在对其进行任何处理之前。

pygame.camera.list_cameras()
returns a list of available cameras
list_cameras() -> [cameras]

检查计算机中是否有可用的摄像头,并返回准备送入的摄像头名称字符串列表 pygame.camera.Cameraload a camera

如果摄像头后台不支持摄像头枚举,则返回如下内容 [0] 。看见 get_backends() 获取更多信息。

pygame.camera.Camera
load a camera
Camera(device, (width, height), format) -> Camera
opens, initializes, and starts capturing
stops, uninitializes, and closes the camera
gets current values of user controls
changes camera settings if supported by the camera
returns the dimensions of the images being recorded
checks if a frame is ready
captures an image as a Surface
returns an unmodified image as bytes

加载一台摄像机。在Linux上,该设备通常类似于“/dev/Video0”。默认宽度和高度为640 x 480。格式是输出所需的色彩空间。这对于计算机视觉目的很有用。默认为 RGB 。支持以下各项:

  • RGB -红、绿、蓝

  • YUV -亮度、蓝色、红色

  • HSV -色调、饱和度、值

start()
opens, initializes, and starts capturing
start() -> None

打开相机设备,尝试对其进行初始化,并开始将图像记录到缓冲区。必须先启动摄像机,然后才能使用以下任何功能。

stop()
stops, uninitializes, and closes the camera
stop() -> None

停止录制,取消初始化摄像机,然后将其关闭。摄像头一旦停机,重新启动后才能使用以下功能。

get_controls()
gets current values of user controls
get_controls() -> (hflip = bool, vflip = bool, brightness)

如果摄像头支持Get_Controls,则Get_Controls将返回水平和垂直图像翻转的当前设置为bools,返回Brightness为int。如果不支持,它将返回(0,0,0)的默认值。请注意,这里的返回值可能不同于SET_CONTROLS返回的值,尽管这些值更有可能是正确的。

set_controls()
changes camera settings if supported by the camera
set_controls(hflip = bool, vflip = bool, brightness) -> (hflip = bool, vflip = bool, brightness)

允许您更改摄像头设置(如果摄像头支持)。如果摄像机声称成功,则返回值将为输入值;如果未成功,返回值将为先前使用的值。每个参数都是可选的,可以通过提供关键字来选择所需的参数,如hflip。请注意,摄像机使用的实际设置可能与Set_Controls返回的设置不同。在Windows上, hflipvflip 都是由pyGame实现的,而不是由相机实现的,所以它们应该总是有效的,但是 brightness 不受支持。

get_size()
returns the dimensions of the images being recorded
get_size() -> (width, height)

返回相机正在捕获的图像的当前尺寸。这将返回实际大小,如果摄像机不支持该大小,则该大小可能与初始化期间指定的大小不同。

query_image()
checks if a frame is ready
query_image() -> bool

如果图像已准备好获取,则返回TRUE。否则,它返回FALSE。请注意,一些网络摄像头将始终返回FALSE,并且仅在使用如下阻止函数调用时将帧排队 get_image() 。在Windows(MSMF)和OpenCV后端上, query_image() 不过,这应该是可靠的。这对于在不使用线程的情况下将游戏的帧速率与相机的帧速率分开非常有用。

get_image()
captures an image as a Surface
get_image(Surface = None) -> Surface

从缓冲区中拉出图像作为 RGB 表面。它可以选择重复使用现有曲面以节省时间。表面的位深度在Linux上为24位,在Windows上为32位,或与可选提供的Surface相同。

get_raw()
returns an unmodified image as bytes
get_raw() -> bytes

以相机的本机像素格式获取来自相机的字符串形式的图像。对于与其他库集成非常有用。这将返回一个字节对象




Edit on GitHub