USB camera on a raspberry pi

11,259

Way N1:

This should work for python and You don't need to install anything extra but be sure to update and upgrade via apt-get:

#!/usr/bin/python
import os
import pygame, sys

from pygame.locals import *
import pygame.camera

width = 640
height = 480

#initialise pygame   
pygame.init()
pygame.camera.init()
cam = pygame.camera.Camera("/dev/video0",(width,height))
cam.start()

#setup window
windowSurfaceObj = pygame.display.set_mode((width,height),1,16)
pygame.display.set_caption('Camera')

#take a picture
image = cam.get_image()
cam.stop()

#display the picture
catSurfaceObj = image
windowSurfaceObj.blit(catSurfaceObj,(0,0))
pygame.display.update()

#save picture
pygame.image.save(windowSurfaceObj,'picture.jpg')

This works, its not so fast and clean but works. Using pygame is one of the classic ways to capture it.



Way N2:
And here is another way that needs this lib v4l2capture and You should use it like this:

import Image
import select
import v4l2capture

# Open the video device.
video = v4l2capture.Video_device("/dev/video0")

# Suggest an image size to the device. The device may choose and
# return another size if it doesn't support the suggested one.
size_x, size_y = video.set_format(1280, 1024)

# Create a buffer to store image data in. This must be done before
# calling 'start' if v4l2capture is compiled with libv4l2. Otherwise
# raises IOError.
video.create_buffers(1)

# Send the buffer to the device. Some devices require this to be done
# before calling 'start'.
video.queue_all_buffers()

# Start the device. This lights the LED if it's a camera that has one.
video.start()

# Wait for the device to fill the buffer.
select.select((video,), (), ())

# The rest is easy :-)
image_data = video.read()
video.close()
image = Image.fromstring("RGB", (size_x, size_y), image_data)
image.save("image.jpg")
print "Saved image.jpg (Size: " + str(size_x) + " x " + str(size_y) + ")"

Installation

For this lib you need to install libv4l like sudo apt-get install libv4l. v4l2capture requires libv4l by default. You can compile v4l2capture without libv4l, but that reduces image format support to YUYV input and RGB output only.

python-v4l2capture uses distutils.

To build: sudo ./setup.py build
To build and install: sudo ./setup.py install



Way N3:
My personal way is through node.js server that gives ability of automatic and network. Here is the example of my work: initalazie_server

But that is without camera, You should add:

var camera = require('v4l2camera');
var cam = new camera.Camera("/dev/video0");
cam.start();
cam.capture(function (success) {
    var frame = cam.frameRaw();
    fs.createWriteStream("/home/pi/result.jpg").end(Buffer(frame));
});

The explanation how to install node.js is here: Instalation of node

Share:
11,259

Related videos on Youtube

KansaiRobot
Author by

KansaiRobot

Updated on September 14, 2022

Comments

  • KansaiRobot
    KansaiRobot over 1 year

    The holidays are coming and I would like to use some time with a Raspberry Pi 3 I got. I also got a USB camera (not the raspberry camera) so I would like to ask:

    How can I get a snapshot of the camera to use for follow up processing (what processing is not important)

    With the following conditions:

    1) There are some resources in the internet that describe downloading applications that apparently do what I ask. I am not interested in this but in actually managing the camera to obtain a snapshot within a program that I write

    2) The program I would like to write (for pic acquisition and processing) can be in C (or similar: C++ etc) or Python. I am open to both

    3) I would not like to use OpenCV (I already got a source code for this but for personal reasons I prefer not to use this)

    Any help much appreciated

  • KansaiRobot
    KansaiRobot about 7 years
    Thanks! I tried the first method and it works. The camera settings need to be modified I think because the image was bluish but ok. The only thing was that it generated a window where I could see the image but this window could not be closed by any means. (And the image did not survive other windows over it, etc) Wonder why and how to close this