Saturday 11 January 2014

Raspberry Pi - Camera, Python & PiCamera

For my raspberry pi car cam project I needed to find a better way of syncing data with video so I modified raspivid and created python class to run raspivid.  I did this hack mainly because I couldn't be bothered and didn't really have the skill to write an interface to the camera in python, luckily someone else has!

Picamera is a python interface for the raspberry pi camera board, created by Dave Jones (aka waveform80), think of it as the python equivalent of raspivid and raspistill.  Using picamera you can 'programmatically' take videos and images and is much easier way of capturing video/images in your python projects.

There is some great documentation for Picamera at picamera.readthedocs.org, which includes detailed instructions about how to install picamera, getting started and a complete reference for the module.  You can also find the code at github.com/waveform80/picamera.

Install picamera
sudo apt-get install python-picamera

Take a video
The following code will record a video and save it to 'foo.h264'.
import picamera

with picamera.PiCamera() as camera:
    camera.resolution = (640, 480)
    camera.start_preview()
    camera.start_recording('foo.h264')
    camera.wait_recording(60)
    camera.stop_recording()
    camera.stop_preview()

Take a picture
The following code will take a single picture and save it to 'foo.jpg'.
import time
import picamera

with picamera.PiCamera() as camera:
    camera.resolution = (1024, 768)
    camera.start_preview()
    # Camera warm-up time
    time.sleep(2)
    camera.capture('foo.jpg')
There is much more information on http://picamera.readthedocs.org and its well worth a read.

There are also some great functions such as split recording, capturing images while recording and pulling out data from the encoder while the video is recording (such as frame number).

14 comments:

  1. hii martin when i followed your code im getting error as
    AttributeError: 'module' object has no attribute 'picamera'... plz help

    ReplyDelete
    Replies
    1. Are you sure picamera has been installed correctly? Did you receive any errors during the installation? Or did this error occur during the install?

      Delete
    2. sudo apt-get install python-picamera

      Delete
    3. Thanks for the update tilaprimera, i didnt know it had been added to the raspbian repositories

      Delete
    4. Nope!. He forgot to put capital C from Picamera to PiCamera

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. I am having a similar problem but have updated, upgraded, installed, uninstalled, and reinstalled picamera. Here is my code

    import time
    import picamera

    with picamera.Picamera() as camera:
    camera.start_preview()
    time.sleep(10)
    camera.stop_preview()

    Every time I try to run it I get:

    Traceback (most recent call last):
    File "/home/pi/picamera.py", line2 in
    import picamera
    File "/home/pi/picamera.py", line 4, in
    with picamera.Picamera() as camera:
    AttributeError: 'module' object has no attribute 'Picamera'

    ReplyDelete
    Replies
    1. U forgot to put capital C

      with picamera.Picamera() as camera:

      to
      with picamera.PiCamera() as camera:

      Delete
    2. Np! I be happier to help them. :-)

      Delete
  5. Hi I am having trouble recording video, I run your exact code and it goes into preview but never comes out, and if i take off the preview, it opens the shell turns on the camera but never records anything and never comes out after 10 or any amount of seconds. Any ideas would be awesome!

    ReplyDelete
    Replies
    1. I would guess you have an error in your code after the start_preview but you cant see it because the preview starts.

      Add an alpha (transparency) to your preview and see if your getting an error.

      camera.start_preview(alpha=175)

      Delete
  6. I should add I'm using a Raspberry Pi 3 and think I have installed all the necessary updates for the camera.

    ReplyDelete

Note: only a member of this blog may post a comment.