11. PiCamera
20 Jun 2017reference
- getting started with pi camera
- pi camera docs
- Andrea Fabrizi's dropbox uploader
- GPAC
equipment
- pi camera
- pi camera adapter (if using the pi zero w)
setup
- run the setup script:
sudo raspi-config
- Enable the camera in
Interfacing Options
- Locate the socket that the pi camera plugs into on your raspberry pi. Note that there are two similar looking ribbon sockets, one of which is labeled
CAMERA
. Plug the camera in (the blue tape material poking out of the socket should face towards the headphone jack on the pi) - Install the Python3 library for PiCamera:
sudo apt-get install python3-picamera
simple photo
Here one can find the code to take a single photo from Python3 with the PiCamera. A few quick notes:
- We import the
datetime
module and usedatetime.now().strftime('%Y-%m-%d-%H-%m-%s')
to give each photo a unique filename (down to the second). This both minimizes the likelihood of accidentally saving over photos and provides a way to more easily distinguish between photos without looking at them - According to the PiCamera docs one is supposed to use
camera.start_preview()
, followed by asleep
statement, to allow the camera to calibrate/focus (the docs say the camera needs to sleep for at least 2 seconds after.start_preview()
) camera.capture()
stores and saves the photo, so this is also where we specify the full filepath (including thedatetime
information)camera.stop_preview()
andcamera.close()
are used to cleanup what was setup to take a photo
viewing photos
camera.start_preview()
will show the camera feed on any display connected to the pi. When running headless, though, we have two options for viewing photos:
scp
the file from our Pi to our Macbook- setup a way to upload photos from our Pi to a shared cloud service (like Dropbox).
viewing photos (scp)
- on your raspberry pi
cd
to wherever the script saved your photo - back on your Macbook, run
ifconfig
in the terminal to get your Macbook's IP address - back on your Pi, use
scp
to send the file to your home directory on your Macbook. The command should look something like this:scp filename.jpg macbookUsername@macbookIPAddress:Path/To/Home
. Once you have formatted this correctly hit [ENTER] - you should be prompted to enter the login password for
macbookUsername
before the file is copied over. After typing it in hit - back on your Macbook, navigate to the home directory and double click on the image to view it in Preview (or whatever)
viewing photos (dropbox sync)
This method relies on Andrea Fabrizi's dropbox uploader.
- on your Pi run the following command to clone the dropbox uploader repo:
git clone https://github.com/andreafabrizi/Dropbox-Uploader.git
- next on your Macbook navigate to dropbox and, if you do not already have one, set up a free account.
- once you have a dropbox account setup, go here to setup an application to interact with dropbox
- this next part will probably change from time-to-time, but as of 4/18/2017 the process was as follows:
- click the
Create app
button - select “Dropbox API” and “Full Dropbox,” give your app a unique name (example: yournamecameraapp) then confirm by clicking
Create the app
- finally, in the settings tab there is a button that reads
Generate access token
. Click this button and then copy the string of seemingly random letters and numbers (referred to as a token) to your clipboard. This string is how dropbox will identify your raspberry pi
- click the
- back on the raspberry pi
cd
into theDropbox-Uploader
folder and makedropbox_uploader.sh
executable by running the following:sudo chmod +x dropbox_uploader.sh
- next run the
bash
script:./dropbox_uploader.sh
- you will be prompted to enter your access token, which should still be in your clipboard, so paste (
) it into the terminal and type y
to confirm - try uploading the
README.md
file in theDropbox-Uploader
folder to dropbox to confirm functionality:./dropbox_uploader upload README.md /
- If successful, you should see something like the following in the terminal:
> Uploading "/home/cta/Dropbox-Uploader/README.md" to "/README.md"... DONE
- back on your Macbook navigate to dropbox to confirm that
README.md
is there
To upload photos simply edit this command to suit your needs: ./dropbox_uploader.sh upload fileorfoldertoupload /
. Notes to keep in mind:
- you have to be in the
Dropbox-Uploader
directory for this command to work - that the trailing
/
is an important part of this command - this command works for single files as well as a directory full of files
simple video
Here one can find the code to take a video from Python3 with the PiCamera. A few quick notes:
- we allow the camera to calibrate for 2 seconds after calling
camera.start_preview()
, which gives the camera time to focus camera.capture()
is replaced here withcamera.start_recording()
, which will record video until we issue the stop command (camera.stop_recording()
). Note that thedatetime
line from simple_photo is reused verbatim here to avoid overwriting files/provide a timeline via filename- the
sleep
command controls how long the video is, so recording video longer than five seconds is simply a matter of increasing the sleep time to the desired duration
converting video
Note: mad props to this article for the conversion process.
simple_video saves video as Raw H264 Video Data, so there are some extra steps to take prior to being able to view it without plugging a display into our Pi:
- Download and install
gpac
:sudo apt-get install -y gpac
cd
to the directory where your video lives and, usingMP4Box
(installed withgpac
), convert your video to.mp4
by editing (to suit your needs) and then execute this command:MP4Box -fps 30 -add myvid.h264 myvid.mp4
- once the video has been converted, delete the original:
rm mywid.h264
- finally
cd
to theDropbox-Uploader
folder, edit (to suit your needs) and then execute the following command:./dropbox_uploader.sh upload ../myvid.mp4 /
- back on your Macbook navigate to your dropbox folder and double click the
.mp4
to watch