Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Access camera WITHOUT OpenCV

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.

like image 638
Peter Avatar asked Aug 17 '16 17:08

Peter


People also ask

How do I access my phone camera with Python?

First, install the OpenCV library in Python; pip install opencv-python. Download and install the IP Webcam application on your smartphones. After installing the IP Webcam app, make sure your phone and PC are connected to the same network. Run the app on your phone and click Start Server.

How do you make a Python camera app?

Back-End Implementation Steps :Create a path variable and set it currently to blank. Add available cameras to the combo box and set the first camera as default. Add action to the Take Photo button. Inside the click action, capture the photo at the given path with name as a timestamp and increment the count.


4 Answers

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))
like image 157
pbreach Avatar answered Oct 16 '22 13:10

pbreach


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]) + ")"
like image 35
elarmando Avatar answered Oct 16 '22 12:10

elarmando


Videocapture for Windows

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

from VideoCapture import Device
cam = Device()
cam.saveSnapshot('image.jpg')
like image 28
G M Avatar answered Oct 16 '22 12:10

G M


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.

like image 38
philipptrenz Avatar answered Oct 16 '22 12:10

philipptrenz