3
votes

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.

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:\Python27\lib\site-packages\pygame_camera_vidcapture.py", line 47, in init
    self.dev = vidcap.new_Dev(device, show_video_window)
TypeError: an integer is required
2
How do you know it's not working? - Peter Wood
I use script which use "/dev/video0", this is read device in linux. But i dont know how to read camera device in windows 7. - Gusan
what is your pygame version? earlier releases supports only linux cameras. the latest version, 1.9 have support for both - marmeladze
i use 1.9.1 version for python27 - Gusan
<!-- language: lang-py --> File "E:/test_python/open_cam2.py", line 10, in <module> cam = pygame.camera.Camera("/dev/video0", (640, 480)) File "C:\Python27\lib\site-packages\pygame_camera_vidcapture.py", line 47, in init self.dev = vidcap.new_Dev(device, show_video_window) TypeError: an integer is required - Gusan

2 Answers

6
votes

Try this,

 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()
0
votes

This should work...

import pygame
import pygame.camera

pygame.init()

gameDisplay = pygame.display.set_mode((1280,720), pygame.RESIZABLE)

pygame.camera.init()
cam = pygame.camera.Camera(0,(1280,720))
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()

I'm using windows 10 , pygame version 1.9.6