Chapter 3. Training a Smart Alarm to Recognize the Villain and His Cat

 

The naming of cats is a difficult matter.

 
 -- T. S. Eliot, Old Possum's Book of Practical Cats (1939)
 

Blofeld: I've taught you to love chickens, to love their flesh, their voice.

 
 --On Her Majesty's Secret Service (1969)

If you saw Ernst Stavro Blofeld, would you recognize him?

Let me remind you that Blofeld, as the Number 1 man in SPECTRE (SPecial Executive for Counterintelligence, Terrorism, Revenge, and Extortion), is a supervillain who eludes James Bond countless times before being written out of the movies due to an intellectual property dispute. Blofeld last appears as an anonymous character in the introductory sequence of the movie For Your Eyes Only (1981), where we see him fall from a helicopter and down a factory's smokestack as he shouts, "Mr. Boooooooooond!"

Despite this dramatic exit, the evidence of Blofeld's death is unclear. After all, Blofeld is a notoriously difficult man to recognize. His face is seldom caught on camera. As early as the 1960s, he was using plastic surgery to change his identity and to turn his henchmen into look-alikes of himself. A half-century later we must ask, is Blofeld a dead man or is he just made over, perhaps as a beautiful actress in a Colombian telenovela?

One thing is certain. If Blofeld is alive, he is accompanied by a blue-eyed, white Angora cat (preserved by a veterinary miracle or taxidermy). Patting this cat is Blofeld's telltale habit in every movie. His face might be different but his lap cat is the same. We last see the cat jumping out of Blofeld's lap just before the fateful helicopter ride in the movie.

The moral is that two approaches to identification are better than one. Though we cannot see the man's face, we should not lose sight of his cat.

To automate the search for villains and their cats, we are going to develop a desktop or a Raspberry Pi application called Angora Blue (an innocent sounding codename that alludes to the blue eyes of Blofeld's cat). Angora Blue will send us an e-mail alert when it recognizes a specified villain or a specified cat with a certain level of confidence. We will also develop a GUI app called Interactive Recognizer, which will train Angora Blue's recognition model based on camera images and names that we will provide interactively. To distinguish faces from the background, Interactive Recognizer depends on a human face detection model that comes with OpenCV and a cat face detection model that we are going to train using an original script and third-party image databases.

This is a big chapter, but it is rewarding because you will learn a process that can be applied to detecting and recognizing any kind of animal face and even objects!

Note

The completed project for this chapter can be downloaded from my website at http://nummist.com/opencv/7376_03.zip. It excludes the datasets of annotated training images, which are large downloads and should be obtained separately from the sources described in this chapter.

Our work throughout this chapter builds on the techniques of machine learning, meaning that the software makes predictions or decisions based on statistical models. Our approach is one of supervised learning, meaning that we (programmers and users) will provide the software with examples of data and correct responses. The software creates the statistical model to extrapolate from these examples. The human-provided examples are referred to as reference data or training data (or reference images or training images in the context of computer vision). Conversely, the software's extrapolations pertain to test data (or test images or scenes in the context of computer vision).

Supervised learning is much like the "flashcard" pedagogy used in early childhood education. The teacher shows the child a series of pictures (training images) and says, "This is a cow. Moo! This is a horse. Neigh!"

Then, on a field trip to a farm (a scene), the child can hopefully distinguish between a horse and a cow. However, I must confess that I once mistook a horse for a cow and I was teased about this misclassification for many years thereafter.

Besides being a computer vision library, OpenCV offers a general-purpose machine learning module that can process any kind of data, not necessarily images. For more information on this module and the underlying machine learning concepts, refer to the Machine Learning section of the OpenCV-Python Tutorials by Alexander Mordvintsev and Abid Rahman K at http://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_ml/py_table_of_contents_ml/py_table_of_contents_ml.html. Meanwhile, our chapter proceeds with more specialized machine learning functionality and concepts that OpenCV users often apply to face detection and recognition.