Book Image

Raspberry Pi 3 Cookbook for Python Programmers - Third Edition

By : Steven Lawrence Fernandes, Tim Cox
Book Image

Raspberry Pi 3 Cookbook for Python Programmers - Third Edition

By: Steven Lawrence Fernandes, Tim Cox

Overview of this book

Raspberry Pi 3 Cookbook for Python Programmers – Third Edition begins by guiding you through setting up Raspberry Pi 3, performing tasks using Python 3.6, and introducing the first steps to interface with electronics. As you work through each chapter, you will build your skills and apply them as you progress. You will learn how to build text classifiers, predict sentiments in words, develop applications using the popular Tkinter library, and create games by controlling graphics on your screen. You will harness the power of a built in graphics processor using Pi3D to generate your own high-quality 3D graphics and environments. You will understand how to connect Raspberry Pi’s hardware pins directly to control electronics, from switching on LEDs and responding to push buttons to driving motors and servos. Get to grips with monitoring sensors to gather real-life data, using it to control other devices, and viewing the results over the internet. You will apply what you have learned by creating your own Pi-Rover or Pi-Hexipod robots. You will also learn about sentiment analysis, face recognition techniques, and building neural network modules for optical character recognition. Finally, you will learn to build movie recommendations system on Raspberry Pi 3.
Table of Contents (23 chapters)
Title Page
Copyright and Credits
Dedication
Packt Upsell
Contributors
Preface
Index

Preface

This book is intended for anyone who wants to build software applications or hardware projects using the Raspberry Pi. The book gradually introduces text classification, creating games, 3D graphics, and sentiment analysis. We also move towards more advanced topics, such as building computer vision applications, robots, and neural network applications. It would be ideal to have basic understanding of Python; however, all programming concepts are explained in detail. All the examples are written using Python 3, with clear and detailed explanations of how everything works so that you can adapt and use all the information in your own projects. By the end of the book, you will have the skills you need to build innovative software applications and hardware projects using the Raspberry Pi.

Who this book is for

This book is for anyone who wants to master the skills of Python programming using Raspberry Pi 3. Prior knowledge of Python will be an added advantage.

What this book covers

Chapter 1, Getting Started with a Raspberry Pi Computer, introduces the Raspberry Pi and explores the various ways in which it can be set up and used.

Chapter 2Dividing Text Data and Building a Text Classifier, guides us to build a text classifier; it can classify text using the bag-of-words model.

Chapter 3Using Python for Automation and Productivity, explains how to use graphical user interfaces to create your own applications and utilities.

Chapter 4, Predicting Sentiments in Words, explains how Naive Bayes classifiers and logistic regression classifiers are constructed to analyze the sentiment in words.

Chapter 5Creating Games and Graphics, explains how to create a drawing application and graphical games using the Tkinter canvas.

Chapter 6, Detecting Edges and Contours in Images, describes in detail how images are loaded, displayed, and saved. It provides detailed implementations of erosion and dilation, image segmentation, histogram equalization, edge detection, detecting corners in images, and more.

Chapter 7Creating 3D Graphics, discusses how we can use the hidden power of the Raspberry Pi's graphical processing unit to learn about 3D graphics and landscapes, and produce our very own 3D maze for exploration.

Chapter 8Building Face Detector and Face Recognition Applications, explains how human faces can be detected from webcams and recognized using images stored in a database. 

Chapter 9Using Python to Drive Hardware, establishes the fact that to experience the Raspberry Pi at its best, we really have to use it with our own electronics. This chapter discusses how to create circuits with LEDs and switches, and how to use them to indicate the status of a system and provide control. Finally, it shows us how to create our own game controller, light display, and a persistence-of-vision text display.

Chapter 10, Sensing and Displaying Real-World Data, explains how to use an analog-to-digital converter to provide sensor readings to the Raspberry Pi. We discover how to store and graph the data in real time, as well as display it on an LCD text display. Next, we record the data in a SQL database and display it in our own web server. Finally, we transfer the data to the internet, which will allow us to view and share the captured data anywhere in the world.

Chapter 11, Building a Neural Network Module for Optical Character Recognition, introduces neural network implementation on Raspberry Pi 3. Optical characters are detected, displayed, and recognized using neural networks.

Chapter 12, Building Robots, takes you through building two different types of robot (a Rover-Pi and a Pi-Bug), plus driving a servo-based robot arm. We look at motor and servo control methods, using sensors, and adding a compass sensor for navigation.

Chapter 13, Interfacing with Technology, teaches us how to use the Raspberry Pi to trigger remote mains sockets, with which we can control household appliances. We learn how to communicate with the Raspberry Pi over a serial interface and use a smartphone to control everything using Bluetooth. Finally, we look at creating our own applications to control USB devices.

Chapter 14, Can I Recommend a Movie for You?, explains how movie recommender systems are built. It elaborates how Euclidean distance and Pearson correlation scores are computed. It also explains how similar users are found in the dataset and the movie recommender module is built.

Appendix, Hardware and Software List, explains the detailed hardware software list used inside the book.

To get the most out of this book

Readers are expected to know the basics of Python programming.

It would be beneficial for readers to have a basic understanding of machine learning, computer vision, and neural networks.

Download the example code files

You can download the example code files for this book from your account at www.packtpub.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

  1. Log in or register at www.packtpub.com.
  2. Select the SUPPORT tab.
  3. Click on Code Downloads & Errata.
  4. Enter the name of the book in the Search box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR/7-Zip for Windows
  • Zipeg/iZip/UnRarX for Mac
  • 7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Raspberry-Pi-3-Cookbook-for-Python-Programmers-Third-Edition. In case there's an update to the code, it will be updated on the existing GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here:http://www.packtpub.com/sites/default/files/downloads/RaspberryPi3CookbookforPythonProgrammersThirdEdition_ColorImages.pdf.

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: 

"We use the bind function here, which will bind a specific event that occurs on this widget (the_canvas) to a specific action or key press."

A block of code is set as follows:

#!/usr/bin/python3 
# bouncingball.py 
import tkinter as TK 
import time 
 
VERT,HOREZ=0,1 
xTOP,yTOP = 0,1 
xBTM,yBTM = 2,3 
MAX_WIDTH,MAX_HEIGHT = 640,480 
xSTART,ySTART = 100,200 
BALL_SIZE=20 
RUNNING=True 

Any command-line input or output is written as follows:

sudo nano /boot/config.txt

Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "Click on the Pair button to begin the pairing process and enter the device's PIN."

Note

Warnings or important notes appear like this.

Note

Tips and tricks appear like this.

Sections

In this book, you will find several headings that appear frequently (Getting ready, How to do it..., How it works..., There's more..., and See also).

To give clear instructions on how to complete a recipe, use these sections as follows:

Getting ready

This section tells you what to expect in the recipe and describes how to set up any software or any preliminary settings required for the recipe.

How to do it...

This section contains the steps required to follow the recipe.

How it works...

This section usually consists of a detailed explanation of what happened in the previous section.

There's more...

This section consists of additional information about the recipe in order to make you more knowledgeable about the recipe.

See also

This section provides helpful links to other useful information for the recipe.

Get in touch

Feedback from our readers is always welcome.

General feedback: Email [email protected] and mention the book title in the subject of your message. If you have questions about any aspect of this book, please email us at [email protected].

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packtpub.com.