Hey everyone, it’s Frenoy! We are going
to start this new year, by learning how to
build a dashcam using the raspberry pi zero.
So let’s dive in
What we are actually building is a system
that can record video continuously, a dashcam
is just one application for this.
The entire process can be broken down into
several modules. The first being the video
recording section which involves capturing
video from the pi camera and storing it to
the microSD card. This also involves taking
care of file numbering, detecting the remaining
storage and using FTP to obtain the video
files.
The next module involves adding additional
information like date, time and so on as an
overlay on the video. We will be using a GPS
module for this which will also give us the
location. In addition to that, we will add
some system parameters like CPU usage for
instance. We will then try to make it a bit
easier to access the video files, perhaps
by using a web browser or something similar.
Once all of this is done, we will look at
enhancements, improving reliability, managing
files and so on. We will then add a safe shutdown
feature that will include some sort of battery
and finally, we will automate everything,
add a case and carry out some road tests.
We will be dealing with the first module in
this video. Future videos will cover the remaining
modules. As always, the video description
will contain links to written posts on instructables,
hackster and our website as well and you can
use them to build this project.
Just a note before we continue: Since I currently
upload 3 videos a week, I decided to keep
this dashcam series for Thursday and we will
start with different videos on Sunday and
Tuesday. This way, if you have no interest
in building a dashcam, then hopefully you
will like the other videos that will be released.
Let’s now start with the build. The first
step is to download the Raspbian image for
the raspberry pi. We do not need the desktop
for this project so I will be using the lite
version. The next step is flashing this to
the microSD card using etcher. Once done,
you need to copy the wpa_config file which
will enable the pi zero to connect to the
WiFi network and the ssh file which will allow
you to remotely control the pi. There’s
a separate video that explains why this is
useful and you can watch that if you are not
familiar with this step.
Once done, you simply need to connect the
camera to the board, insert the microSD card
and power it ON. After a minute, the board
will connect to the network and we can use
angry IP scanner to obtain it’s IP address.
We can then open the command prompt window
and log into the board by using SSH. The first
step is to enable the camera and this can
be done by running the raspi-config command.
Once done, you need to wait for the board
to reboot and then log in again. We then need
to update the rasbian operating system by
running the following command. Finally, we
need to install proftpd so that we can access
the files using filezilla like so.
Now that we’ve taken care of this, we can
work on the script. The first thing we need
to do is test to see if the camera is working.
This can be done by running the following
command which will capture an image. You can
run the ls command to verify if the file has
been created. We can then transfer this to
our computer and check to see if it is OK.
This means that the camera has been detected
and is working as expected.
The previous projects used script files that
had a .sh extension which meant that they
were bash scripts. You might have heard about
something called python scripts and that is
what we will be using in this project as it
gives us access to a lot of modules that can
be used to simplify our scripts. Think of
python modules are being similar to the libraries
that we use when creating Arduino projects.Of
course, we would have to install some of them
in order to use them. We will start by installing
picamera module which allows us to use the
raspberry pi camera in our python scripts.
This can be done by running the following
command.
Once completed, we simply create a new script
file called dashcam.py and then type in the
following. If you have not worked with python,
then this might seem a little complicated
at first, but it is actually very simple.
We first need to import the picamera module
as we will be using it. The next line is similar
to creating an object in C++ or when using
an Arduino. We then set the resolution to
1920x1080 and the framerate to 30fps. You
can change these to suit your requirements,
but I prefer starting with these values. The
script then creates a file and starts recording
video to it. H264 is a video compression codec
so don’t worry about it as the files can
be played back using VLC. The wait_recording
statement acts like a 5 second delay. This
causes a 5 second video to be recorded, before
the stop_recording command is called. You
can update the recording duration as needed.
Once done, save the file by typing CTRL+X,
Y and then ENTER. The script can be executed
by running python dashcam.py. And if you refresh
FileZilla then you will be able to see the
new video file that was created. You can then
transfer this to your computer, and view it
using VLC.
Now that we know how to record video using
a python script, the next thing is learning
how to use the record_sequence command which
is used to record continuously while also
creating multiple video files. Here’s what
the updated script looks like and we’ve
mainly updated the last section. First lets
talk about the file name. This section tells
the system to add a number to the file name,
which has 3 digits and leading zeros. This
means that the first file will be video000,
then video001 depending on the value of the
variable i. The value of i can range from
0 to 2 that’s a total of 3 different values.
The wait_recording statement determines the
recording time. If we run the script now,
then it will record 3 video files each having
a duration of 5 seconds as can be seen here.
If you refresh filezilla then you will be
able to see these files being created. This
script has one major issue, it overwrites
existing files so if we run it again, then
it will overwrite the previous video files
which is not what we want.
In the final script for this video, we will
update the script to prevent overwriting existing
files, we will increase the maximum number
of files created to 999 instead of 3 and we
will also check the storage being used and
stop the script when it runs low.
Here’s what the final script looks like
and we will look at all the sections one at
a time. I decided to create a file_root variable
which specifies the video file location. This
makes it easier to update the location if
needed. This section is similar to before
and hasn’t changed. In order to avoid overwriting
existing files, we need to check to see which
files are currently present and then start
with the next file name. All we are doing
here is creating different file names from
1 to 999 and checking to see if each file
exists. If it exists then we increment the
number and check again until we find a file_name
that doesn’t exist. We can then use this
file name to create a new file. We use the
functions from the os python module to check
if a file exists and for this to work, we
need to import the os module. We also define
a variable called MAX_FILES which limits the
total number of files that can be created.
999 is a pretty large number and you will
most likely run out of storage space before
you reach it.
The next section is similar to the previous
script. We simply update the range to start
from this new file_number that we obtained
previously, as we know that it doesn’t exist.
The recording duration has also been updated
to 1 minute but can you can change this by
simply updating this variable.
The final thing we need to add in the script
is some sort of memory or storage check and
this is what this last section does. We use
the psutil module to obtain system information.
In particular the disk usage which reported
as a percentage. You can read about the documentation
for this to learn more and you can even obtain
the actual usage if needed. I have set the
space limit to 80 and this section checks
to see if more than 80% of the storage has
been used. If it has, it then prints out a
warning and stops recording. Because this
check is located here, it will only come in
effect once recording has already started.
That is why we place a similar check at the
start of the script and if we are running
out of space then we simply print a warning
to the terminal and exit the script.
There are a few things we need to do before
we can run this script. The psutil library
needs to be installed and inorder to do this,
we first need to install something called
pip which is a package installer for python.
It is very useful and makes installing libraries
extremely simple. Pip can be installed by
running this command. Once done, we can install
psutil by running pip install psutil. Once
that is done, we simply create the videos
folder and we can then run the script. It
will record the videos as expected.
Before we conclude part 1 of this series,
let’s quickly test to make sure the storage
check works. This can be done by changing
the SPACE limit to 5, which will trigger the
warning if more than 5% of the storage has
been used. We simply save the file and run
the script which should give us a warning
like so. We can then increase the limit to
80% and continue like before. All files can
be accessed using Filezilla and these can
be played back using VLC.
So that’s part 1 of this series. We will
continue with this next Thursday so make sure
you’re subscribed to get notified. We will
start with image processing using OpenCV on
Sunday’s video. Thank you for watching and
I will see you in the next one.
