如何在 Windows 中使用 pygame 打开相机? [英] How to open camera with pygame in Windows?

查看:28
本文介绍了如何在 Windows 中使用 pygame 打开相机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Windows 7 机器上使用 pygame 模块用 Python 打开相机,但它不起作用.我以前使用过 "/dev/video0" 它是 Linux 中的读取设备.pygame 文档仅展示了如何在 Linux 中打开相机设备.我使用的是 pygame 版本 1.9.1 和 Python 2.7.

I want to open the camera with Python using the pygame module on a Windows 7 machine, but it's not working. I have previously used "/dev/video0" which is the read device in Linux. The pygame documentation just shows how to open a camera device in Linux. I am using pygame version 1.9.1 and Python 2.7.

如何在 Windows 设备上打开相机?当我尝试我现有的脚本时,我得到的错误是:

How can I open the camera on a Windows device? When I try my existing script, the error I get is:

File "E:/test_python/open_cam2.py", line 10, in <module>
    cam = pygame.camera.Camera("/dev/video0", (640, 480))
File "C:Python27libsite-packagespygame_camera_vidcapture.py", line 47, in init
    self.dev = vidcap.new_Dev(device, show_video_window)
TypeError: an integer is required

推荐答案

试试这个,

 import pygame.camera
 import pygame.image
 import sys

 pygame.camera.init()

 cameras = pygame.camera.list_cameras()

 print "Using camera %s ..." % cameras[0]

 webcam = pygame.camera.Camera(cameras[0])

 webcam.start()

 # grab first frame
 img = webcam.get_image()

 WIDTH = img.get_width()
 HEIGHT = img.get_height()

 screen = pygame.display.set_mode( ( WIDTH, HEIGHT ) )
 pygame.display.set_caption("pyGame Camera View")

 while True :
     for e in pygame.event.get() :
         if e.type == pygame.QUIT :
             sys.exit()

     # draw frame
     screen.blit(img, (0,0))
     pygame.display.flip()
     # grab next frame    
     img = webcam.get_image()

这篇关于如何在 Windows 中使用 pygame 打开相机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆