6

I am trying to initialize the camera module in pygame and display video from a usb webcam. This is my code:

import pygame
import pygame.camera
from pygame.camera import *
from pygame.locals import *

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

cam = pygame.camera.Camera("/dev/video0",(640,480))
cam.start()
image = cam.get_image()

Yet i get this error:

Traceback (most recent call last):
  File "C:/Users/Freddie/Desktop/CAMERA/Test1.py", line 7, in <module>
    pygame.camera.init()
  File "C:\Python27\lib\site-packages\pygame\camera.py", line 67, in init
    _camera_vidcapture.init()
  File "C:\Python27\lib\site-packages\pygame\_camera_vidcapture.py", line 21, in init
    import vidcap as vc
ImportError: No module named vidcap

PLS HELP!!! Im on Windows

Vogon Jeltz
  • 1,166
  • 1
  • 14
  • 31
  • ... and what should we say? The error is clear: it cannot find the module `vidcap`. Did you install it? How did you install it? without these information how are we supposed to tell you what's wrong with the installation? – Bakuriu Apr 28 '13 at 19:01
  • From the pygame.camera docs: `Pygame currently supports only Linux and v4l2 cameras.` http://www.pygame.org/docs/ref/camera.html It's possible that the documentation is outdated, however. – Haz Apr 29 '13 at 17:06

5 Answers5

6

I met the same problem. The error info of "ImportError: No module named vidcap" indicates that python interpreter didn't find the vidcap module on you machine.

so you'd better follow these steps.

  1. Download the vidcap from http://videocapture.sourceforge.net/

2.Then copy the corresponding version of dll (which named "vidcap.pyd" in VideoCapture-0.9-5\VideoCapture-0.9-5\Python27\DLLs) to "your python path"\DLLs\ .

3.restart you script.

Done!.

Song
  • 298
  • 5
  • 20
fandyst
  • 2,740
  • 2
  • 14
  • 15
3

The camera module can only be used on linux

2

I met the same problem but I found out that its not included on windows ONLY LINUX

user2731117
  • 109
  • 1
  • 2
0

Try this:

import pygame

import pygame.camera

import time, string


from VideoCapture import Device

from pygame.locals import *

pygame.camera.init()

cam = pygame.camera.Camera(0,(640,480),"RGB")

cam.start()

img = pygame.Surface((640,480))

cam.get_image(img)

pygame.image.save(img, "img2.jpg")

cam.stop()
YoungHobbit
  • 13,254
  • 9
  • 50
  • 73
Sagar
  • 13
  • 3
0

The pygame.camera is only supported on linux:

Pygame currently supports only Linux and v4l2 cameras.

An alternative solution is to use the OpenCV VideoCapture. Install OpenCV for Python (cv2) (see opencv-python).

Opens a camera for video capturing:

capture = cv2.VideoCapture(0)

Grabs a camera frame:

success, camera_image = capture.read()

Convert the camera frame to a pygame.Surface object using pygame.image.frombuffer:

camera_surf = pygame.image.frombuffer(
              camera_image.tobytes(), camera_image.shape[1::-1], "BGR")

See also Camera and Video


Minimal example:

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()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174