Raspberry Pi AI Kit Beginner Guide
Raspberry Pi AI Kit Beginner Guide
books books
books
Two fully tested and working projects are given in the book. The first
project explains how a person can be detected and how an LED can be
activated after the detection, and how the detection can be acknowledged
by pressing an external button. The second project illustrates how a person
can be detected, and how this information can be passed to a smart phone
over a Wi-Fi link, as well as how the detection can be acknowledged by
sending a message from the smartphone to your Raspberry Pi 5.
Dogan Ibrahim
Raspberry Pi AI Kit
A Beginner's Guide to AI
and Edge Computing
●
Dogan Ibrahim
● All rights reserved. No part of this book may be reproduced in any material form, including photocopying, or
storing in any medium by electronic means and whether or not transiently or incidentally to some other use of this
publication, without the written permission of the copyright holder except in accordance with the provisions of the
Copyright Designs and Patents Act 1988 or under the terms of a licence issued by the Copyright Licencing Agency
Ltd., 90 Tottenham Court Road, London, England W1P 9HE. Applications for the copyright holder's permission to
reproduce any part of the publication should be addressed to the publishers.
● Declaration
The authors and publisher have used their best efforts in ensuring the correctness of the information contained in
this book. They do not assume, and hereby disclaim, any liability to any party for any loss or damage caused by
errors or omissions in this book, whether such errors or omissions result from negligence, accident or any other
cause.
Elektor is the world's leading source of essential technical information and electronics products for pro engineers,
electronics designers, and the companies seeking to engage them. Each day, our international team develops and delivers
high-quality content - via a variety of media channels (including magazines, video, digital media, and social media) in
several languages - relating to electronics design and DIY electronics. [Link]
●4
Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.5 Benchmarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Chapter 2 • I
nstalling the Raspberry Pi 5 Operating System and
Console Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.4 Accessing your Raspberry Pi 5 Console from your PC – the Putty program . . . . . . . 19
2.9 Installing the Raspberry Pi 5 Bookworm operating system on a blank microSD card . 28
2.10.5 Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
●5
2.10.12 Networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
3.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
4.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
6.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
7.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
●6
7.4 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
7.8 Indentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
7.10 Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
7.11 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
7.22 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
●7
8.4 Project 1 – Detect person presence using the AI Kit – LED output . . . . . . . . . . . 118
8.5 Project 2 – Detect person presence using the AI Kit – WiFi based . . . . . . . . . . . . 120
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
●8
Artificial Intelligence (AI) is now part of our lives. Most microcontroller based systems are
expected to include some form of AI in their future designs. In the past, AI was implemented
using rather expensive and fast parallel computer structures and this technology was not
available to general users. With companies developing low-cost AI powered hardware
into their products, it is now becoming a reality to purchase AI accelerator hardware at
comparatively very low costs. One such hardware accelerator is the Hailo module. The good
news is that recently this module has been compatible with the Raspberry Pi 5 as it requires
the PCIe hardware support.
The Raspberry Pi AI Kit is a cleverly designed hardware as it bundles an M.2 based Hailo-
8L accelerator with the Raspberry Pi M.2 HAT+ to offer high speed inferencing on the
Raspberry Pi 5.
Using the Raspberry Pi AI Kit you can build complex AI based vision applications, running
in real time with low power requirements. Examples include object detection, pose
estimation, instance segmentation, home automation, security, robotics, and many more
neural network based applications.
The AI Kit offers 13 tera operations per second (TOPS) neural network based inference
accelerator built around the Hailo08L chip. The Raspberry Pi 5 automatically detects the
installed Hailo module and makes the NPU available for AI based applications.
The AI Kit hardware fully integrated with the Raspberry Pi 5 board and it is supplied with
the following parts:
This book is an introduction to the Raspberry Pi AI Kit, and it is aimed to provide some help
to readers who are new to the kit and who want to run some simple AI based visual models
on their Raspberry Pi 5 computers. The book is not meant to cover the detailed process of
model creation and compilation, which is done on an Ubuntu computer having a large disk
●9
space and 32 GB memory. These are covered in detail with examples at the web sites of
Hailo.
Learning to use the AI software takes a bit of learning, but it is expected that this may
change as the time goes on and as the Hailo software is more integrated into the Raspberry
Pi 5 operating system.
Two fully tested and working projects are given in the book. The first project explains how
a person can be detected and how an LED can be activated after the detection, and how
the detection can be acknowledged by pressing an external button. The second project
illustrates how a person can be detected, and how this information can be passed to a
smart phone over a WiFi link, and how the detection can be acknowledged by sending a
message from the smart phone to Raspberry Pi 5.
The AI Kit software is new and it is currently under development and changes. The book
presents the currently available software features of the kit.
The author hopes that the book becomes useful to those readers seeking to understand
how to install and use the AI Kit.
● 10
1.1 Overview
Raspberry Pi 5 is the latest credit card size computer from the Raspberry Pi Foundation.
In this Chapter we will look at the specifications of this new computer and compare it with
the Raspberry Pi 4.
Raspberry Pi 5 is based on 2.4GHz Cortex-A76 Arm processor with a new Southgate bridge
for handling the peripheral interface. A new VideoCore VII GPU is provided with 800MHz
speed. Dual camera interface is another nice feature of the Raspberry Pi 5. Another major
improvement is the microSD card slot with support for high-speed SDR104 mode. In our
testing, the SD card speeds were twice as fast as the last generation. This is thanks to the
new RP1 southbridge developed by Raspberry Pi.
Table 1.1 shows a comparison of Raspberry Pi 4 and 5. Notice that both devices have dual 2 x
4Kp60 HDMI display interfaces although Pi 5 supports HDR output. The new Raspberry Pi 5
rocks DDR4X RAM instead of the DDR4 RAM used before. This enables the RPi 5 to have
improved clock speeds with less power consumption, which is a step in the right direction.
The RPi 5 can perform close to 30,000 MB writes and reads per second as compared to the
Raspberry Pi 4, which can only do 4,000 to 6,000. 2 x 20 pin GPIO interface is the same in
both devices. Raspberry Pi 5 additionally supports 2 camera interfaces, PCIe bus connector,
UART interface, RTC clock power connector, and fan power connector. Wi-Fi and Bluetooth
are supported by both devices. The on-board power switch on Pi 5 is a useful addition and
was requested by many users. Pi 5 is powered from 5V 4A USB-C type power supply where
Pi 4 is powered from a 3A power supply. Pi 5 is slightly more expensive than Pi 4.
● 11
Raspberry Pi 4 Raspberry Pi 5
Display 500 MHz VideoCore Vi GPU 800 MHz VideoCore VII GPU
L2 Cache 1 MB (shared) 2 MB
There are 2 micro HDMI based monitor ports on both devices, with both having the same
specifications.
The Ethernet port and USB ports are swapped. As a result of this the Raspberry Pi 4 case
is not compatible with the Pi 5 case and a new case is required.
The camera and display connectors on Raspberry Pi 5 are 15-pin and smaller instead of
the original 22-pin connector used on Pi 4. A ribbon cable with 22-pin on one side and
15-pin on the other side is required to connect an existing Raspberry Pi 4 camera to
Raspberry Pi 5. Raspberry Pi 5 has two connectors, allowing two cameras or DSI displays
(or a mix of either) to be connected. The PCIe connector is for fast external PCIe compatible
peripherals, such as SSDs.
● 12
The new power button on Raspberry Pi 5 could be very useful. When the device is On,
pressing the button brings the shutdown (logout) menu. A safe shutdown will occur with
another press of the power button.
Figure 1.1 shows the front view of the Raspberry Pi 5 with the components labelled for
reference.
While the Raspberry Pi 4 and 5 share a similar form factor (e.g., same size: 85mm x
56mm), the Pi 5 has many subtle differences that can be seen at first glance. For example,
the USB and Ethernet ports are in reverse order. A Raspberry Pi 5 looks more like a Pi 3B+
from this angle (see Figure 1.2).
● 13
The active cooler cools down the SoC, RAM, and the Southgate chipset. Without any cooling,
the Raspberry Pi 5 idle temperature is about 65ºC. With the cooler, when the temperature
is 60ºC the blower's fan turns on. At 67.5ºC the fan speed is increased, and at 75ºC the fan
speed increases to its maximum. When the temperature drops, the fan speed also drops
automatically. You can mount a HAT above the Active Cooler using a set of 16mm GPIO
extenders. Inevitably there is some disruption to the air flow which will cause the Raspberry
Pi to run hotter, but the Active Cooler is still able to handle extended stress tests without
significant temperature rises.
Another major software change in Bookworm is that the [Link] (created by Ben Croston)
was not available at the time of writing this book. As a result of this, all of the GPIO based
Python programs in the book have been developed using the gpiozero. Most third-party
● 14
HATs are based on the [Link] and these will not work until their software is changed by
their manufacturers. It is hoped that the manufacturers will change their software by the
time Raspberry Pi 5 becomes officially widely available.
ALSA was the original audio interface on Raspberry Pi computers. PulseAudio then has
replaced the ALSA interface which was still sitting on top of the low-level ALSA interface. This
made it easier to manage things like Bluetooth audio devices, allowed audio from multiple
applications to be mixed together, and allowed audio to easily be switched between outputs
while playing. In Bookworm, PulseAudio has been replaced with the newer PipeWire audio
system. This new system manages audio applications, remember the ones used at power-
down, and automatically connects them at reboot time. From a user point of view, both the
PulseAudio and PipeWire look identical.
1.5 Benchmarks
Figure 1.4 and Figure 1.5 show the overall benchmark scores of the Raspberry Pi 5
and Raspberry Pi 4 with both having 8 GB of memory, which clearly indicate that the
Raspberry Pi 5 is two to three times faster than its predecessor in all types of tests. In
these tests, same SD card, same power supply, and same cooling environments were used.
The ncnn framework (neural network models) tests from Seeed Studio show in Figure
1.6 the power of the Raspberry Pi 5 compared to Raspberry Pi 4. Note here that smaller
numbers are better.
● 15
● 16
Chapter 2 • I
nstalling the Raspberry Pi 5 Operating
System and Console Commands
2.1 Overview
Raspberry Pi 5 operating system Bookworm is available either on a pre-installed micro
SD card, or you can download the operating system image on a blank micro SD card. In
this Chapter you will learn to install the operating system using both methods. Additionally,
some commonly used console commands are described briefly in this chapter.
• You should see the Raspberry Pi 5 booting the first time and asking you various
questions to setup the device, such as the username, password, WiFi name and
password, any updates if necessary, etc. etc. In this book the username is set
to pi.
● 17
• The Raspberry Pi will boot in Desktop mode and will display the default screen.
You can press Cntrl+Alt+F1 at any time to change to the Console mode
● 18
● 19
• Click OK
You will now have to install a terminal emulation software on your PC. The one used by the
author is the popular Putty. Download Putty from the following web site:
[Link]
• Putty is a standalone program and there is no need to install it. Simply double
click to run it. You should see the Putty startup screen as in Figure 2.7.
• Make sure that the Connection type is SSH and enter the IP address of your
Raspberry Pi 5. You can obtain the IP address by entering the command
ifconfig in Console mode (see Figure 2.8). In this example the IP address was:
[Link] (see under wlan0:)
● 20
• Click Open in Putty after entering the IP address and selecting SSH
• First time you run Putty you may get a security message. Click Yes to accept
this security alert.
• You will then be prompted to enter the Raspberry Pi 5 username and password.
You can now enter all Console based commands through your PC.
pi@raspberrypi: ~ $ passwd
• To shut down the Raspberry Pi 5 enter the following command. Never shutdown
by pulling the power cable as this may result in the corruption or loss of files:
• Restart Putty
● 21
• Set the Default Foreground and Default Bold Foreground colours to black
(Red:0, Green:0, Blue:0)
• Set the Cursor Text and Cursor Colour to black (Red:0, Green:0, Blue:0)
• Select Appearance under Window and click Change in Font settings. Set
the font to Bold 12.
• Select Session and give a name to the session (e.g. MyZero) and click Save.
• Click Open to open the Putty session with the saved configuration
• Next time you re-start the Putty, select the saved session and click Load
followed by Open to start a session with the saved configuration
pi$raspberrypi:~ $ tightvncserver
You will be prompted to create a password for remotely accessing the Raspberry Pi desktop.
You can also set-up an optional read-only password. The password should be entered every
time you want to access the Pi 5 remotely. Enter a password and remember your password.
pi$raspberrypi:~ $ vncserver :1
● 22
You can optionally specify screen pixel size and colour depth in bits as follows:
• You must now setup a VNC viewer on your laptop (or desktop) PC. There are
many VNC viewers available, but the recommended one is the TightVNC for
the PC, which can be downloaded from the following link:
[Link]
• Download and install the TightVNC software on your PC. You will have to
choose a password during the installation.
• Enter the password you have chosen earlier. You should now see the Raspberry
Pi 5 Desktop image displayed on your PC screen (Figure 2.10, the image
displayed on your PC may be different)
● 23
• The VNC Viewer is now running on your Raspberry Pi 5 and you have access to
its Desktop GUI from your PC.
In this section, you will learn how to fix your IP address so that it does not change between
reboots. The steps are as follows:
● 24
• Find the IP address of your router by entering the command ip r (Figure 2.12).
In this example the IP address was: [Link]
• Find the IP address of your DNS by entering the following command (Figure
2.13). This is usually same as your router address:
• Add the following lines to the bottom of the file (these will be different for your
router). If these lines already exist, remove the comment character # at the
beginning of the lines and change the lines as follows (you may notice that
eth0 for Ethernet is listed):
interface wlan0
static_routers=[Link]
static domain_name_servers=[Link]
static ip_address=[Link]/24
• Save the file by entering CNTRL + X followed by Y and reboot your Raspberry
Pi 5
• In this example, the Raspberry Pi 5 should reboot with the static IP address:
[Link]
● 25
• Click on the Bluetooth icon on your Raspberry Pi 5 at the top right hand side,
and select Make Discoverable. You should see the Bluetooth icon flashing
• You should now see the message Connected Successfully on your Raspberry
Pi 5 and you can exchange files between your other device and the Raspberry
Pi computer.
Step 1: Connect a network cable between your Raspberry Pi 5 and your Wi-Fi router.
Step 2: Connect keyboard, mouse and monitor to your Raspberry Pi and power up as
normal
Step 4: Providing your network hub supports DHCP (nearly all network routers support
DHCP), you will be connected automatically to the network and will be assigned a unique
IP address within your network. Note that DHCP assigns IP addresses to newly connected
devices.
Step 5: Check to find out the IP address assigned to your Raspberry Pi 5 by the network
router. Enter command ifconfig as described earlier
In most cases it is very unlikely that the network cable is faulty. Also, most network hubs
support the DHCP protocol. If you are having problems with the network it is possible that
your Raspberry Pi is not configured to accept DHCP issued addresses. The Raspberry Pi is
normally configured to accept DHCP addresses but it is possible that you have changed the
configuration somehow.
● 26
To resolve the wired network connectivity problem follow the steps given below:
Step 1: find out whether your Raspberry Pi 5 is configured for DHCP or fixed IP addresses.
Enter the following command:
If your Raspberry Pi 5 is configured to use the DHCP protocol (which is normally the default
configuration), the word dhcp should appear at the end of the following line:
If on the other hand your Raspberry Pi is configured to use static addresses then you should
see the word static at the end of the following line:
Step 2: To use the DHCP protocol, edit file interfaces (e.g. using the nano text editor)
and change the word static to dhcp. It is recommended to make a backup copy of the file
interfaces before you change it:
You should now re-start your Raspberry Pi and an IP address will probably be assigned to
your device.
Step 3: To use static addressing, make sure that the word static appears as shown above.
If not, edit file interfaces and change dhcp to static
Step 4: You need to edit and add the required unique IP address, subnet mask and
gateway addresses to file interfaces as in the following example (this example assumes
that the required fixed IP address is [Link], the subnet mask used in the network
is [Link], and the gateway address is [Link]):
Save the changes and exit the editor. If you are using the nano editor, exit by pressing
Ctrl+X, then enter Y to save the changes, and enter the filename to write to as /etc/
network/interfaces.
● 27
• Insert a microSD card into your PC. You may need to use an SD card adapter
• Click Download for Windows (or your PC operating system) to download the
Raspberry Pi Imager. At the time of writing this book this file was called:
imager_1.[Link]
• Click Operating System and select the operating system at the top of the list
as: Raspberry Pi OS (64-bit). See Figure 2.14
● 28
• Click Save
If you have monitor and keyboard you can login to your raspberry Pi 5 directly and start
using it. Otherwise, find the IP address of your Raspberry Pi 5 (e.g. from your router, or
there are many apps for smart phones, e.g. who's on my wifi on the Android that shows
all the devices connected to your router with their IP addresses). Then, login to your
Raspberry Pi 5 and start using it.
Linux commands are text-based. In this section we will be looking at some of the useful
commands and see how you can manage your Raspberry Pi 5 using these commands. In
the following sections, Commands entered by the user are shown in bold for clarity.
● 29
Also, it is important to remind you that all the commands must be terminated by the Enter
key.
pi@raspberrypi: ~$
Here, pi is the name of the user who is logged in. raspberrypi is the name of the computer,
used to identify it when connecting over the network. The ~ character indicates that you
are currently in your default directory, which is /home/pi.
Command uname –s displays the operating system Kernel name, which is Linux. Command
uname –a displays complete detailed information about the Kernel and the operating
system. An example is shown in Figure 2.17.
Command cat /proc/meminfo displays information about the memory on your Raspberry
Pi. Information such as the total memory and free memory at the time of issuing the
● 30
command are displayed. Figure 2.18 shows an example, where only part of the display is
shown here.
Command whoami displays the name of the current user. In this case, pi is displayed as
the current user.
A new user can be added to your Raspberry Pi 5 using the command useradd. In the
example in Figure 2.19, user called John is added. A password for the new user can be
added using the passwd command followed by the username. In the figure, the password
for user John is set to mypassword (not displayed for security reasons). Notice that
both the useradd and passwd are privileged commands and the keyword sudo must be
entered before these commands. Notice that the –m option creates a home directory for
the new user.
You can login to the new user account by specifying the username and the password as
shown in Figure 2.20. You can type command exit to logout from the new account.
Command sudo apt-get upgrade is used to upgrade all the software packages on the
system.
● 31
Figure 2.21 shows part of the Raspberry Pi 5 directory structure. Notice that the root
directory is identified by the "/" symbol. Under the root we have directories named such
as bin, boot, dev, etc, home, lib, lost+found, media, mnt, opt, proc, and many more. The
important directory as far as the users are concerned is the home directory. The home
directory contains subdirectories for each user of the system. In the example in Figure
3.7, pi is the subdirectory for user pi. In a new system this subdirectory contains two
subdirectories called Desktop and python_games.
Some useful directory commands are given below. Command pwd displays the user home
directory:
pi@raspberrypi: ~$ pwd
/home/pi
pi@raspberry: ~$
To show the subdirectories and files in your working directory, enter ls:
pi@raspberrypi: ~$ ls
Bookshelf Documents Music Public Videos
Desktop Downloads Pictures Templates
pi@raspberrypi: ~$
● 32
Notice that the subdirectories are displayed in blue colour and files in black colour.
The ls command can take a few arguments. Some examples are given below.
pi@raspberrypi: ~$ ls -1
Bookshelf
Desktop
Documents
Downloads
Music
Pictures
Public
Templates
Videos
pi@raspberrypi: ~$
To display the file types, enter the following command. Note that directories have a "/" after
their names, and executable files have a "*" character after their names:
pi@raspberrypi: ~$ ls –F
Bookshelf/ Documents/ Music/ Public/ Videos/
Desktop/ Downloads/ Pictures/ Templates/
pi@raspberrypi: ~$
pi@raspberrypi: ~$ ls –m
Bookshelf, Desktop, Documents, Downloads, Music, Pictures, Public, Templates,
Videos
pi@raspberrypi: ~$
Subdirectories are created using command mkdir followed by the name of the subdirectory.
Command find is used to search the whole system for a file and outputs a list of all
directories that contain the file. For example the command find / -name [Link]
searches the whole system for the file [Link].
● 33
The first word pi in the example in Figure 2.24 shows who the user of the file (or directory)
is, and the second word pi shows the group name that owns the file. In this example, both
the user and the group names are pi.
The permissions can be analysed by breaking down the characters into four chunks for:
File type, User, Group, World. The first character for a file is "-" and for a directory it is "d".
Next comes the permissions for the User, Group and World. The permissions are as follows:
• Read permission (r): the permission to open and read a file or to list a directory
• Write permission (w): the permission to modify a file, or to delete or create a
file in a directory
• Execute permission (x): the permission to execute the file (applies to
executable files), or to enter a directory
The three letters rwx are used as a group and if there is no permission assigned then a "-"
character is used.
As an example, considering the Music directory, we have the following permission codes:
● 34
d: it is a directory
rwx: user (owner) can read, write, and execute
r-x: group can read and execute, but cannot write (e.g. create or delete)
r-x: world (everyone else) can read and execute, but cannot write
The chmod command is used to change the file permissions. Before going into details of
how to change the permissions, let us look and see what arguments are available in chmod
for changing the file permissions.
The available arguments for changing file permissions are given below. We can use these
arguments to add/remove permissions or to explicitly set permissions. It is important
to realize that if we explicitly set permissions then any unspecified permissions in the
command will be revoked:
+: add
-: remove
=: set
r: read
w: write
x: execute
To change the permissions of a file we type the chmod command, followed by one of the
letters u,g,o,or a to select the people, followed by the +- or = to select the type of change,
and finally followed by the filename. In this example, a file with the name [Link]
was created in the home directory for demonstration purposes (See Figure 2.25). In this
example, file [Link] has the user read and write permissions.
We will be changing the permissions so that the user does not have read permission on
this file:
● 35
Notice that if you now try to display the contents of file [Link] using the cat
command you will get an error message:
All the permissions can be removed from a file by the following command:
In the following example, rwx user permissions are given to file [Link]:
● 36
To change our working directory the command cd is used. In the following example we
change our working directory to Music:
pi@raspberrypi: ~$ cd /home/pi/Music
pi@raspberrypi: ~/Music $
To change your working directory to Music, you can also enter the command:
pi@raspberrypi: ~$ cd ~/Music
pi@raspberrypi: ~/myfiles $
pi@raspberrypi: ~/Music $ cd ~
pi@raspberrypi: ~$
To find out more information about a file you can use the file command. For example:
The –R argument of command ls lists all the files in all the subdirectories of the current
working directory. An example is given below (only part of the display is shown). Notice
here in Figure 2.28 that subdirectory Bookshelf contains file BeginnersGuide-4thEd-
Eng_v2.pdf
To display information on how to use a command, you can use the man command. Enter
q to exit from the man display.
● 37
2.10.5 Help
The man command usually gives several pages of information on how to use a command.
You can type q to exit the man command and return to the operating system prompt.
The less command can be used to display a long listing one page at a time. Using the up
and down arrow keys we can move between pages. An example is given below. Type q to
exit:
Wildcards
You can use wildcard characters to select multiple files with similar characteristics. e.g. files
having the same file-extension names. The * character is used to match any number of
characters. Similarly, the ? character is used to match any single character. In the example
below all the files with extensions .txt are listed:
pi@raspberrypi: ~$ ls *.txt
[Link] [Link]
pi@raspberrypi: ~$
The wildcard characters [a-z] can be used to match any single character in the specified
character range. An example is given below which matches any files that start with letters
o, p, q, r, s, and t, and with the .txt extension:
pi@raspberrypi: ~$ ls [o-t]*.txt
[Link]
pi@raspberrypi: ~$
Renaming a File
You can rename a file using the mv command. In the example below, the name of file test.
txt is changed to [Link]:
● 38
Deleting a File
The command rm can be used to remove (delete) a file. In the example below file test2.
txt is deleted:
pi@raspberrypi: ~$ rm [Link]
pi@raspberrypi: ~$
The argument –v can be used to display a message when a file is removed. Also, the –i
argument asks for confirmation before a file is removed. In general the two arguments are
used together as –vi. An example is given below:
Sorting a file
The command sort displays the contents of a file in ascending order. The general format of
this command is:
Word count
Command wc <filename> displays the word count in a file
File differences
Command diff <file1> <file2) displays the differences between two files line by line
Removing a Directory
A directory can be removed using the rmdir command:
● 39
The echo command can also be used to write a line of text to a file. An example is shown
below:
Matching a string
The grep command can be used to match a string in a file. An example is given below
assuming that the file [Link] contains sting a line of text. Notice that the matched word is
shown in bold:
Similarly, the tail command is used to display the last 10 lines of a file. The format of this
command is as follows:
● 40
The which command displays the location of an executable program. For example, the
location of the python program can be found as follows:
pi@raspberrypi: ~$ dpkg –l
…………………………….
…………………………….
pi@raspberrypi: ~$
You can also find out if a certain software package is already installed on our computer. An
example is given below which checks whether or not software called xpdf (PDF reader) is
installed. In this example xpdf is installed and the details of this software are displayed:
If the software is not installed you get a message similar to the following (assuming we are
checking to see if a software package called bbgd is installed):
● 41
……………………………………………………………………………..
…………………………………………………………………………….
pi@raspberrypi: ~$
Figure 2.29 shows a typical system resource display obtained by entering the following
command (only part of the display is shown, Enter q to exit):
pi@raspberrypi: ~$ top
pi@raspberrypi: ~$
Some of the important points in Figure 2.29 are summarized below (for lines 1 to 5 of the
display):
● 42
• The total usable memory is 8053.6 bytes, of which 247.3 bytes are in use,
7589.7 bytes are free, and 303.7 bytes are used by buffers/cache
• Line 5 displays the swap space usage
The process table gives the following information for all the processes loaded to the system:
The command htop is similar to the top command, except it has more features and is more
user-friendly.
The ps command can be used to list all the processes used by the current user. An example
is shown in Figure 2.30.
Command ps –ef gives a lot more information about the processes running in the system.
Killing a process
here are many options for killing (or stopping) a process. A process can be killed by
specifying its PID and using the following command:
● 43
Command free shows how much memory is used and the amount of free memory.
The following command will stop all the processes and make the file system safe and then
turn off the system safely:
The system can also be shut down and then re-started after a time by entering the following
command. Optionally, a shut down message can be displayed if desired:
e.g.
Broadcast message from root @raspberrypi on pts/1 (Tue 2023-10-03 12:03:00 BST)
● 44
Note: Raspberry Pi 5 includes a power switch at its side. When the Raspberry Pi is ON, a
single press brings the shutdown/logout menu. Another press triggers a safe shutdown
which is a standby with the Raspberry Pi consuming about 1.4W. A press of the button
will start up the Raspberry Pi 5.
2.10.12 Networking
Some useful networking commands are:
iwconfig: check which network the Raspberry Pi is using. An example is shown in Figure
2.32. Here, the SSID of the WiFi adapter used is BTHub5-6SPN
ping: used to test the availability of a network device. an example is shown in Figure 2.33
wget: this command is used to download a file from the web and saves the file in the
current directory.
● 45
If you have executed many commands and want to use some of them again but you cannot
remember the command name, you can use the history command. An example is shown
in Figure 2.36. To execute a command from the history, enter ! followed by the command
number. For example, to execute the ls command again, you can enter !6 followed by the
Enter key.
The clear command is also useful and it is used to clear the screen.
● 46
The & operator allows you to run any command in the background so that you can use the
terminal for other tasks. This operator must be added to the end of a command.
The && operator allows you to run two or more commands at the same time. For example
command1 && command2.
● 47
3.1 Overview
The Raspberry Pi 5 AI Kit (Figure 3.1) is based on the Hailo-8L M.2 AI acceleration
hardware module which delivers 13 TOPS of inferencing performance. The module is
plugged-in on top of the Raspberry Pi 5 M.2 M-Key HAT (also called the M.2 HAT+), which
connects to the PCIe port of the Raspberry Pi 5.
The Hailo-8L accelerator module is designed to deliver high performance, thus making it
an ideal for entry-level AI applications. The chip has superior area and power efficiency,
making it competitive compared to other AI chips. Hailo-8L features low-latency that can
manage complex operations with real-time streams and concurrent processing. The Hailo-
8L accelerator includes a comprehensive software package which makes it easy to install,
setup, and operate. The module is fully compatible with the Raspberry Pi 5. Additionally,
Hailo-8L is compatible with the ARM structures and supports the framework such as
TensorFlow, TensorFlow Lite, Keras, PyTorch, and ONNX. The Hailo-8L is optimized for
vision related applications, making it ideal for tasks such as automated quality inspection,
video surveillance, face identification, object identification, healthcare imaging, robotics,
home automation, factory automation, and many others.
● 48
Before looking at the details of the AI kit, it is worthwhile to look at the specifications of the
Raspberry Pi 5 M.2 M-Key HAT and learn how to install it, as well as learn how to install the
AI kit hardware and software.
The M.2 HAT+ adapter board converts between the PCIe connector on Raspberry Pi 5 and a
single M.2 M-Key edge connector. You can connect any device that uses the 2230 or 2242
form factors. The HAT+ can supply up to 3A of current.
The Raspberry Pi 5 operating system automatically detects the HAT+ and any connected
devices to it.
It is recommended to use an active cooler with the Raspberry Pi 5 while using the HAT+.
The active cooler must be installed before installing the HAT+. Spacers are provided to
leave ample room between the active cooler and the HAT+. Additionally, you should install
your camera before installing the HAT+.
● 49
• Install the spacers using the four provided screws (Figure 3.4). Disconnect the
ribbon cable from the HAT+ and insert the other end into the PICe port of your
Raspberry Pi 5. Lift the ribbon cable holder from both sides, then insert the
cable with the copper contact points facing inward, towards the USB ports. With
the ribbon cable fully inserted into the PCIe port, push the cable holder down
from both ends to secure it.
● 50
• Place the HAT+ on top of the spacers, and use the four remaining screws to
secure it in place (Figure 3.5)
● 51
• Insert the ribbon cable (Figure 3.6) into slot on the HAT+. Lift the ribbon cable
header from both sides as before, then insert the cable with the copper contact
points facing up. Push the cable holder down from both sides to secure it.
• Push the notch on the screw into the slot at the end of the AI Kit (Figure 3.8).
Insert the attachment screw by turning the screw clockwise until it feels secure.
Do not over-tighten the screw.
● 52
● 53
If you see December 2023 or a later date, proceed to the next step. If you see a
date earlier than 6 December 2023, run the following command:
Under Advanced Options -> Bootloader Version, choose Latest. Then exit
raspi-config with Finish or the Escape key. Run the following command:
Select option 6. Advanced Options, then select option A8 PCIe Speed. Choose
Yes to enable PCIe Gen 3 mode. Click Finish to exit.
Note that at the time of writing this book the latest version of the Hailo software (HailoRT)
was 4.17. It was announced that a new version (4.18) would be available soon with some
enhanced features and compatible with some pre-trained demo models.
● 54
If everything is ok, you should see an output like Figure 3.10. Note that the software
version is 4.17.
Figure 3.11 Test TAPPAS Core installation (part of the display is shown)
If hailo or hailotools are not found, try deleting the GStreamer registry:
pi@raspberrypi:~ $ rm ~/.cache/gstreamer-1.0/[Link]
● 55
• Additionally, you can run the following command to check the kernel logs:
If everything is OK, you can continue installing and using the hailo_rpi5_examples
(demo programs), which are the topic of chapter 8. Before that, we will review the
important topics of creating text files, Python programming basics, and commonly used
Raspberry Pi 5 camera commands. These topics could be useful if you want to modify the
supplied demo programs.
● 56
4.1 Overview
Cameras are used widely in almost all vision based AI applications. In fact, the Raspberry
Pi AI Kit demo programs are based on using cameras. It is therefore worthwhile to learn
about the Raspberry Pi compatible available cameras, and also learn how to use them with
Raspberry Pi commands. In this chapter we will briefly look at the various Raspberry Pi 5
compatible cameras and learn how to use the basic camera commands.
[Link]
the-camera-modules
V1 V2 V3 HQ GS
● 57
The high quality camera is an interesting one, having 12.3 Megapixels with 7.9mm diagonal
image size and adjustable focus. The specifications of this camera are:
Note: Raspberry Pi 5 camera socket is a 15-pin socket, where most of the Raspberry Pi
cameras have 22 pins. It is therefore necessary to purchase a 15-pin to 22-pin flat cable
to connect your camera to Raspberry Pi 5 (see Figure 4.1).
• Pull up the plastic cable holder slider of the camera port gently by holding from
both ends. The cable holder slider will open
• Insert the 15-pin camera ribbon cable (with the connector side facing the white
side of the connector ) into the cable holder
● 58
• Push down the plastic cable holder slider so that it locks and holds the cable
Note that two dashes "--" must be entered before list-cameras. You should see the
specifications of your camera displayed. If you get an error, check that the camera has
been installed correctly.
You should now be able to use your camera interface and carry out camera operations to
capture still images and video frames. You should be in Desktop GUI mode with a monitor
directly attached to Raspberry Pi 5 through the micro HDMI cable to capture images with
your camera. Notice that, if you are familiar with camera commands, the old camera
commands raspistill, raspiyuv, raspivid, and raspividyuv are now obsolete and can not
be used with Raspberry Pi 5. It is also recommended not to use the libcamera commands
with the Raspberry Pi 5 Bookworm operating system even though they may work at present
time. Operating systems prior to Bookworm can still use the libcamera commands.
In this Chapter a Version 2 visible light camera is used by the author. Notice that the
camera type is detected automatically by the operating system.
If you are using a V3 camera module with your Raspberry Pi 5, add the following lines to
your [Link] file:
rpicam-hello: A "hello world" equivalent for cameras, which starts a camera preview
stream and displays it on the screen for 5 seconds. You can pass an optional duration
in milliseconds with the timeout option (--timeout nn). A value of 0 runs the preview
indefinitely where the Cntrl+C can be entered to close the preview window. Alternatively
the preview window can be closed by clicking on its close button.
● 59
For example, the following command takes a snapshot image and the preview window
remains open for 2 seconds:
You should connect your camera to your Raspberry Pi 5 and verify that you can capture
images using the rpicam-hello command before using the DEMO programs of the AI Kit.
The --info-text displays image information. Table 4.2 shows the options of this command.
For example, to display the shutter speed used to capture the image, use the following
command:
Directive Substitution
%focus Focus metric for the image, where a larger value implies a sharper image.
In the following command, the preview duration is set to 2 seconds, image is saved in file
[Link] with width 640 pixels and height 480 pixels:
● 60
rpicam-still: Emulates many of the features of the original raspistill application, having
many options, such as raw images, long exposures, timelapse images, and so on. Raw still
images can be captured using the –raw option. The crontab scheduling command of the
operating system can be used to take images at a future data and time. Further details can
be obtained from the following document:
[Link]
rpicam-vid: Captures video. A preview window is shown and then the encoded bitstream
is written to the specified output file. This command uses the H.264 image format or the
mp4 format. For example, the following command writes a 5 second video to file test.mp4:
The saved video file can be played using the VLC program or any other video player program.
Further details on this command can be obtained from the above document.
rpicam-raw: Captures raw video frames directly from the sensor without showing any
preview window. Raw video files are generally large files.
rpicam-detect: Not built by default, but users can build it if they have TensorFlow Lite
installed on their Raspberry Pi. Captures JPEG images when certain objects are detected.
There are many useful camera related commands that the readers may like to use. Some
of these are given below. Full list of the commands are available at the document whose
link is specified earlier:
list-cameras: Lists the detected cameras attached to your Raspberry Pi and their available
sensor modes. An example is shown in Figure 4.2.
● 61
preview: Sets the location (x, y coordinates) and size (with, height) of the preview window
rotation n: Rotates the image by the specified angle n, where n is between 0 and 180
help: Displays a list of all valid command. An example is shown in Figure 4.4 (only part of
the output is shown).
Notice that the commands can be shortened to its first character with a single – sign
in-front of it. e.g. instead of --timeout 10s we can write –t 10s. Similarly, instead of
--output we can simply write –o.
● 62
A text editor is used to create a new text file (e.g. a program) or to modify the contents
of a text file. There are many text editors available for the Linux operating system. Some
popular ones are: nano, vim, vi, Thonny, and many more.
AI Kit demo programs have been written using programming languages. You may need
to modify some of these programs to suit your needs. This process requires the use of a
text editor. In this chapter, we shall be looking at two of these text editors available on the
Raspberry Pi 5 and see how to use them.
Start the nano text editor by entering the word nano, followed by the filename you wish
to create or modify. An example is given below where a new file called [Link] is created:
You should see the editor screen as in Figure 5.1. The name of the file to be edited is written
at the top middle part of the screen. The message New File at the bottom of the screen
shows that this is a newly created file. The shortcuts at the bottom of the screen are there
to perform various editing functions. These shortcuts are accessed by pressing the Ctrl key
together with another key. Some of the useful shortcuts are given below:
● 63
Step 2: Look for word simple by pressing Ctrl+W and then typing simple in the window
opened at the bottom left hand corner of the screen. Press the Enter key. The cursor will be
positioned on the word simple (see Figure 5.3).
● 64
Step 3: Cut the first line by placing the cursor anywhere on the line and then pressing
Ctrl+K. The first line will disappear as in Figure 5.4.
Step 4: Paste the line cut after the first line. Place the cursor on the second line and press
Ctrl+U (see Figure 5.5).
Step 5: Place cursor at the beginning of word simple on the first row. Enter Ctrl+C. The
row and column positions of this word will be displayed at the bottom of the screen (Figure
5.6).
● 65
Step 6: Press Ctrl+G to display help page as in Figure 5.7. Notice that the display is many
pages long and you can jump to the next pages by pressing Ctrl+Y or to the previous
pages by pressing Ctrl+V. Press Ctrl+X to exit the help page.
Step 7: Press Ctrl+- and enter line and column numbers as 2 and 5, followed by the Enter
key, to move cursor to line 2, column 5 (see Figure 5.8).
Step 8: Replace word example with word file. Press Ctrl+\ and type the first word as
example (see Figure 5.9). Press Enter and then type the replacement word as file. Press
Enter and accept the change by typing y.
● 66
Step 9: Save the changes. Press Ctrl+X to exit the file. Type Y to accept the saving, then
enter the filename to be written to, or simply press Enter to write to the existing file (first.
txt in this example). The file will be saved in your current working directory.
In summary, nano is a simple and yet powerful text editor allowing you to create new text
files or to edit existing files. It is recommended to use this editor while developing long and
complex programs.
In this section we shall be looking at the basic features of this editor and see how we can
use it in simple editing applications.
Notice that you cannot use the keyboard arrow keys with the vi editor. Some of the useful
vi editor commands are listed below:
● 67
Start the vi text editor by typing vi followed by the name of the file to be created or
modified. In this example it is assumed that anew file called [Link] is to be created:
pi@raspberrypi: ~ $ vi [Link]
You should see the vi text editor screen displayed as in Figure 5.10. The name of the file
being edited is displayed at the bottom of the screen.
● 68
The vi editor is different from most other text editors in that it is not possible to start typing
inside the editor window. The steps for editing this file are given below:
Step 1: The vi editor have different modes and you must be in the insert mode to be able
to write to the window. Press i to move to the insert mode. Then type in the following text
(see Figure 5.11):
Step 2: To come out of the insert mode, press the ESC key. To save the file, type characters
:w. You can exit the editor after saving the changes by typing :q. Alternatively, you can
type ZZ (note upper case) to save and exit. If you made changes to the file and attempt to
quit without saving, you will get an error message. If you want to exit without saving the
changes, simply type :q!
Step 3: Make sure you are in the command mode and type the character / followed by a
word to search for this word in the text. For example, type /editor to search for the word
editor (see Figure 5.12) in the text.
● 69
Step 4: Insert word is before word editor. Type i followed by is and space, and terminate
insert mode by pressing ESC key.
Step 5: Move cursor right by pressing the l key. Similarly, move cursor left by pressing the
h key. Move cursor down (to the second line) by pressing the j key.
Step 6: Search for word this and delete it. Type /this followed by the Enter key. Type dw
to delete the word.
Step 8: Search for word help and replace it with word guide. Go to the line where word
help is. Type /help, then type :s/help/guide/
Step 9: You can search and replace a word in any other line than the current line. For this
example, position the cursor on the first line. Change word basic in the second line to
BASIC. Type:
:1,2s/basic/BASIC/
Notice that you can specify the range of lines by separating them with a comma. In this
example, the search starts from line 1 and terminates at line 2.
To use the Thonny, you have to login to your Raspberry Pi 5 using either a directly connected
terminal through the mini HDMI port, or if you don't have a monitor, you can login to the
Desktop using the VNC as described in earlier chapters. Then you can create and run your
Python programs in GUI mode using the Thonny IDE. It is worthwhile at this stage to learn
the basics of using the Thonny IDE.
● 70
The screen consists of two parts: the upper part is where you write your programs. The
lower part is the shell where small interactive program codes can be written. This part is
mainly used for testing small codes.
The Thonny IDE must be configured before it is used to write and upload programs to your
Raspberry Pi. Click the bottom right corner of the screen to select your processor type
and select Local Python 3. You are now ready to write your program in the middle of the
screen. Click File -> Save to save your program. The advantage of using the Thonny is
that you can run small programs in interactive mode by entering them at the lower part of
the screen under shell. The results will be displayed under shell immediately. The Thonny
IDE is suitable for developing Python programs.
● 71
Chapter 6 • C
reating and Running a Simple Python
Program
6.1 Overview
The AI Kit demo programs have in general been written using the Python programming
language. It is worthwhile to look at the creation and running of a simple Python program
on your Raspberry Pi 5 computer so that you are familiar on how to run a Python program.
In this Chapter, the message Hello From Raspberry Pi 5 will be displayed on your PC
screen.
There are 3 methods that you can effectively create and run Python programs on your
Raspberry Pi 5.
• At the command prompt enter python. You should see the Python command
mode which is identified by three characters >>>
• The text will be displayed interactively on the screen as shown in Figure 6.1.
Note that at the time of writing this book the Python version was: 3.11.2.
● 72
• Click File -> Save and save with the name [Link] (Figure 6.4)
● 73
• Click the Run icon (green menu button at the top) to run the program. The
output of the program will be displayed at the bottom of the screen as shown in
Figure 6.5.
● 74
7.1 Overview
Python is an interpreted, interactive and object oriented programming language. It was
developed by Guido van Rossum in the 1980s at the National Institute for Mathematics and
Computer Science in the Netherlands. It is derived from many other languages including
C, C++, Modula-3, SmallTalk, and Unix shell. The language is now maintained by a team
of people at the Institute.
Python is interactive which means that you can issue a command and see the result
immediately without having to compile the command. It is interpreted, thus requiring no
pre-compilation before it is run.
In this chapter you will be learning the basic details of the Python programming language
on the Raspberry Pi 5 computer, and see how you can write programs using this language.
Some simple programming examples are given towards the end of the chapter.
Python is a large and rich language having many features. It is not the aim of this chapter
to teach the readers the complete Python programming features. The aim is here to teach
the basic features of the Python programming language so that readers have a better
understanding of the AI Kit demo programs, and hopefully they can develop or modify the
AI Kit demo programs to suit their own applications.
SUM - valid
Sum - valid
SUm - valid
_total - valid
Cnt5 - valid
8tot - invalid
%int - invalid
&xyz - invalid
My_Number - valid
@loop - invalid
_Account - valid
● 75
Note that variables total, Total, TOTAL, ToTaL, or toTAL are all different.
7.4 Comments
Comment lines in Python start with a hash sign "#". All characters after the # sign are
ignored by the Python interpreter. An example comment line is shown below:
Sum = a +\
b +\
c
Sum = a + b + c
● 76
7.8 Indentation
In most programming languages blocks of code are identified by using braces at the
beginning and end of the block, or by identifying the end of the block using a suitable
statement. e.g. END, WEND, or ENDIF. In Python language there are no braces or
statements to indicate the start and end of a block. Instead, blocks of code are identified
by line indentation. All statements within a block must be indented the same amount. The
actual number of spaces used to indent a block is not important if all the statements in the
block use the same number of spaces.
A valid block of code is given below (don't worry at this stage what the code does):
if j == 5:
a = a + 1
b = a + 2
else:
a = 0
b = 0
The following block of code is not valid since the indentation is not correct:
if j == 5:
a = a + 1
b = a + 2
else:
a = 0
b = 0
• Numbers
• Strings
• Lists
• Dictionaries
• Tuples
• Sets
• Files
● 77
7.10 Numbers
Python supports the following numeric variable types:
Numbers can be represented in decimal, Octal, binary, or hexadecimal. Long integers are
shown with an upper case letter L.
Integer
100 - decimal
-67 - decimal
500 - decimal
0x20 - hexadecimal
0b10000001 - binary
0o2377 - octal
202334567L - long decimal
0x3AEEFAE - hexadecimal
Floating point
2.355
23.780
-45.6
1.298
24.45E4
Complex
24.4+2,6j
0.78-4.2j
23.7j
We can assign numeric values to variables. These variable objects are created when values
are assigned to them:
sum = 28
a=0
del sum, a
● 78
w=x=y=z=0
w, x, y = 3, 5, 8
w=3
x=5
y=8
Expression operators
+ addition
- Subtraction
* multiplication
/ division
>> shift right
<< shift left
** power (exponentiation)
% remainder
Bitwise operators
| bitwise OR
& bitwise AND
^ bitwise exclusive-or
~ bitwise complement
● 79
Figure 7.1 to Figure 7.3 show examples of using numbers in Python. Statement import
is used to import a library to a Python program. math library contains large number of
mathematical functions, such as logarithmic functions, trigonometric function, square root,
hyperbolic functions, angular conversion, and so on. Further details on these functions can
be obtained from the following link:
[Link]
[Link]
● 80
7.11 Strings
In Python strings are declared by enclosing characters between a pair of single or double
quotation marks. An example is given below:
We can manipulate strings by extracting characters, joining two strings, assigning a string
to another string, and so on. Some commonly used string manipulation operations are
shown in Figure 7.4 and Figure 7.5.
● 81
Notice that a third index as the step can be used in string slicing operation. The step
is added to the first offset until the second offset and the character at this position is
extracted. In the following example, the characters at positions 0, 2, 4, 6 are extracted:
>>> a = "computer"
>>> b = a[0:7:2]
>>> print(b)
cmue
● 82
• \n newline
• \a bell
• \b backspace
• \f formfeed
● 83
• \r carriage return
• \t horizontal tab
• \v vertical tab
• \xhh character with 2 hexadecimal value hh
As an example, the following statement will display letter a followed by two newlines:
print("a\n\n")
• %c character
• %s string
• %d signed integer
• %u unsigned integer
• %x lower case hexadecimal number
• %X upper case hexadecimal number
• %f floating point number
• %E exponential notation
● 84
s =
mylist[0] # s = 'John'
s =
mylist[2] # s = 230
s =
mylist[2:4] # s = 230, 12.25
s =
mylist[3:] # s = 12.25, 'Peter', 89
s =
mylist * 2 # s = 'John', 'Adam', 230, 12.25, 'Peter', 89, 'John',
'Adam', 230, 12.25, 'Peter', 89
s = mylist + second # s = 'John', 'Adam', 230, 12.25, 'Peter', 89, 30, 23
The contents of a list can be modified by assigning a new value to the required index
position. For example, we can change the 2nd element of the list mylist from 230 to 100 as:
mylist[2] = 100
Python does not allow to reference items that are not present in a list. For example, the
following statement gives an error message:
mylist[200]
Lists can be nested to form two dimensional matrices. An example is given below:
M = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
The nested list is indexed starting from [0][0]. For example, the elements of row 1 can be
accessed as follows:
● 85
● 86
s =
mytuple[0] # s = 'John'
s =
mytuple[2] # s = 230
s =
mytuple[2:4] # s = 230, 12.25
s =
mytuple[3:] # s = 12.25, 'Peter', 89
s =
mytuple * 2 # s = 'John', 'Adam', 230, 12.25, 'Peter', 89, 'John',
'Adam', 230, 12.25, 'Peter', 89
s = mytuple + second # s = 'John', 'Adam', 230, 12.25, 'Peter', 89, 30, 23
The following statement is not valid since we cannot change the contents of a tuple:
mytuple[2] = 200
● 87
Figure 7.10 shows examples of using the keyboard input function. Notice that the function
returns a string. Therefore, if numeric data is entered then it should be converted into a
numeric data type before being used in mathematical operations.
● 88
• if
• if-else
• elif
• for
• while
• break
• continue
• pass
if expression: statement
or
if expression:
Statement 1
Statement 2
else:
Statement 1
Statement 2
Notice the use of indentation inside the if blocks and the colon character at the end of the
if and else statements.
if a == 5: print('a is 5')
if there is only one statement after the if then it can be typed on the same line. If there is
more than one statement then all the statements must be written on the next lines with
the same amount of indentation. An example is given below:
if a == 100:
x = 0
y = 0
else:
x = 1
y = 10
● 89
The elif statement is used to check for different conditions in an if block. An example is
given below:
if a > 10:
b = 0
c = 0
elif a == 10:
b = 2
c = 4
Notice that the if statements can be nested as shown in the following example:
if a == 100:
c = 0
k = 1
if b == 10:
c = 20
m = 1
else:
c = 23
Here, the sequence is evaluated first and the first item in the sequence is assigned to the
variable and the statements are executed. Then the second item is assigned to the variable
and the statements are executed. This continues until there are no more items in the
sequence. An example use of the for statement is shown below:
C
O
M
P
U
T
E
R
● 90
The for statement is commonly used to create loops in programs. The range statement
denotes the range of the variable as in the following example:
0
1
2
3
4
Notice that the upper value of the range is one less than the specified value i.e. in the above
example, range is from 0 to 4 and not to 5.
We can specify a step size in the last parameter when using the range statement, in the
following example, the step size is 5 and the list takes values 0, 5, 10, 15, 20, 25:
while expression:
statements
The statements are executed while the expression evaluates to True. An example is given
below:
cnt = 0
while cnt < 5:
print(cnt)
cnt = cnt + 1
0
1
2
3
4
● 91
Notice that the statements that belong to the while statement must be indented. It is
important to make sure that the expression is modified inside the loop, otherwise an infinite
loop will be formed as shown in the following example:
cnt = 0
while cnt < 5:
print(cnt)
cnt = 0
while cnt < 5:
cnt = cnt + 1
if cnt == 3:
continue
print(cnt)
1
2
4
5
cnt = 0
while cnt < 5:
cnt = cnt + 1
if cnt == 3:
break
print(cnt)
1
2
● 92
C
O
M
Passed
P
U
T
E
R
A function that we create can be called from anywhere in a program. Functions have their
own variables and their own commands. As we have seen in earlier parts of this chapter,
Python has many built-in functions for various operations such as arithmetic, trigonometric,
string manipulation and so on. User defined functions are created by programmers. In this
section we shall be looking at how functions can be created and used in our programs.
• functions begin with the keyword def, followed by function name, and round
brackets, followed by a colon sign.
• Input arguments to the function must be placed inside the brackets at the
beginning of the function definition.
• The body of a function must be indented with the same number of spaces on
the left hand side
● 93
An example function, named Mult is given below. This function takes two numbers first and
second as its arguments, multiplies them, and returns the result:
A function is called from the main program by specifying the name of the function and
enclosing any arguments in a pair of brackets. For example, to call the above function to
multiply numbers 5 and 3 and sore the result in variable called a, we include the following
statement in our program:
a = Mult(5, 3)
a = Mult(first = 5, second = 3)
Another example is shown in Figure 7.12. In this example the function displays a string
passed as an argument. Notice that there is no data returned from this function.
The variables used in a function are local to that function. Thus, for example, if there are
two variables with the same name, one inside the function and the other one outside,
changing the one inside the function does not change the one outside. Variables outside
a function are called global variables, whereas the ones inside a function are called local
variables. See Figure 7.13 for an example where the contents of variable res are not
● 94
• Global variables are variables assigned at the top of the program outside the
function definitions
• Global names must be declared only if they are assigned within a function
• Global names may be referenced within a function without being declared
Therefore, by declaring a variable outside the functions and also inside a function but with
the global keyword allows us to change its contents inside the function. An example is given
below which identifies the use of global variables:
as explained above, if the value of a global variable is not changed inside a function then
there is no need to define it as global. In the following code there is no need to define x as
global inside the function:
x = 10
y = 4
def tst():
global y
y = x + 2
It is important to note that the variables in a function call are passed by value. This means
that the value of a parameter cannot be changed inside a function. An example is shown
in Figure 7.14. In this example, notice that the value of variable cnt is not changed inside
the function call.
● 95
A function normally returns only one item back to the calling program. In some applications
we may want to return more than one item to the calling program. This is easily done by
returning a tuple and then unpacking it in the main program. An example is shown in Figure
7.15. In this example, function MyFunc is declared with two arguments. The arguments are
added and stored in local variable called sum. Similarly, the difference of the arguments is
stored in variable diff. The function returns both sum and diff as a tuple. The calling main
program unpacks the returned data and stores in variables x and y.
7.22 Examples
Example 1
Write a program to read an angle from the keyboard in degrees and display the trigonometric
sine of this angle. Repeat until the user stops the program.
Solution 1
The required program listing and example output are shown in Figure 7.16 (program: trig.
py). The angle entered by the user is converted into floating point and is stored in variable
angle. Then the trigonometric sine of this angle is displayed. The program continues until
the user enters n in response to prompt Any more?
● 96
Example 2
Modify the program in Example 1 so that the user can choose between sine, cosine, and
tangent.
Solution 2
The modified program listing and example output are shown in Figure 7.17 and Figure 7.18
(program: [Link]). The user is given a menu with four choices: sine, cosine, tangent,
exit. The angle is read from the keyboard and is converted into radians. The program then
calculates the trigonometric value and displays on the screen. This process is repeated until
the user selects the exit option.
#--------------------------------------------------
# TRIGONOMETRIC SINE,COSINE,TANGENT PROGRAM
# =========================================
#
# This program reads an angle from the keyboard
# and displays its trigonometric sine, cosine, or
# tangent depending on user choice. The angle is
# read in degrees,converted into radians and then
# the required trigonometric function is calculated
#
● 97
choice = '1'
while choice != '0':
print("Trigonometric Sine, Cosine, or Tangent")
print("======================================\n")
print("1. Sine")
print("2. Cosine")
print("3. Tangent")
print("0. Exit")
choice = input("Enter choice: ")
if choice != '0':
angle = float(input("Enter angle in degrees: "))
r = [Link](angle)
if choice == '1':
s = [Link](r)
strng = "sine"
elif choice == '2':
s = [Link](r)
strng = "cosine"
elif choice == '3':
s = [Link](r)
strng = "tangent"
print(strng + " of %3.2f degrees is: %f\n" %(angle, s))
print("End of program")
● 98
This program was created using the nano text editor and then run using the command:
Example 3
Write a program to tabulate the trigonometric sines of angles from 0º to 90º in steps of 5º.
Solution 3
The required program listing is shown in Figure 7.19 (program: [Link]). After
displaying a heading, the for statement is used to create a loop. Variable angle takes values
from 0 to 90 (inclusive) in steps of 5. The trigonometric sine is calculated and displayed.
#--------------------------------------------------
# TRIGONOMETRIC SINE TABLE
# ========================
#
# This program tabulates the trigonometric sine of
# angles from 0 to 90 degrees in steps of 5 degress
#
# Author: Dogan Ibrahim
# File : [Link]
# Date : July, 2024
#--------------------------------------------------
import math
● 99
print("End of program")
Example 4
Write a program to read metres from the keyboard. Convert into yards and inches and
display the result.
Solution 4
The required program listing and example output are shown in Figure 7.21 program:
[Link]). After displaying a heading, metres is read from the keyboard using the input
statement. The value is then converted into yards and inches by multiplying with 1.0936
and 39.370 respectively. The results are displayed on the screen.
● 100
Example 5
Repeat Example 7 but do the conversion in a function called Conv. Show how this function
can be called from the main program.
Solution 5
The required program listing and example output are shown in Figure 7.22 (program:
[Link]). Function Conv is declared at the beginning of the program. Metres to be
converted into yards and inches is passed as an argument to the function. The function
returns the yards and inches in a tuple. The main program reads the metres from the
keyboard and calls function Conv. The result is displayed on the screen.
● 101
7.23 Exceptions
It is possible that there are major errors in our programs, such as dividing by zero, file
privilege error, and so on. Normally, when Python encounters such errors it cannot handle
them and the program crashes.
One way to handle such errors orderly and avoid crashes is to use exception handling in
our programs. The basic method is that whenever an error occurs the program detects
this error and takes appropriate measures to handle the error and continue to execute
normally. Exception handling is also useful if we wish to terminate a running program in an
orderly manner, for example to shut down any input-output operations when the program
is terminated asynchronously by the user (e.g. by pressing the Cntrl+C key).
The statements try and except are used to handle unexpected error or terminations in our
programs. The general format of exception handling is as follows:
try:
Normal program statements
Normal program statements
except condition 1:
if condition 1 type error occurs then execute this block of code
……………..
……………..
except condition 2:
if condition 2 type error occurs then execute this block of code
……………….
……………….
else:
if there are no errors detected then execute this block of code
…………………
…………………
We can use the except statement with no condition to handle any type of exception. Some
of the commonly used exceptions are:
● 102
Example 6
Write a program to detect division by zero and to display the message Divide by Zero
when this exception is detected.
Solution 6
Figure 7.23 shows the program listing (program: [Link]). Here, the program is forced
to divide a number by zero and this is detected as an exception and the program displays
a message when this occurs.
#=====================================================
# ZeroDivisionError EXCEPTION
#
# This program detects when a number is divided by zero
# and generates an exception to display a message
##
# Author : Dogan Ibrahim
# File : [Link]
# Date : July, 2024
#======================================================
print("Divide by zero exception")
try:
s = 10 / 0
except ZeroDivisionError:
print("Divide by Zero")
● 103
The datetime module can also be used for date and time functions. This module must be
imported to use these functions. Some examples of date functions are shown in Figure
7.25.
Function strftime(format) is very useful as it can be used to format a date and time
string. Some examples of using this function are given in Figure 7.26.
● 104
● 105
8.1 Overview
The Raspberry Pi 5 AI kit allows you to build complex real-time AI vision applications
with low-power requirements. Neural network applications run entirely on the Hailo-8L co-
processor, leaving the Raspberry Pi 5 CPU free to perform other tasks. The Neural Network
inference accelerator runs at 13-Tera operations per second (TOPS) on the PCIe bus, and is
fully integrated to the Raspberry Pi 5 camera.
The Hailo-8L demo programs repository contains examples of basic pipelines for Hailo's RPi
5 platform. The examples demonstrate object detection, human pose estimation, instance
segmentation, etc. They are built to allow you to use these applications as a basis for
your own projects. After trying out the supplied demos, you can move on to testing the AI
models Hailo made, which you can access from their model zoo (link: [Link]
hailo-ai/hailo_model_zoo/tree/master/docs/public_models/HAILO8L), where you can find
several models for various applications, such as face recognition, depth estimation, and
[Link]
md#hailo-rpi5-basic-pipelines
The following You Tube videos can be helpful before you start using the AI kit:
The following video (by Jeff Geerling) is also available to help understand the AI kit features:
[Link]
● 106
cd hailo-rpi5-examples
• To run the examples, you should ensure your environment is set up correctly.
You can set it all up by sourcing the following script. This script will set the
required environment variables and activate Hailo virtual environment (if it
doesn't exist, it will create it).
source setup_env.sh
• Make sure you are in the virtual environment and run the following command.
This may take some time, wait until it is finished.
• Resource download. This may take some time, wait until it is finished:
./download_resources.sh
This repository contains examples of basic pipelines for Hailo's RPi5 platform. The examples
demonstrate object detection, human pose estimation, and instance segmentation. It is
built as templates to allow you to use these applications as a basis for your own projects.
The hailo-rpi5-examples folder contains the folders and files shown in Figure 8.1.
● 107
This user-defined class is passed as an input to the callback function, which runs on the
pipeline output. It is used for communication between the main application and the callback
function. It extends hailo_rpi_common::app_callback_class and can be customized
with additional variables and methods specific to the application.
This is where you should add your code. A user-defined function that processes each
frame in the pipeline. It is called from the identity_callback element in the pipeline,
placed after the network inference and post-processing. The GStreamer buffer passed as
an input to this function includes the network output as Hailo Metadata and the frame
itself. Each example demonstrates how to parse the specific metadata for its task. For
more information on Hailo Metadata objects, refer to the Hailo Objects API in the TAPPAS
documentation.
No changes needed for using the basic pipelines. This class sets up the GStreamer
pipeline and handles events and callbacks, extending hailo_rpi_common::GStreamerApp.
Applications can modify network parameters and the pipeline by overloading the get_
pipeline_string function. For more details on TAPPAS pipelines and elements, see the
TAPPAS Documentation at:
[Link]
blob/4341aa360b7f8b9eac9b2d3b26f79fca562b34e4/docs/TAPPAS_architecture.
rst
To run the example use (make sure you are in Desktop GUI mode):
cd hailo-rpi5-examples
source setup_env.sh
● 108
This application reads the detection0.mp4 movie file and detects and indicates various
items in the file. The default labels used by the models are COCO labels (80 classes) as
shown in Figure 8.3.
/**
* Copyright (c) 2021-2022 Hailo Technologies Ltd. All rights reserved.
* Distributed under the LGPL license ([Link]
[Link])
**/
#pragma once
#include <map>
namespace common
{
static std::map<uint8_t, std::string> coco_eighty = {
{0, «unlabeled»},
{1, «person»},
{2, «bicycle»},
{3, «car»},
{4, «motorcycle»},
{5, «airplane»},
{6, «bus»},
{7, «train»},
{8, «truck»},
{9, «boat»},
{10, «traffic light»},
{11, «fire hydrant»},
{12, «stop sign»},
{13, «parking meter»},
{14, «bench»},
{15, «bird»},
{16, «cat»},
{17, «dog»},
{18, «horse»},
{19, «sheep»},
{20, «cow»},
{21, «elephant»},
{22, «bear»},
{23, «zebra»},
{24, «giraffe»},
{25, «backpack»},
{26, «umbrella»},
{27, «handbag»},
{28, «tie»},
{29, «suitcase»},
{30, «frisbee»},
{31, «skis»},
● 109
{32, «snowboard»},
{33, «sports ball»},
{34, «kite»},
{35, «baseball bat»},
{36, «baseball glove»},
{37, «skateboard»},
{38, «surfboard»},
{39, «tennis racket»},
{40, «bottle»},
{41, «wine glass»},
{42, «cup»},
{43, «fork»},
{44, «knife»},
{45, «spoon»},
{46, «bowl»},
{47, «banana»},
{48, «apple»},
{49, «sandwich»},
{50, «orange»},
{51, «broccoli»},
{52, «carrot»},
{53, «hot dog»},
{54, «pizza»},
{55, «donut»},
{56, «cake»},
{57, «chair»},
{58, «couch»},
{59, «potted plant»},
{60, «bed»},
{61, «dining table»},
{62, «toilet»},
{63, «tv»},
{64, «laptop»},
{65, «mouse»},
{66, «remote»},
{67, «keyboard»},
{68, «cell phone»},
{69, «microwave»},
{70, «oven»},
{71, «toaster»},
{72, «sink»},
{73, «refrigerator»},
{74, «book»},
{75, «clock»},
{76, «vase»},
{77, «scissors»},
● 110
Figure 8.4 shows the output of the program displayed on the Raspberry Pi monitor where
the identified objects are shown with rectangles.
options:
-h, --help show this help message and exit
--input INPUT, -i INPUT
Input source. Can be a file, USB or RPi camera (CSI
camera module). For RPi camera use '-i rpi' (Still in Beta). Defaults to
/dev/video0
--use-frame, -u Use frame from the callback function
--show-fps, -f Print FPS on sink
--disable-sync Disables display sink sync, will run as fast possible.
● 111
For example:
Note that the USB camera is not guaranteed to be /dev/video0. You can check which
video devices are available by entering the command:
ls /dev/video*
v4l2-ctl –list-devices
You can test whether the camera is working by entering (Note that it is lower case L not 1):
If you get an error try another device (e.g. /dev/video2). In author's setup the USB camera
was named as /dev/video8. Your USB camera will probably have different device name.
For example, to read images from the USB camera, the author used the option –i /dev/
video8. In the following example, a keyboard is detected using a USB camera:
The output is shown in Figure 8.5 with the keyboard label displayed at the top left corner
of the display.
● 112
Use the following command if using a Raspberry Pi camera (with auto-focus, e.g. V3):
[Link]
md#running-with-different-input-sources
● 113
cd hailo-rpi5-examples
source setup_env.sh
python basic_pipelines/pose_estimation.py --input resources/detection0.mp4
This application reads the detection0.mp4 movie file for pose estimation. The output is
shown in Figure 8.6.
● 114
This application reads the detection0.mp4 movie file for pose estimation. The output is
shown in Figure 8.67
The Callback function showcases how to get the instance segmentation metadata from
the network output. Each instance is represented as a HAILO_DETECTION with a mask
(HAILO_CONF_CLASS_MASK object). If --use-frame flag is set the code will parse the
masks, resize and reshape them according to the frame coordinates. It will print their
shape to the terminal. Drawing the mask on the user buffer is possible but not implemented
in this example due to performance reasons.
[Link]
For example, the negate stage turns light pixels dark and dark pixels light. Because the
negate stage is basic, requiring no configuration, [Link] just names the stage:
● 115
{
"negate": {}
}
To apply the negate stage to an image, pass [Link] to the post-process-file option
as shown below:
To run multiple post-processing stages, create a JSON file that contains multiple stages as
top-level keys. For example, to the following configuration runs the Sobel stage, then the
negate stage:
{
"sobel_cv":
{
"ksize": 5
},
"negate": {}
}
The sobel stage uses OpenCV. It has a user-configurable parameter, ksize, that specifies
the kernel size of the filter to be used. The Sobel filter produces bright edges on a black
background, and the negate stage turns this into dark edges on a white background.
hdr stage: Details in images are emphasized in this stage using Dynamic Range Compression,
where hdr stands for High Dynamic Range. A few parameters can be used with this stage.
Motion-detect stage: The motion_detect stage analyses frames from the low-resolution
image stream. You must configure the low-resolution stream to use this stage. The stage
detects motion by comparing a region of interest (ROI) in the frame to the corresponding
part of a previous frame. If enough pixels change between frames, this stage indicates
the motion in metadata under the motion_detect.result key. Many parameters are
available with this stage.
There are post-processing stages with the OpenCV and TensorFlow Lite.
The rpicam-apps supplied with Raspberry Pi OS do not include OpenCV and TensorFlow
Lite. As a result, certain post-processing stages that rely on them are disabled.
Example JSON files that use the built-in post-processing stages are in assets folder of the
rpicam-apps repository.
● 116
Specifying option –n suppresses preview window, option –v 2 gives text output, and –t
0 runs indefinitely. The contents of file yolov5_personface.json is shown in Figure 8.9.
● 117
The command below supresses the preview window and gives text output:
You can display the contents of folder /rpicam-apps/assets for other json files and run
them as described above.
8.4 Project 1 – Detect person presence using the AI Kit – LED output
Description: In this project person presence is detected and an LED is turned ON when
a person is detected. The program waits with the LED ON until the user presses a reset
button to acknowledge the detection. At this time, the LED turns OFF and the detection
process starts again. This is a simple but useful example. The LED can be replaced with
a relay or a buzzer if required. Also, the program can be modified to detect objects other
than persons.
Circuit diagram: The circuit diagram is shown in Figure 8.11. The LED is connected to
port pin 21 of Raspberry Pi 5 through a 330 Ohm current limiting resistor. The pushbutton
is connected to port pin 20. The default state of the button is at logic 1 and goes to logic 0
when the button is pressed.
● 118
Program listing: The Python program (Program: [Link]) listing is shown in Figure
8.12. The program creates a subprogram and runs as a shell program. The rpicam-apps
post-processing hailo inference program is started, and the output of this program is
compared if it contains the word person. i.e. if a person has been detected. If so, the
shell program exits and the LED is turned ON. The program then waits until the button is
pressed. Pressing the button is like an acknowledgement from the user. Pressing the button
turns OFF the LED. The program then waits for 15 seconds and then the detection process
resumes from the beginning.
This program can be modified by replacing the LED with a relay or a buzzer and changing
the waiting time to suit the required application. Also, other objects can be detected and
then required actions can be taken.
#---------------------------------------------------
# DETECT PERSON AND TURN ON LED
# -----------------------------
#
# File : [Link]
# Date : October, 2024
# Author: Dogan Ibrahim
#---------------------------------------------------
import os
import subprocess
from gpiozero import LED, Button
from time import sleep
while(True):
p=[Link]("rpicam-hello -n -v 2 -t 0 \
● 119
--post-process-file /home/pi/rpicam-apps/assets/hailo_yolov8_inference.json\
--lores-width 640 --lores-height 640 2>&1 | grep -m 1 -c \
--line-buffered 'person'",shell=True)
print("Person detected...")
#
# At this point person has been detected
#
[Link]()
8.5 Project 2 – Detect person presence using the AI Kit – WiFi based
Description: This project is similar to the previous one but here when a person is detected
a message is sent to a smart phone over a WiFi link using the TCP/IP protocol and an LED
is turned ON. The user can acknowledge the message on the smart phone by sending OK
to the Raspberry Pi 5. At this point the LED turns OFF and the process repeats from the
beginning. As in the previous project the LED can be replaced with a buzzer, or a relay and
the program can easily be modified to detect other objects.
● 120
Circuit diagram: The circuit diagram is shown in Figure 8.14. The LED is connected to
port pin 21 of Raspberry Pi 5 through a current limiting resistor as in the previous project.
#--------------------------------------------------------
# Detect Person and Send Message over WiFi
# ----------------------------------------
#
# This program sends the message Person detected to smart phone
# when the camera detects a person. At the same time the LED is
# turned ON. The program then waits for acknowledhgement from
# the smart phone. When OK is sent by the smart phone the LED
# is turned OFF and program continues from the beginning
#
# File : [Link]
# Date : October 2024
# Author: Dogan Ibrahim
#-----------------------------------------------------------
import os
import subprocess
from gpiozero import LED
● 121
while(True):
# [Link](":>[Link]")
p=[Link]("rpicam-hello -n -v 2 -t 0 \
--post-process-file /home/pi/rpicam-apps/assets/hailo_yolov8_inference.json\
--lores-width 640 --lores-height 640 2>&1 | grep -m 1 -c \
--line-buffered 'person'",shell=True)
print("Person detected...")
#
# At this point person has been detected
#
[Link]()
[Link]([Link]('utf-8')) # Send message
print("Message sent")
resp = [Link](1024) # receive message
print([Link]('utf-8')) # Print mesaage
if [Link]('utf-8') == "OK": # If OK?
[Link]() # LED OFF
sleep(15) # Wait for 15s
Testing: There are several TCP apps freely available on the Internet for smart phones that
can be used to test the project. The apps used in this project was for the Android phones,
called Simple TCP Socket Tester by J. Armando as shown in Figure 8.16.
● 122
python [Link]
bind
listen
• Enter the following details (Figure 8.17). Your IP address will be different:
IP Address: [Link]
Port: 5000
Figure 8.17 Start the apps and enter the host IP address and port number
● 123
• Click button CONNECT. You should see message Socket connected displayed
in green colour at top of the IP address
Accepted
Waiting to detect a person…
1
Person detected…
Message sent
• The LED will turn ON and the message Person detected… will be displayed on
the Android phone (Figure 8.18)
• The LED will turn OFF and OK will be displayed on the Raspberry Pi 5 monitor
as well as on the Android apps (Figure 8.19)
• Click DISCONNECT on the apps to disconnect the link. Type Cntrl+C to stop
the Raspberry Pi 5 program.
● 124
Model building is usually carried out on an Ubuntu based development computer with large
disk (e.g. 100 GB) and large amount of memory (e.g. 32 GB). Model building includes tools
such as TensorFlow Lite, Keras, PyTorch, and Onnx, AI Software Suite of programs are
used for model building. Model Zoo includes a variety of pre-trained models, and tasks are
available in TensorFlow and ONNX. The Dataflow compiler is used to compile and optimize
user's models for Hailo hardware devices. The Dataflow compiler runs on an Ubuntu based
development computer and produces executable HEF files that can be input to the HailoRT
program on Raspberry Pi 5.
The runtime environment is on the host computer and includes user applications, TAPPAS
application examples and application pipeline elements, HailoRT runtime software library
running on the host computer (e.g. Raspberry Pi 5) including HailoRT CLI, and the Hailo
firmware on the Hailo hardware.
● 125
● 126
9.1 Overview
Hailo provides many pre-trained models that can be used with the Raspberry Pi 5 AI kit.
Some of these models are pre-compiled and can be used as they are. Some others need
to be compiled using the Dataflow compiler. In this chapter you will learn how to install the
Dataflow compiler.
Models Hailo provides different pre-trained models in ONNX / TF formats and pre-compiled
HEF (Hailo Executable Format) binary file to execute on the Hailo devices.
• Hailo models - which were trained in-house for specific use-cases on internal
datasets. Each Hailo Model is accompanied with retraining instructions. These
models are: Person and Face detection, License Plate recognition, Person Re-
identification, and others
Note that the current version of Python does not support the DFC installation. You will have
to install an earlier version before the DFC can be installed.
● 127
• Download the Dataflow Compiler from the following site. You will have to
register before using it (Figure 9.1 and Figure 9.2). At the time of writing this
book, the DFC compiler name was: hailo_dataflow_compiler-3.28.0-py-3-
none-linux_x86_64.whl.
[Link]
ai%2Fdeveloper-zone%2Fsoftware-downloads%2F
● 128
• The DFC file will be copied to your Downloads folder on your Ubuntu
computer. Change to this folder, and then:
hailo –h
This completes the installation of the DFC. HEF files can be downloaded from the following
web link:
[Link]
[Link]
The Hailo Model Zoo uses the Hailo Dataflow Compiler for parsing, model optimization,
emulation and compilation of the deep learning models. Full functionality includes (Figure
9.3):
• Parse: model translation of the input model into Hailo's internal representation.
• Profiler: generate profiler report of the model. The report contains information
about your model and expected performance on the Hailo hardware.
• Optimize: optimize the deep learning model for inference and generate
a numeric translation of the input model into a compressed integer
representation.
• Compile: run the Hailo compiler DFC to generate the Hailo Executable Format
file (HEF) which can be executed on the Hailo hardware.
• Evaluate: infer the model using the Hailo Emulator or the Hailo hardware and
produce the model accuracy.
● 129
Note: In chapter 3 we have seen how to install the Hailo software on Raspberry Pi 5.
In that process, version 4.17 of the HailoRT software was installed. Unfortunately, most
of the demo programs have been compiled using version 3.28 of the Dataflow compiler
and as such they are not compatible with version 4.17 of the HailoRT software and they
will not run under version 4.17.
● 130
10.1 Overview
The Hailo AI SW Suite includes several programs that can be used to create and train
models as well as compile them ready for the Hailo hardware device. In this chapter you will
learn how to install this suite of programs on a development computer running the Ubuntu
operating system.
In this section we will install the Hailo AI SW-Suite on an Ubuntu development computer
since it can be used to retrain models as well. For further details, refer to the SW Suite
Installation guide available at the Software Downloads page at the link given below.
• Download the Hailo AI SW-Suite from the following site. You will have to
register before using it (Figure 10.1):
[Link]
ai%2Fdeveloper-zone%2Fsoftware-downloads%2F
● 131
In this chapter, we will be installing using the docker file option. The development system
requirements are:
The steps for docker installation are given below. Characters typed by the user are in bold
for clarity.
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/
keyrings/[Link]] [Link]
\
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/[Link].d/[Link] > /dev/null
sudo apt-get update
● 132
• Verify that the Docker Engine installation is successful by running the hello-
world image. This command downloads a test image and runs it in a container.
When the container runs, it prints a confirmation message and exits.
• Add the user to the docker group with the following steps:
Nvidia's Pascal/Turing/Ampere GPU architecture (such as Titan X Pascal, GTX 1080 Ti, RTX
2080 Ti, or RTX A4000)
GPU driver version 525 nvidia-docker2. It can be installed by running the following
commands:
• Assuming the downloaded files are in Downloads folder: Change to this folder
and install the hailo driver software:
• Extract the suite archive, then run the script - which opens a new container,
and attaches to it ("gets inside it"). This may take some time, wait until
finished.
● 133
cd Downloads
unzip hailo_ai_sw_suite_2024-07.1_docker.zip
sudo ./hailo_ai_sw_suite_docker_run.sh
The above command may take some time. Wait until it is finished.
It is possible to enable/disable HailoRT features inside the Suite Docker. To see available
flags, run:
./hailo_ai_sw_suite_docker_run.sh –help
./hailo_ai_sw_suite_docker_run.sh –hailort-enable-service
Enter hailo -h to see a list of the available CLI commands of Hailo Dataflow Compiler and
HailoRT
You can exit the docker by using the exit command, and then attach to it using the
commands shown on:
[Link]
referrer=suite/working_with_dockers.html
Resume – To go back to the existing container, change folder to Downloads and enter:
● 134
Enter pip list | grep hailo to see the Hailo packages that are installed in the activated
virtual environment (Figure 10.3)
[Link]
In the next chapter we will look at a Yolov8n re-training example using the COCO dataset.
● 135
11.1 Overview
In this chapter the steps of Yolov8n object detection training through deployment using the
AI Kit with the Raspberry Pi 5 are given. COCO 128 dataset is used in this chapter. COCO
128 is a subset of images of the larger COCO dataset. COCO 128 is meant to be a good
dataset for first time testing out a model.
11.2 YOLO
Ultralyticks YOLO (You Only Look Once) is a popular real-time object detection and image
segmentation model which was developed at the University of Washington in 2015. YOLO
was developed at different versions and it was named as YOLOvn where n was the version
number:
YOLOv2: was released in 2016 and it incorporated anchor boxes, dimension clusters, and
batch normalization.
YOLOv3: was released in 2018 and the earlier version was enhanced using more efficient
network, pyramid pooling, and multiple anchors.
YOLOv4: was released in 2020 and this version introduced Mosaic data augmentation, new
loss function anchor-free detection head.
YOLOv5: was released in 2021 and it incorporates new features such as hyperparameter
optimization, automatic export to export formats etc.
YOLOv6: was released in 2022 and is currently in use in autonomous delivery robotic
applications.
YOLOv8: this is the latest state-of-the-art model by Ultralyticks. This version offers several
enhancements such as improved performance, efficiency, flexibility, support for full range
of vision AI tasks including pose estimation, detection, segmentation, tracking, and
classification. There are several versions of YOLOv8, such as YOLOv8n (nano version),
YOLOv8s, YOLOv8l (large version). YOLOv8m, and so on.
In this chapter we will train Yolov8n using the COCO 128 dataset. COCO (Common Objects
in Context) is a large-scale object detection, segmentation, and captioning dataset designed
to encourage research on a wide variety of categories. COCO models are used in many AI
based vision applications. It has the following features:
• object recognition
• object segmentation
• 330,000 images
• 1.5 million object instances
● 136
• 80 object classes
• 91 stuff categories
• 5 captions per image
• 250,000 people with key points
The dataset has two main parts: the images and their annotations. The images are
organized into a hierarchy of directories. The annotations are provided in JSON format with
each file corresponding to an image.
The COCO dataset classes are divided into two main categories: things and stuff. Things
classes include objects that can be handled, such as animals, vehicles, and household items
etc. Stuff classes include environmental items such as sky, water, and road.
COCO 128 is a subset of 128 images of the COCO dataset 2017, and it is a good dataset
to use the first time you are working with AI. COCO 128 consists of annotations (in JSON
format), images (in jpg format), and labels (in txt format).
The training in this chapter was carried out on a core I7 Windows PC laptop running 64-bit
Ubuntu operating system under VMware workstation with 200 GB SSD disk and 16 GB of
memory. Python 3.8.10 was installed on Ubuntu.
The steps for the training are given below. The characters typed by the user are in bold for
clarity.
Note: The following training steps are taken from the following site:
[Link]
object_detection/
● 137
cd Downloads
sudo ./hailo_ai_sw_suite_docker_run.sh --resume
• Install ultralytics
mkdir yolomodel
cd yolomodel
• Use COCO 128 dataset to train Yolov8n. This may take some time, wait until it
is finished. Enter:
• This process may take hours depending on the power and the features of the
development computer you are using. Figure 11.1 shows running the program.
The objects modelled are shown in Figure 11.2a and Figure 11.2b.
● 138
● 139
• After the training you should see the model file [Link] in folder: runs/
detect/retrain_yolov8n/weights. This is shown in Figure 11.3.
cd ./runs/detect/retrain_yolov8n/weights/
• At the end of the program, a file named [Link] will be created. You can
check this with the ls command.
hailomz –h
● 140
• Install the COCO dataset to evaluate, optimize, and compile the yolov8n model
(Figure 11.6)
cd hailo_model_zoo
python hailo_model_zoo/datasets/create_coco_tfrecord.py
val2017
python hailo_model_zoo/datasets/create_coco_tfrecord.py
calib2017
cd ..
hailomz parse --hw-arch hailo8l --ckpt ./[Link] yolov8n
● 141
You should check the folders at the following location as probably there is no folder with
the name 2021-06-18:
/local/shared_with_docker/.hailomz/models_files/coco/
Find the name of the folder at the above location, e.g. in author's application, there was a
folder with the name: 2023-08-03. Copy the contents of his folder to folder 2021-06-18
and re-run the command to optimize the model.
• Compile the model (Figure 11.9). This make some time and wait until it is
finished:
Figure 11.10 shows the display when the compilation process is finished. Note that a file
with the name [Link] is create in the current folder. This is the program that can be
deployed on the Raspberry Pi 5.
Now we should transfer the compiled file to Raspberry Pi 5 and deploy it there. The steps
are:
● 142
On Raspberry Pi 5
• Clone the project:
• Create folder
mkdir hailomodel
ON Ubuntu Computer
Make sure that your Raspberry Pi 5 is connected to the same network as your Ubuntu
computer. Copy the compiled file [Link] to Raspberry Pi 5 using command scp.
Assuming that the username of your Raspberry Pi 5 is pi and its IP address is 192,168.1.251,
enter the following command (change the username and IP address for your own values):
On Raspberry Pi 5
Make sure that file [Link] is copied to your Raspberry Pi 5:
• cd /home/pi/Benchmarking-YOLOv8-on-Raspberry-PI-reComputer-
r1000-and-AIkit-Hailo-8L/hailomodel/
• Use the ls command to check that file [Link] has been copied
cd ..
nano [Link]
Change the following lines (at lines 105, 106, and 172):
● 143
parser.add_argument("--network", default="yolov8n",
choices=['yolov6n', 'yolov8s', 'yolox_s_leaky'], help="Which
Network to use, defult is yolov6n")
nano [Link]
Change the following line 10 lines before the end of the file so that the program
works with the Raspberry Pi 5 camera:
We can now run the program on our Raspberry Pi 5 and detect objects in-front of the
camera. Make sure that you run the following command in Desktop GUI mode so that the
camera view can be displayed on the monitor.
● 144
mkdir /home/pi/hailo
• Copy the compiled file [Link] from the Ubuntu computer to the hailo
directory (or if you already have it in your /home/pi/Benchmarking-
YOLOv8-on-Raspberry-PI-reComputer-r1000-and-AIkit-Hailo-8L/
hailomodel/, directory on your Raspberry Pi 5, just copy it to /home/pi/
hailo).
cd hailo
cd hailo-rpi5-examples
source setup_env.sh
cd ..
• Install setproctitle
Now run the application and enable the Raspberry Pi 5 camera as the input for the
application:
11.5 Hailortcli
HailoRT comes with a CLI tool so that you can easily check the operation of the HEF file you
have created. Some examples are given below.
● 145
• To get information about the compiled model components (Figure 11.14) enter:
● 146
hailortcli --help
● 147
Some useful Hailo module based web sites are given below.
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
pipelines
[Link]
[Link]
[Link]
[Link]
hailo-ai-kit-16ef44
[Link]
kit-9bcd30
[Link]
[Link]
detection
[Link]
object_detection/
● 148
Index
A GPIO 12
ActiveCooler 13 gpiozero 14
ALSA 15 GPU 11
AI Kit software 53 GUI 22
B H
break statement 92 Hailo AI SW Suire 131
Bluetooth 12, 25 Hailo-8L accelerator 48
Bookworm 14 Hailortcli 55, 145
Benchmarks 15 Hailo Model Zoo 127
Hailo software compinents 125
C HAT+ adapter 49
Cache 12 HDMI 17
Cat 36 hostname 45
Chmod 35
Comments 76 I
Console commands 29 ifconfig 20
Cpu info 30 if else
CPU temperature 13 elif 89
CSI port 58 Indentation 77
Instance segmentation 114
D IMX477 58
Dataflow compiler 127 iwconfig 45
Data types 77
DDR4 11 K
Date 38 Keras 48
Demo software 106 Keyboard input 88
Detection demo 108 kill 43
Dictionary 87
Directory 32 L
dmesg 56 Larger font 18
dpkg 18 Line continuation 76
List variables 85
E ls 33
Echo 40
Escape sequences 83 M
Exceptions 102 man 38
mv 38
F meminfo 30
for statement 90
Functions 93 N
nano 63
G
grep 40
● 149
O U
ONNX 48 uname 30
USB ports 12
P
Passwd 21, 31 V
PCIe connector Variable names 75
Pose estimation 113 vi 67
Post processing 115
print statement 84 W
PulseAudio 15 while statement 91
Putty 19 Wildcard 38
pwd 32 Wired network 26
Python file 73
PyTorch 48 Y
YOLO 136
R
raspi-config 19
Reboot 2
Reserved words 76
Retraining models 135
rm 39
rpicam 59
S
SD card 17
Shutdown 21
SSH 19
SoC 12
sort 39
SSD 12
Static IP 24
String functions 82
Strings 81
T
tail 41
Tensorflow 48
Terminus 19
Thonny IDE 72
TightVNC 23
Time 38, 103
Top 42
Tuple variables 86
● 150
Two fully tested and working projects are given in the book. The first
project explains how a person can be detected and how an LED can be
activated after the detection, and how the detection can be acknowledged
by pressing an external button. The second project illustrates how a person
can be detected, and how this information can be passed to a smart phone
over a Wi-Fi link, as well as how the detection can be acknowledged by
sending a message from the smartphone to your Raspberry Pi 5.
Dogan Ibrahim