如何在Python中使用网络摄像头,在Windows中使用Pygame?错误:无法设置捕获分辨率



所以我在我的Pygame中下载了用于视频捕获的Windows轮。 但是当我创建网络摄像头实例时,我收到以下错误。 在我开始实现相机模块之前,代码运行良好。 我看到的答案很少,它们是从2013年开始的。 我希望从那以后,Pygame会在Windows上得到更多改进,但很可能是我导致了这个问题。 有什么建议吗?

import pygame
import pygame.camera
pygame.init()
# https://www.lfd.uci.edu/~gohlke/pythonlibs/#videocapture need to get the VideoCapture whl
pygame.camera.init()
webcam = pygame.camera.Camera(0, (640, 480), "RGB")
webcam.start()
pygame.display.set_caption("S.H.A.N.E.")
icon = pygame.image.load("SHANE pic.jpg")
pygame.display.set_icon(icon)
background = pygame.image.load("SHANE pic.jpg")
# (WIDTH, HEIGHT)
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
running = True
while running:
# STARTING POINT (L/R, T/B), ENDING POINT (L/R, T/B)
screen.fill((0, 0, 0))
img = webcam.get_image()
screen.blit(img, (0, 0))
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False

这是我得到的错误。

Traceback (most recent call last):
File "C:/Users/Notebook/PycharmProjects/Jarvis/Gui2.py", line 10, in <module>
webcam = pygame.camera.Camera(0, (640, 480), "RGB")
File "C:UsersNotebookAppDataLocalProgramsPythonPython37-32libsite-<packagespygame_camera_vidcapture.py", line 60, in __init__
self.dev.setresolution(width, height)
vidcap.Error: Cannot set capture resolution.
webcam = pygame.camera.Camera("/dev/video0",(640,480))

您不会将摄像机的坐标/大小/比例放置在放置它们的位置,这是为文件输入保留的。 干杯! 文档: https://www.pygame.org/docs/tut/CameraIntro.html

您可以使用以下代码来检查您拥有的相机。

pygame.camera.init()
camlist = pygame.camera.list_cameras()
print(camlist)

例如,地雷打印["FaceTime高清摄像头"]。然后你走

cam = pygame.camera.Camera('FaceTime HD Camera', (640,480))

如果您的意思是您的打印 ['0'],我想知道您是否可以通过简单地将 0 更改为"0"来解决问题。

最新更新