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

The software list


The book uses a range of software packages to extend the capabilities of the pre-installed software.

PC software utilities

In most cases, the latest version of the software available should be used (versions are listed just in case there is a compatibility issue in a later release). The list of software used is as follows:

Raspberry Pi packages

This section lists each of the packages used in the chapters in the book in the following format (versions are listed just in case there is a compatibility issue in a later release):

  • Package name (version) Supporting website:
Install command

Chapter 1

  • This chapter describes the hardware setup, and, therefore, the following packages are optional (or specific hardware drivers where necessary):
sudo apt-get install tightvncserver
sudo apt-get install samba

Chapter 2

Following are the commands used in Chapter 2Dividing Text Data and Building Text Classifier:

sudo apt-get install geany
sudo apt-get -y install python-pip
sudo apt-get -y install python-git 
sudo apt-get -y install python-numpy 
sudo apt-get -y install python-scipy 
sudo pip install --upgrade cython 
sudo pip install -U scikit-learn 
sudo pip install imutils 
sudo apt-get -y install python-sklearn
sudo apt-get -y install python-skimage

 

Chapter 3

sudo apt-get install python3-tk
sudo apt-get install python3-pip
sudo apt-get install libjpeg-dev
sudo pip-3.2 install pillow

Chapter 4

Following are the commands used in Chapter 4, Predicting Sentiments in Words:

sudo apt-get install geany
sudo apt-get -y install python-pip 
sudo apt-get -y install python-git 
sudo apt-get -y install python-numpy 
sudo apt-get -y install python-scipy 
sudo pip install --upgrade cython 
sudo pip install -U scikit-learn 
sudo pip install imutils 
sudo apt-get -y install python-sklearn
sudo apt-get -y install python-skimage

Chapter 5

sudo apt-get install python3-tk

Chapter 6

Following are the commands used in Chapter 6, Detecting Edges and Contours in Images:

sudo apt-get install geany
sudo apt-get -y install python-pip 
sudo apt-get -y install python-opencv
sudo apt-get -y install python-numpy 
sudo apt-get -y install python-scipy 
sudo pip install --upgrade cython 
sudo pip install -U scikit-learn 
sudo pip install imutils 
sudo apt-get -y install python-sklearn
sudo apt-get -y install python-skimage

Chapter 7

sudo apt-get install python3-pip
pip-3.2 install pi3d

Also, take a look at 3D Graphics with Pi3D:

http://paddywwoof.github.io/pi3d_book/_build/latex/pi3d_book.pdf

Chapter 8

Following are the commands used in Chapter 8, Building Face Detector and Face Recognition Applications:

sudo apt-get install geany
sudo apt-get -y install python-pip 
sudo apt-get -y install python-opencv
sudo apt-get -y install python-numpy 
sudo apt-get -y install python-scipy 
sudo pip install --upgrade cython 
sudo pip install -U scikit-learn 
sudo pip install imutils 
sudo apt-get -y install python-sklearn
sudo apt-get -y install python-skimage

Chapter 9

sudo apt-get install python3-rpi.gpio
sudo apt-get install flite

Installation instructions are provided in Chapter 9Using Python to Drive Hardware:

Chapter 10

sudo apt-get install i2c-tools
sudo apt-get install python3-pip
  • python3-dev (Version 3.4.2-2): header files and static library for Python required for some software
sudo apt-get install python3-dev
sudo pip-3.2 install wiringpi2

Chapter 11

Following are the commands used in Chapter 11, Building Neural Network Module for Optical Character Recognition:

sudo apt-get install geany
sudo apt-get -y install python-pip 
sudo apt-get -y install python-opencv
sudo apt-get -y install python-numpy 
sudo apt-get -y install python-scipy 
sudo pip install --upgrade cython 
sudo pip install -U scikit-learn 
sudo pip install imutils 
sudo apt-get -y install python-sklearn
sudo apt-get -y install python-skimage
sudo pip install -U nltk
sudo pip install neurolab

Chapter 12

sudo pip-3.2 install wiringpi2
sudo pip-3.2 install wiringpi2

Chapter 13

sudo apt-get install python3-rpi.gpio
sudo apt-get install python3-tk
sudo pip-3.2 install wiringpi2
sudo apt-get install minicom
sudo pip-3.2 install pyserial
sudo pip-3.2 install pyusb

Chapter 14

Following are the commands used in Chapter 14, Can I Recommend a Movie for You?:

sudo apt-get install geany
sudo apt-get -y install python-pip 
sudo apt-get -y install python-opencv
sudo apt-get -y install python-numpy 
sudo apt-get -y install python-scipy 
sudo pip install --upgrade cython 
sudo pip install -U scikit-learn 
sudo pip install imutils 
sudo apt-get -y install python-sklearn
sudo apt-get -y install python-skimage

There's more...

The majority of the Raspberry Pi software packages used in the book have been installed and configured using apt-get and pip. Useful commands have been given for each in the following sections.

APT commands

The following are the useful commands for APT (this is pre-installed by default on Raspbian):

  • Always update the package list to obtain the latest versions and programs before installing a package with the sudo apt-get update command
  • Find software by searching for any packages that include the<searchtext> command in the package name or description usingsudo apt-cache search <seachtext>
  • Install software with a particular <packagename> usingsudo apt-get install <packagename>
  • Uninstall a particular software package usingsudo apt-get remove <packagename>
  • Display the currently installed version of a software package usingsudo apt-cache showpkg <packagename>

If you want to install a specific version of a software package, use sudo apt-get install <package name>=<version>

Note

If you need to use the packages on a system without internet access, you can use the following command to download the packages (and their dependencies) to the specified directory:sudo apt-get -o dir::cache::archives="<target_directory>" -d -y install <package name>

You can see the details of additional commands by running sudo apt-get and sudo apt-cache. Alternatively, they are listed by reading the manual pages using the man command, the man apt-get command, and the man apt-cache command.

Pip Python package manager commands

Useful commands for Pip (this is not usually pre-installed on Raspbian) are listed as follows:

  • To install Pip or Python 3, use the sudoapt-get install python3-pip command
  • Install the required package using sudo pip-3.2 install <packagename>
  • Uninstall a particular package using sudo pip-3.2 uninstall<packagename>
  • To find out the version of an installed package, usepip-3.2 freeze | grep <packagename>
  • Install a specific package version usingsudo pip-3.2 install <packagename>==<version>

For example, to check the version of Pi3D installed on your system, usepip-3.2 freeze | grep pi3d.

To replace the installed version of Pi3D with Version 2.13, usesudo pip-3.2 uninstall pi3d and sudo pip-3.2 install pi3d==2.13.