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.
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.
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.
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))
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]) + ")"
I've used videocapture in the past and it was perfect:
from VideoCapture import Device
cam = Device()
cam.saveSnapshot('image.jpg')
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With