Python Picamera with GPIO Zero - Picademy USA

A1995c9abe48450ce2f82d93ca0b863f?s=47 Ben Nuttall
February 27, 2016

Python Picamera with GPIO Zero - Picademy USA

Python camera GPIO workshop given at Picademy USA, featuring picamera and GPIO Zero

A1995c9abe48450ce2f82d93ca0b863f?s=128

Ben Nuttall

February 27, 2016
Tweet

Transcript

  1. 2.

    Who's this guy? Ben Nuttall Education Developer Advocate Raspberry Pi

    Foundation @ben_nuttall ben@raspberrypi.org github.com/bennuttall
  2. 4.

    Raspberry Pi camera module - 5Mpx - Full HD -

    Photo & video - Command line - Python module - Infra-red camera
  3. 5.

    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
  4. 9.

    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
  5. 11.
  6. 13.
  7. 15.

    Open a new file • File > New File •

    File > Save • Save as camera.py
  8. 16.

    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
  9. 17.

    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
  10. 18.

    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
  11. 19.

    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)
  12. 22.

    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?
  13. 24.

    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