100% found this document useful (4 votes)
214 views152 pages

Raspberry Pi AI Kit Beginner Guide

The document is a beginner's guide to the Raspberry Pi AI Kit, authored by Prof Dr Dogan Ibrahim, which integrates an AI accelerator with the Raspberry Pi 5 for real-time AI applications. It covers the basics of the hardware, installation, and provides two practical projects for object detection and smartphone integration. The book aims to assist newcomers in utilizing AI capabilities on their Raspberry Pi systems.

Uploaded by

Stefan Oprea
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (4 votes)
214 views152 pages

Raspberry Pi AI Kit Beginner Guide

The document is a beginner's guide to the Raspberry Pi AI Kit, authored by Prof Dr Dogan Ibrahim, which integrates an AI accelerator with the Raspberry Pi 5 for real-time AI applications. It covers the basics of the hardware, installation, and provides two practical projects for object detection and smartphone integration. The book aims to assist newcomers in utilizing AI capabilities on their Raspberry Pi systems.

Uploaded by

Stefan Oprea
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

books

books books
books

Get Started with the Get Started with the


Raspberry Pi AI Kit Raspberry Pi AI Kit

Get Started with the Raspberry Pi AI Kit • Dogan Ibrahim


A Beginner’s Guide to AI and
Edge Computing
A Beginner’s Guide to AI and Edge Computing
Artificial Intelligence (AI) is now part of our daily lives. With companies Prof Dr Dogan Ibrahim has a BSc
developing low-cost AI powered hardware into their products, it is now degree in electronic engineering,
an MSc degree in automatic control
becoming a reality to purchase AI accelerator hardware at comparatively
engineering, and a PhD degree in
very low costs. One such hardware accelerator is the Hailo module digital signal processing. Dogan
which is fully compatible with the Raspberry Pi 5. The Raspberry Pi AI has worked in many industrial
Kit is a cleverly designed hardware as it bundles an M.2 based Hailo-8L organizations before he returned
accelerator with the Raspberry Pi M.2 HAT+ to offer high speed inferencing to academic life. Prof Ibrahim is the
author of over 70 technical books
on the Raspberry Pi 5. Using the Raspberry Pi AI Kit, you can build complex
and published over 200 technical
AI based vision applications, running in real time, such as object detec- articles on microcontrollers,
tion, pose estimation, instance segmentation, home automation, security, microprocessors, and related fields.
robotics, and many more neural network-based applications. He is a Chartered electrical engineer
and a Fellow of the Institution of
the Engineering Technology. He has
This book is an introduction to the Raspberry Pi AI Kit, and it is aimed to
been an amateur radio operator for
provide some help to readers who are new to the kit and wanting to run several decades and also holds an
some simple AI based visual models on their Raspberry Pi 5 computers. Arduino certification.
The book is not meant to cover the detailed process of model creation
and compilation, which is done on an Ubuntu computer with massive disk
space and 32 GB memory. Examples of pre-trained and custom object
detection are given in the book.

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.

Elektor International Media


[Link]

Dogan Ibrahim

SKU21031_COV_Get Started with the Raspberry Pi AI Kit_170x240_v01.indd 3 24-10-2024 10:22


Get Started with the

Raspberry Pi AI Kit
A Beginner's Guide to AI
and Edge Computing


Dogan Ibrahim

Getting Started with the Raspberry Pi 5 AI [Link] 3 24-10-2024 09:19


● This is an Elektor Publication. Elektor is the media brand of
Elektor International Media B.V.
PO Box 11, NL-6114-ZG Susteren, The Netherlands
Phone: +31 46 4389444

● 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.

● ISBN ​978-3-89576-638-1 Print


ISBN 978-3-89576-639-8 eBook

● © Copyright 2024 Elektor International Media


[Link]
Prepress Production: D-Vision, Julian van den Berg
Printers: Ipskamp, Enschede, The Netherlands

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

Getting Started with the Raspberry Pi 5 AI [Link] 4 24-10-2024 09:19


Contents

Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

Chapter 1 • The Raspberry Pi 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

1.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

1.2 The Raspberry Pi 5 hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

1.3 Active cooler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

1.4 Raspberry Pi 5 Operating System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

1.5 Benchmarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

Chapter 2 • I
 nstalling the Raspberry Pi 5 Operating System and
Console Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

2.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

2.2 Using a pre-installed SD card . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

2.3 Larger font in Console mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

2.4 Accessing your Raspberry Pi 5 Console from your PC – the Putty program . . . . . . . 19

2.4.1 Configuring the Putty . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

2.5 Accessing the Desktop GUI from your PC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

2.6 Assigning static IP address to your Raspberry Pi 5 . . . . . . . . . . . . . . . . . . . . . . . 24

2.7 Enabling Bluetooth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

2.8 Connecting the Raspberry Pi 5 to a wired network . . . . . . . . . . . . . . . . . . . . . . . 26

2.8.1 Unable to connect to a wired network . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

2.9 Installing the Raspberry Pi 5 Bookworm operating system on a blank microSD card . 28

2.10 Some commonly used Console commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

2.10.1 The Console command prompt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

2.10.2 System and user information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

2.10.3 The directory structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

2.10.4 File Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34

2.10.5 Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38

2.10.6 Date and Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38

2.10.7 File processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38

2.10.8 Head and tail commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

2.10.9 Super User commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41

●5

Getting Started with the Raspberry Pi 5 AI [Link] 5 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

2.10.10 Resource monitoring on Raspberry Pi 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42

2.10.11 Shutting down . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44

2.10.12 Networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45

2.10.13 System information and some other useful commands . . . . . . . . . . . . . . . . . 46

Chapter 3 • The Raspberry Pi AI Kit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48

3.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48

3.2 Raspberry Pi 5 M.2 HAT+ adapter board . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49

3.2.1 Installing the HAT+ and the AI Kit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50

3.2.2 Installing the AI Kit software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53

3.2.3 Verifying the software installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54

Chapter 4 • Raspberry Pi Cameras . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

4.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

4.2 Raspberry Pi cameras . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

4.3 Installing the camera . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58

4.3.1 Verify the camera installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59

4.4 rpicam camera commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59

Chapter 5 • Using a Text Editor in Console Mode . . . . . . . . . . . . . . . . . . . . . . . . . . 63

5.1 nano text editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

5.2 vi text editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67

5.3 Using the Thonny . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70

5.3.1 The Thonny IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71

Chapter 6 • Creating and Running a Simple Python Program . . . . . . . . . . . . . . . . . 72

6.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72

6.2 Method 1 – Interactively from command prompt in Console mode . . . . . . . . . . . . 72

6.3 Method 2 – Create a Python file in Console mode . . . . . . . . . . . . . . . . . . . . . . . . 72

6.4 Method 3 – Create a Python file in Desktop GUI mode . . . . . . . . . . . . . . . . . . . . 73

6.5 Which method? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74

Chapter 7 • Python Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75

7.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75

7.2 Variable names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75

7.3 Reserved words . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76

●6

Getting Started with the Raspberry Pi 5 AI [Link] 6 24-10-2024 09:19


Contents

7.4 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76

7.5 Line continuation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76

7.6 Blank lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76

7.7 More than one statement on a line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77

7.8 Indentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77

7.9 Python data types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77

7.10 Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78

7.11 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81

7.11.1 String functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82

7.11.2 Escape sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83

7.12 Print statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84

7.13 List variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85

7.13.1 List functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86

7.14 Tuple variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86

7.15 Dictionary variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87

7.15.1 Dictionary functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87

7.16 Keyboard input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88

7.17 Comparison operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88

7.18 Logical operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88

7.19 Assignment operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88

7.20 Control of flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89

7.20.1 if, if..else, and elif . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89

7.20.2 for statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90

7.20.3 while statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91

7.20.4 continue statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92

7.20.5 break statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92

7.20.6 pass statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93

7.21 User defined functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93

7.22 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96

7.23 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102

7.23.1 try/final exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103

●7

Getting Started with the Raspberry Pi 5 AI [Link] 7 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

7.24 Date and time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103

Chapter 8 • The AI Kit Demo Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106

8.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106

8.2 Demo software installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106

8.2.1 The application structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108

8.2.2 Detection demo example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108

8.2.3 Pose Estimation demo example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113

8.2.4 Instance segmentation demo example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114

8.3 Post-processing with rpicam-apps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115

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

8.6 Hailo software components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125

Chapter 9 • Pre-trained models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127

9.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127

9.2 Hailo Model Zoo pre-trained models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127

9.3 Installing the DataFlow compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127

Chapter 10 • The Hailo AI SW Suite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131

10.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131

10.2 The Hailo AI SW Suite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131

10.3 Re-training models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135

Chapter 11 • Raspberry Pi 5 Object Detection Training . . . . . . . . . . . . . . . . . . . . 136

11.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136

11.2 YOLO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136

11.3 The training . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137

11.4 Modified procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145

11.5 Hailortcli . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145

APPENDIX – Useful Web Sites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149

●8

Getting Started with the Raspberry Pi 5 AI [Link] 8 24-10-2024 09:19


Preface

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:

• Hailo AI module containing the Hailo-8L chip


• Raspberry Pi 5 M.2 HAT+ interface to connect the AI module to your Raspberry
Pi 5
• Mounting kit
• GPIO header

The basic features of the AI Kit are:

• 13 TOPS neural network inference accelerator


• Fully integrated with the Raspberry Pi camera system
• Compatible with the Raspberry Pi HAT+ specification
• Thermal pad to spread heat across components
• Easy fitting to the Raspberry Pi 5

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

Getting Started with the Raspberry Pi 5 AI [Link] 9 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

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.

Dr. Dogan Ibrahim


London, 2024

● 10

Getting Started with the Raspberry Pi 5 AI [Link] 10 24-10-2024 09:19


Chapter 1 • The Raspberry Pi 5

Chapter 1 • The Raspberry Pi 5

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.

1.2 The Raspberry Pi 5 hardware


Raspberry Pi 4 was released in June 2019. There has been long wait for a newer model and
finally Raspberry Pi 5 is here and will be on sale from the end of October 2023.

Raspberry Pi 5 is claimed to be two or three times the processing power of Raspberry Pi


4 which is already a very popular single board computer. Raspberry Pi 5 will initially be
available in 4GB and 8GB memory capacities, although smaller memory devices will be
announced later. Although the Raspberry Pi 5 is same size and shape as the Model 4B, it
has a number of interesting new features such as PCIe connector, power button, built-in
real-time clock, and some others that we will investigate in this Chapter.

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

Getting Started with the Raspberry Pi 5 AI [Link] 11 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

Raspberry Pi 4 Raspberry Pi 5

SoC BCM2711 SoC BCM2712 Soc


Cortex-A72 CPU at 1.8 GHz Cortex-A76 CPU at 2.4 GHz

CPU 4 core 4 core

Instruction set ARMv8-A ARMv8-2

Display 500 MHz VideoCore Vi GPU 800 MHz VideoCore VII GPU

L2 Cache 1 MB (shared) 2 MB

L3 Cache None 2 MB (shared)

RAM 1,2,4 GB LPDDR4 4,8 GB LPDDR4X

SD Card microSD microSD (high speed SDR104


compatible)

GPIO 2 x 20 pin 2 x 20 pin

USB ports 2 x USB2 2 x USB2


2 x USB3 2 x USB3

Networking Gigabit Ethernet port Gigabit Ethernet port

Connectors 2-lane MIPI display port 2 x MIPI camera

2-lane MIPI CSI camera port 2 x 4-lane MIPI camera/display


4-pole stereo audio and 2xPCIe 2.0 interface
composite video port UART port
RTC clock power port
Fan power port

Wi-Fi/Bluetooth 802.11ac, Bluetooth 5/BLE 802.11ac,Bluetooth 5/BLE

Power button None Yes

Power 5V 3A USB-C 5V 4A USB-C

Size 85 x 56mm 85 x 56mm


Table 1.1 Comparison of Raspberry Pi 5 and raspberry Pi 4

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

Getting Started with the Raspberry Pi 5 AI [Link] 12 24-10-2024 09:19


Chapter 1 • The Raspberry Pi 5

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.

Figure 1.1 Raspberry Pi 5

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).

Figure 1.2 RPI 4 (left) and RPI 5 (right)

1.3 Active cooler


The Raspberry Pi 5 gets very hot and it is recommended to use an active cooler to lower the
CPU temperature. Although the idle CPU temperature is around 50 degrees Celsius, it can
go higher than 85 degrees under a stress test. An active cooler is available for Raspberry Pi
5. Holes and power points are provided on the board to install and power the active cooler.
Figure 1.3 shows the Raspberry Pi 5 with the recommended official active cooler installed.

● 13

Getting Started with the Raspberry Pi 5 AI [Link] 13 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

The specifications of this active cooler are:

Input voltage: 5 V DC supplied via four-pin fan header on Raspberry Pi 5 board


Fan speed control: Pulse width modulation control with tachometer
Maximum airflow: 1.09 CFM
Material: anodised aluminium

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.

Figure 1.3 Raspberry Pi 5 with active cooler

1.4 Raspberry Pi 5 Operating System


Raspberry Pi 5 OS is based upon Debian 12 with the codename Bookworm. This OS,
released in July 2023, comes with a new Python interpreter (Python 3.11). A new change
is that a Python package cannot be installed using the pip commands. Also, Bookworm
operating system uses the Wayland rather than X11 as the display system. For many years,
or most Unix desktop environments, including the Raspberry Pi Desktop, have been based
on the X11 window system which has several limitations.

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

Getting Started with the Raspberry Pi 5 AI [Link] 14 24-10-2024 09:19


Chapter 1 • The Raspberry Pi 5

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.

Figure 1.4 Geekbench 5 benchmark scores for the Raspberry Pi 5 (Geekbench)

Figure 1.5 Geekbench 5 benchmark scores for the Raspberry Pi 4 (Geekbench)

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

Getting Started with the Raspberry Pi 5 AI [Link] 15 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

Figure 1.6 Some ncnn framework tests from Seeed Studio

● 16

Getting Started with the Raspberry Pi 5 AI [Link] 16 24-10-2024 09:19


Chapter 2 • Installing the Raspberry Pi 5 Operating System and Console Commands

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.

2.2 Using a pre-installed SD card


The pre-installed Raspberry Pi 5 operating system is available in various size micro SD
cards. In this section the author used the pre-installed 32GB micro SD card supplied by
Elektor. Additionally, the author used a 7-inch HDMI compatible monitor, a Raspberry Pi
official keyboard, and a mouse. The author's hardware setup between the Raspberry Pi 5
and various devices is shown in Figure 2.1.

Figure 2.1 Author's hardware setup

The steps are as follows:

• Insert the pre-installed micro SD card into your Raspberry Pi 5

• Connect all the devices as in Figure 2.1

• Connect the Raspberry Pi 5 adapter to mains

• 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

Getting Started with the Raspberry Pi 5 AI [Link] 17 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

• 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

2.3 Larger font in Console mode


It is probably hard to see the characters on a 7-inch monitor in Console mode. You can follow
the steps below to increase the font size (skip this section if you have a large monitor):

• Make sure you are in the Console mode

• Enter the following command:

pi@raspberrypi: ~ $ sudo dpkg-reconfigure console-setup

• Select UTF-8 in the Package Configuration screen (Figure 2.2)

Figure 2.2 Select UTF-8

• Select Guess optimal character set (Figure 2.3)

Figure 2.3 Select Guess optimal character set

● 18

Getting Started with the Raspberry Pi 5 AI [Link] 18 24-10-2024 09:19


Chapter 2 • Installing the Raspberry Pi 5 Operating System and Console Commands

• Select Terminus (Figure 2.4)

Figure 2.4 Select Terminus

• Select font 16x32 (Figure 2.5)

Figure 2.5 Select font 16x32

2.4 Accessing your Raspberry Pi 5 Console from your PC – the Putty


program
In many applications you may want to access your Raspberry Pi 5 from your PC. This
requires enabling the SSH on your Raspberry Pi and then using a terminal emulation
software on your PC. The steps to enable the SSH are as follows:

• Make sure you are in Console mode

• Type: sudo raspi-config

• Move down to Interface Options

• Highlight SSH and press Enter (Figure 2.6)

● 19

Getting Started with the Raspberry Pi 5 AI [Link] 19 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

Figure 2.6 Highlight SSH

• Click Yes to enable SSH

• Click OK

• Move down and click Finish

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.

Figure 2.7 Putty startup screen

• 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

Getting Started with the Raspberry Pi 5 AI [Link] 20 24-10-2024 09:19


Chapter 2 • Installing the Raspberry Pi 5 Operating System and Console Commands

Figure 2.8 Command ifconfig

• 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.

• To change your password, enter the following command:

pi@raspberrypi: ~ $ passwd

• To restart the Raspberry Pi 5 enter the following command:

pi@raspberrypi: ~ $ sudo reboot

• 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:

pi@raspberrypi: ~ $ sudo shutdown –h now

2.4.1 Configuring the Putty


By default, the Putty screen background is black with white foreground characters. The
author prefers to have white background with black foreground characters, with the
character size set to 12 points bold. You should save your settings so that they are available
next time you want to use the Putty. The steps to configure the Putty with these settings
are given below:

• Restart Putty

• Select SSH and enter the Raspberry Pi IP address

● 21

Getting Started with the Raspberry Pi 5 AI [Link] 21 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

• Click Colours under Window

• Set the Default Foreground and Default Bold Foreground colours to black
(Red:0, Green:0, Blue:0)

• Set the Default Background and Default Bold Background to white


(Red:255, Green:255, Blue:255)

• 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

2.5 Accessing the Desktop GUI from your PC


If you are using your Raspberry Pi 5 with local keyboard, mouse, and display you can skip
this section. If on the other hand you want to access your Raspberry Pi 5 remotely over
the network, you will find that SSH services cannot be used. The easiest and simplest
way to access your Pi 5 remotely from a computer is by using the VNC (Virtual Network
Connection) client and server. The VNC server runs on your Pi 5 and the VNC client (or VNC
Viewer) runs on your computer. It is recommended to use the tightvncserver on your
Raspberry Pi 5. The steps are:

• Enter the following command:

pi$raspberrypi:~ $ sudo apt-get install tightvncserver

• Run the tightvncserver:

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.

• Start the VNC server after reboot by the following command:

pi$raspberrypi:~ $ vncserver :1

● 22

Getting Started with the Raspberry Pi 5 AI [Link] 22 24-10-2024 09:19


Chapter 2 • Installing the Raspberry Pi 5 Operating System and Console Commands

You can optionally specify screen pixel size and colour depth in bits as follows:

pi$raspberrypi:~ $ vncserver :1 –geometry 1920x1080 –depth 24

• 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.

• Start the TightVNC Viewer on your PC and enter the Raspberry Pi 5 IP


address followed by :1. Click Connect to connect to your Raspberry Pi 5
(Figure 2.9)

Figure 2.9 Connect to TightVNC Viewer

• 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

Getting Started with the Raspberry Pi 5 AI [Link] 23 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

Figure 2.10 Raspberry Pi 5 Desktop

• The VNC Viewer is now running on your Raspberry Pi 5 and you have access to
its Desktop GUI from your PC.

2.6 Assigning static IP address to your Raspberry Pi 5


When you try to access your Raspberry Pi 5 remotely over your local network, it is possible
that the IP address given by your Wi-Fi router can change from time to time. This is
annoying as you may have to find out the new IP address allocated to your Raspberry Pi 5
every time you want to use your Pi 5. Without a knowledge of the IP address you cannot
login using the SSH or the VNC.

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:

• Login to your Raspberry Pi 5 via the Putty

• Check whether DHCP is active on your Raspberry Pi (it should normally be


active):

pi@raspberrypi:~ $ sudo service dhcpcd status

If DHCP is not active, activate it by entering the following commands:

pi@raspberrypi:~ $ sudo service dhcpcd start


pi@raspberrypi:~ $ sudo systemctl enable dhcpcd

• Find the IP address currently allocated to you by entering command ifconfig


or hostname – I (Figure 2.11). In this example, the IP address was:
[Link]. We can use this IP address as our fixed address since no other
device on the network is currently using it.

● 24

Getting Started with the Raspberry Pi 5 AI [Link] 24 24-10-2024 09:19


Chapter 2 • Installing the Raspberry Pi 5 Operating System and Console Commands

Figure 2.11 Find the IP address using command hostname -I

• Find the IP address of your router by entering the command ip r (Figure 2.12).
In this example the IP address was: [Link]

Figure 2.12 Find the IP address of your router

• Find the IP address of your DNS by entering the following command (Figure
2.13). This is usually same as your router address:

pi@raspberrypi:~ $ grep "nameserver" /etc/[Link]

Figure 2.13 Find the DNS address

• Edit file /etc/[Link] by entering the command:

pi@raspberrypi:~ $ nano /etc/[Link]

• 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]

2.7 Enabling Bluetooth


In this section, you will learn how to enable the Bluetooth on your Raspberry Pi 5 so that it
can communicate with other Bluetooth devices. The steps are given below:

● 25

Getting Started with the Raspberry Pi 5 AI [Link] 25 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

• Enable the Bluetooth on your other device

• 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

• Select raspberrypi in the Bluetooth menu on your other device

• Accept the pairing request on your Raspberry Pi 5

• 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.

2.8 Connecting the Raspberry Pi 5 to a wired network


You may want to connect your Raspberry Pi 5 physically to a network through an Ethernet
cable. The steps are as follows:

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 3: Login to the system by entering your username and password

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

2.8.1 Unable to connect to a wired network


If you find out that you are not assigned an IP address by your router's DHCP, possible
causes are:

• Your network cable is faulty


• The network hub does not support DHCP
• Your Raspberry Pi is not enabled to accept DHCP issued addresses. i.e. it may
have been configured for fixed IP addresses

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

Getting Started with the Raspberry Pi 5 AI [Link] 26 24-10-2024 09:19


Chapter 2 • Installing the Raspberry Pi 5 Operating System and Console Commands

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:

pi@raspberrypi ~$ cat /etc/network/interfaces

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:

iface eth0 inet dhcp

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:

iface eth0 inet static

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:

pi@raspberrypi ~$ sudo cp /etc/network/interfaces /etc/network/[Link]

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]):

iface eth0 inet static


address [Link]
netmask [Link]
gateway [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.

Re-start your Raspberry Pi 5.

● 27

Getting Started with the Raspberry Pi 5 AI [Link] 27 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

2.9 Installing the Raspberry Pi 5 Bookworm operating system on a


blank microSD card
If you have a pre-installed the Raspberry Pi 5 operating system Bookworm on a microSD
card then you can start using it as described earlier in this Chapter and skip this section.
In this section, you will learn how to install the latest Bookworm operating system on a
microSD card if you do not have a pre-installed card.

The steps are as follows:

• Insert a microSD card into your PC. You may need to use an SD card adapter

• Goto site: [Link]

• 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]

• Double click to start the imager program and click to install it

• Click to Finish to run the imager

• Click Operating System and select the operating system at the top of the list
as: Raspberry Pi OS (64-bit). See Figure 2.14

Figure 2.14 Select the operating system

• Click Storage and select the SD card storage

• Click to open the settings (gear shape)

• Click to enable SSH

• Click to enable password authentication

● 28

Getting Started with the Raspberry Pi 5 AI [Link] 28 24-10-2024 09:19


Chapter 2 • Installing the Raspberry Pi 5 Operating System and Console Commands

• Set username and password

• Click to Configure wireless LAN

• Click Save

• Click Write to write the operating system to the microSD card

• Wait until writing and verifying are finished (Figure 2.15)

• Remove the microSD card and insert into your Raspberry Pi 5

Figure 2.15 Wait until finished

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.

2.10 Some commonly used Console commands


Raspberry Pi 5 is based on a version of the Linux operating system. Linux is one of the most
popular operating systems in use today. Linux is very similar to other operating systems,
such as Windows and UNIX. Linux is an Open operating system based on UNIX and has
been developed collaboratively by many companies since 1991. In general, Linux is harder
to manage than some other operating systems like Windows, but offers more flexibility and
configuration options. There are several popular versions of the Linux operating system
such as Debian, Ubuntu, Red Hat, Fedora and so on.

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

Getting Started with the Raspberry Pi 5 AI [Link] 29 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

Also, it is important to remind you that all the commands must be terminated by the Enter
key.

2.10.1 The Console command prompt


Assuming your username is pi, after you login to Raspberry Pi 5, you will see the following
prompt displayed where the system waits for you to enter a command:

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.

2.10.2 System and user information


These commands are useful as they tell you information about the system. Command
cat /proc/cpuinfo displays information about the processor (command cat displays the
contents of a file. In this example, the contents of file /proc/cpuinfo is displayed). Since
there are 4 cores in the Raspberry Pi 5, the display is in 4 sections. Figure 2.16 shows an
example display, where only part of the display is shown here.

Figure 2.16 Command: cat /proc/cpuinfo

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.

Figure 2.17 Command: uname – a

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

Getting Started with the Raspberry Pi 5 AI [Link] 30 24-10-2024 09:19


Chapter 2 • Installing the Raspberry Pi 5 Operating System and Console Commands

command are displayed. Figure 2.18 shows an example, where only part of the display is
shown here.

Figure 2.18 Command: cat /proc/meminfo

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.

Figure 2.19 Commands: useradd and passwd

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.

Figure 2.20 Logging in to a new account

Command sudo apt-get upgrade is used to upgrade all the software packages on the
system.

● 31

Getting Started with the Raspberry Pi 5 AI [Link] 31 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

2.10.3 The directory structure


The Raspberry Pi 5 directory structure consists of a single root directory, with directories
and subdirectories under the root. Different types of operating system programs and
application programs are stored in different directories and subdirectories.

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.

Figure 2.21 Raspberry Pi 5 directory structure (only part of it is shown)

Some useful directory commands are given below. Command pwd displays the user home
directory:

pi@raspberrypi: ~$ pwd
/home/pi
pi@raspberry: ~$

To show the directory structure, enter the command ls / (Figure 2.22):

Figure 2.22 Directory structure

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

Getting Started with the Raspberry Pi 5 AI [Link] 32 24-10-2024 09:19


Chapter 2 • Installing the Raspberry Pi 5 Operating System and Console Commands

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.

To display the subdirectories and files in a single row:

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: ~$

To list the results, separated by commas:

pi@raspberrypi: ~$ ls –m
Bookshelf, Desktop, Documents, Downloads, Music, Pictures, Public, Templates,
Videos
pi@raspberrypi: ~$

You can mix the arguments as shown in Figure 2.23.

Figure 2.23 Mixing the arguments

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

Getting Started with the Raspberry Pi 5 AI [Link] 33 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

2.10.4 File Permissions


One of the important arguments used with the ls command is -l (lower case letter l) which
displays the file permissions, file sizes, and when they were last modified. In the example
below, each line relates to one directory or file. Reading from right to left, the name of the
directory or the file is on the right-hand side. The date the directory or file was created is
on the left-hand side of its name. Next comes the size, given in bytes. The characters at
the beginning of each line are about the permissions. i.e. who is allowed to use or modify
the file or the directory.

The permissions are divided into 3 categories:

• What the user (or owner, or creator) can do – called USER


• What the group owner (people in the same group) can do - GROUP
• What everyone else can do – called WORLD

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.

Figure 2.24 File permissions example

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:

drwxr-xr-x which translates to:

● 34

Getting Started with the Raspberry Pi 5 AI [Link] 34 24-10-2024 09:19


Chapter 2 • Installing the Raspberry Pi 5 Operating System and Console Commands

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:

u: user (or owner)


g: group
o: other (world)
a: all

+: 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.

Figure 2.25 File permissions

We will be changing the permissions so that the user does not have read permission on
this file:

● 35

Getting Started with the Raspberry Pi 5 AI [Link] 35 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

pi@raspberrypi: ~$ chmod u-r [Link]


pi@raspberrypi: ~$ ls –lh

The result is shown in Figure 2.26.

Figure 2.26 File permissions of [Link]

Notice that if you now try to display the contents of file [Link] using the cat
command you will get an error message:

pi@raspberrypi: ~$ cat [Link]


cat: [Link]: Permission denied
pi@raspberrypi: ~$

All the permissions can be removed from a file by the following command:

pi@raspberrypi: ~$ chmod a= [Link]

In the following example, rwx user permissions are given to file [Link]:

pi@raspberrypi: ~$ chmod u+rwx [Link]

Figure 2.27 shows the new permissions of file [Link].

Figure 2.27 New permissions of file [Link]

● 36

Getting Started with the Raspberry Pi 5 AI [Link] 36 24-10-2024 09:19


Chapter 2 • Installing the Raspberry Pi 5 Operating System and Console Commands

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 go up one directory level, i.e. to our default working directory:

pi@raspberrypi: ~/Music $ cd..


pi@raspberrypi: ~$

To change your working directory to Music, you can also enter the command:

pi@raspberrypi: ~$ cd ~/Music
pi@raspberrypi: ~/myfiles $

To go back to the default working directory, you can enter:

pi@raspberrypi: ~/Music $ cd ~
pi@raspberrypi: ~$

To find out more information about a file you can use the file command. For example:

pi@raspberrypi: ~$ file [Link]


[Link]: ASCII text
pi@raspberrypi: ~$

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

Figure 2.28 Command: ls –R

To display information on how to use a command, you can use the man command. Enter
q to exit from the man display.

● 37

Getting Started with the Raspberry Pi 5 AI [Link] 37 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

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:

pi@raspberrypi: ~$ man ls | less


<display of help on using the ls command>
pi@raspberrypi: ~$

2.10.6 Date and Time


To display the current date and time the date command is used.

2.10.7 File processing


To make copy of a file, use the command cp. In the following example, a copy of file
[Link] is made and the new file is given the name [Link]:

pi@raspberrypi: ~$ cp [Link] [Link]


pi@raspberrypi: ~$

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]:

pi@raspberrypi: ~$ mv [Link] [Link]


pi@raspberrypi: ~$

● 38

Getting Started with the Raspberry Pi 5 AI [Link] 38 24-10-2024 09:19


Chapter 2 • Installing the Raspberry Pi 5 Operating System and Console Commands

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:

pi@raspberrypi: ~$ rm –vi [Link]


rm: remove regular file '[Link]'? y
removed '[Link]'
pi@raspberrypi: ~$

Sorting a file
The command sort displays the contents of a file in ascending order. The general format of
this command is:

sort <options> <filename>

Valid options are:


-u removes duplicates from the output
-r sorts the output in descending order
-o writes the sorted output to a file

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:

pi@raspberrypi: ~$ rmdir Music


pi@raspberrypi: ~$

Re-directing the output


The greater sign > can be used to re-direct the output of a command to a file. For example,
we can re-direct the output of the ls command to a file called [Link]:

pi@raspberrypi: ~$ ls > [Link]


pi@raspberrypi: ~$

● 39

Getting Started with the Raspberry Pi 5 AI [Link] 39 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

The cat command can be used to display the contents of a file:

pi@raspberrypi: ~$ cat [Link]


This is a file
This is line 2
pi@raspberrypi: ~$

Using two greater signs >> adds to the end of a file.

Writing to the screen or to a file


The echo command can be used to write to the screen. It can be used to perform simple
mathematical operations if the numbers and the operation are enclosed in two brackets,
preceded by a $ character:

pi@raspberrypi: ~$ echo $((5*6))


30
pi@raspberrypi: ~$

The echo command can also be used to write a line of text to a file. An example is shown
below:

pi@raspberrypi: ~$ echo a line of text > [Link]


pi@raspberrypi: ~$ cat [Link]
a line of text
pi@raspberrypi: ~$

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:

pi@raspberrypi: ~$ grep line [Link]


a line of text
pi@raspberrypi: ~$

2.10.8 Head and tail commands


The head command can be used to display the first 10 lines of a file. The format of this
command is as follows:

pi@raspberrypi: ~$ head [Link]


…………………………………..
…………………………………..
pi@raspberrypi: ~$

Similarly, the tail command is used to display the last 10 lines of a file. The format of this
command is as follows:

● 40

Getting Started with the Raspberry Pi 5 AI [Link] 40 24-10-2024 09:19


Chapter 2 • Installing the Raspberry Pi 5 Operating System and Console Commands

pi@raspberrypi: ~$ tail [Link]


………………………………….
………………………………….
pi@raspberrypi: ~$

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: ~$ which python


/usr/bin/python
pi@raspberrypi: ~$

2.10.9 Super User commands


Some of the commands are privileged and only the authorized persons can use them.
Inserting the word sudo at the beginning of a command gives us the authority to use the
command without having to login as an authorized user.

What software is installed on my Raspberry Pi 5


To find out what software is installed on your Raspberry Pi 5, enter the following command.
You should get several pages of display:

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:

pi@raspberrypi: ~$ dpkg --s xpdf


Package: xpdf
Status: install ok installed
Priority: optional
Section: text
Installed-Size: 395
………………………….
………………………….
pi@raspberrypi: ~$

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):

pi@raspberrypi: ~$ dpkg –s bbgd

dpkg-query: package 'bbgd' is not installed and no information is available

● 41

Getting Started with the Raspberry Pi 5 AI [Link] 41 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

……………………………………………………………………………..
…………………………………………………………………………….
pi@raspberrypi: ~$

2.10.10 Resource monitoring on Raspberry Pi 5


System monitoring is an important topic for managing usage of your Raspberry Pi. One of
the most useful system monitoring commands is the top, which displays the current usage
of system resources and displays which processes are running and how much memory and
CPU time they are consuming.

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: ~$

Figure 2.29 Typical system resource display

Some of the important points in Figure 2.29 are summarized below (for lines 1 to 5 of the
display):

• There are total of 136 processes in the system


• Currently only one process is running, 135 process are sleeping, and 0
processes are stopped
• The percentage CPU utilization is 0.0us for user applications (us)
• The percentage CPU utilization for system applications is 33.3 (sy)
• There are no processes requiring more or less priority (ni)
• 100% of the time the CPU is idle (id)
• There are no processes waiting for I/O completion (wa)
• There are no processes waiting for hardware interrupts (hi)
• There are no processes waiting for software interrupts (si)
• There is no time reserved for a hypervisor (st)

● 42

Getting Started with the Raspberry Pi 5 AI [Link] 42 24-10-2024 09:19


Chapter 2 • Installing the Raspberry Pi 5 Operating System and Console Commands

• 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:

• PID: the process ID number


• USER: owner of the process
• PR: priority of the process
• NI: the nice value of the process
• VIRT: the amount of virtual memory used by the process
• RES: size of the resident memory
• SHR: shared memory the process is using
• S: process status (sleeping, running,, zombie)
• %CPU: the percentage of CPU consumed
• %MEM: percentage of RAM used
• TIME+: total CPU time the task used
• COMMAND: The actual name of the command

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.

Figure 2.30 Command: ps

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:

pi@raspberrypi: ~$ kill -9 <PID>

Disk (micro SD card) usage


The disk free command df can be used to display the disk usage statistics. An example is
shown in Figure 2.31. option –h displays in human readable form.

● 43

Getting Started with the Raspberry Pi 5 AI [Link] 43 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

Figure 2.31 Command: df -h

Command free shows how much memory is used and the amount of free memory.

2.10.11 Shutting down


Although you can disconnect the power supply from your Raspberry Pi 5 when you finish
working with it, it is not recommended since there are many processes running on the
system and it is possible to corrupt the file system. It is much better to shut down the
system in an orderly manner.

The following command will stop all the processes and make the file system safe and then
turn off the system safely:

pi@raspberrypi: ~$ sudo halt

The following command stops and then re-starts the system:

pi@raspberrypi: ~$ sudo reboot

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:

pi@raspberrypi: ~$ shutdown –r <time> <message>

e.g.

pi@raspberrypi: ~$ sudo shutdown -h 01:55: To shutdown at 1:55 AM.

or enter the following command to shutdown now:

pi@raspberrypi: ~$ sudo shutdown now

Broadcast message from root @raspberrypi on pts/1 (Tue 2023-10-03 12:03:00 BST)

The system will power off now!

● 44

Getting Started with the Raspberry Pi 5 AI [Link] 44 24-10-2024 09:19


Chapter 2 • Installing the Raspberry Pi 5 Operating System and Console Commands

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:

ifconfig: check the IP address of your Raspberry Pi

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

Figure 2.32 Command iwconfig

ping: used to test the availability of a network device. an example is shown in Figure 2.33

Figure 2.33 Comamnd ping

wget: this command is used to download a file from the web and saves the file in the
current directory.

hostname – I: shows the IP address of the Raspberry Pi

The command vcgencmd measure_temp displays the CPU temperature as shown in


Figure 2.34

Figure 2.34 Displaying the CPU temperature

● 45

Getting Started with the Raspberry Pi 5 AI [Link] 45 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

2.10.13 System information and some other useful commands


The uname command is used to display system information. This command has the
following options:

-a Show all system information

-s display the kernel name

-n print the network node hostname

-r print the kernel release

-v print the kernel version number

-m print the system hardware name

-p print the processor type

-i print the hardware platform type

-o print the operating system type

Some examples are shown in Figure 2.35

Figure 2.35 The uname command

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.

Figure 2.36 The history command

The clear command is also useful and it is used to clear the screen.

To install a package use the command: sudo apt install <package_name>

● 46

Getting Started with the Raspberry Pi 5 AI [Link] 46 24-10-2024 09:19


Chapter 2 • Installing the Raspberry Pi 5 Operating System and Console Commands

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

Getting Started with the Raspberry Pi 5 AI [Link] 47 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

Chapter 3 • The Raspberry Pi AI Kit

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.

Figure 3.1 The Raspberry Pi 5 AI Kit

● 48

Getting Started with the Raspberry Pi 5 AI [Link] 48 24-10-2024 09:19


Chapter 3 • The Raspberry Pi AI Kit

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.

3.2 Raspberry Pi 5 M.2 HAT+ adapter board


The Raspberry Pi M.2 HAT+ adapter board (or HAT+ for short) enables you to connect M.2
compatible devices such as NVMe drives and other PCIe peripherals to the PCIe interface
of your Raspberry Pi 5.

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+.

Figure 3.2 shows the HAT+ mounted on top of a Raspberry Pi 5.

Figure 3.2 Raspberry Pi 5 with the HAT+

The HAT+ has the following features (see link: [Link]


accessories/[Link] for further details):

• Single-lane PCIe 2.0 interface (500 MB/s peak transfer rate)


• Supports devices that use the M.2 M key edge connector
• Supports devices with the 2230 or 2242 form factor
• Supplies up to 3A to connected M.2 devices
• Power and activity LEDs

● 49

Getting Started with the Raspberry Pi 5 AI [Link] 49 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

• Conforms to the Raspberry Pi HAT_ specification


• Includes:
- ribbon cable
- 16mm GPIO stacking header
- 4 threaded spacers
- 8 screws
- 1 knurled double-flanged drive attachment screw to secure and support the
M.2 peripheral

3.2.1 Installing the HAT+ and the AI Kit


The steps to install the HAT+ are as follows (the figures in this section are taken from:
[Link]

• Remove power from your Raspberry Pi 5

• Install the active cooler (Figure 3.3)

Figure 3.3 Install the active cooler

• Install your camera as described in section 4.3

• 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

Getting Started with the Raspberry Pi 5 AI [Link] 50 24-10-2024 09:19


Chapter 3 • The Raspberry Pi AI Kit

Figure 3.4 Install the spacers using the provided screws

• Place the HAT+ on top of the spacers, and use the four remaining screws to
secure it in place (Figure 3.5)

Figure 3.5 Place the HAT+ on top of spacers

● 51

Getting Started with the Raspberry Pi 5 AI [Link] 51 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

• 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.

Figure 3.6 Insert the ribbon cable

• Remove the drive attachment screw by turning the screw counter-clockwise.


Insert your AI Kit into the M.2 key edge connector, sliding into the slot at a
slight upward angle (Figure 3.7)

Figure 3.7 Insert the AI Kit

• 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

Getting Started with the Raspberry Pi 5 AI [Link] 52 24-10-2024 09:19


Chapter 3 • The Raspberry Pi AI Kit

Figure 3.8 Screw until it feels secure

• Figure 3.9 shows the complete installation.

Figure 3.9 Complete installation

3.2.2 Installing the AI Kit software


You should have installed the Raspberry Pi 5 operating system by now as described in
chapter 2. Following additional software instructions are needed before you can start using
your AI Kit (characters entered by the user are in bold for clarity).

• Update the operating system to latest version:

pi@raspberrypi:~ $ sudo apt update


pi@raspberrypi:~ $ sudo apt full-upgrade

• Ensure that the Raspberry Pi 5 firmware is up to date:

pi@raspberrypi:~ $ sudo rpi-eeprom-update

● 53

Getting Started with the Raspberry Pi 5 AI [Link] 53 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

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:

pi@raspberrypi:~ $ sudo raspi-config

Under Advanced Options -> Bootloader Version, choose Latest. Then exit
raspi-config with Finish or the Escape key. Run the following command:

pi@raspberrypi:~ $ sudo rpi-eeprom-update –a

• To achieve optimal performance from the Hailo device, it is necessary to set


PCIe to Gen3. While using Gen2 is an option, it will result in lower performance.
The steps are:

pi@raspberrypi:~ $ sudo raspi-config

Select option 6. Advanced Options, then select option A8 PCIe Speed. Choose
Yes to enable PCIe Gen 3 mode. Click Finish to exit.

• Reboot your Raspberry Pi 5:

pi@raspberrypi:~ $ sudo reboot

• Now, we will install the Hailo software (version 4.17):

pi@raspberrypi:~ $ sudo apt install hailo-all

• The above command installs the following dependencies:


- Hailo kernel device driver and firmware
- HailoRT runtime software
- Hailo Tappas core package
- The rpicam-apps Hailo post-processing software demo stages

• Now, reboot your Raspberry Pi 5:

pi@raspberrypi:~ $ sudo reboot

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.

3.2.3 Verifying the software installation


You should verify the installation to make sure that all the software components are in place
and the AI kit can be recognized by the Raspberry Pi 5. The steps are:

● 54

Getting Started with the Raspberry Pi 5 AI [Link] 54 24-10-2024 09:19


Chapter 3 • The Raspberry Pi AI Kit

• Make sure the AI Kit hardware is installed on your Raspberry Pi 5 as described


earlier in this chapter.

• Enter the following command:

pi@raspberrypi:~ $ hailortcli fw-control identify

If everything is ok, you should see an output like Figure 3.10. Note that the software
version is 4.17.

Figure 3.10 Verifying the software installation

• Test TAPPAS Core installation by running the following commands:

pi@raspberrypi:~ $ gst-inspect-1.0 hailotools

You should see something similar to Figure 3.11.

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

Getting Started with the Raspberry Pi 5 AI [Link] 55 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

• Additionally, you can run the following command to check the kernel logs:

pi@raspberrypi:~ $ dmesg | grep –I hailo

You should see an output like Figure 3.12.

Figure 3.12 Check 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

Getting Started with the Raspberry Pi 5 AI [Link] 56 24-10-2024 09:19


Chapter 4 • Raspberry Pi Cameras

Chapter 4 • Raspberry Pi Cameras

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.

4.2 Raspberry Pi cameras


There are several Raspberry Pi camera modules. Version 1 was released in 2013, and it
was a 5-megapixel model, which is not available anymore from Raspberry Pi. This was
followed by Version 2, which was 8-megapixels and was released in 2016. The latest model
Version 3 is 12-megapixels and was released in 2023. Additionally, 12-megapixel high
quality camera for use with external lenses with CS or M12 type mounting were released in
2020 and 2023 respectively. Raspberry Pi cameras are available as either visible light type
or infrared type for night vision. All cameras can take high-resolution pictures along with
HD 1080p video and they can all be controlled by software. See the following link for further
information on Raspberry Pi cameras:


[Link]
the-camera-modules

A brief comparison of the various Raspberry Pi cameras is shown in Table 4.1.

V1 V2 V3 HQ GS

Size (mm) 25x24x9 25x24x9 25x24x11.5 38x38x18 38x38x19

Weight (g) 3 3 4 30.4 34

Sensor OmniVision Sony Sony Sony Sony


OV5647 IMX219 IMX708 IMX477 IMX296

Still resolution 5 8 11.9 12.3 1.58


(Mpixels)

Video modes 1080p30, 1080p47 2304x1296p56 2028x1080p50 1456x1088p60


720p60 1640x1232p41 2304x1296p30 2028x1520p40
640x480p60/90 640x480p206 1536x864p120 1332x990p120

Sensor resolu- 2592x1944 3280x2464 4608x2592 4056x3040 1456x1088


tion (pixels)

Pixel size 1.4x1.4 1.12x1.12 1.4x1.4 1.55x1.55 3.45x3.45


(um)

Focal length 3.60 3.04 4.74 Dep on lens Dep on lens


(mm)

Focus Fixed Adjustable Motorized Adjustable Adjustable

Table 4.1 Raspberry Pi cameras

● 57

Getting Started with the Raspberry Pi 5 AI [Link] 57 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

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:

• Sony IMX477 back illuminated sensor


• 12.3 Megapixels, 7.9mm sensor diagonal
• Output: RAW12/10/8
• Back focus length of lens: 2.6mm-11.8mm
• Lens sensor format: 7.9mm or larger
• IR cut filter: Integrated
• Ribbon cable length: 200mm

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).

Figure 4.1 15-pin to 22-pin camera cable

4.3 Installing the camera


The steps to install the camera on your Raspberry Pi 5 are as follows:

• Make sure that your Raspberry Pi 5 is switched off

• Locate one of the CSI camera ports (Figure 1.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

Getting Started with the Raspberry Pi 5 AI [Link] 58 24-10-2024 09:19


Chapter 4 • Raspberry Pi Cameras

• Push down the plastic cable holder slider so that it locks and holds the cable

• Connect the 22-pin side of the cable to your camera

• Apply power to your Raspberry Pi 5

4.3.1 Verify the camera installation


Enter the following command to ensure that your camera has been installed successfully:

rpi@raspberrypi:~ $ rpicam-hello --list-cameras

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:

Edit [Link]: sudo nano /boot/firmware/[Link]


change line: camera_auto_detect=1 to camera_auto_detect=0
insert line: dtoverlay=imx708
Type CntrlX followed by Y to save and exit

4.4 rpicam camera commands


The command rpicam is used to take images. This command has the following options:

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

Getting Started with the Raspberry Pi 5 AI [Link] 59 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

For example, the following command takes a snapshot image and the preview window
remains open for 2 seconds:

rpi@raspberrypi:~ $ rpicam-hello --timeout 2000

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 –n flag can be used to suppress the preview window.

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:

rpi@raspberrypi:~ $ rpicam-hello --info-text "shutter speed %exp"

Directive Substitution

%frame Sequence number of the frame.

%fps Instantaneous frame rate.

%exp Shutter speed used to capture the image, in microseconds.

%ag Analogue gain applied to the image in the sensor.

%dg Digital gain applied to the image by the ISP.

%rg Gain applied to the red component of each pixel.

%bg Gain applied to the blue component of each pixel.

%focus Focus metric for the image, where a larger value implies a sharper image.

%lp Current lens position in dioptres (1 / distance in metres).

%afstate Autofocus algorithm state (idle, scanning, focused or failed).

Table 4.2 –info-text options

rpicam-jpeg: Runs a preview window, then captures high-resolution still images. To


capture a JPEG image to a file named [Link], enter the following command. This
command will capture the image, leave the preview window open for 5 seconds, and then
save the image in file [Link]. The --timeout option can be used to change the
preview window duration Also, options –width and –height can be used to change the
resolution of the saved image.

rpi@raspberrypi:~ $ rpicam-jpeg --output [Link]

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

Getting Started with the Raspberry Pi 5 AI [Link] 60 24-10-2024 09:19


Chapter 4 • Raspberry Pi Cameras

rpi@raspberrypi:~ $ rpicam-jpeg –output [Link] –timeout 2000 –width


640 –height 480

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:

rpi@raspberrypi:~ $ rpicam-vid –t 5s –o 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.

Figure 4.2 Command: list-cameras

● 61

Getting Started with the Raspberry Pi 5 AI [Link] 61 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

version: Displays the version number. An example is shown in Figure 4.3.

Figure 4.3 Command: version

camera: Selects the camera to use

preview: Sets the location (x, y coordinates) and size (with, height) of the preview window

fullscreen: Forces the preview window to display in full screen format

hflip: Flips the image horizontally

vflip: Flips the image vertically

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).

Figure 4.4 Command: help

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

Getting Started with the Raspberry Pi 5 AI [Link] 62 24-10-2024 09:19


Chapter 5 • Using a Text Editor in Console Mode

Chapter 5 • Using a Text Editor in Console Mode

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.

5.1 nano text editor


This is the recommended text editor for developing Raspberry Pi 5 programs in Console
mode.

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:

pi@raspberrypi: ~ $ nano [Link]

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:

Ctrl+W: Search for a word


Ctrl+V: Move to next page
Ctrl+Y: Move to previous page
Ctrl+K: Cut the current row of txt
Ctrl+R: Read file
Ctrl+U: Paste the text you previously cut
Ctrl+J: Justify
Ctrl+\: Search and replace text
Ctrl+C: Display current column and row position
Ctrl+G: Get detailed help on using the nano
Ctrl+-: Go to specified line and column position
Ctrl+O: Save (write out) the file currently open
Ctrl+X: Exit nano

● 63

Getting Started with the Raspberry Pi 5 AI [Link] 63 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

Figure 5.1 nano text editor screen

Now, type the following text as shown in Figure 5.2:

nano is a simple and yet powerful text editor.


This simple text example demonstrates how to use nano.
This is the last line of the example.

Figure 5.2 Sample text

The use of nano is now demonstrated with the following steps:

Step 1: Go the beginning of the file by moving the cursor.

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

Getting Started with the Raspberry Pi 5 AI [Link] 64 24-10-2024 09:19


Chapter 5 • Using a Text Editor in Console Mode

Figure 5.3 searching word simple

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.

Figure 5.4 Cutting the first line

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).

Figure 5.5 Paste the line cut previously

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).

Figure 5.6 Displaying row and column position of a word

● 65

Getting Started with the Raspberry Pi 5 AI [Link] 65 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

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.

Figure 5.7 Displaying 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).

Figure 5.8 Moving to line 2, column 5

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

Getting Started with the Raspberry Pi 5 AI [Link] 66 24-10-2024 09:19


Chapter 5 • Using a Text Editor in Console Mode

Figure 5.9 Replacing text

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.

Step 10: Display the contents of the file:

pi@raspberrypi: ~ $ cat [Link]


This simple text file demonstrates how to use nano.
Nano is a simple and yet powerful text editor
This is the last line of the example.

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.

5.2 vi text editor


The vi text editor has been around for many years where it has been the standard Unix
operating system default text editor. The vi editor is a fully featured powerful text editor for
doing many different tasks. The only problem with using vi is that it is not very user friendly
and learning it may take some time. This is not the recommended text editor, but readers
who are familiar with it can continue using it..

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:

ZZ save changes and exit vi


:wq save changes and exit vi
:q! exit without saving changes

● 67

Getting Started with the Raspberry Pi 5 AI [Link] 67 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

h move cursor left (backwards)


j move cursor down
k move cursor up
l move cursor right (spacebar)

$ move to the last column on current line


o move cursor to the first column on current line
w move cursor to the beginning of the next word
b move cursor to the beginning of the previous word
H move cursor to the top of the screen
M move cursor to the middle of the screen
L move cursor to the bottom of the screen

G move to the last line in the file


nG move to line n

r replace character under cursor with next character typed


i insert before cursor
a append after cursor
A append at end of line

x delete character under cursor


dd delete line under cursor
dw delete word under cursor

/ search for a word (forwards)


? search a word (backwards)
:s search and replace a word in current line

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

Getting Started with the Raspberry Pi 5 AI [Link] 68 24-10-2024 09:19


Chapter 5 • Using a Text Editor in Console Mode

Figure 5.10 vi text editor screen

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):

The vi text editor is a powerful text editor.


But it is not easy to use this editor.
This exercise should help you understand the basic commands.

Figure 5.11 Entering the text

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

Getting Started with the Raspberry Pi 5 AI [Link] 69 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

Figure 5.12 Searching for text

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 7: Delete the second line where the cursor is on by typing dd

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.

5.3 Using the Thonny


Thonny is more than a simple text editor. It is an Integrated Development Environment
(IDE) which can be used to write programs and then to upload them to your Raspberry Pi 5.

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

Getting Started with the Raspberry Pi 5 AI [Link] 70 24-10-2024 09:19


Chapter 5 • Using a Text Editor in Console Mode

5.3.1 The Thonny IDE


Start the Thonny IDE from the Desktop under the Programming menu. Figure 5.13 shows
the Thonny startup menu.

Figure 5.13 Thonny IDE startup menu

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.

In the upper part contains the following menu items:

New: click to create a new program


Load: load an existing program from a folder on Raspberry Pi
Save: save an existing program in the screen to a file
Run: run the program in the screen
Debug: debug the program in the screen
Over: used by the debugger
Into: used by the debugger
Out: used by the debugger
Stop: stop a running program
Zoom: zoom the screen
Quit: Exit the Thonny IDE

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

Getting Started with the Raspberry Pi 5 AI [Link] 71 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

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.

6.2 Method 1 – Interactively from command prompt in Console mode


In this method, you will login to your Raspberry Pi 5 using the SSH and then create and run
the Python program interactively. This method is excellent for small programs. The steps
are as follows:

• Login to the Raspberry Pi 5 using SSH

• At the command prompt enter python. You should see the Python command
mode which is identified by three characters >>>

• Type the program:

print ("Hello From Raspberry Pi 5")

• 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.

Figure 6.1 Running a program interactively

• Type Cntrl+z to exit from the program

6.3 Method 2 – Create a Python file in Console mode


In this method, you will login to your Raspberrry Pi 5 using the SSH as before and then
create a Python file. A Python file is simply a text file with the extension .py. You can use a
text editor, e.g. the nano text editor to create your file. In this example, a file called hello.
py is created using the nano text editor. Figure 6.2 shows the contents of file [Link].
This figure also shows how to run the file under Python. Notice that the program is run by
entering the command:

● 72

Getting Started with the Raspberry Pi 5 AI [Link] 72 24-10-2024 09:19


Chapter 6 • Creating and Running a Simple Python Program

>>> python [Link]

Figure 6.2 Creating and running a Python file

6.4 Method 3 – Create a Python file in Desktop GUI mode


In this method, you can login to your Raspberry Pi 5 using either a directly connected
terminal through the mini HDMI port, or if you do not have a monitor, you can login to the
Desktop using the VNC as described earlier, and then create and run your Python programs
in GUI mode using the Thonny IDE. The steps are:

• Start the Thonny program (Figure 6.3)

Figure 6.3 Thonny startup screen

• Type the following code to upper part of the screen:

print("Hello from Raspberry Pi 5")

• Click File -> Save and save with the name [Link] (Figure 6.4)

Figure 6.4 Type your program and save it

● 73

Getting Started with the Raspberry Pi 5 AI [Link] 73 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

• 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.

Figure 6.5 Run the program

6.5 Which method?


The choice of a method depends upon the size and complexity of the program to be
developed. Small programs can be run interactively without creating a program file. Larger
programs can be created as Python files and then they can run either in the console mode
or in Desktop GUI mode under the Thonny IDE. Running under the Thonny IDE has the
advantage that justification of the code is corrected automatically as you write the code.
If the program accesses the camera interface (which is the case with the AI Kit demo
programs) then it is necessary to run your programs in Desktop GUI mode by using a
directly connected monitor to your Raspberry Pi mini HDMI port.

● 74

Getting Started with the Raspberry Pi 5 AI [Link] 74 24-10-2024 09:19


Chapter 7 • Python Programming

Chapter 7 • Python Programming

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.

Python supports object oriented techniques of programming. It is a beginners' language


which is easy to learn and easy to maintain. Beginners can easily learn programming on
relatively short period of time. Python supports a large library of functions which makes
it very powerful. The language is portable, meaning that it can run on several different
popular platforms.

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.

7.2 Variable names


Python variable names are case sensitive and can start with a letter A to Z or a to z or
an underscore character "_", followed by more letters or numbers 0 to 9. Some valid and
invalid example variable names are given below:

SUM - valid
Sum - valid
SUm - valid
_total - valid
Cnt5 - valid
8tot - invalid
%int - invalid
&xyz - invalid
My_Number - valid
@loop - invalid
_Account - valid

● 75

Getting Started with the Raspberry Pi 5 AI [Link] 75 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

Note that variables total, Total, TOTAL, ToTaL, or toTAL are all different.

7.3 Reserved words


There are some words which are reserved for use by the Python interpreter and thus cannot
be used as variable names by programmers. A list of these reserved words are given below.
Notice that all the reserved words contain lower case letters:

and for raise


assert from return
break global try
class if while
continue import with
def in yield
del is
elif lambda
else not
except or
exec pass
finally print

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:

# This is a comment line

Comments can also be inserted after a statement:

Sum = 0 # Another comment

7.5 Line continuation


The line continuation character "\" can be used to continue a statement on following lines.
An example is shown below:

Sum = a +\
b +\
c

Which is equivalent to:

Sum = a + b + c

7.6 Blank lines


A line containing no statements is ignored by the Python interpreter.

● 76

Getting Started with the Raspberry Pi 5 AI [Link] 76 24-10-2024 09:19


Chapter 7 • Python Programming

7.7 More than one statement on a line


It is permissible to have more than one statement on a single line by separating the
statements with a semicolon character. An example is given below:

cnt = 5; sum = 0; tot = 20;

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

7.9 Python data types


Python supports the following data types:

• Numbers
• Strings
• Lists
• Dictionaries
• Tuples
• Sets
• Files

● 77

Getting Started with the Raspberry Pi 5 AI [Link] 77 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

7.10 Numbers
Python supports the following numeric variable types:

• int - signed integer


• long - long integer
• float - floating point real number
• Complex number

Numbers can be represented in decimal, Octal, binary, or hexadecimal. Long integers are
shown with an upper case letter L.

Some example numbers are shown below:

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

We can delete a variable object by using the del statement:

del sum, a

● 78

Getting Started with the Raspberry Pi 5 AI [Link] 78 24-10-2024 09:19


Chapter 7 • Python Programming

We can assign a value to several variables at the same time:

w=x=y=z=0

Similarly, we can have statements of the form:

w, x, y = 3, 5, 8

Which is equivalent to:

w=3
x=5
y=8

we can perform the following mathematical operations on numbers:

Expression operators
+ addition
- Subtraction
* multiplication
/ division
>> shift right
<< shift left
** power (exponentiation)
% remainder

Bitwise operators
| bitwise OR
& bitwise AND
^ bitwise exclusive-or
~ bitwise complement

Some mathematical functions


pow(x,y) same as x**y
abs(x) absolute value of x
round(x,n) round x to n digits from the decimal point
floor(x) largest integer not greater than x
int(x) convert x to integer
hex(x) hexadecimal equivalent of integer x
bin(x) binary equivalent of integer x
exp(x) exponential of x
factorial(n) factorial of number n
ceil(x) smallest integer not less than x
log(x) natural logarithm of x (base 2)
log10(x) logarithm of x (base 10)

● 79

Getting Started with the Raspberry Pi 5 AI [Link] 79 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

Some mathematical utility libraries


random random number library
math mathematics library

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]

random library is useful to generate random numbers. Function randint(a, b) in this


library generates an integer random number between integers a and b inclusive. Details of
functions available in the random library can be obtained from the following link:

[Link]

Figure 7.1 Using numbers in Python

● 80

Getting Started with the Raspberry Pi 5 AI [Link] 80 24-10-2024 09:19


Chapter 7 • Python Programming

Figure 7.2 Using numbers in Python

Figure 7.3 Using numbers in Python

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:

myname = "James Booth"

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

Getting Started with the Raspberry Pi 5 AI [Link] 81 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

Figure 7.4 String manipulation operations

Figure 7.5 String manipulation operations

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

7.11.1 String functions


Python supports many string functions. Some commonly used string functions are given
below:

● 82

Getting Started with the Raspberry Pi 5 AI [Link] 82 24-10-2024 09:19


Chapter 7 • Python Programming

• capitalize() change first letter of a string to upper case and all


other characters to lower case
• count(str,beg,end) find how many times str occurs in a string. String
starting and ending positions should be specified
• find(str,beg,end) determine if str occurs in a string. String starting
and ending positions should be specified. The index is
returned if the str is found, otherwise -1 is returned
• len(string) return the length of a string
• isalpha() return true if string contains all alphabetical characters
• isalnum() return true if string contains alphabetical and numeric
characters
• isdigit() return true if string contains all digits
• islower() return true if string contains all lower case letters
• isupper() return true if string contains all upper case letters
• lower() convert all upper case characters to lower case
• upper() convert all lower case characters to upper case
• lstrip() remove all leading white spaces
• rstrip() remove all trailing spaces
• swapcase() change case of all letters

Figure 7.6 shows examples of using some of the string functions.

Figure 7.6 Using the string functions

7.11.2 Escape sequences


Escape sequences are special non-printable characters used to generate functions such as
newline, tab, formfeed, carriage return and so on. Escape sequences start with character
"\." A list of the commonly used escape sequences is given below:

• \n newline
• \a bell
• \b backspace
• \f formfeed

● 83

Getting Started with the Raspberry Pi 5 AI [Link] 83 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

• \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")

7.12 Print statement


The print statement is one of the most commonly used statements. It displays text
or numbers on the screen. Text is displayed by enclosing it in quotes. Numeric data is
displayed by simply entering the variable name. The data to be displayed is enclosed in
round brackets. Text and numeric data can be mixed in display outputs and the type of the
variable to be displayed can be declared using formatting characters. A list of the commonly
used formatting characters is given below:

• %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

Figure 7.7 shows some examples of using the print statement.

Figure 7.7 Using the print statement

● 84

Getting Started with the Raspberry Pi 5 AI [Link] 84 24-10-2024 09:19


Chapter 7 • Python Programming

7.13 List variables


List variables are variables separated by commas and enclosed in square brackets. The
variables in a list can be of different types. The contents of a list can be accessed using
square brackets to index te required item inside the list. Indexing starts from 0. As with the
strings, the "*" character can be used for repetition and the "+" character can be used for
concatenation. Some examples are given below:

mylist = ['John', 'Adam', 230, 12.25, 'Peter', 89]


second = [30, 23]

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:

>>> M[1] # Elements of row 1


[4, 5, 6]

>>> M[1][1] # Element at row 1, column 1


5

The statement L = [ ] creates an empty list called L.

● 85

Getting Started with the Raspberry Pi 5 AI [Link] 85 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

7.13.1 List functions


Python language supports large number of list functions. Some commonly used list functions
are given below:

• del([i:j]) delete elements from i to j-1


• [Link](x) append an item to the end of a list
• [Link]([x,y,z]) add several items to the list
• cmp(L1,L2) compare elements of lists L1 and L2
• len(L) length of list L
• max(L) item with the maximum value
• min(L) item with the minimum value
• [Link](x) returns how many times x occurs in a list
• [Link](x) returns the position of the first occurrence of x
• [Link](i,x) inserts x at position i in the list
• [Link](x) removes the indexed item from the list
• [Link]() reverses a list
• [Link]() sorts a list
• [Link]() delete and return the last item

Figure 7.8 shows some examples of using the print statement.

Figure 7.8 Using the list functions

7.14 Tuple variables


Tuples are similar to lists, but their contents cannot be changed. i.e. they are read-only.
Also, tuple variables are enclosed in round brackets (parenthesis). Some examples are
given below:

mytuple = ['John', 'Adam', 230, 12.25, 'Peter', 89]


second = [30, 23]

● 86

Getting Started with the Raspberry Pi 5 AI [Link] 86 24-10-2024 09:19


Chapter 7 • Python Programming

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

7.15 Dictionary variables


Dictionaries are similar to hash tables with keys and values. Each key is separated from
its value by a colon sign, the items are separated by command, and the whole thing is
enclosed in curly brackets. The keys in a dictionary must have data types of numbers,
strings, or tuples. The values can be of any data type. An example is given below:

mydict = {'Name': 'John', 'Surname': 'Adams', 'Age': 25}


s = mydict['Name'] # s = 'John'
s = mydict['Age'] # s = 25
s = [Link]() # s = ['Age', 'Surname', 'Name']
s = [Link]() # s = [125, 'Adams', 'John']

7.15.1 Dictionary functions


Python language supports large number of dictionary functions. Some commonly used
dictionary functions are given below:

• cmp(d1, d2) compare two dictionaries d1 and d2


• len() the number of items in a dictionary
• del(d[key]) delete an item from the dictionary
• [Link] remove all items from the dictionary
• [Link]() return a list of dictionary keys
• [Link]() return a list of dictionary values

Figure 7.9 shows some examples of using the print statement.

Figure 7.9 Using the dictionary functions

● 87

Getting Started with the Raspberry Pi 5 AI [Link] 87 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

7.16 Keyboard input


Python provides the following function for reading data from the keyboard:

• input provides a prompted read. The data from the keyboard is


returned as a string

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.

Figure 7.10 Keyboard input examples

7.17 Comparison operators


Valid Python comparison operators are:

• == checks if two operands are equal


• != checks if two operands are not equal
• > checks if the left operand is greater than the right one
• < checks if the left operand is less than the right one
• >= checks if the left operand is greater than or equal to the right one
• <= checks if the left operand is less than or equal to the right one

7.18 Logical operators


Valid Python logical operators are:

• and logical AND of the two operands


• or logical OR of the two operands
• not logical inverse of the operand

7.19 Assignment operators


• = assignment operator
• += compound add operator
• -= compound subtract operator
• *= compound multiply operator
• /= compound divide operator

● 88

Getting Started with the Raspberry Pi 5 AI [Link] 88 24-10-2024 09:19


Chapter 7 • Python Programming

7.20 Control of flow


In normal program flow, statements are executed sequentially one after another one. The
flow control statements are used to make decisions and change the order of execution
depending on the results of these decisions.

Python programming language supports the following flow control statements:

• if
• if-else
• elif
• for
• while
• break
• continue
• pass

7.20.1 if, if..else, and elif


The general format of the if statement is:

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.

An example use of the if statement is:

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

Getting Started with the Raspberry Pi 5 AI [Link] 89 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

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

7.20.2 for statement


The for statement is used to create loops (iteration) in programs. The general format of
this statement is:

for variable in sequence:


statements

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:

for letter in "COMPUTER":


print(letter)

The following will be displayed on the screen:

C
O
M
P
U
T
E
R

● 90

Getting Started with the Raspberry Pi 5 AI [Link] 90 24-10-2024 09:19


Chapter 7 • Python Programming

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:

for cnt in range(0, 5):


print(cnt)

The following will be displayed on the screen:

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:

List(range(0, 30, 5))

The for statement can be nested if desired.

7.20.3 while statement


The while statement can also be used to create loops (iteration) in programs. The general
format of this statement is:

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

The output of the program is as follows:

0
1
2
3
4

● 91

Getting Started with the Raspberry Pi 5 AI [Link] 91 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

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)

7.20.4 continue statement


The continue statement is used in for and while loops and this statement skips all the
remaining statements in a loop and returns to the beginning of the loop. An example is
given below. In this example, number 3 is not displayed by the print statement:

cnt = 0
while cnt < 5:
cnt = cnt + 1
if cnt == 3:
continue
print(cnt)

The output of this example is as follows:

1
2
4
5

7.20.5 break statement


The break statement is used in for and while loops and this statement terminates the loop
and execution continues with the next statement. An example is given below:

cnt = 0
while cnt < 5:
cnt = cnt + 1
if cnt == 3:
break
print(cnt)

The output of this program is as follows:

1
2

● 92

Getting Started with the Raspberry Pi 5 AI [Link] 92 24-10-2024 09:19


Chapter 7 • Python Programming
.

7.20.6 pass statement


The pass statement is used when a statement is required syntactically but you do not
want any command or code to execute. The pass statement is a null operation and nothing
happens when it executes. An example is given below:

for letter in 'COMPUTER':


if letter == 'P':
pass
print('Passed')
print(letter)

The output of this program is:

C
O
M
Passed
P
U
T
E
R

7.21 User defined functions


Functions are like small programs within a program. We can use functions to break up a
complex program into several manageable sections where each section can be implemented
as a function. Functions enable us to reuse parts of our programs. For example, we can
create a function to calculate the cube-root of a number and then call this function from
different parts of our program. Another advantage of using functions is that they make it
easier to maintain and update our programs.

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.

A user defined function consists of the following:

• 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

Getting Started with the Raspberry Pi 5 AI [Link] 93 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

• An optional text message can be displayed at the first line of a function to


describe what the function does.
• A function must be terminated with the return statement

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:

def Mult(first, second):


"This is a simple multiplication function"
result = first * second
return 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)

we can also call a function by specifying the keyword arguments. i.e.:

a = Mult(first = 5, second = 3)

Figure 7.11 shows the above example in a Python program.

Figure 7.11 Creating and calling a function

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.

Figure 7.12 A function displaying a string

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

Getting Started with the Raspberry Pi 5 AI [Link] 94 24-10-2024 09:19


Chapter 7 • Python Programming

changes outside the function.

Figure 7.13 Variables in a function are local

The rules for global variables are as follows:

• 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:

cnt = 10 # variable cnt is global


def tstfunc(): # function declaration
global cnt # variable cnt defined as global
cnt = 200 # value of global cnt is changed

tstfunc() # function is called


print(cnt) # value of cnt is 200

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

Getting Started with the Raspberry Pi 5 AI [Link] 95 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

Figure 7.14 Variables are passed by value

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.

Figure 7.15 Returning more than one variable from a function

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

Getting Started with the Raspberry Pi 5 AI [Link] 96 24-10-2024 09:19


Chapter 7 • Python Programming

Figure 7.16 Program listing

This program was created using the Thonny IDE.

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

Getting Started with the Raspberry Pi 5 AI [Link] 97 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

# Author: Dogan Ibrahim


# File : [Link]
# Date : July, 2024
#--------------------------------------------------
import math

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")

Figure 7.17 Modified program listing

● 98

Getting Started with the Raspberry Pi 5 AI [Link] 98 24-10-2024 09:19


Chapter 7 • Python Programming

Figure 7.18 Example output

This program was created using the nano text editor and then run using the command:

pi@raspberrypi:~ $ python [Link]

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

print("TABLE OF TRIGONOMETRIC SINE")


print("=========================\n")
print(" ANGLE SINE")

● 99

Getting Started with the Raspberry Pi 5 AI [Link] 99 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

for angle in range(0, 95, 5):


r = [Link](angle)
s = [Link](r)
print(" %d %f" %(angle, s))

print("End of program")

Figure 7.19 Program listing

An example run of the program is shown in Figure 7.20.

Figure 7.20 Example run of the 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

Getting Started with the Raspberry Pi 5 AI [Link] 100 24-10-2024 09:19


Chapter 7 • Python Programming

Figure 7.21 Program listing and example output

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.

Figure 7.22 Program listing and example output

● 101

Getting Started with the Raspberry Pi 5 AI [Link] 101 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

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:

exception EOFError: end-of-file condition is reached while reading data


exception ImportError: import statement could not load a module
exception IndexError: sequence subscript is out of range
exception KeyError: a dictionary key is not found in the set of existing keys
exception KeyboardInterrupt: user hit the interrupt key (normally the Cntrl+C or Delete
key)
exception MemoryError: operation ran out of memory
exception OverFlowError: arithmetic operation resulted in overflow
exception RuntimeError: an error is detected that does not fall in any other categories
exception ValueError: an operation or function receives an argument that has the right
type but an appropriate value
exception ZeroDivisionError: a division by zero occurred

● 102

Getting Started with the Raspberry Pi 5 AI [Link] 102 24-10-2024 09:19


Chapter 7 • Python Programming

Some examples of using exceptions in programs are given below.

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")

Figure 7.23 Program listing

When the program is run it displays the following message:

Divide by zero exception


Divide by Zero

7.23.1 try/final exceptions


Statement finally can be used in exception handling. Try/finally combination specifies
exception where the block beginning with finally is always executed on the way out,
regardless of whether an exception occurs in the try block.

7.24 Date and time


In some applications it may be necessary to get the current date and time. Python supports
several functions to get the current date and time. Module time must be imported before
these functions can be used. Some of the commonly used date and time functions are as
follows:

● 103

Getting Started with the Raspberry Pi 5 AI [Link] 103 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

• [Link]() returns the current date and time in the following


format:
 time.struct_time(tm_year=2013,tm_mon=12,tm_
mday=18, tm_hour=12,tm_min=45,tm_sec=3,tw_
wday=2,tm_yday=352, tm_isdst=0)

• [Link]() returns the date and time in standard readable format


• [Link]() returns the current CPU time in seconds
• [Link]() returns the current date and time
• [Link]() returns the current time in seconds since the epoch
• [Link](x) suspends the calling program for x seconds

Some examples are given in Figure 7.24.

Figure 7.24 Example date and time functions

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.

Figure 7.25 Examples of using the datetime date functions

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

Getting Started with the Raspberry Pi 5 AI [Link] 104 24-10-2024 09:19


Chapter 7 • Python Programming

Figure 7.26 Examples of using strftime

● 105

Getting Started with the Raspberry Pi 5 AI [Link] 105 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

Chapter 8 • The AI Kit Demo Programs

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

single-person pose estimation.

Further information on examples are available at the following site:

[Link]
md#hailo-rpi5-basic-pipelines

The following You Tube videos can be helpful before you start using the AI kit:

• Object recognition in traffic ([Link]


huMW13Fp7U)
• Object recognition in office ([Link]
• Pose/posture estimation ([Link]
• Subject segmentation ([Link]

The following video (by Jeff Geerling) is also available to help understand the AI kit features:

[Link]

8.2 Demo software installation


The steps to install the demo software files are (check web site: [Link]
ai/hailo-rpi5-examples/blob/main/doc/[Link]#hailo-rpi5-basic-pipelines):

• Clone the repository

git clone [Link]

● 106

Getting Started with the Raspberry Pi 5 AI [Link] 106 24-10-2024 09:19


Chapter 8 • The AI Kit Demo Programs

• Enter the repository directory:

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.

pip install –r [Link]

• 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.

Figure 8.1 Examples folder contents

The applications programs (detection, pose estimation, instance segmentation) are in


folder basic_pipelines as shown in Figure 8.2.

Figure 8.2 Applications programs

● 107

Getting Started with the Raspberry Pi 5 AI [Link] 107 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

8.2.1 The application structure


(taken from [Link]
[Link]#installation)

User-defined Data Class (user_app_callback_class)

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.

Application Callback Function (app_callback)

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.

GStreamer Application Class (GStreamerApp)

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

8.2.2 Detection demo example


This example demonstrates object detection. It uses YOLOv6n model as default. It supports
also yolov8s and yolox_s_leaky models. It uses Hailo's NMS (Non-Maximum Suppression)
layer as part of the HEF file, so all detection networks which are compiled with NMS can be
used with the same code.

To run the example use (make sure you are in Desktop GUI mode):

cd hailo-rpi5-examples

source setup_env.sh

python basic_pipelines/[Link] --input resources/detection0.mp4

● 108

Getting Started with the Raspberry Pi 5 AI [Link] 108 24-10-2024 09:19


Chapter 8 • The AI Kit Demo Programs

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

Getting Started with the Raspberry Pi 5 AI [Link] 109 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

{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

Getting Started with the Raspberry Pi 5 AI [Link] 110 24-10-2024 09:19


Chapter 8 • The AI Kit Demo Programs

{78, «teddy bear»},


{79, «hair drier»},
{80, «toothbrush»}};
}

Figure 8.3 80 class COCO models

Figure 8.4 shows the output of the program displayed on the Raspberry Pi monitor where
the identified objects are shown with rectangles.

Figure 8.4 Output of the program

To close the application press 'Ctrl+C'.

For additional input options enter the following command:

python basic_pipelines/[Link] --help

The following input options are available:

usage: pose_estimation.py [-h] [--input INPUT] [--use-frame] [--show-fps]


[--disable-sync] [--dump-dot]

Hailo App Help

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

Getting Started with the Raspberry Pi 5 AI [Link] 111 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

Relevant when using file source.


--dump-dot Dump the pipeline graph to a dot file [Link]

For example:

To read images from a Raspberry Pi camera, enter --input rpi


To read images from a USB camera (webcam), enter --input /dev/video0
To read from a movie file, enter: --input resources/detection0.mp4

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*

or enter the command:

v4l2-ctl –list-devices

You can test whether the camera is working by entering (Note that it is lower case L not 1):

ffplay –f v4l2 /dev/video0

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:

python basic_pipelines/[Link] -i /dev/video8

The output is shown in Figure 8.5 with the keyboard label displayed at the top left corner
of the display.

Figure 8.5 Detecting a keyboard

● 112

Getting Started with the Raspberry Pi 5 AI [Link] 112 24-10-2024 09:19


Chapter 8 • The AI Kit Demo Programs

Use the following command if using a Raspberry Pi camera (with auto-focus, e.g. V3):

python basic_pipelines/[Link] -i rpi

Printing the frame rate


To print the frame rate add the --print-fps flag. This will print the frame rate to the
terminal and to the video output window.

See also Running with Different Input Sources at:

[Link]
md#running-with-different-input-sources

What is in this example (taken from [Link]


blob/main/doc/[Link]#installation)

Custom Callback Class


This is an example of a custom callback class. It can be used to send user-defined data
to the callback function. It inherits from the app_callback_class and can be extended with
user-defined variables and functions. In this example we added a variable and a function to
the class. These are used in the callback function when under the --use-frame flag. These
values are then displayed on the user frame.

Application Callback function


In this function we see example how to parse HAILO_DETECTION metadata. Each
Gstreamer buffer include HAILO ROI object. This object is the root of all Hailo metadata
objects attached to this buffer. All detections are read, and their label, bounding box and
confidence are extracted. For this example we assume you have a "person" nearby. All
detections are parsed and the information for each person detected is printed to terminal.
If the --use-frame flag is used, the frame is extracted from the buffer and displayed. The
number of persons detected is displayed on the frame. In addition, the user-defined data
is displayed on the frame.

Using Retrained Models


This application includes support for using retrained detection models. The model should
be compiled with HailoRT NMS Post Process (HailortPP). To use a custom model, you can
load its HEF using the --hef-path flag. If you are using a custom model with different
labels, you can use the --labels-path flag to load your labels file. For an example config
file, see hailo-rpi5-examples/resources/[Link]. The network trained
while writing the Retaining Example ([Link]
blob/main/doc/[Link]#using-yolov8-retraining-docker) is downloaded by
the download_resources.sh script. So you can use it as an example.

8.2.3 Pose Estimation demo example


This example demonstrates human pose estimation (position and orientation). It uses
yolov8s_pose model.

● 113

Getting Started with the Raspberry Pi 5 AI [Link] 113 24-10-2024 09:19


Get Started with the Raspberry Pi AI Kit

To run the example use:

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.

Figure 8.6 Pose estimation output

To close the application press 'Ctrl+C'.

What is in this example (taken from [Link]


blob/main/doc/[Link]#installation)

Pose Estimation Callback Class


The Callback function showcases how to get the pose estimation metadata from the
network output. Each person is represented as a HAILO_DETECTION with 17 keypoints
(HAILO_LANDMARKS objects). The code parses the landmarks and extracts the left and
right eye coordinates and prints them to the terminal. If --use-frame flag is set the eyes
will be drawn on the user frame. The keypoints dictionary can be obtained from the get_
keypoints function.

8.2.4 Instance segmentation demo example


This example demonstrates instance segmentation. It uses yolov5n_seg model.

To run the example use:


cd hailo-rpi5-examples
source setup_env.sh
 python basic_pipelines/instance_segmentation.py –input resources/detection0.
mp4

● 114

Getting Started with the Raspberry Pi 5 AI [Link] 114 24-10-2024 09:20


Chapter 8 • The AI Kit Demo Programs

This application reads the detection0.mp4 movie file for pose estimation. The output is
shown in Figure 8.67

Figure 8.7 Instance segmentation output

To close the application press 'Ctrl+C'.

What is in this example (taken from the github)

Instance Segmentation Callback Class

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.

8.3 Post-processing with rpicam-apps


With the rpicam-apps you can pass images received from the camera system through a
number of custom image-processing and image analysis routines, where each such routine
is known as a stage. To run the post-processing stages, you supply a JSON file telling the
application which stages and options to apply. You can find example JSON files that use the
built-in post-processing stages in the assets subfolder of the rpicam-apps folder.

Further information on rpicam-apps are available at the following link:

[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

Getting Started with the Raspberry Pi 5 AI [Link] 115 24-10-2024 09:20


Get Started with the Raspberry Pi AI Kit

{
"negate": {}
}

To apply the negate stage to an image, pass [Link] to the post-process-file option
as shown below:

$ rpicam-hello --post-process-file [Link]

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.

To access these files enter the commands:

● 116

Getting Started with the Raspberry Pi 5 AI [Link] 116 24-10-2024 09:20


Chapter 8 • The AI Kit Demo Programs

git clone --depth 1 [Link]


cd rpicam-apps/assets/

A list of the files in this folder are shown in Figure 8.8.

Figure 8.8 List of files

For example, to run the personface application, enter:

rpicam-hello -t 0 --post-process-file hailo_yolov5_personface.json --lores-width


640 --lores-height 640

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.

Figure 8.9 The JSON file

● 117

Getting Started with the Raspberry Pi 5 AI [Link] 117 24-10-2024 09:20


Get Started with the Raspberry Pi AI Kit

Another example with yolov6_inference is given below:

rpicam-hello -t 0 --post-process-file hailo_yolov6_inference.json --lores-width 640


--lores-height 640

The command below supresses the preview window and gives text output:

rpicam-hello –n –v 2 -t 0 --post-process-file hailo_yolov6_inference.json --lores-


width 640 --lores-height 640

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.

Block diagram: Figure 8.10 shows the project block diagram.

Figure 8.10 Block diagram

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

Getting Started with the Raspberry Pi 5 AI [Link] 118 24-10-2024 09:20


Chapter 8 • The AI Kit Demo Programs

Figure 8.11 Circuit diagram

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

led = LED(21) # LED port


button = Button(20) # Button port
[Link]() # LED OFF

while(True):
p=[Link]("rpicam-hello -n -v 2 -t 0 \

● 119

Getting Started with the Raspberry Pi 5 AI [Link] 119 24-10-2024 09:20


Get Started with the Raspberry Pi AI Kit

--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]()

button.wait_for_press() # Wait for bitton


[Link]()
sleep(15)

Figure 8.12 Program listing

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.

Block diagram: Figure 8.13 shows the project block diagram.

Figure 8.13 Block diagram

● 120

Getting Started with the Raspberry Pi 5 AI [Link] 120 24-10-2024 09:20


Chapter 8 • The AI Kit Demo Programs

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.

Figure 8.14 Circuit diagram

Program listing: The Python program (Program: [Link]) listing is shown in


Figure 8.15. At the beginning of the program the socket library, gpiozero library, subprocess
library and time library are imported to the program. Then, a TCP socket is created with
port number 5000. The program waits for connection from the smart phone running a
TCP program. When a connection is made, the program waits until a person is detected
by the camera. When a person is detected, the message Person detected… is sent to
the smart phone and the LED is turned ON at the same time. The program then waits to
receive acknowledgement from the smart phone. Sending message OK from the smart
phone turns OFF the LED and the detection process starts again from the beginning after
15 seconds of delay.

#--------------------------------------------------------
# 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

Getting Started with the Raspberry Pi 5 AI [Link] 121 24-10-2024 09:20


Get Started with the Raspberry Pi AI Kit

from time import sleep


import socket

led = LED(21) # LED port


[Link]() # LED OFF

sock = [Link](socket.AF_INET, socket.SOCK_STREAM)


print("bind")
[Link](("[Link]", 5000))
print("listen")
[Link](2)
(conn,addr)=[Link]()
print("Accepted")
msg = "Person detected\n"
print("")
print("Waiting to detect a person...")

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

Figure 8.15 Program listing

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

Getting Started with the Raspberry Pi 5 AI [Link] 122 24-10-2024 09:20


Chapter 8 • The AI Kit Demo Programs

Figure 8.16 Simple TCP Socket Tester apps

The steps to test are:

• Run the Raspberry Pi 5 program by entering:

python [Link]

You should see the following displayed on your Raspberry Pi monitor:

bind
listen

• Start the TCP apps and click CLIENT

• 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

Getting Started with the Raspberry Pi 5 AI [Link] 123 24-10-2024 09:20


Get Started with the Raspberry Pi AI Kit

• Click button CONNECT. You should see message Socket connected displayed
in green colour at top of the IP address

• The following will be displayed on the Raspberry Pi 5 monitor:

Accepted
Waiting to detect a person…

• Position the camera to detect your face

• The following will be displayed on the Raspberry Pi 5 monitor:

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)

Figure 8.18 Person detected message

• Write OK and click SEND

• 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

Getting Started with the Raspberry Pi 5 AI [Link] 124 24-10-2024 09:20


Chapter 8 • The AI Kit Demo Programs

Figure 8.19 OK displayed on the apps

8.6 Hailo software components


The Hailo software components are part of the Hailo AI Software Suite and can be
described in two environments: Model building environment, and Runtime environment
(Figure 8.20)

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

Getting Started with the Raspberry Pi 5 AI [Link] 125 24-10-2024 09:20


Get Started with the Raspberry Pi AI Kit

Figure 8.20 Hailo software components ([Link]

● 126

Getting Started with the Raspberry Pi 5 AI [Link] 126 24-10-2024 09:20


Chapter 9 • Pre-trained models

Chapter 9 • Pre-trained models

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.

9.2 Hailo Model Zoo pre-trained models


The Hailo Model Zoo provides pre-trained models for high-performance deep learning
applications. Using the Hailo Model Zoo you can measure the full precision accuracy of
each model, the quantized accuracy using the Hailo Emulator and measure the accuracy on
the Hailo-8 device. Finally, you will be able to generate the Hailo Executable Format (HEF)
binary file to speed-up development and generate high quality applications accelerated with
Hailo-8. The Hailo Model Zoo also provides re-training instructions to train the models on
custom datasets and models that were trained for specific use-cases on internal datasets.

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.

The models are divided into two:

• Public models - which were trained on publicly available datasets. These


models are: Classification, Object detection, Segmentation, and others (look at
the Model Zoo repository at: [Link]

• 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

9.3 Installing the DataFlow compiler


The Hailo DataflowCompiler (DFC) is used to compile the pre-trained models into executable
binary files (HEF) based on input from TensorFlow, TFLite file, or an ONNX file. The compiled
HEF files can then run on Raspberry Pi 5 using the HailoRT program.

Installation of the DFC requires the following:

• Ubuntu 20./22.04 64-bit computer (or support on Windows)


• Minimum 16 GB memory (32 GB recommended)
• Python 3.8/3/9/3.10 including pip and virtualenv
• Python3.x-dev, [Link], python3tk, libfuse2, graphviz, libgraphvizdev

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

Getting Started with the Raspberry Pi 5 AI [Link] 127 24-10-2024 09:20


Get Started with the Raspberry Pi AI Kit

Additionally, for GPU based hardware the following are required:

• 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
• CUDA 11.8
• CUDNN 8.9

The steps to install the DFC on Ubuntu computer are:

• 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

Figure 9.1 Register for the site

Figure 9.2 Download the Dataflow Compiler

● 128

Getting Started with the Raspberry Pi 5 AI [Link] 128 24-10-2024 09:20


Chapter 9 • Pre-trained models

• The DFC file will be copied to your Downloads folder on your Ubuntu
computer. Change to this folder, and then:

• Create virtual environment

python3.8 –m venv venv


source venv/bin/activate

• Install the Dataflow Compiler

pip install hailo_dataflow_compiler-3.28.0-py3-none-linux_x86_64.whl

Wait until the installation finishes.

Enter the following command to test the installation:

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

Getting Started with the Raspberry Pi 5 AI [Link] 129 24-10-2024 09:20


Get Started with the Raspberry Pi AI Kit

Figure 9.3 Parsing, optimization, and compilation


([Link]

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

Getting Started with the Raspberry Pi 5 AI [Link] 130 24-10-2024 09:20


Chapter 10 • The Hailo AI SW Suite

Chapter 10 • The Hailo AI SW Suite

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.

10.2 The Hailo AI SW Suite


Hailo AI SW-Suite consists of the Dataflow Compiler, HailoRT, TAPPAS and the Model
Zoo.

The steps to install this suite of programs are:

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.

The steps are:

• 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

Figure 10.1 Register for the site

The suite can be installed in one of three ways:

1. Using all-in-one docker file


2. Using all-in-one self extracted executable
3. Manual installation of the packages in a defined order

● 131

Getting Started with the Raspberry Pi 5 AI [Link] 131 24-10-2024 09:20


Get Started with the Raspberry Pi AI Kit

In this chapter, we will be installing using the docker file option. The development system
requirements are:

1. Ubuntu 20.04/22.04, 64 bit

2. 16+ GB RAM (32+ GB recommended)

3. Docker package, either [Link] 20.10.07 (from Ubuntu repo), or docker-ce


20.10.6 (from Docker website).

• Click to download the Hailo AI Software Suite - Docker and HailoRT


- PCIe driver Ubuntu package (deb) to your development Ubuntu
computer. At the time of writing this book the file names were: hailo_ai_sw_
suite_2024-07.1_docker.zip and hailort-pcie-driver_4.18.0_all.deb.
The downloaded files should be in your Downloads folder on your Ubuntu
computer.

The steps for docker installation are given below. Characters typed by the user are in bold
for clarity.

Enter the commands:

• Add Docker's official GPG key:

sudo apt-get update


sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL [Link] -o
/etc/apt/keyrings/[Link]
sudo chmod a+r /etc/apt/keyrings/[Link]

• Add the repository to Apt sources

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

• Install the Docker package

sudo apt-get install docker-ce docker-ce-cli [Link] docker-


buildx-plugin docker-compose-plugin

● 132

Getting Started with the Raspberry Pi 5 AI [Link] 132 24-10-2024 09:20


Chapter 10 • The Hailo AI SW Suite

• 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.

You have now successfully installed and started Docker Engine.

sudo docker run hello-world

• Add the user to the docker group with the following steps:

sudo usermod –aG docker ${USER}

• Log out and log in

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:

distribution=$(. /etc/os-release;echo $ID$VERSION_ID) \


&& curl -s -L [Link] | sudo apt-key add
-\
&& curl -s -L \
[Link] \
| sudo tee /etc/apt/[Link].d/[Link]

sudo apt-get update


sudo apt-get install -y nvidia-docker2
sudo systemctl restart docker

sudo apt install dkms linux-headers-$(uname –r)

• Assuming the downloaded files are in Downloads folder: Change to this folder
and install the hailo driver software:

sudo dpkg –i hailort-pcie-driver_4.18.0_all.deb

• Reboot the computer

• 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

Getting Started with the Raspberry Pi 5 AI [Link] 133 24-10-2024 09:20


Get Started with the Raspberry Pi AI Kit

Change to Downloads folder:

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

For example, to enable the HailoRT multi-process service, run:

./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

Another option is to use the script's additional options:

Resume – To go back to the existing container, change folder to Downloads and enter:

sudo ./hailo_ai_sw_suite_docker_run.sh --resume

To see information on hailo, enter: hailo –h (Figure 10.2)

● 134

Getting Started with the Raspberry Pi 5 AI [Link] 134 24-10-2024 09:20


Chapter 10 • The Hailo AI SW Suite

Figure 10.2 Part of the display is shown

Enter pip list | grep hailo to see the Hailo packages that are installed in the activated
virtual environment (Figure 10.3)

Figure 10.3 Hailo packages installed in the virtual environment

The Hailo AI SW-Suite has now been installed.

10.3 Re-training models


Re-training models is a complex process and is done on an Ubuntu based development
computer. Interested readers can get detailed information on model re-training at the
following site:

[Link]

In the next chapter we will look at a Yolov8n re-training example using the COCO dataset.

● 135

Getting Started with the Raspberry Pi 5 AI [Link] 135 24-10-2024 09:20


Get Started with the Raspberry Pi AI Kit

Chapter 11 • Raspberry Pi 5 Object Detection Training

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.

YOLOv7: this version added pose estimation to datasets.

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

Getting Started with the Raspberry Pi 5 AI [Link] 136 24-10-2024 09:20


Chapter 11 • Raspberry Pi 5 Object Detection Training

• 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.

A complete list of 80 classes that COCO offers is shown in Table 11.1.

Table 11.1 COCO classes


([Link]

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).

11.3 The training


Training your own dataset is a complex process and is not covered in this book. As mentioned
earlier, this chapter covers only training using the COCO 128 data set with the Yolov8n.

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

Getting Started with the Raspberry Pi 5 AI [Link] 137 24-10-2024 09:20


Get Started with the Raspberry Pi AI Kit

On the Ubuntu development computer:


• Assuming that the docker was installed as described in the previous chapter,
resume the docker. The docker is named (hailo_virtualenv):

cd Downloads
sudo ./hailo_ai_sw_suite_docker_run.sh --resume

• Install ultralytics

pip install ultralytics

• create folder and change folder:

mkdir yolomodel
cd yolomodel

• Use COCO 128 dataset to train Yolov8n. This may take some time, wait until it
is finished. Enter:

yolo detect train data=[Link] model=[Link]


name=retrain_yolov8n epochs=100 batch=16

• 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.

Figure 11.1 Program output

● 138

Getting Started with the Raspberry Pi 5 AI [Link] 138 24-10-2024 09:20


Chapter 11 • Raspberry Pi 5 Object Detection Training

Figure 11.2a Object modelled (continued)

Figure 11.2b Object modelled (continued from Figure 11.2a)

● 139

Getting Started with the Raspberry Pi 5 AI [Link] 139 24-10-2024 09:20


Get Started with the Raspberry Pi AI Kit

• After the training you should see the model file [Link] in folder: runs/
detect/retrain_yolov8n/weights. This is shown in Figure 11.3.

Figure 11.3 Model file [Link]

• Change directory to runs/detect/retrain_yolov8n/weights:

cd ./runs/detect/retrain_yolov8n/weights/

• Now, convert the .pt model to .onnx model:

yolo export model=./[Link] imgsz=640 format=onnx opset=11

• At the end of the program, a file named [Link] will be created. You can
check this with the ls command.

• Enter the following command to test that hailo_model_zoo is functioning


correctly (Figure 11.4):

hailomz –h

Figure 11.4 Test that the hailo command is working

• Install the hailo_model_zoo github file (Figure 11.5):

git clone [Link]

Figure 11.5 Install the hailo_model_zoo_github

● 140

Getting Started with the Raspberry Pi 5 AI [Link] 140 24-10-2024 09:20


Chapter 11 • Raspberry Pi 5 Object Detection Training

• 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

Figure 11.6 Run the python program

python hailo_model_zoo/datasets/create_coco_tfrecord.py
calib2017

• Parse the model (Figure 11.7)

cd ..
hailomz parse --hw-arch hailo8l --ckpt ./[Link] yolov8n

Figure 11.7 Parse the model

• Optimize the model (Figure 11.8)

hailomz optimize --hw-arch hailo8l --har ./[Link] yolov8n

Figure 11.8 Optimize the model

At this stage, you may get an error similar to the following:

FileNotFoundError: Couldn't find dataset in /local/shared_with_docker/.hailomz/


models_files/coco/2021-06-18/coco_calib2017.tfrecord

● 141

Getting Started with the Raspberry Pi 5 AI [Link] 141 24-10-2024 09:20


Get Started with the Raspberry Pi AI Kit

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:

hailomz compile yolov8n --hw-arch hailo8l --har ./[Link]

Figure 11.9 Compile the model

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.

Figure 11.10 End of the compilation

Now we should transfer the compiled file to Raspberry Pi 5 and deploy it there. The steps
are:

● 142

Getting Started with the Raspberry Pi 5 AI [Link] 142 24-10-2024 09:20


Chapter 11 • Raspberry Pi 5 Object Detection Training

On Raspberry Pi 5
• Clone the project:

git clone [Link]


YOLOv8-on-Raspberry-PI-reComputer-r1000-and-AIkit-Hailo-8L.
git
cd Benchmarking-YOLOv8-on-Raspberry-PI-reComputer-r1000-
and-AIkit-Hailo-8L

• 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):

scp [Link] pi@[Link]:/home/pi/Benchmarking-YOLOv8-on-


Raspberry-PI-reComputer-r1000-and-AIkit-Hailo-8L/hailomodel/

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

• Back to old folder

cd ..

• Edit file [Link]

nano [Link]

Change the following lines (at lines 105, 106, and 172):

elif [Link] == "yolov8n":


self.hef_path = [Link](self.current_path, './
hailomodel/[Link]')

● 143

Getting Started with the Raspberry Pi 5 AI [Link] 143 24-10-2024 09:20


Get Started with the Raspberry Pi AI Kit

parser.add_argument("--network", default="yolov8n",
choices=['yolov6n', 'yolov8s', 'yolox_s_leaky'], help="Which
Network to use, defult is yolov6n")

• Enter Cntrl+X followed by Y to save and exit nano

• Edit file [Link]

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:

python3 "$1".py –input rpi

• Enter Cntrl+X followed by Y to save and exit nano

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.

Enter the following command:

bash [Link] object-detection-hailo

Figure 11.11 shows the detection of a laptop.

Figure 11.11 Laptop detection

● 144

Getting Started with the Raspberry Pi 5 AI [Link] 144 24-10-2024 09:20


Chapter 11 • Raspberry Pi 5 Object Detection Training

11.4 Modified procedure


The above training procedure can be simplified as follows. The process below runs on the
Raspberry Pi 5:

• create a directoy called hailo on your Raspberry Pi 5 home directory.

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).

• Change to the hailo directory

cd hailo

git clone [Link]

cd hailo-rpi5-examples
source setup_env.sh
cd ..

• Install setproctitle

pip install setproctitle

Now run the application and enable the Raspberry Pi 5 camera as the input for the
application:

• python hailo-rpi5-examples/basic_pipelines/[Link] –i rpi –hef [Link]

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.

• Move to the directory where the HEF file is stored

• To check the performance (FPS) enter the command (Figure 11.12):

hailortcli run [Link]

● 145

Getting Started with the Raspberry Pi 5 AI [Link] 145 24-10-2024 09:20


Get Started with the Raspberry Pi AI Kit

Figure 11.12 Check the performance

• To measure latency, enter the following command (Figure 11.13):

hailortcli run [Link] --measure-latency

Figure 11.13 Measure latency

• To measure power, enter:

hailortcli run [Link] --measure-power

• To get information about the compiled model components (Figure 11.14) enter:

hailortcli parse-hef [Link]

● 146

Getting Started with the Raspberry Pi 5 AI [Link] 146 24-10-2024 09:20


Chapter 11 • Raspberry Pi 5 Object Detection Training

Figure 11.14 Parse-hef

• To display list of available options, enter (Figure 11.15)

hailortcli --help

Figure 11.15 Options

● 147

Getting Started with the Raspberry Pi 5 AI [Link] 147 24-10-2024 09:20


Get Started with the Raspberry Pi AI Kit

APPENDIX – Useful Web Sites

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

Getting Started with the Raspberry Pi 5 AI [Link] 148 24-10-2024 09:20


Index

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

Getting Started with the Raspberry Pi 5 AI [Link] 149 24-10-2024 09:20


Get Started with the Raspberry Pi AI Kit

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

Getting Started with the Raspberry Pi 5 AI [Link] 150 24-10-2024 09:20


books
books books
books

Get Started with the Get Started with the


Raspberry Pi AI Kit Raspberry Pi AI Kit

Get Started with the Raspberry Pi AI Kit • Dogan Ibrahim


A Beginner’s Guide to AI and
Edge Computing
A Beginner’s Guide to AI and Edge Computing
Artificial Intelligence (AI) is now part of our daily lives. With companies Prof Dr Dogan Ibrahim has a BSc
developing low-cost AI powered hardware into their products, it is now degree in electronic engineering,
an MSc degree in automatic control
becoming a reality to purchase AI accelerator hardware at comparatively
engineering, and a PhD degree in
very low costs. One such hardware accelerator is the Hailo module digital signal processing. Dogan
which is fully compatible with the Raspberry Pi 5. The Raspberry Pi AI has worked in many industrial
Kit is a cleverly designed hardware as it bundles an M.2 based Hailo-8L organizations before he returned
accelerator with the Raspberry Pi M.2 HAT+ to offer high speed inferencing to academic life. Prof Ibrahim is the
author of over 70 technical books
on the Raspberry Pi 5. Using the Raspberry Pi AI Kit, you can build complex
and published over 200 technical
AI based vision applications, running in real time, such as object detec- articles on microcontrollers,
tion, pose estimation, instance segmentation, home automation, security, microprocessors, and related fields.
robotics, and many more neural network-based applications. He is a Chartered electrical engineer
and a Fellow of the Institution of
the Engineering Technology. He has
This book is an introduction to the Raspberry Pi AI Kit, and it is aimed to
been an amateur radio operator for
provide some help to readers who are new to the kit and wanting to run several decades and also holds an
some simple AI based visual models on their Raspberry Pi 5 computers. Arduino certification.
The book is not meant to cover the detailed process of model creation
and compilation, which is done on an Ubuntu computer with massive disk
space and 32 GB memory. Examples of pre-trained and custom object
detection are given in the book.

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.

Elektor International Media


[Link]

Dogan Ibrahim

SKU21031_COV_Get Started with the Raspberry Pi AI Kit_170x240_v01.indd 3 24-10-2024 10:22

You might also like