Reader small image

You're reading from  Raspberry Pi Computer Vision Programming. - Second Edition

Product typeBook
Published inJun 2020
Reading LevelBeginner
PublisherPackt
ISBN-139781800207219
Edition2nd Edition
Languages
Tools
Right arrow
Author (1)
Ashwin Pajankar
Ashwin Pajankar
author image
Ashwin Pajankar

Ashwin Pajankar is an author, a YouTuber, and an instructor. He graduated from the International Institute of Information Technology, Hyderabad, with an MTech in Computer Science and Engineering. He has been writing programs for over two and a half decades. He is proficient in Linux, Unix shell scripting, C, C++, Java, JavaScript, Python, PowerShell, Golang, HTML, and assembly language. He has worked on single-board computers such as Raspberry Pi and Banana Pro. He is also proficient with microcontroller boards such as Arduino and the BBC Micro:bit. He is currently self-employed and teaches on Udemy and YouTube. He also organizes programming boot camps for working professionals and software companies.
Read more about Ashwin Pajankar

Right arrow

Chapter 3: Introduction to Python Programming

In the previous chapter, we learned how to remotely access the Command Prompt and the desktop of a Raspberry Pi (RPi) board. We also installed OpenCV for Python 3. Finally, we learned how to overclock the RPi and examined the various heatsinks for the RPi.

Continuing from where we left off at the end of the previous chapter, in this chapter, we will start by looking at Python 3 programming on the RPi. We will have a brief look at the Scientific Python (SciPy) ecosystem and all the libraries in it. Then, we will write basic programs for numerical computation with NumPy N-Dimensional Arrays (ndarrays). We will also learn how to visualize data with Matplotlib. Finally, we will explore the hardware aspects of the RPi with the General Purpose Input Output (GPIO) library of Python for the RPi.

In short, we will cover the following topics:

  • Understanding Python 3
  • The SciPy ecosystem
  • Programming with NumPy and Matplotlib
  • ...

Technical requirements

The code files of this chapter can be found on GitHub at https://github.com/PacktPublishing/raspberry-pi-computer-vision-programming/tree/master/Chapter03/programs.

Check out the following video to see the Code in Action at https://bit.ly/37UVwmO.

Understanding Python 3

Python is a high-level, interpreted, general-purpose programming language. It was created by Guido van Rossum and was started as a personal hobby project but has since grown into what it is today. The following is a timeline of the major milestones in the development of the Python programming language:

Figure 3.1 – Timeline of Python development milestones

Guido van Rossum held the title of benevolent dictator for life for the Python project for most of its life cycle. He stepped down from the role in July 2018 and has been part of the Python Steering Council ever since.

You can read more about Python on its home page at www.python.org.

The Python programming language has two major versions—Python 2 and Python 3. They are mostly incompatible with one another. As the preceding timeline shows, Python 2's sunset happened on 31st December 2019. This means that there is no further development of Python 2. Official...

The SciPy ecosystem

The SciPy ecosystem is a collection of libraries for programming science, mathematics, and engineering functionalities. It has the following libraries as core components:

  • NumPy
  • SciPy
  • Matplotlib
  • IPython
  • SymPy
  • pandas

In this book, we will use all of these libraries except SymPy and pandas. In this section, we will have a look at the NumPy and Matplotlib libraries. We will learn the useful aspects of the other two libraries in the later chapters of this book.

The basics of NumPy

NumPy is a fundamental package that can be used for numerical computation with Python. It is a matrix library for linear algebra. NumPy ndarrays can also be used as an efficient multi-dimensional container of generic data. Arbitrary data types can also be defined and used. NumPy is an extension of the Python programming language. It adds support for large multi-dimensional arrays and matrices, along with a large library of high-level mathematical functions...

RPi GPIO programming with Python 3

One of the main unique selling points of the RPi and similar single-board computers is the onboard GPIO pins. A few early models of the RPi boards have 26 pins. Most recent models have 40 pins for GPIO. We can obtain the details of the pins on a board by running the pinout command on the Command Prompt. The following is the output of the command for my RPi 4B board:

Figure 3.16 – Part 1 of the command pinout

In the top left, we can see the 40 pins for GPIO. Pin number 1 is labeled there. The red circle above it is pin number 2. The pin adjacent to pin number 1 is pin number 3, and so on. The following part of the output shows the numbering of all the pins:

Figure 3.17 – Part 2 of the command pinout

As we can see in the preceding output, we have power pins (3V3, 5V, and GND) and digital I/O pins, marked as GPIOxx.

LED programming with GPIO

Now, we will see how to program LEDs...

Summary

We learned the basics of Python 3 programming in this chapter. We also learned about the SciPy ecosystem and experimented with the NumPy and Matplotlib libraries. Finally, we saw how to use the GPIO pins of the RPi with LEDs and push buttons.

In the next chapter, we will get started with Python 3 and OpenCV programming. We will also try out a lot of hands-on exercises to learn about programming using a webcam and a RPi camera module.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Raspberry Pi Computer Vision Programming. - Second Edition
Published in: Jun 2020Publisher: PacktISBN-13: 9781800207219
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Ashwin Pajankar

Ashwin Pajankar is an author, a YouTuber, and an instructor. He graduated from the International Institute of Information Technology, Hyderabad, with an MTech in Computer Science and Engineering. He has been writing programs for over two and a half decades. He is proficient in Linux, Unix shell scripting, C, C++, Java, JavaScript, Python, PowerShell, Golang, HTML, and assembly language. He has worked on single-board computers such as Raspberry Pi and Banana Pro. He is also proficient with microcontroller boards such as Arduino and the BBC Micro:bit. He is currently self-employed and teaches on Udemy and YouTube. He also organizes programming boot camps for working professionals and software companies.
Read more about Ashwin Pajankar