我是 pygame 的新手,以前從未使用過它,我想知道如何使用 pygame 和 opencv 將我的網路攝像頭顯示到表面,但我不斷收到訊息:
Traceback (most recent call last): File "<filename>.py", line 51, in <module> mainWindow() File "<filename>.py", line 43, in mainWindow draw_window() File "<filename>.py", line 24, in draw_window WINDOW.blit(camera) TypeError: argument 1 must be pygame.Surface, not cv2.VideoCapture
import pygame
import cv2
pygame.init()
# setting the width and height of the window
WIDTH, HEIGHT = 1280, 720
WINDOW = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("name me")
# background color
color = (0, 0, 0)
# 0 is the built in webcam
camera = cv2.VideoCapture(0)
camera.set(cv2.CAP_PROP_FRAME_WIDTH, 700)
camera.set(cv2.CAP_PROP_FRAME_HEIGHT, 900)
def draw_window():
# background color
WINDOW.fill((color))
# display object onto the surface (screen)
WINDOW.blit(camera)
# update the display
pygame.display.update()
FPS = 30
def mainWindow():
# keeping the window open
run = True
clock = pygame.time.Clock()
while run:
# capping it at the set frame rate
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
draw_window()
# closing the window
pygame.quit()
# main #
mainWindow()
uj5u.com熱心網友回復:
你只能blit一個pygame.Surface. 因此,您必須從相機中逐幀獲取并將其轉換為pygame.Surface物件。
抓住相機框架:
success, camera_image = capture.read()
使用以下方法將相機幀轉換為pygame.Surface物件pygame.image.frombuffer:
camera_surf = pygame.image.frombuffer(
camera_image.tobytes(), camera_image.shape[1::-1], "BGR")
在函式中執行此操作draw_window:
def draw_window():
# background color
WINDOW.fill((color))
# display object onto the surface (screen)
success, camera_image = camera.read()
if success:
camera_surf = pygame.image.frombuffer(camera_image.tobytes(), camera_image.shape[1::-1], "BGR")
WINDOW.blit(camera_surf, (0, 0))
# update the display
pygame.display.update()
另請參閱python pygame.camera.init() NO vidcapture和PyGameExamplesAndAnswers - 相機和視頻
替代但最小的示例:
import pygame
import cv2
capture = cv2.VideoCapture(0)
success, camera_image = capture.read()
window = pygame.display.set_mode(camera_image.shape[1::-1])
clock = pygame.time.Clock()
run = success
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
success, camera_image = capture.read()
if success:
camera_surf = pygame.image.frombuffer(
camera_image.tobytes(), camera_image.shape[1::-1], "BGR")
else:
run = False
window.blit(camera_surf, (0, 0))
pygame.display.flip()
pygame.quit()
exit()
uj5u.com熱心網友回復:
如果您的目標是將網路攝像頭輸入到 pygame,您可以使用pygame.camera
它本機支持 Windows 和 Linux 網路攝像頭,回退到 MacOS 上的 OpenCV 后端。
import pygame
import pygame.camera
pygame.init()
pygame.camera.init()
screen = pygame.display.set_mode((720,500))
cam_list = pygame.camera.list_cameras()
camera = pygame.camera.Camera(cam_list[0], (720,500))
camera.start()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill("black")
screen.blit(camera.get_image(), (0,0))
pygame.display.flip()
pygame.quit()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/517755.html
