Viola-Jones Example

Prerequisits

Install viola-jones package by running:

pip install -e <path to viola-jones>

Imports


In [1]:
import violajones.IntegralImage as ii
import violajones.AdaBoost as ab
import violajones.Utils as utils
from IPython.core.display import Image, display

Config


In [3]:
pos_training_path = 'trainingdata/faces'
neg_training_path = 'trainingdata/nonfaces'
pos_testing_path = 'trainingdata/faces/test'
neg_testing_path = 'trainingdata/nonfaces/test'

num_classifiers = 20
# For performance reasons restricting feature size
min_feature_height = 4
max_feature_height = 10
min_feature_width = 4
max_feature_width = 10

Load images


In [4]:
print('Loading faces for training..')
faces_training = utils.load_images(pos_training_path)
faces_ii_training = list(map(ii.to_integral_image, faces_training))
print('..done. ' + str(len(faces_training)) + ' faces loaded.\n\nLoading non faces..')
non_faces_training = utils.load_images(neg_training_path)
non_faces_ii_training = list(map(ii.to_integral_image, non_faces_training))
print('..done. ' + str(len(non_faces_training)) + ' non faces loaded.\n')

print('Loading faces for testing..')
faces_testing = utils.load_images(pos_testing_path)
faces_ii_testing = list(map(ii.to_integral_image, faces_testing))
print('..done. ' + str(len(faces_testing)) + ' faces loaded.\n\nLoading test non faces..')
non_faces_testing = utils.load_images(neg_testing_path)
non_faces_ii_testing = list(map(ii.to_integral_image, non_faces_testing))
print('..done. ' + str(len(non_faces_testing)) + ' non faces loaded.\n')


Loading faces for training..
..done. 10 faces loaded.

Loading non faces..
..done. 10 non faces loaded.

Loading faces for testing..
..done. 279 faces loaded.

Loading test non faces..
..done. 215 non faces loaded.

Learn (boost) classifiers (Haar-Like features)


In [5]:
# This will take a while
classifiers = ab.learn(faces_ii_training, non_faces_ii_training, num_classifiers, min_feature_height, max_feature_height, min_feature_width, max_feature_width)


Creating haar-like features..
..done. 49122 features created.

Calculating scores for images..
100% (20 of 20) |#########################| Elapsed Time: 0:00:11 Time: 0:00:11
N/A% (0 of 20) |                         | Elapsed Time: 0:00:00 ETA:  --:--:--
Selecting classifiers..
100% (20 of 20) |#########################| Elapsed Time: 0:00:18 Time: 0:00:18

Testing


In [6]:
print('Testing selected classifiers..')
correct_faces = 0
correct_non_faces = 0
correct_faces = sum(utils.ensemble_vote_all(faces_ii_testing, classifiers))
correct_non_faces = len(non_faces_testing) - sum(utils.ensemble_vote_all(non_faces_ii_testing, classifiers))

print('..done.\n\nResult:\n      Faces: ' + str(correct_faces) + '/' + str(len(faces_testing))
      + '  (' + str((float(correct_faces) / len(faces_testing)) * 100) + '%)\n  non-Faces: '
      + str(correct_non_faces) + '/' + str(len(non_faces_testing)) + '  ('
      + str((float(correct_non_faces) / len(non_faces_testing)) * 100) + '%)')


Testing selected classifiers..
..done.

Result:
      Faces: 209/279  (74.91039426523297%)
  non-Faces: 98/215  (45.58139534883721%)

In [ ]: