Obtaining the dataset

For the purpose of this section, we will work with the MIT People dataset, which we are free to use for non-commercial purposes. So make sure not to use this in your groundbreaking autonomous start-up company before obtaining a corresponding software license.

However, if you followed our installation instructions from earlier and checked out the code on GitHub, you already have the dataset and are ready to go! The file can be found at https://github.com/PacktPublishing/Machine-Learning-for-OpenCV-Second-Edition/blob/master/data/chapter6/pedestrians128x64.tar.gz.

By referring to the following steps, you will learn to detect pedestrians in the wild:

  1. Since we are supposed to run this code from a Jupyter Notebook in the notebooks/ directory, the relative path to the data directory is simply data/:
In [1]: datadir = "data"
... dataset = "pedestrians128x64"
... datafile = "%s/%s.tar.gz" % (datadir, dataset)
  1. The first thing to do is to unzip the file. We will extract all files into their own subdirectories in data/pedestrians128x64/:
In [2]: extractdir = "%s/%s" % (datadir, dataset)
  1. We can do this either by hand (outside of Python) or with the following function:
In [3]: def extract_tar(filename):
... try:
... import tarfile
... except ImportError:
... raise ImportError("You do not have tarfile installed. "
... "Try unzipping the file outside of "
... "Python.")
... tar = tarfile.open(datafile)
... tar.extractall(path=extractdir)
... tar.close()
... print("%s successfully extracted to %s", % (datafile,
... extractdir))

  1. Then we can call the function like this:
In [4]: extract_tar(datafile)
Out[4]: data/pedestrians128x64.tar.gz successfully extracted to
data/pedestrians128x64
If you haven't seen the try...except statement before, it is a great way to deal with exceptions as they occur. For example, trying to import a module called tarfile could result in an ImportError if the module does not exist. We capture this exception with except ImportError and display a custom error message. You can find more information about handling exceptions in the official Python docs at https://docs.python.org/3/tutorial/errors.html#handling-exceptions.

The dataset comes with a total of 924 color images of pedestrians, each scaled to 64 x 128 pixels and aligned so that the person's body is in the center of the image. Scaling and aligning all data samples is an important step of the process, and we are glad that we don't have to do it ourselves.

  1. These images were taken in Boston and Cambridge in a variety of seasons and under several different lighting conditions. We can visualize a few example images by reading the image with OpenCV and passing an RGB version of the image to matplotlib:
In [5]: import cv2
... import matplotlib.pyplot as plt
... %matplotlib inline
  1. We will do this in a loop for images 100-104 in the dataset, so we get an idea of the different lighting conditions:
In [6]: for i in range(5):
... filename = "%s/per0010%d.ppm" % (extractdir, i)
... img = cv2.imread(filename)
... plt.subplot(1, 5, i + 1)
... plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
... plt.axis('off')
  1. The output looks as follows:

The preceding screenshot shows examples of images of people in the database. The examples vary in color, texture, viewpoint (either frontal or rear), and background.

We can look at additional pictures, but it's pretty clear that there is no straightforward way to describe pictures of people as easily as we did in the previous section for + and - data points. Therefore, part of the problem is finding a good way to represent these images. Does this ring a bell? It should! We're talking about feature engineering.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset