There’s nothing quite like the power of real, first-person video. If you’re at home playing with your children or pets, wearing a body camera lets you save those special memories, without sticking your face in your phone. And, if you’re attending a protest and want to capture malfeasance on anyone’s part, it also fits the bill.
While you could go out of your way to buy a dedicated body camera, it’s easy enough to build one using a Raspberry Pi Zero W and a few inexpensive parts. Note that, if you are going to a protest, you’ll want to make your setup as unobtrusive and non-threatening as possible. If you have wires and duct tape hanging out of your jacket, people could get the wrong impression. The photos in this article show our best take on how to make the setup look like the body camera that it is, but you may be able to make it more compact and less scary looking.
This project will record your daily life at the push of a button. It will be constantly recording in two minute chunks, and when triggered it will save the video to disk, before uploading for safe keeping to a private Dropbox folder. So, even if your camera breaks or is confiscated, your video will be available in the cloud. From Dropbox, you can edit and share your videos as necessary.
What You’ll Need to Build a Raspberry Pi Body Camera
- A Raspberry Pi Zero W
- A microSD Card with Raspberry Pi OS on it
- Raspberry Pi Zero official case
- Raspberry Pi Camera Module. Note that Pi Zeros use a narrower camera cable than other Pis.
- Female to female jumper wires
- A USB power bank
- Soldering iron or Pimoroni Hammerhead to attach GPIO pins to Pi Zero W
- One momentary push button
- A mobile hotspot or phone that goes into hotspot mode
- Double sided tape and sticky tape
Hardware Setup of Raspberry Pi Body Cam
1. Attach two wires to GPIO 16 and GND on the Raspberry Pi Zero W. You can either solder the wires on or attach GPIO pins to the Pi Zero W, either by soldering them on or using a Pimoroni Hammer Header. Pre-soldered Raspberry Pi Zero Ws, known as the Zero WH, are also available for sale.
Using 2x female to female jumper wires and a 2 pin push button connect GPIO16 and GND to the button.
2. Use a little tape or heat shrink tubing to tidy up the connection. Wrap the tape so that the button and the wires are secure.
3. Insert the camera module to the Raspberry Pi Zero W Camera slot using the adapter. Ensure that the cable is inserted correctly and locked into place. If using the official Raspberry Pi Zero W case, feed the ribbon cable through the slot on the underside of the case.
Software Setup of Raspberry Pi Body Camera
4. Boot your Raspberry Pi Zero W with peripherals connected. If you don’t already have a microSD card see our article on how to set up a Raspberry Pi for the first time.
5. Turn on your mobile hotspot, or set your cell phone to act as an access point. Make a note of the access point name and the password.
6. Connect your Raspberry Pi to the hotspot via Wi-Fi.
7. Enable the camera module in Raspberry Pi OS. You can do that, by going to Preferences >> Raspberry Pi Configuration, clicking on the Interfaces tab and ensuring that.the Camera interface is enabled. Reboot the Pi for this to take effect.
8. Install the Dropbox library for Python 3 by entering the following at the command prompt.
sudo pip3 install dropbox
9. Navigate to Dropbox Developers www.dropbox.com/developers in your browser and Click on Create Apps to sign in. If you don’t already have a Dropbox account, register for one (2GB Basic Plan is free) and then return to this page.
10. Select the Dropbox API and App Folder access. Then name the app, which creates a folder to store the video files and click Create App.
A new screen appears.
11. Click Generate under Generated access token then copy this token into a spare text document for later reference.
Coding a Python Script for Raspberry Pi Body Camera
12. Open the Geany IDE, found in the Raspberry Pi Programming menu.
13. Enter the code as described below. The first line of Python is the location of the Python3 interpreter, this is needed to make the code executable from the terminal.
14. Import the following libraries to add support for basic IO, a push button, the camera, date / time and Dropbox.
import io from gpiozero import Button import picamera from datetime import datetime import dropbox
15. Paste the Dropbox token into the code, and then assign it to a variable called “token”. Then a connection to Dropbox is made using the token for authentication.
token = "DROPBOX TOKEN HERE" dbx = dropbox.Dropbox(token)
16. Create an object, “trigger” that will be used to link the button connected to GPIO 16 to the code.
trigger = Button(16)
17. Create a connection to the Raspberry Pi Camera, then set the resolution to 720p.
camera = picamera.PiCamera() camera.resolution = (1280, 720)
18. Create a recording stream. A stream is used to create a circular buffer that will record events before the button is pressed. Here the camera is instructed to record 2 minutes before activation. The stream is recorded to an h264 file.
stream = picamera.PiCameraCircularIO(camera, seconds=120) camera.start_recording(stream, format='h264')
19. Add a try statement which will attempt to run the code indented within it, in this case it will start with a loop to constantly run the code within.
try: while True:
20. Create a delay, forcing the camera to wait for one second each time the loop iterates.
21. Create an if condition for the trigger and set the current date and time, used later as a timestamp. When the user presses the button, this if condition will start the process of capturing video.
if trigger.is_pressed: now = datetime.now()
22. Print a message to the terminal to show that the recorder has been triggered and set the recording time to 120 seconds.
23. Add this code to save the video stream to a file, which uses the timestamp as the filename, and appends the file format to create a valid file. Another message is printed to advise the user.
stream.copy_to(str(now)+'.h264') print("Stream saved to disk...will upload to Dropbox")
24. Add this code to enable the script to open the freshly created video file in read only mode and upload it to Dropbox, overwriting any files that share the same name. A message is printed to the Terminal when this is complete.
with open(str(now)+'.h264',"rb") as data: dbx.files_upload(data.read(),'/'+str(now)+'.h264',mute=True, mode=dropbox.files.WriteMode.overwrite) print("File uploaded to Dropbox")
25. Create these last two lines of code to stop the camera recording when the user stops the code from running.
26. Save the script as video-recorder.py and then close Geany.