Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Computer Vision Projects with OpenCV and Python 3
Computer Vision Projects with OpenCV and Python 3

Computer Vision Projects with OpenCV and Python 3: Six end-to-end projects built using machine learning with OpenCV, Python, and TensorFlow

By Matthew Rever
R$147.99 R$80.00
Book Dec 2018 182 pages 1st Edition
eBook
R$147.99 R$80.00
Print
R$183.99
Subscription
Free Trial
eBook
R$147.99 R$80.00
Print
R$183.99
Subscription
Free Trial

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now

Product Details


Publication date : Dec 28, 2018
Length 182 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781789954555
Category :
Table of content icon View table of contents Preview book icon Preview Book

Computer Vision Projects with OpenCV and Python 3

Setting Up an Anaconda Environment

Welcome to Computer Vision Projects with OpenCV and Python 3. This book is one you might want to check out if you're new to OpenCV, and to computer vision in general.

In this chapter, we will be installing all the required tools that we're going to use in the book. We will be dealing with Python 3, OpenCV, and TensorFlow.

You might be wondering: why should I be using Python 3, and not Python 2? The answer to your question is on Python's own website:

"Python 2 is legacy, Python 3 is the present future of the language."

We are looking to the future here, and if we want to future-proof our code, it's better to use Python 3. If you're using Python 2, some of the code examples here might not run, so we'll install Python 3 and use that for all the projects in the book.

In this chapter, we will cover the following topics:

  • Introducing and installing Python and Anaconda
  • Installing the additional libraries
  • Exploring Jupyter Notebook

Introducing and installing Python and Anaconda

The first thing we need is Python 3. The best way to install this is by downloading Continuum Analytics and the Anaconda distribution.

Anaconda is a fully-featured Python distribution that comes with a lot of packages, including numerical analytics, data science, and computer vision. It's going to make our lives a whole lot easier, because it provides us with libraries that are not present in the base Python distribution.

The best part about Anaconda is that it gives us the conda package manager, along with pip, which makes it very easy to install external packages for our Python distribution.

Let's get started.

Installing Anaconda

We will begin by setting up our Anaconda and Python distribution, using the following steps:

  1. Go to the Anaconda website, using the following link www.anaconda.com/download. You should see a landing page that looks similar to the following screenshot:
  1. Next, select your OS and download the latest version of the Anaconda distribution, which includes Python 3.7. Click the Download button, as shown in the following screenshot:
The installer for Windows is graphical; however, you might need to use a command-line installer for macOS or Linux.

Installing the setup file is pretty straightforward, so we won't go through each step here.

  1. When you have everything properly installed and your path variables defined, go to the Command Prompt and make sure everything is good to go by typing the where python command. This shows us all the directories in which Python is installed. You should see something similar to the following screenshot:

As seen in the preceding screenshot, we see that the first instance of Python is in our Anaconda distribution. This means that we can proceed with our Python programs.

In macOS or Linux, the command would be which python instead of where python.
  1. Now, let's make sure we have our other tools. Our first tool will be IPython, which is essentially a command shell for interactive computing in multiple programming languages. We will check it using the where ipython command, as shown in the following screenshot:
  1. The next package we will check is the pip tool, which is the Python installer package. We do this with the where pip command, as shown in the following screenshot:
  1. The next tool to check is the conda package, which is Anaconda's built-in package manager. This is done using the where conda command, as shown in the following screenshot:

We should be good to go with Python now.

If you run which python on macOS or Linux, and it says something such as user/bin/Python, that means Python is either not installed or it's not the first thing in our path, so we should modify that as per our system.

In the next section, we're going to cover installing additional libraries such as OpenCV, TensorFlow, dlib, and Tesseract, which will be used for the projects in this book.

Installing additional libraries

All the packages that we will be installing in this section are vital for our upcoming projects. So, let's get started.

Installing OpenCV

To get OpenCV, go to the following link: anaconda.org/conda-forge/opencv. Technically, we don't need to access the website to install this package. The site just shows the various versions of OpenCV and all the different systems we can install it on.

Copy and paste the installation command from the site into Command Prompt and then run it, as shown in the following screenshot:

The preceding command is a simple, platform-independent way to get OpenCV. There are other methods for getting it; however, using this command ensures that we are installing the latest version.

Installing dlib

We need to install dlib from the Anaconda distribution, similar to OpenCV. Just as with OpenCV, installing dlib is a straightforward process.

Run the following command:

conda install -c menpo dlib

You will get the following output:

This will take around 10 to 20 seconds to run. If everything goes well, we should be good to go with dlib.

Installing Tesseract

Tesseract is Google's optical character recognition library, and is not natively a Python package. Because of this, there's a Python binding for it that calls the executable, which can then be installed manually.

Go to the GitHub repository for Tesseract, which is found at the following link: https://github.com/tesseract-ocr/tesseract.

Scroll down to the Installing Tesseract section in the GitHub readme. Here, we are presented with two options:

  • Installing it via a pre-built binary package
  • Building it from source

We want to install it via the pre-built binary package, so click on that link. We can also build it from source if we want to, but that doesn't really offer any advantages. The Tesseract Wiki explains the steps to install it on various different operating systems.

As we're using Windows, and we want to install a pre-built one, click on the Tesseract at UB Mannheim link, where you will find all the latest setup files. Download the latest setup from the site.

Once downloaded, run the installer or execute the command. However, this is not going to put Tesseract in your path. We need to make sure it is in your path; otherwise, when you call Tesseract from within Python, you're going to get an error message.

So, we need to figure out where Tesseract is and modify our path variable. To do this, type the where tesseract command in Command Prompt, as shown in the following screenshot:

Once you have the binary packages, use the pip command to apply the Python binding to the packages. Use the following commands:

$ pip install tesseract
$ pip install pytesseract

We should be good to go with Tesseract now.

Installing TensorFlow

Last but not least, we will install TensorFlow, which is a software library for data flow programming across a range of tasks. It is usually used for machine learning applications such as neural networks.

To install it, go to TensorFlow's website at the following link: tensorflow.org/install/. The website contains instructions for all the major operating systems.

As we're using Windows, the installation process is very simple. We just have to run the pip install tensorflow command in Command Prompt, as seen in the following screenshot:

As seen in the preceding screenshot, TensorFlow is already installed on the system, so it says that the requirements are satisfied. We should be good to go with TensorFlow now.

Install tensorflow-hub using the following command:

pip install tensorflow-hub

Next, install tflearn using the following command:

pip install tflearn

Finally, Keras is a high-level interface, which can be installed using the following command:

pip install keras

We have installed OpenCV, TensorFlow, dlib, and Tesseract, so we should be good to go with the tools for our book. Our next step will be exploring Jupyter Notebook, which should be fun!

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.

Summary

In this chapter, we learned about the Anaconda distribution and different ways to install Python. We learned how to set up Python using the Anaconda distribution.

Next, we looked at how to install various libraries in Anaconda, to make it easier for us to run various programs. Finally, we learned the basics of Jupyter Notebook and how it works.

In the next chapter, Chapter 2, Image Captioning with TensorFlow, we will look at how to carry out image captioning using TensorFlow.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Implement image classification and object detection using machine learning and deep learning
  • Perform image classification, object detection, image segmentation, and other Computer Vision tasks
  • Crisp content with a practical approach to solving real-world problems in Computer Vision

Description

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.

What you will learn

Install and run major Computer Vision packages within Python Apply powerful support vector machines for simple digit classification Understand deep learning with TensorFlow Build a deep learning classifier for general images Use LSTMs for automated image captioning Read text from real-world images Extract human pose data from images

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now

Product Details


Publication date : Dec 28, 2018
Length 182 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781789954555
Category :

Table of Contents

9 Chapters
Preface Chevron down icon Chevron up icon
Setting Up an Anaconda Environment Chevron down icon Chevron up icon
Image Captioning with TensorFlow Chevron down icon Chevron up icon
Reading License Plates with OpenCV Chevron down icon Chevron up icon
Human Pose Estimation with TensorFlow Chevron down icon Chevron up icon
Handwritten Digit Recognition with scikit-learn and TensorFlow Chevron down icon Chevron up icon
Facial Feature Tracking and Classification with dlib Chevron down icon Chevron up icon
Deep Learning Image Classification with TensorFlow Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Filter icon Filter
Top Reviews
Rating distribution
Empty star icon Empty star icon Empty star icon Empty star icon Empty star icon 0
(0 Ratings)
5 star 0%
4 star 0%
3 star 0%
2 star 0%
1 star 0%

Filter reviews by


No reviews found
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.