Raspberry Pi Camera Training - HI-TEC Conference

Preview:

Citation preview

Raspberry Pi Training Presenters:

Michael Davis, Ph.D. Jonathan Ashdown, Ph.D. Abraham Michelen, Ph.D.

Hi-TEC Preconference Workshop July 27th, 2015

Raspberry Pi Training Presenters:

Michael Davis, Ph.D. Jonathan Ashdown, Ph.D. Abraham Michelen, Ph.D.

Hi-TEC Preconference Workshop July 27th, 2015

Welcome!

• This training is fully sponsored by BATEC (Broadening Advanced Technological Education Connections) and NEATEC (Northeast Advanced Technological Education Center)

• You will leave with the equipment you are trained on today. • Before today is over, we will:

• Turn on and configure the Raspberry Pi. • Update its operating system. • Tinker with the GUI • Connect the camera • Take pictures with a Bash script • Use a Python program to take time lapse photos. • Connect a sensor to the Raspberry Pi to activate the camera based on movement.

• A similar training has been delivered to more than 100 middle/high school educators in Illinois.

• Our expectation is that you will use it in your classrooms and/or after school clubs to engage students in technology to solve problems

Think back to an earlier computer

What is the Raspberry Pi?

Description • Designed by Eben Upton with an

emphasis in computer programming. • Small computer, runs on Linux. • Very small power draw (1.21 Watts) • 40 GPIOs • 4 USB Ports • HDMI output • 3.5mm jack • Micro USB for power • Ethernet plug-in

Raspberry Pi B+

Plug it in!

Connections

• Plug in the HDMI cable / Pi-View

• Plug in the USB Devices • Keyboard • Mouse Wireless dongle

• Plug in the Ethernet cable.

• Plug in the power (last)

Should Look a bit like this

What you should see

What you are doing

• We have modified SD cards that have the Raspian Wheezy OS.

• It will load the Raspian image directly, and quickly.

• This is the operating system for the computer.

• Login = pi

• Password = raspberry

Look a little like this

Access the Graphical User Interface (GUI)

What you are doing

• At the command line type

startx

• This will start the graphical user interface for the RPi.

• Its like seeing the windows desktop.

Look a little like this

Play Around in the GUI

Goals

• Find the web browser

• Open Scratch

• Open Minecraft

• Find the wireless configuration

• Find the terminal

Play around and see what you find

Play Around in the Terminal

Terminal Commands Command Effect

pwd Shows you the present working directory

mkdir name Makes a directory

cd name Changes the directory

cd .. Moves back one level in the directory

cd ~ Moves to the home directory

ls Lists the contents of the directory.

sudo Super User Do Common Linux Commands

Make a Directory to Put Pictures In

Instructions

• Command

cd ~

mkdir camera

Reason

• We will plug in the camera and start taking pictures.

• We will have to tell the camera where those pictures should be stored.

• We will want to access those pictures from another computer.

File management – Prepare to take pictures • You may want to set up a folder to store the pictures you are taking.

• The goal is to take a picture and put it in file, that goes in to the command.

Command Effect Example

pwd Present working directory, where you currently are.

mkdir Make a new folder mkdir camera

cd Change directory cd camera

cd .. Return to previous directory cd ..

cd /home/pi/camera Go to a specific directory cd /home/pi/camera

ls List contents ls

Helpful Video for Plugging in the Camera

• Lift up the small black guide by the Ethernet port.

• The small electrical contacts on the ribbon face the end with the power cord.

• Avoid any kind of static or sparks.

• Plug in, turn on the Rasberry Pi after it is connected.

Configure the Camera

• You may need to configure the camera.

sudo raspi-config Select Enable Camera and then Finish. • You will be asked to reboot. Go

ahead. • This only needs to be done once.

• Camera Specs • 5 M Pixel • Video Mode – 1080p30, 720p60, and

640x480p60/90 • Full Frame SLR Lens Eq. – 35mm • Fixed Focus – 1m to infinity • F-Stop – 2.9 • Focal length – 3.60mm • Can do up to 90 frames/second video at

640x480 resolution.

Full Specs

Lets take a selfie.

• From the command line use this script

raspistill –o picture.jpg

• This tells the computer to use a program called raspistill and output (o) a file called picture.jpg

• With the default settings this will be a 2.4MB file.

Raspistill has a lot of customizable commands

Effect Command Default

Preview -p TRUE

No Preview -n FALSE

Sharpness -sh 0

Contrast -co 0

Brightness -br 0

Horizontal Flip -hf FALSE

Vertical Flip -vf FALSE

Shutter Speed -ss N/A Up to 6s

Sample commands

• Flips the image • raspistill –vf -hf –o picture.jpg

• Increase the contrast • raspistill –co 50 –o picture.jpg

Complete list

Raspistill Image Effects

Effect Command

Negative -ifx negative

Solarise -ifx solarize

Sketch -ifx sketch

Oilpaint -ifx oilpaint

Watercolour -ifx watercolour

Film -ifx film

Try various image effects.

Save each image separately.

Save over individual images.

raspistill –ifx negative –o picture.jpg

Complete list

How to do time lapse photos

1. Make sure you have a folder you want to save everything to.

2. Type the command

raspistill –o /home/pi/camera/picture%03d.jpg –tl 10000 –t 120000

Program Output

destination and file name.

3 digit number to appear after camera (001, 002, 003, etc)

Time lapse in milliseconds between pictures

Total time in milliseconds for all pictures

The End Product

Things to think about with time lapse projects

• What is something that will change slowly but dramatically? • Seedlings • Crystals • Bacterial growth • Decay

• How often will you need to take pictures? • How much space will you need for those pictures? • What can I control?

• Lights • Power

A python program for doing time lapse

• Open your web browser and visit the site below. Scroll down to the portion on time lapse. • https://www.raspberrypi.org/learning/python-picamera-setup/worksheet.md • Copy the program. • Open terminal

• cd camera • sudo nano camera.py (Makes a file called camera.py) • Right click will paste the program. • Control O saves the program. Control X exits the program.

• Run the program • sudo apt-get update • sudo apt-get install python-pip • sudo pip install –U RPi.GPIO

• sudo python camera.py

Make some changes to the program

• Video_Days = 1 • Frames_Per_Hour = • Frames = Frames_Per_Hour * 8 * Video_Days • Cam.capture(‘/home/pi/camera/frame%03d.jpg’ % frame)

• Run the program

• sudo python camera.py & • The & runs it in the background. You will see a job number.

• End the program • sudo kill job number

Schedule the program to begin when you want • sudo crontab –e • Opens a table of repeatable events for crontab to run. • Format

• * * * * * python /home/pi/camera/python.py • (min) (hour) (day of month) (month) (day of week) • 30 10 * * * python /home/pi/camera/python.py

• Will run this program every day at 10:30 • 30 14 13 * * python /home/pi/camera/python.py

• Will run this program at 14:30 (2:30) on the 13th day of every month. • 30 8 * * 0 python /home/pi/camera/python.py

• Will run this program at 8:30 every Sunday. • @reboot python /home/pi/camera/python.py

These pictures were taken with a Raspberry Pi using this Python program.

Camera Takes Video Too

Raspivid

• The camera can also take video.

• Use it to take slow motion video with a high frame rate.

• Up to 90 frames per second depending on the resolution.

Video Made in my Lab

How to do videos

1. Make sure you have a folder you want to save everything to.

2. Type the command

Raspivid –w 640 –h 480 –fps 90 –t 20000 –o /home/pi/camera/movie.h264

Program Width and height of the image

Length of video in milliseconds

Output location Frame speed

Now lets get those pictures/files

On your Raspberry Pi

• Determine the IP address

sudo ifconfig

On your preferred computer

• Make sure you are on the same network as the Raspberry Pi.

• Download WinSCP • This is a program that will enable

File Transfer Protocol between your computer and the Pi.

• Very user friendly interface.

Local computer on the left, remote computer (RPi) on the right.

Got a lot of photos, how do I make a time lapse movie?

Download Movie Maker Light, free video editing • Import all photos.

• Since they are ordered sequentially, they will be in time order.

• Select all photos. • Change the time to something

like 0.16 seconds per frame. • Export as a AVI or MP4 or… • Enjoy your movie!

Get clever

Use other things as lenses Other Hacks

• Loosen the lens to make a macro camera.

• Zoom in on things.

• Attach different lenses for panoramic pictures.

• Use the blue filter with the Pi NoIR camera to take infragram images.

Macro Lens and Infragram

Infragram Image – Raspberry Pi NoIR Macro Lens

Lots of ways to skin a cat.

Can I use Dropbox? • You can, and its pretty easy.

• Instructions • Requirements

• Dropbox account • Developer access

• Benefits • You can automatically upload files to

Dropbox. • You can have a Python program take

pictures and send them to Dropbox. • Check on your photos from

anywhere.

Can I use a Python program • You can, and build it to your

needs. • Instructions

• Requirements • A little programming experience,

but not much.

• Benefits • You can tell your computer to take

pictures in the way you want, when you want.

Potential Projects / Asks

• Make a Photobooth that will take a picture when a button is pushed, and email it to the user.

• Example • Take a picture of something at the same time everyday. • Start taking time lapse photos at the same time everyday and upload them

to Dropbox or a remote server at the end of the day. • Take video with the Raspberry Pi Camera

• Example • Control the Raspberry Pi Camera from another computer.

• Example • Control the Raspberry Pi Camera from your phone.

• Example

Raspberry Pi Training: Part II Make Your Own Security Camera Using the

Raspberry Pi Jonathan Ashdown, Ph.D. Abraham Michelen, Ph.D.

Michael Davis, Ph.D. Hi-TEC Preconference Workshop

July 27th, 2015

What You Will Learn

• After completing this project, you will be able to: – Set up and configure a Raspberry Pi

– Take pictures with the Pi Camera

– Connect and configure a Passive Infrared (PIR) sensor

– Write a basic Python program to trigger the camera after detecting movement using the PIR sensor

– Store the recorded video files

– Play the recorded video files back from the command line

What You Will Need

• Raspberry Pi, keyboard, mouse, and monitor:

What You Will Need

• To implement security camera using the Raspberry Pi, you will need the following (in addition to keyboard, mouse, and monitor):

– Raspberry Pi Camera Module

– Passive Infrared (PIR) Sensor

– Three Female to Female Jumper wires

Step 1: Connecting Camera To Pi

– Step-by-Step Instructions:

– Pull up on locking lever using thumb and index finger for the CSI Camera Connector (pictured above as a white tab to the right of HDMI port)

– Insert ribbon cable into slot with metal pins facing AWAY from ethernet port

– Press down on both sides of white locking lever to secure the connector in place

– Video Instructions: https://www.youtube.com/watch?v=DosQ0V39WAA

Step 2: Connecting PIR Sensor

– Connect VCC pin of PIR sensor to +5 V on Pi (use pin 2 pictured above)

– Connect GND pin of PIR sensor to GND on Pi (use pin 4 pictured above)

– Connect OUT pint of PIR sensor to GPIO 4 on Pi (Pin 7 pictured above) *Note: All three of the above connections use female-to-female jumper wire

Step 3: Update Packages

• Before moving forward, we need to update the packages to ensure we all have the latest, most up-to-date versions. To do this, follow these steps: – Press startx if in command line mode to enter graphical user interface

– Open up LXterminal

– Type the following line in Lxterminal exactly as it appears below:

sudo apt-get update && sudo apt-get upgrade

– These commands will download the latest packages and update the old packages with new packages

– Wait...(7 – 10 minutes)

Step 4: Enable Camera

– Open Lxterminal

– Type the following line in Lxterminal exactly as it appears below:

sudo raspi-config

– Use the down arrow on keyboard to choose setup option 5

– Press Enter

– Under “Enable Support for Raspberry Pi camera”?

– Select <Enable>

– Under setup options, Select <Finish>

– Under “Would you like to reboot now”? Select <Yes>

– Wait until Raspberry Pi reboots

• Next we need to make sure the camera is enabled. To do this, follow these steps:

Step 5: Taking Pictures

– Open Lxterminal

– Type the following line in Lxterminal exactly as it appears below:

raspistill –o image.jpg –t 3000

– The –t 3000 portion above is optional but basically functions as a self timer and delays the taking of the photo by 3000 milliseconds (or 3 seconds). You can change this delay to match your preference.

– Image will be stored on desktop

– Click on image file to view it

• To take still photos using the Raspberry Pi, follow these steps:

Step 6: Taking Videos

– Open Lxterminal

– Type the following line in Lxterminal exactly as it appears below:

raspivid –o video.h264 –t 10000

– The –t 10000 portion above will take a video for 10000 milliseconds (or 10 seconds). You can change the video length to match your preference.

– H264 is the video codec format

– Filename is video. You can change the filename to your preference.

– Ex: raspivid –o Jon.h264 –t 5000

– The above would make a 5 second video called Jon in the h264 video format

• To take videos using the Raspberry Pi, follow these steps:

Step 7: Video Playback

– Open Lxterminal

– Navigate to appropriate directory where video is stored using cd and ls commands

– Type the following line in Lxterminal exactly as it appears below:

omxplayer yourvideoname.h264

– Example: To play back my video named Jon.h264, I would type:

omxplayer Jon.h264

– Did your video play back ok!? If so, GREAT! If not, feel free to call over an instructor for some help.

• To play back videos using the Raspberry Pi, follow these steps:

Step 8: Testing the PIR Motion Sensor

– Note: This python program will be extended to include the camera later

– When PIR motion sensor detects movement, the sensor applies power to its OUT pin

– (recall from step 2 that OUT is connected to GPIO pin 4 on the Pi)

– Program breakdown

– Set up Raspberry Pi GPIO pins to allow us to use pin 4 as an input

– It can then detect when the PIR module sends power

– We need to monitor the status of this pin constantly for any changes and so will use a while True loop for this.

– This is an infinite loop so the program will run forever unless we stop it manually by typing Ctrl + C.

• We will use the Python programming language to write some basic code that will detect movement and print out some text.

Step 8: Testing the PIR Motion Sensor (Cont’d)

– Open up Lxterminal, and type the following line exactly as it appears below to create a new python (.py) file named pirtest:

nano pirtest.py

– Enter or copy and paste the following code exactly as it appears below:

– Press Ctrl + O to save and Ctrl + X to quit

Step 8: Testing the PIR Motion Sensor (Cont’d)

– Now run the Python file by entering the following line in LXterminal:

sudo python3 pirtest.py

– Wave hand in front of PIR sensor. What do you see?

– If you see something like the following then your PIR sensor and our program are working properly:

GPIO pin 4 is HIGH

GPIO pin 4 is LOW

GPIO pin 4 is HIGH

GPIO pin 4 is HIGH

– Press Ctrl + C when you want to exit

– Note: You can adjust the sensitivity and detection time of the PIR sensor using the two orange potentiometers and a phillips head screwdriver.

– Begin by setting the sesitivity to max and the time to min (feel free to vary this later).

Step 9: Program Camera to Trigger on Movement

– Make a copy of the preview program and we’ll use that for this step:

– Within Lxterminal, type the line below exactly:

cp pirtest.py pirCamera.py

– Now use the following command to edit the file:

nano pirCamera.py

– We need to add some lines of code to make the camera and sensor work together:

Step 9: Program Camera to Trigger on Movement (Cont’d)

– We need to add some lines of code to make the camera and sensor work together.

– Enter or copy and paste the lines of code below into your new file named pirCamera.py

3 new additions

(note: the hashtag symbol (#) is used for writing comments in Python)

– Press Ctrl + O to save and Ctrl + X to quit

Step 10: Recording to a File and Playing Back

– We need to add some more lines of code to make the camera and sensor work together to actually record video to the SD card of your Raspberry Pi.

– Within Lxterminal, type the following to edit your pirCamera.py file:

nano pirCamera.py

– Add the new lines of code shown

– Press Ctrl + O to save and Ctrl + X to quit

Step 10: Recording to a File and Playing Back(Cont’d)

– We are now ready to run our security camera program!

– Within Lxterminal, type the following line exactly:

sudo python3 pirCamera.py

– Wave your hand in front of your PIR sensor

– What happens?

– If you are hooked up to your monitor, you should see a short video record whenever your sensor detects motion!

– Press Ctrl + C to exit

– These files are stored on your desktop and are saved in the following format: MONTH-DAY_HOUR.MINUTE.SECOND.h264

– h264 is the format the video will be recorded in (Note: it’s the same format used by YouTube).

Step 10: Recording to a File and Playing Back(Cont’d)

Looks like somebody found the cookie jar… With some help from dad, of course!

Step 10: Recording to a File and Playing Back(Cont’d)

Filling up her pool on a hot day. Caught on camera!

Step 10: Recording to a File and Playing Back(Cont’d)

– To playback video, we will use omxplayer.

– Within Lxterminal, type the following line exactly:

omxplayer <file> -o hdmi

– For example,

omxplayer 2015-03-11_10.24.18.h264 –o hdmi

– Tip: Use the ls command to see what files have been recorded and are available for playback

Discussions/Conclusions

• In this workshop, you learned how to: – Set up and configure a Raspberry Pi – Take pictures with the Pi Camera – Connect and configure a Passive Infrared (PIR) sensor – Write basic Python programs to trigger the camera after detecting

movement using the PIR sensor – Store the recorded video files – Play the recorded video files back from the command line

• Question: How might you integrate this knowledge gleaned into the classroom or into your daily life?

Required Evaluation

• http://conta.cc/1LNVjD6

• Required to leave with the equipment.

• Complete before leaving.

Recommended