Slide 1

Slide 1 text

Python Picamera featuring GPIO Zero Introducing the camera board and Python module

Slide 2

Slide 2 text

Who's this guy? Ben Nuttall Education Developer Advocate Raspberry Pi Foundation @ben_nuttall [email protected] github.com/bennuttall

Slide 3

Slide 3 text

USA Tour 2014

Slide 4

Slide 4 text

Raspberry Pi camera module - 5Mpx - Full HD - Photo & video - Command line - Python module - Infra-red camera

Slide 5

Slide 5 text

What you will learn ● How to connect the camera module ● How to use the Python picamera module to control the camera ● How to use the GPIO Zero Button interface to control the camera ● How to use loops to repeat commands ● How simple changes to code make different projects ● Ideas for camera projects

Slide 6

Slide 6 text

Connect the camera

Slide 7

Slide 7 text

Add a GPIO Button

Slide 8

Slide 8 text

Boot the Pi and open Python 3

Slide 9

Slide 9 text

IDLE Python Shell >>> from picamera import PiCamera >>> from gpiozero import Button >>> camera = PiCamera() >>> button = Button(17) >>> button.when_pressed = camera.start_preview >>> button.when_released = camera.stop_preview

Slide 10

Slide 10 text

GPIO Zero Button button.is_pressed button.wait_for_press() button.wait_for_release() button.when_pressed = some_function button.when_released = some_function

Slide 11

Slide 11 text

GPIO Zero Button while True: if button.is_pressed: print("Pressed") while True: if not button.is_pressed: print("Not pressed")

Slide 12

Slide 12 text

GPIO Zero Button while True: button.wait_for_press() print("Pressed") button.wait_for_release() print("Released")

Slide 13

Slide 13 text

GPIO Zero Button led = LED(4) button = Button(17) button.when_pressed = led.on button.when_released = led.off

Slide 14

Slide 14 text

GPIO Zero Button def hello(): print("Pressed") button.when_pressed = hello

Slide 15

Slide 15 text

Open a new file ● File > New File ● File > Save ● Save as camera.py

Slide 16

Slide 16 text

Take a selfie from picamera import PiCamera from time import sleep with PiCamera() as camera: camera.start_preview() sleep(3) camera.capture("/home/pi/image.jpg") camera.stop_preview() Save and run: Ctrl + S F5

Slide 17

Slide 17 text

Add GPIO Button code from picamera import PiCamera from gpiozero import Button from time import sleep button = Button(17) with PiCamera() as camera: camera.start_preview() button.wait_for_press() sleep(3) camera.capture("/home/pi/image2.jpg") camera.stop_preview() Save and run: Ctrl + S F5

Slide 18

Slide 18 text

Add a loop with PiCamera() as camera: camera.start_preview() for i in range(5): button.wait_for_press() sleep(3) camera.capture("/home/pi/image%s.jpg" % i) camera.stop_preview() Save and run: Ctrl + S F5

Slide 19

Slide 19 text

What's the difference? for i in range(5): button.wait_for_press() sleep(3) camera.capture("/home/pi/image%s.jpg" % i) button.wait_for_press() for i in range(5): sleep(3) camera.capture("/home/pi/image%s.jpg" % i)

Slide 20

Slide 20 text

What next? ● Time-lapse ● Stop motion animation ● Wildlife camera

Slide 21

Slide 21 text

What next? ● Sensor trigger ● Send to social media ● Robotics

Slide 22

Slide 22 text

Two Buttons? left = Button(4) right = Button(14) left.when_pressed = camera.start_preview left.when_released = camera.stop_preview right.when_pressed = camera.capture ← Why wouldn't this work?

Slide 23

Slide 23 text

Custom capture function def capture(): camera.capture("/home/pi/image.jpg") right.when_pressed = capture

Slide 24

Slide 24 text

Custom capture function i = 0 def capture(): global i camera.capture("/home/pi/image%s.jpg" % i) i += 1 right.when_pressed = capture Alternatively, use the datetime module

Slide 25

Slide 25 text

GPIO Zero Motion Sensor sensor = MotionSensor(14) sensor.is_motion sensor.wait_for_motion() sensor.wait_for_no_motion() sensor.when_motion = some_function sensor.when_no_motion = some_function

Slide 26

Slide 26 text

picamera camera.start_preview() camera.stop_preview() camera.capture(output) camera.start_recording(output) camera.stop_recording() camera.add_overlay(source)

Slide 27

Slide 27 text

picamera camera.resolution = (1280, 720) camera.rotation = 180 camera.annotate_text = "Hello world"

Slide 28

Slide 28 text

Documentation and help guides ● picamera.readthedocs.org ● gpiozero.readthedocs.org ● raspberrypi.org/resources ● raspberrypi.org/education/downloads

Slide 29

Slide 29 text

Python Picamera featuring GPIO Zero Introducing the camera board and Python module