O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Raspberry Pi 3 Cookbook for Python Programmers - Third Edition

Book Description

A recipe-based guide to programming your Raspberry Pi 3 using Python

About This Book
  • Leverage the power of Raspberry Pi 3 using Python programming
  • Create 3D games, build neural network modules, and interface with your own circuits
  • Packed with clear, step-by-step recipes to walk you through the capabilities of 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 You Will Learn
  • Learn to set up and run Raspberry Pi 3
  • Build text classifiers and perform automation using Python
  • Predict sentiments in words and create games and graphics
  • Detect edges and contours in images
  • Build human face detection and recognition system
  • Use Python to drive hardware
  • Sense and display real-world data
  • Build a neural network module for optical character recognition
  • Build movie recommendations system
In Detail

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.

Style and approach

Written in a cookbook style, this book contains a series of recipes on various topics. It is an easy-to-follow step-by-step guide with examples of feature integration suitable for any search application.

Downloading the example code for this book You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. Title Page
  2. Copyright and Credits
    1. Raspberry Pi 3 Cookbook for Python Programmers Third Edition
  3. Dedication
  4. Packt Upsell
    1. Why subscribe?
    2. PacktPub.com
  5. Contributors
    1. About the authors
    2. About the reviewers
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Sections
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    5. Get in touch
      1. Reviews
  7. Getting Started with a Raspberry Pi 3 Computer
    1. Introduction
      1. Introducing Raspberry Pi
      2. What's with the name?
      3. Why Python?
        1. Python 2 and Python 3
        2. Which version of Python should you use?
        3. The Raspberry Pi family – a brief history of Pi
        4. Which Pi to choose?
    2. Connecting to Raspberry Pi
      1. Getting ready
      2. How to do it...
      3. There's more...
        1. Secondary hardware connections
    3. Using NOOBS to set up your Raspberry Pi SD card
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Changing the default user password
        2. Ensuring that you shut down safely
        3. Preparing an SD card manually
        4. Expanding the system to fit in your SD card
        5. Accessing the RECOVERY/BOOT partition
        6. Using the tools to back up your SD card in case of failure
    4. Networking and connecting your Raspberry Pi to the internet via an Ethernet port, using a CAT6 Ethernet cable
      1. Getting ready
      2. How to do it...
      3. There's more...
    5. Using built-in Wi-Fi and Bluetooth on Raspberry Pi
      1. Getting ready
      2. How to do it...
        1. Connecting to your Wi-Fi network
        2. Connecting to Bluetooth devices
    6. Configuring your network manually
      1. Getting ready
      2. How to do it...
      3. There's more...
    7. Networking directly to a laptop or computer
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Direct network link
      5. See also
    8. Networking and connecting your Raspberry Pi to the internet via a USB Wi-Fi dongle
      1. Getting ready
      2. How to do it...
      3. There's more...
        1. Using USB wired network adapters
    9. Connecting to the internet through a proxy server
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    10. Connecting remotely to Raspberry Pi over the network using VNC
      1. Getting ready
      2. How to do it...
      3. There's more...
    11. Connecting remotely to Raspberry Pi over the network using SSH (and X11 forwarding)
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Running multiple programs with X11 forwarding
        2. Running as a desktop with X11 forwarding
        3. Running Pygame and Tkinter with X11 forwarding
    12. Sharing the home folder of Raspberry Pi with SMB
      1. Getting ready
      2. How to do it...
    13. Keeping Raspberry Pi up to date
      1. Getting ready
      2. How to do it...
      3. There's more...
  8. Dividing Text Data and Building Text Classifiers
    1. Introduction
    2. Building a text classifier
      1. How to do it...
      2. How it works...
      3. See also
    3. Pre-processing data using tokenization
      1. How to do it...
    4. Stemming text data
      1. How to do it...
    5. Dividing text using chunking
      1. How to do it...
    6. Building a bag-of-words model
      1. How to do it...
    7. Applications of text classifiers
  9. Using Python for Automation and Productivity
    1. Introduction
    2. Using Tkinter to create graphical user interfaces
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Creating a graphical application – Start menu
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    4. Displaying photo information in an application
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    5. Organizing your photos automatically
      1. Getting ready
      2. How to do it...
      3. How it works...
  10. Predicting Sentiments in Words
    1. Building a Naive Bayes classifier
      1. How to do it...
      2. See also
    2. Logistic regression classifier
      1. How to do it...
    3. Splitting the dataset for training and testing
      1. How to do it...
    4. Evaluating the accuracy using cross-validation
      1. How to do it...
    5. Analyzing the sentiment of a sentence
      1. How to do it...
    6. Identifying patterns in text using topic modeling
      1. How to do it...
    7. Applications of sentiment analysis
  11. Creating Games and Graphics
    1. Introduction
    2. Using IDLE3 to debug your programs
      1. How to do it...
      2. How it works...
    3. Drawing lines using a mouse on Tkinter Canvas
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Creating a bat and ball game
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Creating an overhead scrolling game
      1. Getting ready
      2. How to do it...
      3. How it works...
  12. Detecting Edges and Contours in Images
    1. Introduction
    2. Loading, displaying, and saving images
      1. How to do it...
    3. Image flipping
      1. How to do it...
    4. Image scaling
      1. How to do it...
    5. Erosion and dilation
      1. How to do it...
    6. Image segmentation
      1. How to do it...
    7. Blurring and sharpening images
      1. How to do it...
    8. Detecting edges in images
      1. How to do it...
      2. How it works...
      3. See also
    9. Histogram equalization
      1. How to do it…
    10. Detecting corners in images
      1. How to do it...
  13. Creating 3D Graphics
    1. Introduction
    2. Getting started with 3D coordinates and vertices
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Camera
        2. Shaders
        3. Lights
        4. Textures
    3. Creating and importing 3D models
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Creating or loading your own objects
        2. Changing the object's textures and .mtl files
        3. Taking screenshots
    4. Creating a 3D world to explore
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Building 3D maps and mazes
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. The Building module
        2. Using SolidObjects to detect collisions
  14. Building Face Detector and Face Recognition Applications
    1. Introduction
    2. Building a face detector application
      1. How to do it...
    3. Building a face recognition application
      1. How to do it...
      2. How it works...
      3. See also
    4. Applications of a face recognition system
  15. Using Python to Drive Hardware
    1. Introduction
    2. Controlling an LED
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Controlling the GPIO current
    3. Responding to a button
      1. Getting ready
        1. Trying a speaker or headphone with Raspberry Pi
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Safe voltages
        2. Pull-up and pull-down resistor circuits
        3. Protection resistors
    4. A controlled shutdown button
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Resetting and rebooting Raspberry Pi
        2. Adding extra functions
    5. The GPIO keypad input
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Generating other key combinations
        2. Emulating mouse events
    6. Multiplexed color LEDs
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Hardware multiplexing
        2. Displaying random patterns
        3. Mixing multiple colors
    7. Writing messages using persistence of vision
      1. Getting ready
      2. How to do it...
      3. How it works...
  16. Sensing and Displaying Real-World Data
    1. Introduction
    2. Using devices with the I2C bus
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Using multiple I2C devices
        2. I2C bus and level shifting
        3. Using just the PCF8591 chip or adding alternative sensors
    3. Reading analog data using an analog-to-digital converter
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Gathering analog data without hardware
    4. Logging and plotting data
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Plotting live data
        2. Scaling and calibrating data
    5. Extending the Raspberry Pi GPIO with an I/O expander
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. I/O expander voltages and limits
        2. Using your own I/O expander module
        3. Directly controlling an LCD alphanumeric display
    6. Capturing data in an SQLite database
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. The CREATE TABLE command
        2. The INSERT command
        3. The SELECT command
        4. The WHERE command
        5. The UPDATE command
        6. The DELETE command
        7. The DROP command
    7. Viewing data from your own webserver
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Security
        2. Using MySQL instead
    8. Sensing and sending data to online services
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
  17. Building Neural Network Modules for Optical Character Recognition
    1. Introduction
    2. Visualizing optical characters
      1. How to do it...
    3. Building an optical character recognizer using neural networks
      1. How to do it...
      2. How it works...
      3. See also
    4. Applications of an OCR system
  18. Building Robots
    1. Introduction
    2. Building a Rover-Pi robot with forward driving motors
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Darlington array circuits
        2. Transistor and relay circuits
        3. Tethered or untethered robots
        4. Rover kits
    3. Using advanced motor control
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Motor speed control using PWM control
        2. Using I/O expanders
    4. Building a six-legged Pi-Bug robot
      1. Getting ready
      2. How to do it...
      3. How it works...
        1. Controlling the servos
        2. The servo class
        3. Learning to walk
        4. The Pi-Bug code for walking
    5. Controlling servos directly with ServoBlaster
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Using an infrared remote control with your Raspberry Pi
      1. Getting ready
      2. How to do it...
      3. There's more...
    7. Avoiding objects and obstacles
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Ultrasonic reversing sensors
    8. Getting a sense of direction
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Calibrating the compass
        2. Calculating the compass bearing
        3. Saving the calibration
        4. Driving the robot using the compass
  19. Interfacing with Technology
    1. Introduction
    2. Automating your home with remotely controlled electrical sockets
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Sending RF control signals directly
        2. Extending the range of the RF transmitter
        3. Determining the structure of the remote control codes
    3. Using SPI to control an LED matrix
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Daisy-chain SPI configuration
    4. Communicating using a serial interface
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Configuring a USB-to-RS232 device for Raspberry Pi
        2. RS232 signals and connections
        3. Using the GPIO built-in serial pins
        4. The RS232 loopback
    5. Controlling Raspberry Pi using Bluetooth
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Configuring Bluetooth module settings
    6. Controlling USB devices
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
        1. Controlling similar missile-type devices
        2. Robot arm
        3. Taking USB control further
  20. Can I Recommend a Movie for You?
    1. Introduction
    2. Computing the Euclidean distance score
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    3. Computing a Pearson correlation score
      1. How to do it...
      2. How it works...
      3. There's more...
      4. See also
    4. Finding similar users in the dataset
      1. How to do it...
      2. See also
    5. Developing a movie recommendation module
      1. How to do it...
      2. See also
    6. Applications of recommender systems
  21. Hardware and Software List
    1. Introduction
    2. General component sources
      1. General electronic component retailers
      2. Makers, hobbyists, and Raspberry Pi specialists
    3. The hardware list
      1. Chapter 1
      2. Chapters 2 – Chapter 7
      3. Chapter 8
      4. Chapter 9
      5. Chapter 10
      6. Chapter 11
      7. Chapter 12
      8. Chapter 13
      9. Chapter 14
    4. The software list
      1. PC software utilities
      2. Raspberry Pi packages
        1. Chapter 1
        2. Chapter 2
        3. Chapter 3
        4. Chapter 4
        5. Chapter 5
        6. Chapter 6
        7. Chapter 7
        8. Chapter 8
        9. Chapter 9
        10. Chapter 10
        11. Chapter 11
        12. Chapter 12
        13. Chapter 13
        14. Chapter 14
      3. There's more...
        1. APT commands
        2. Pip Python package manager commands
  22. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think