14

Comrades,

I'd like to capture images from the laptop camera in Python. Currently all signs point to OpenCV. Problem is OpenCV is a nightmare to install, and it's a nightmare that reoccurs every time you reinstall your code on a new system.

Is there a more lightweight way to capture camera data in Python? I'm looking for something to the effect of:

$ pip install something
$ python
>>> import something
>>> im = something.get_camera_image()

I'm on Mac, but solutions on any platform are welcome.

Miki
  • 40,887
  • 13
  • 123
  • 202
Peter
  • 12,274
  • 9
  • 71
  • 86
  • 2
    http://stackoverflow.com/a/11094891/5008845 – Miki Aug 17 '16 at 17:51
  • Not directly answer to **without** but if you have problem installing OpenCV for Windows you could try http://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv whl binary package – Dawid Aug 26 '16 at 15:04

4 Answers4

3

I've done this before using pygame. But I can't seem to find the script that I used... It seems there is a tutorial here which uses the native camera module and is not dependent on OpenCV.

Try this:

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

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

cam = pygame.camera.Camera("/path/to/camera",(640,480))
cam.start()

image = cam.get_image()

If you don't know the path to the camera you can also get a list of all available which should include you laptop webcam:

camlist = pygame.camera.list_cameras()
    if camlist:
        cam = pygame.camera.Camera(camlist[0],(640,480))
pbreach
  • 16,049
  • 27
  • 82
  • 120
  • 1
    Hmm, at least on my pygame install I get `File "/Library/Python/2.7/site-packages/pygame/camera.py"`, `line 42, in init` `import _camera` `ImportError: No module named _camera` on the `camera.init()` line. – Peter Aug 26 '16 at 09:32
  • Interesting, try edited answer. Changed the imports to what the tutorial has written. – pbreach Aug 26 '16 at 14:55
  • Still the same, but this may be something to do with me not having properly installed pygame. – Peter Aug 26 '16 at 15:06
3

As the documentation states, currently pygame.camera only supports linux and v4l2 cameras, so this would not work on macOS. On Linux instead it works fine.

Syden
  • 8,425
  • 5
  • 26
  • 45
2

You can use pyavfcam github. You will need to install xcode and cython, first. This is the official demo.

import pyavfcam

# Open the default video source
cam = pyavfcam.AVFCam(sinks='image')
cam.snap_picture('test.jpg')

print "Saved test.jpg (Size: " + str(cam.shape[0]) + " x " + str(cam.shape[1]) + ")"
elarmando
  • 569
  • 1
  • 7
  • 16
2

Videocapture for Windows

I've used videocapture in the past and it was perfect:

from VideoCapture import Device
cam = Device()
cam.saveSnapshot('image.jpg')
G M
  • 20,759
  • 10
  • 81
  • 84