Book Image

Computer Vision Projects with OpenCV and Python 3

By : Matthew Rever
Book Image

Computer Vision Projects with OpenCV and Python 3

By: Matthew Rever

Overview of this book

Python is the ideal programming language for rapidly prototyping and developing production-grade codes for image processing and Computer Vision with its robust syntax and wealth of powerful libraries. This book will help you design and develop production-grade Computer Vision projects tackling real-world problems. With the help of this book, you will learn how to set up Anaconda and Python for the major OSes with cutting-edge third-party libraries for Computer Vision. You'll learn state-of-the-art techniques for classifying images, finding and identifying human postures, and detecting faces within videos. You will use powerful machine learning tools such as OpenCV, Dlib, and TensorFlow to build exciting projects such as classifying handwritten digits, detecting facial features,and much more. The book also covers some advanced projects, such as reading text from license plates from real-world images using Google’s Tesseract software, and tracking human body poses using DeeperCut within TensorFlow. By the end of this book, you will have the expertise required to build your own Computer Vision projects using Python and its associated libraries.
Table of Contents (9 chapters)

Exploring Jupyter Notebook

Now that we have our libraries installed, we're ready to get started with Jupyter Notebook. Jupyter Notebook is a really nice way of creating interactive code and widgets. It lets us create interactive presentations with live code and experiment, as we're doing here.

If everything is set up correctly using Anaconda, we should already have Jupyter installed. Let's go ahead and take a look at Jupyter Notebook now.

Open Command Prompt in the directory where your code files are, and then run the jupyter notebook command. This will open up a web browser in the directory where the command was executed. This should result in an interface that looks similar to the following screenshot:

Next, open the .ipynb file so you can explore the basic functionalities of Jupyter Notebook. Once opened, we should see a page similar to the following screenshot:

As shown, there are blocks (called cells) where we can put in Python commands and Python code. We can also enter other commands, also known as magic commands, which are not part of Python per se, but allow us to do nice things within Jupyter or IPython (an interactive shell for Python). The % at the beginning means that the command is a magic command.

The biggest advantage here is that we can execute individual lines of code, instead of having to type out the entire code block in one go.

If you're new to Jupyter Notebook, go to the following link: https://www.cheatography.com/weidadeyue/cheat-sheets/jupyter-notebook/. Here, they list Jupyter Notebook keyboard shortcuts, which can be really useful for quick code testing.

Let's go through some of the commands seen in the preceding screenshot, and see what they do, as follows:

  1. The %pylab notebook command, as seen in the first cell, imports a lot of very useful and common libraries, particularly NumPy and PyPlot, without us needing to explicitly call the import commands. It also sets up a Notebook easily.
  2. Also in the first cell, we assign the directory that we will be working from, as follows:
%cd C:\Users\<user_name>\Documents\<Folder_name>\Section1-Getting_started

This results in the following output:

So far, so good!

  1. The following cell shows how to import our libraries. We're going to import OpenCV, TensorFlow, dlib, and Tesseract, just to make sure that everything is working and that there aren't any nasty surprises. This is done using the following code block:
import cv2
import tensorflow as tf
import dlib
import pytesseract as ptess

If you get an error message here, redo the installation of the libraries, following the instructions carefully. Sometimes things do go wrong, depending on our system.

  1. The third cell in the screenshot contains the command for importing the graph module in TensorFlow. This can come in handy for getting help on a function from within the Notebook, as follows:
tf.Graph?

We will discuss this function in Chapter 7, Deep Learning Image Classification with TensorFlow.

  1. Another neat thing about Jupyter Notebooks is that we can run shell commands right in the cell. As seen in the fourth cell in the screenshot (repeated here), the ls command shows us all the files in the directory we are working from:
  1. In this book, we'll be working with a lot of images, so we'll want to see the images right in the Notebook. Use the imread() function to read the image file from your directory. After that, your next step is to create a figure() widget to display the image. Finally, use the imshow() function to actually display the image.

This entire process is summarized in the following screenshot:

This is awesome, because we have widgets that are flexible.

  1. By grabbing the bottom-right corner, we can shrink this to a reasonable size to see our color image with pixel axes displayed. We also have the pan option available. By clicking on it, we can pan our image and box zoom it. Hitting the home button resets the original view.

We will want take a look at our images, and processed images and so forth—as seen previously, this is a very handy and simple way to do it. We can also use PyLab inline, which is useful for certain things, as we'll see.

  1. As we know, part of computer vision is working with videos. To play videos within the notebook, we need to import some libraries and use IPython's HTML capability, as shown in the following screenshot:

Essentially, we're using our web browser's capability to play back videos. So, it's not really Python that's doing it, but our web browser, which enables interactivity between Jupyter Notebook and our browser.

Here, we defined the playvideo() function, which takes the video filename as an input and returns an HTML object with our video.

  1. Execute the following command on Jupyter to play the Megamind video. It's just a clip of the movie Megamind, which (for some reason) comes with OpenCV if we download all the source code:
playvideo(' ./Megamind.mp4')
  1. You will see a black box, and if you scroll down, you'll find a play button. Hit this and the movie will play, as seen in the following screenshot:

This can be used to play our own videos. All you have to do is point the command to the video that you want to play.

Once you have all this running, you should be in good shape to run the projects that we're going to take a look at in the coming chapters.