Camera Workshop
with Ben Nuttall
Test the Camera
raspistill -o image.jpg
Capturing with Python
import time
import picamera
with picamera.PiCamera() as camera:
camera.start_preview()
time.sleep(5)
camera.capture('/home/pi/Desktop/image.jpg')
camera.stop_preview()
Capturing with a button (wiring)
Capturing with a button (code)
import time
import picamera
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN, GPIO.PUD_UP)
with picamera.PiCamera() as camera:
camera.start_preview()
GPIO.wait_for_edge(17, GPIO.FALLING)
camera.capture('/home/pi/Desktop/image.jpg')
camera.stop_preview()
Capture on a timer
import time
import picamera
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN, GPIO.PUD_UP)
with picamera.PiCamera() as camera:
camera.start_preview()
GPIO.wait_for_edge(17, GPIO.FALLING)
time.sleep(5)
camera.capture('/home/pi/Desktop/image.jpg')
camera.stop_preview()
Recording Video
import time
import picamera
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN, GPIO.PUD_UP)
with picamera.PiCamera() as camera:
camera.start_preview()
GPIO.wait_for_edge(17, GPIO.FALLING)
camera.start_recording('/home/pi/Desktop/video.h264')
time.sleep(1)
GPIO.wait_for_edge(17, GPIO.FALLING)
camera.stop_recording()
camera.stop_preview()
Recording the past
...
with picamera.PiCamera() as camera:
stream = picamera.PiCameraCircularIO(camera, seconds=20)
camera.start_preview()
camera.start_recording(stream, format='h264')
GPIO.wait_for_edge(17, GPIO.FALLING)
camera.stop_recording()
camera.stop_preview()
for frame in stream.frames:
if frame.header:
stream.seek(frame.position)
break
with io.open('/home/pi/Desktop/video.h264', 'wb') as output:
while True:
data = stream.read1()
if not data:
break
output.write(data)
Fin!
Picamera documentation (including lots of recipes!)
http://picamera.readthedocs.org/
Raspberry Pi Learning Resources (including this workshop)
http://www.raspberrypi.org/resources/learn/
Dave Jones (picamera author and database know-it-all)