test.py 4.26 KB
Newer Older
Sushil BHATTACHARJEE's avatar
Sushil BHATTACHARJEE committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#!/usr/bin/env python
# encoding: utf-8
# sushil bhattacharjee <sushil.bhattacharjee@idiap.ch>
# Fri. 10 March 2017

'''Unit-tests for bob.ip.qualitymeasure'''

import os
import numpy as np
import nose.tools
import pkg_resources

import bob.io.base
import bob.io.base.test_utils
import bob.io.video
import bob.ip.color

18 19
from . import galbally_iqm_features as iqm
from . import msu_iqa_features as iqa
Sushil BHATTACHARJEE's avatar
Sushil BHATTACHARJEE committed
20

21
from bob.io import image
22
from ._library import remove_highlights
23

Sushil BHATTACHARJEE's avatar
Sushil BHATTACHARJEE committed
24 25 26 27

REF_VIDEO_FILE = 'real_client001_android_SD_scene01.mp4'
REF_FEATURE_FILE = 'real_client001_android_SD_scene01_ref_feats.h5'

Amir Mohammadi's avatar
Amir Mohammadi committed
28 29 30 31 32

def F(n):
  return pkg_resources.resource_filename(
      __name__, os.path.join('data', n))

Sushil BHATTACHARJEE's avatar
Sushil BHATTACHARJEE committed
33 34

input_video_file = F(REF_VIDEO_FILE)
Amir Mohammadi's avatar
Amir Mohammadi committed
35 36
assert os.path.isfile(
    input_video_file), "File: not found: %s" % input_video_file
Sushil BHATTACHARJEE's avatar
Sushil BHATTACHARJEE committed
37 38
inputVideo = bob.io.video.reader(input_video_file)
video_data = inputVideo.load()
Amir Mohammadi's avatar
Amir Mohammadi committed
39 40
numframes = 3

Sushil BHATTACHARJEE's avatar
Sushil BHATTACHARJEE committed
41 42

def load_reference_features():
Amir Mohammadi's avatar
Amir Mohammadi committed
43 44 45 46 47 48 49 50 51 52 53 54
  ref_feat_file = F(REF_FEATURE_FILE)
  assert os.path.isfile(ref_feat_file), "File: not found: %s" % ref_feat_file
  rf = bob.io.base.HDF5File(ref_feat_file)
  assert rf.has_key('/bobiqm'), "Key: /bobiqm not found in file %s" % ref_feat_file
  assert rf.has_key('/msuiqa'), "Key: /msuiqa not found in file %s" % ref_feat_file
  galbally_ref_features = rf.read('/bobiqm')
  msu_ref_features = rf.read('/msuiqa')
  del rf
  return (galbally_ref_features, msu_ref_features)


# load reference-features into global vars.
Sushil BHATTACHARJEE's avatar
Sushil BHATTACHARJEE committed
55 56 57 58
galbally_ref_features, msu_ref_features = load_reference_features()


def test_galbally_feat_extr():
Amir Mohammadi's avatar
Amir Mohammadi committed
59 60 61 62 63
  # change this, if you add more features to galbally_iqm_features module.
  iqm_len = 18
  # feature-array to hold features for several frames
  bobfset = np.zeros([numframes, iqm_len])
  f = 0
Sushil BHATTACHARJEE's avatar
Sushil BHATTACHARJEE committed
64

Amir Mohammadi's avatar
Amir Mohammadi committed
65
  # process first frame separately, to get the no. of iqm features
Sushil BHATTACHARJEE's avatar
Sushil BHATTACHARJEE committed
66 67 68 69
  rgbFrame = video_data[f]
  iqmSet = iqm.compute_quality_features(rgbFrame)
  numIQM = len(iqmSet)

Amir Mohammadi's avatar
Amir Mohammadi committed
70 71 72 73
  # test: check that numIQM is the same as expected iqm_len (18)
  nose.tools.eq_(numIQM, iqm_len)

  # store features for first frame in feature-array
Sushil BHATTACHARJEE's avatar
Sushil BHATTACHARJEE committed
74 75
  bobfset[f] = iqmSet

Amir Mohammadi's avatar
Amir Mohammadi committed
76 77
  # now store iqm features for remaining test-frames of input video.
  for f in range(1, numframes):
Sushil BHATTACHARJEE's avatar
Sushil BHATTACHARJEE committed
78 79 80
    rgbFrame = video_data[f]
    bobfset[f] = iqm.compute_quality_features(rgbFrame)

Amir Mohammadi's avatar
Amir Mohammadi committed
81 82 83
  # test: compare feature-values in bobfset[] with those loaded from hdf5 file
  nose.tools.assert_true((bobfset == galbally_ref_features).all())
  # np.allclose(A,B)
Sushil BHATTACHARJEE's avatar
Sushil BHATTACHARJEE committed
84 85 86


def test_msu_feat_extr():
Amir Mohammadi's avatar
Amir Mohammadi committed
87 88 89 90 91
  # change this, if you change the no. of features in msu_iqa_features module.
  iqa_len = 121
  # feature-array to hold features for several frames
  msufset = np.zeros([numframes, iqa_len])
  f = 0
Sushil BHATTACHARJEE's avatar
Sushil BHATTACHARJEE committed
92

Amir Mohammadi's avatar
Amir Mohammadi committed
93
  # process first frame separately, to get the no. of iqa features
Sushil BHATTACHARJEE's avatar
Sushil BHATTACHARJEE committed
94 95 96 97
  rgbFrame = video_data[f]
  iqaSet = iqa.compute_msu_iqa_features(rgbFrame)
  numIQA = len(iqaSet)

Amir Mohammadi's avatar
Amir Mohammadi committed
98 99 100 101
  # test: check that numIQA matches the expected iqa_len(121)
  nose.tools.eq_(numIQA, iqa_len)

  # store features for first frame in feature-array
Sushil BHATTACHARJEE's avatar
Sushil BHATTACHARJEE committed
102 103
  msufset[f] = iqaSet

Amir Mohammadi's avatar
Amir Mohammadi committed
104 105
  # now store iqm features for remaining test-frames of input video.
  for f in range(1, numframes):
Sushil BHATTACHARJEE's avatar
Sushil BHATTACHARJEE committed
106
    rgbFrame = video_data[f]
Amir Mohammadi's avatar
Amir Mohammadi committed
107
    msuQFeats = iqa.compute_msu_iqa_features(rgbFrame)
Sushil BHATTACHARJEE's avatar
Sushil BHATTACHARJEE committed
108 109
    msufset[f] = msuQFeats

Amir Mohammadi's avatar
Amir Mohammadi committed
110 111
  # test: compare feature-values in bobfset[] with those loaded from hdf5 file
  nose.tools.assert_true((msufset == msu_ref_features).all())
112 113 114 115 116 117 118 119 120 121 122

# test if the original version of the specular highlights algorithm
# (remove_highlights_orig.cpp::remove_highlights_orig) performs exactly
# like the original code from which it was mostly copied.
# (this test can probabalty be dropped.)
def test_remove_highlights_orig_integrity():
    # open pictures
    img1 = image.load(F('old_man.ppm'))
    img2 = image.load(F('toys.ppm'))

    # compute
123 124
    sfi1, diff1, residue1 = remove_highlights(img1.astype(np.float32), 0.5)
    sfi2, diff2, residue2 = remove_highlights(img2.astype(np.float32), 0.5)
125 126 127 128 129 130 131 132 133 134 135 136 137 138

    #speckleImg[np.where(np.isinf(speckleImg))] = 0
    #speckleImg[np.where(np.isnan(speckleImg))] = 0

    diff1_u8 = diff1.astype('uint8')
    diff2_u8 = diff2.astype('uint8')

    # reference files
    ref1 = image.load(F('old_man_diffuse.ppm'))
    ref2 = image.load(F('toys_diffuse.ppm'))

    # test: compare results
    nose.tools.assert_true((diff1_u8 == ref1).all())
    nose.tools.assert_true((diff2_u8 == ref2).all())