Reader small image

You're reading from  Computer Vision Projects with OpenCV and Python 3

Product typeBook
Published inDec 2018
Reading LevelIntermediate
PublisherPackt
ISBN-139781789954555
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Matthew Rever
Matthew Rever
author image
Matthew Rever

Matthew Rever received his PhD. in electrical engineering from the University of Michigan, Ann Arbor. His career revolves around image processing, computer vision, and machine learning for scientific research applications. He started programming in C++, a language he still uses today, over 20 years ago, and has also used Matlab and most heavily Python in the past few years, using OpenCV, SciPy, scikit-learn, TensorFlow, and PyTorch. He believes it is important to stay up to date on the latest tools to be as productive as possible. Dr. Rever is the author of Packt's Computer Vision Projects with Python 3 and Advanced Computer Vision Projects.
Read more about Matthew Rever

Right arrow

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.

Previous PageNext Page
You have been reading a chapter from
Computer Vision Projects with OpenCV and Python 3
Published in: Dec 2018Publisher: PacktISBN-13: 9781789954555
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
Matthew Rever

Matthew Rever received his PhD. in electrical engineering from the University of Michigan, Ann Arbor. His career revolves around image processing, computer vision, and machine learning for scientific research applications. He started programming in C++, a language he still uses today, over 20 years ago, and has also used Matlab and most heavily Python in the past few years, using OpenCV, SciPy, scikit-learn, TensorFlow, and PyTorch. He believes it is important to stay up to date on the latest tools to be as productive as possible. Dr. Rever is the author of Packt's Computer Vision Projects with Python 3 and Advanced Computer Vision Projects.
Read more about Matthew Rever