Overview of Part 5
Part 5 of this series is all about the code, and optimising the performance of our Pi.
If you’ve followed all the previous parts of this series, then so far, you’ll have:
- A Raspberry Pi, running Raspian (or an OS of your choice, as described within Part 2)
- The Pi will also be connected to following components;
- An arcade button, connected across GPIO21 and Ground.
- A camera module (i.e. PiCamera)
- An LED screen
- (Presumably, all of these components will now all be housed in a photo booth enclosure also)
For the remaining steps we can either;
- Connect a mouse and keyboard to the Pi, or
- We can remotely connect to the Raspberry Pi (via the steps described in Part 2).
Connecting to the Pi remotely has the advantage of giving you a larger screen, and is the approach I would recommend.
Downloading the code
As we did in Part 2, open your
terminal application on the Pi, and enter the following commands:
The above will create a new directory (
/home/pi/photo-booth/), and then download the Raspberry Pi Photo Booth code, for you to use.
Getting the code to run
Now that the code has downloaded, we can open up Python and run the Photo Booth code.
One method of doing this, is opening the Applications menu, and selecting
Python 3 (IDLE).
- My code is also compatible with
Python 2, but
Python 3is currently recommended.
Within the Python GUI, you can then the code we downloaded via
/home/pi/photo-booth, locate and open
We can then run the code via
Examining the code
Looking under the hood
In case you are curious, why not have a look at the code within the python file
camera.py? I’ll try to explain the various parts as they appear in the file.
We start off with the file header, which tells us that this is an app written in the
Python programming language:
Then we have our import statements. These statements used to include some specific Python libraries into our app:
These libraries provide simple mechanisms for dealing with images, timestamps, the Pi’s GPIO pins, and other complex features. This means our code doesn’t need to consider all of the intricacies that are associated with such things, and that saves us a bunch of time.
Further down the file, are a set of variables that will be used by our code:
You can modify any of the values above, to adjust the behaviour of the photo booth.
Additional configuration applies when initialising the camera:
Per the above, my camera is mounted with a rotation of 270°, but if your camera is mounted at a different angle then you will need to adjust this value.
The main loop
After defining our configuration options, there is a number of “helper functions” defined. It’s not super important to understand how these work.
Finally, the file defines the
main function, which is effectively the “start” of the program.
The first bit loads up some “intro” images, which we alternate between whilst waiting for a user to push the button:
The logic for alternating between the images looks like this:
Once the button is pressed, we:
- Generate a filename for our images (which is based on the current date + time).
- We are going to take 4 photos, and for each photo there is a “prep for photo” screen.
- Taking all four photos, we display a “playback” animation, of each photo.
- After the playback animation, the app restarts at the beginning (waiting for the button to be pressed again)
The photo booth app has more lines of code than makes sense to explore within this blog post. However, if you’ve followed along this far, feel free to explore these functions further.
(And if you’re feeling super excited, perhaps you might even want to code up some additional features of your own?).
For the next article in this series, I’ll be talking about further optimisations to our Photo Booth’s software, namely:
- Replacing the app’s stock images with your own custom ones,
- Running the photo booth code as soon as the Pi starts up.
Subscribe to my blog to stay informed of my progress.