Pygame - 使用攝像頭模組



Pygame 1.9.6 及之前的版本包含 pygame.camera 模組。此模組包含在遊戲視窗上捕獲攝像頭饋送並從中抓取影像的功能。系統可用的攝像頭裝置列在 list_cameras() 方法返回的列表中。

pygame.camera.list_cameras()

要初始化攝像頭物件,請使用攝像頭 ID、解析度和格式引數。

pygame.camera.Camera(device, (width, height), format)

預設格式為 RGB。寬度和高度引數預設為 640x480。

攝像頭模組在 Camera 類中定義了以下方法。

pygame.camera.Camera.start() 開啟、初始化並開始捕獲
pygame.camera.Camera.stop() 停止、取消初始化並關閉攝像頭
pygame.camera.Camera.get_controls() 獲取使用者控制元件的當前值
pygame.camera.Camera.set_controls() 如果攝像頭支援,則更改攝像頭設定
pygame.camera.Camera.get_size() 返回正在記錄的影像的尺寸
pygame.camera.Camera.query_image() 檢查幀是否已準備好
pygame.camera.Camera.get_image() 捕獲影像作為 Surface
pygame.camera.Camera.get_raw() 將未修改的影像作為字串返回

示例

以下程式捕獲計算機預設網路攝像頭的即時饋送。

import pygame
import pygame.camera

pygame.init()

gameDisplay = pygame.display.set_mode((640,480))

pygame.camera.init()
print (pygame.camera.list_cameras())
cam = pygame.camera.Camera(0)
cam.start()
while True:
   img = cam.get_image()
   gameDisplay.blit(img,(0,0))
   pygame.display.update()
   for event in pygame.event.get() :
      if event.type == pygame.QUIT :
         cam.stop()
         pygame.quit()
         exit()

請注意,在 Windows 作業系統上,您可能需要安裝 Videocapture 模組。

pip3 install VideoCapture

輸出

Videocapture
廣告

© . All rights reserved.