Putting it all together

In order to run our app, we will need to execute the main function routine (in chapter7.py) that loads the pre-trained cascade classifier and the pre-trained multi-layer perceptron, and applies them to each frame of the webcam live stream.

However, this time, instead of collecting more training samples, we will select the radio button that says Test. This will trigger an EVT_RADIOBUTTON event, which binds to FaceLayout._on_testing, disabling all training-related buttons in the GUI and switching the app to the testing mode. In this mode, the pre-trained MLP classifier is applied to every frame of the live stream, trying to predict the current facial expression.

As promised earlier, we now return to FaceLayout._process_frame:

def _process_frame(self, frame):
    """ detects face, predicts face label in testing mode """

Unchanged from what we discussed earlier, the method begins by detecting faces in a downscaled grayscale version of the current frame:

    success, frame, self.head = self.faces.detect(frame)

However, in the testing mode, there is more to the function:

    # in testing mode: predict label of facial expression
    if success and self.testing.GetValue():

In order to apply our pre-trained MLP classifier to the current frame, we need to apply the same preprocessing to the current frame as we did with the entire training set. After aligning the head region, we apply PCA by using the pre-loaded basis vectors (self.pca_V) and mean values (self.pca_m):

    # if face found: preprocess (align)
    success, head = self.faces.align_head(self.head)
    if success:
        # extract features using PCA (loaded from file)
        X, _, _ = homebrew.extract_features([head.flatten()], self.pca_V, self.pca_m)

Then, we are ready to predict the class label of the current frame:

    # predict label with pre-trained MLP
    label = self.MLP.predict(np.array(X))[0]

Since the predict method already returns a string label, all that is left to do is to display it above the bounding box in the current frame:

    # draw label above bounding box
    cv2.putText(frame, str(label), (x,y-20), cv2.FONT_HERSHEY_COMPLEX, 1, (0,255,0), 2)
    break # need only look at first, largest face

Finally, we are done!

    return frame

The end result looks like the following:

Putting it all together

Although the classifier has only been trained on (roughly) 100 training samples, it reliably detects my various facial expressions in every frame of the live stream, no matter how distorted my face seemed to be at the moment. This is a good indication that the neural network that was learned neither underfits nor overfits the data, since it is capable of predicting the correct class labels even for new data samples.

Summary

The final chapter of this book has really rounded up our experience and made us combine a variety of our skills to arrive at an end-to-end app that consists of both object detection and object recognition. We became familiar with a range of pre-trained cascade classifiers that OpenCV has to offer, collected our very own training dataset, learned about multi-layer perceptrons, and trained them to recognize emotional expressions in faces. Well, at least my face.

The classifier was undoubtedly able to benefit from the fact that I was the only subject in the dataset, but with all the knowledge and experience that you have gathered with this book, it is now time to overcome these limitations! You can start small and train the classifier on images of you indoors and outdoors, at night and day, during summer and winter. Or, maybe, you are anxious to tackle a real-world dataset and be part of Kaggle's Facial Expression Recognition challenge (see https://www.kaggle.com/c/challenges-in-representation-learning-facial-expression-recognition-challenge).

If you are into machine learning, you might already know that there is a variety of accessible libraries out there, such as pylearn (https://github.com/lisa-lab/pylearn2), scikit-learn (http://scikit-learn.org), and pycaffe (http://caffe.berkeleyvision.org). Deep learning enthusiasts might want to look into Theano (http://deeplearning.net/software/theano) or Torch (http://torch.ch). Finally, if you find yourself stuck with all these algorithms and no datasets to apply them to, make sure to stop by the UC Irvine Machine Learning Repository (http://archive.ics.uci.edu/ml).

Congratulations! You are now an OpenCV expert.

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

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