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).
hii martin when i followed your code im getting error as
ReplyDeleteAttributeError: 'module' object has no attribute 'picamera'... plz help
Are you sure picamera has been installed correctly? Did you receive any errors during the installation? Or did this error occur during the install?
Deletesudo apt-get install python-picamera
DeleteThanks for the update tilaprimera, i didnt know it had been added to the raspbian repositories
DeleteNope!. He forgot to put capital C from Picamera to PiCamera
DeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteI am having a similar problem but have updated, upgraded, installed, uninstalled, and reinstalled picamera. Here is my code
ReplyDeleteimport 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'
U forgot to put capital C
Deletewith picamera.Picamera() as camera:
to
with picamera.PiCamera() as camera:
Thanks Supra
DeleteNp! I be happier to help them. :-)
DeleteHi 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!
ReplyDeleteI would guess you have an error in your code after the start_preview but you cant see it because the preview starts.
DeleteAdd an alpha (transparency) to your preview and see if your getting an error.
camera.start_preview(alpha=175)
I should add I'm using a Raspberry Pi 3 and think I have installed all the necessary updates for the camera.
ReplyDelete