4

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
J Richard Snape
  • 20,116
  • 5
  • 51
  • 79
Gusan
  • 411
  • 3
  • 5
  • 19
  • How do you know it's not working? – Peter Wood Apr 16 '15 at 11:33
  • 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 Apr 16 '15 at 11:46
  • what is your pygame version? earlier releases supports only linux cameras. the latest version, 1.9 have support for both – marmeladze Apr 16 '15 at 11:50
  • i use 1.9.1 version for python27 – Gusan Apr 16 '15 at 11:54
  • File "E:/test_python/open_cam2.py", line 10, in 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 Apr 16 '15 at 11:55
  • Welcome to Stack Overflow! Your question came to me in a review queue - I've edited it a little to make it more easily understandable, as had another user before me. It might help you to read [How to Ask](http://stackoverflow.com/help/how-to-ask) to help you get the best assistance on the site. In particular - avoid statements like "it doesn't work" and include Tracebacks instead and show what you have already tried. Good luck! – J Richard Snape Apr 17 '15 at 15:20

3 Answers3

7

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()
Dass
  • 326
  • 1
  • 11
  • I'll try to change **WIDTH**, and **HEIGHT** 'File "E:/test_python/open_cam2.py", line 28, in screen.blit(img, (0,0)) TypeError : argument 1 must be pygame.Surface, not None ' – Gusan Apr 16 '15 at 12:47
  • The error implies that image is None, not that it is a valid image in the wrong format. Apparently cam.get_image() failed for some reason. – Dass Apr 16 '15 at 14:30
  • am i necessary make some scripts to open the image file? @Dass – Gusan Apr 17 '15 at 02:19
  • If you want, you can use Image.open method to open an Image file. – Dass Apr 17 '15 at 07:52
  • Is there any updates to this? I'm trying it in 2019 and it's not working on Windows 10. I ran each line separately and I got an error when establishing the webcam = pygame.camera.Camera(cameras[0]). I get this error. self.dev.setresolution(width, height) vidcap.Error: Cannot set capture resolution. – Brandon Jacobson Dec 22 '19 at 00:23
1

The pygame.camera module natively supports cameras under Windows since version 2.0.2. See a minimal example using the pygame.camera module (tested with Windows):

import pygame
import pygame.camera

pygame.init()
pygame.camera.init()

camera_list = pygame.camera.list_cameras()
camera = pygame.camera.Camera(camera_list[0])

window = pygame.display.set_mode(camera.get_size())
clock = pygame.time.Clock()
camera.start()

run = True
while run:
    clock.tick(100)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    camera_frame = camera.get_image()

    window.fill(0)
    window.blit(camera_frame, (0, 0))
    pygame.display.flip()

pygame.quit()
exit()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
0

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