test.py 4.62 KB
Newer Older
Pedro TOME's avatar
Pedro TOME committed
1 2 3 4 5 6 7 8
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :


"""Test Units
"""

import os
9 10 11
import numpy
import nose.tools

Pedro TOME's avatar
Pedro TOME committed
12 13
import pkg_resources

14 15
import bob.io.base
import bob.io.matlab
André Anjos's avatar
André Anjos committed
16
import bob.io.image
Pedro TOME's avatar
Pedro TOME committed
17 18


19 20 21 22 23 24 25 26
def F(parts):
  """Returns the test file path"""

  return pkg_resources.resource_filename(__name__, os.path.join(*parts))


def test_finger_crop():

André Anjos's avatar
André Anjos committed
27
  #Test finger vein image preprocessors
28

André Anjos's avatar
André Anjos committed
29 30
  input_filename = F(('preprocessors', '0019_3_1_120509-160517.png'))
  output_img_filename  = F(('preprocessors',
31
    '0019_3_1_120509-160517_img_lee_huang.mat'))
André Anjos's avatar
André Anjos committed
32
  output_fvr_filename  = F(('preprocessors',
33 34 35 36
    '0019_3_1_120509-160517_fvr_lee_huang.mat'))

  img = bob.io.base.load(input_filename)

André Anjos's avatar
André Anjos committed
37
  from bob.bio.vein.preprocessors.FingerCrop import FingerCrop
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
  FC = FingerCrop(4, 40, False, False)
  #FC = FingerCrop(4, 40, False, 5, 0.2, False)

  output_img, finger_mask_norm, finger_mask2, spoofingValue = FC(img)

  # Load Matlab reference
  output_img_ref = bob.io.base.load(output_img_filename)
  output_fvr_ref = bob.io.base.load(output_fvr_filename)

  # Compare output of python's implementation to matlab reference
  # (loose comparison!)
  assert numpy.mean(numpy.abs(output_img - output_img_ref)) < 1e2


def test_miuramax():

  #Maximum Curvature method against Matlab reference

André Anjos's avatar
André Anjos committed
56 57 58
  input_img_filename  = F(('extractors', 'miuramax_input_img.mat'))
  input_fvr_filename  = F(('extractors', 'miuramax_input_fvr.mat'))
  output_filename     = F(('extractors', 'miuramax_output.mat'))
59 60 61 62 63 64

  # Load inputs
  input_img = bob.io.base.load(input_img_filename)
  input_fvr = bob.io.base.load(input_fvr_filename)

  # Apply Python implementation
André Anjos's avatar
André Anjos committed
65
  from bob.bio.vein.extractors.MaximumCurvature import MaximumCurvature
66 67 68 69 70 71 72 73 74
  MC = MaximumCurvature(5, False)
  output_img = MC((input_img, input_fvr))

  # Load Matlab reference
  output_img_ref = bob.io.base.load(output_filename)

  # Compare output of python's implementation to matlab reference
  # (loose comparison!)
  assert numpy.mean(numpy.abs(output_img - output_img_ref)) < 8e-3
Pedro TOME's avatar
Pedro TOME committed
75 76


77 78 79 80
def test_miurarlt():

  #Repeated Line Tracking method against Matlab reference

André Anjos's avatar
André Anjos committed
81 82 83
  input_img_filename  = F(('extractors', 'miurarlt_input_img.mat'))
  input_fvr_filename  = F(('extractors', 'miurarlt_input_fvr.mat'))
  output_filename     = F(('extractors', 'miurarlt_output.mat'))
84 85 86 87 88 89

  # Load inputs
  input_img = bob.io.base.load(input_img_filename)
  input_fvr = bob.io.base.load(input_fvr_filename)

  # Apply Python implementation
André Anjos's avatar
André Anjos committed
90
  from bob.bio.vein.extractors.RepeatedLineTracking import RepeatedLineTracking
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
  RLT = RepeatedLineTracking(3000, 1, 21, False)
  output_img = RLT((input_img, input_fvr))

  # Load Matlab reference
  output_img_ref = bob.io.base.load(output_filename)

  # Compare output of python's implementation to matlab reference
  # (loose comparison!)
  assert numpy.mean(numpy.abs(output_img - output_img_ref)) < 0.5


def test_huangwl():

  #Wide Line Detector method against Matlab reference

André Anjos's avatar
André Anjos committed
106 107 108
  input_img_filename  = F(('extractors', 'huangwl_input_img.mat'))
  input_fvr_filename  = F(('extractors', 'huangwl_input_fvr.mat'))
  output_filename     = F(('extractors', 'huangwl_output.mat'))
109 110 111 112 113 114

  # Load inputs
  input_img = bob.io.base.load(input_img_filename)
  input_fvr = bob.io.base.load(input_fvr_filename)

  # Apply Python implementation
André Anjos's avatar
André Anjos committed
115
  from bob.bio.vein.extractors.WideLineDetector import WideLineDetector
116 117 118 119 120 121 122 123 124 125 126 127
  WL = WideLineDetector(5, 1, 41, False)
  output_img = WL((input_img, input_fvr))

  # Load Matlab reference
  output_img_ref = bob.io.base.load(output_filename)

  # Compare output of python's implementation to matlab reference
  assert numpy.allclose(output_img, output_img_ref)


def test_miura_match():

André Anjos's avatar
André Anjos committed
128 129 130 131 132
  #Match Ratio method against Matlab reference

  template_filename = F(('algorithms', '0001_2_1_120509-135338.mat'))
  probe_gen_filename = F(('algorithms', '0001_2_2_120509-135558.mat'))
  probe_imp_filename = F(('algorithms', '0003_2_1_120509-141255.mat'))
133 134 135 136 137

  template_vein = bob.io.base.load(template_filename)
  probe_gen_vein = bob.io.base.load(probe_gen_filename)
  probe_imp_vein = bob.io.base.load(probe_imp_filename)

André Anjos's avatar
André Anjos committed
138
  from bob.bio.vein.algorithms.MiuraMatch import MiuraMatch
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
  MM = MiuraMatch(ch=18, cw=28)
  score_gen = MM.score(template_vein, probe_gen_vein)

  assert numpy.isclose(score_gen, 0.382689335394127)

  score_imp = MM.score(template_vein, probe_imp_vein)
  assert numpy.isclose(score_imp, 0.172906739278421)

  if False: #testing gpu enabled calculations
    MM = MiuraMatch(ch=18, cw=28, gpu=True)
    score_gen = MM.score(template_vein, probe_gen_vein)
    assert numpy.isclose(score_gen, 0.382689335394127)

    score_imp = MM.score(template_vein, probe_imp_vein)
    assert numpy.isclose(score_imp, 0.172906739278421)