|
import configparser |
|
import ast |
|
import pandas as pd |
|
import numpy as np |
|
import plotly.graph_objects as go |
|
from ..datasets.dataset import datasetFactory |
|
from ..methods.base import methodFactory |
|
from ..signals.video import Video |
|
from ..utils.errors import getErrors, printErrors, displayErrors |
|
|
|
class TestSuite(): |
|
""" Test suite for a given video dataset and multiple VHR methods""" |
|
|
|
def __init__(self, configFilename='default'): |
|
if configFilename == 'default': |
|
configFilename = '../pyVHR/analysis/default_test.cfg' |
|
self.parse_cfg(configFilename) |
|
|
|
def start(self, saveResults=True, outFilename=None, verb=0): |
|
""" Runs the tests as specified in the loaded config file. |
|
|
|
verbose degree: |
|
0 - not verbose |
|
1 - show the main steps |
|
2 - display graphic |
|
3 - display spectra |
|
4 - display errors |
|
(use also combinations, e.g. verb=21, verb=321) |
|
""" |
|
|
|
|
|
if '1' in str(verb): |
|
self.__verbose('a') |
|
|
|
|
|
|
|
dataset = datasetFactory(self.videodict['dataset'], videodataDIR=self.videodict['videodataDIR'], BVPdataDIR=self.videodict['BVPdataDIR']) |
|
|
|
|
|
res = TestResult() |
|
|
|
|
|
for m in self.methods: |
|
|
|
|
|
if self.videoIdx == 'all': |
|
self.videoIdx = np.arange(0,dataset.numVideos) |
|
for v in self.videoIdx: |
|
|
|
|
|
if '1' in str(verb): |
|
print("\n**** Using Method: %s on videoID: %d" % (m,v)) |
|
|
|
|
|
res.newDataSerie() |
|
res.addData('method', m) |
|
res.addData('dataset', dataset.name) |
|
res.addData('videoIdx', v) |
|
|
|
|
|
videoFilename = dataset.getVideoFilename(v) |
|
video = Video(videoFilename, verb) |
|
video.getCroppedFaces(detector=self.videodict['detector'], |
|
extractor=self.videodict['extractor']) |
|
etime = float(self.videodict['endTime']) |
|
if etime < 0: |
|
self.videodict['endTime'] = str(video.duration-etime) |
|
|
|
res.addData('videoFilename', videoFilename) |
|
|
|
|
|
fname = dataset.getSigFilename(v) |
|
sigGT = dataset.readSigfile(fname) |
|
winSizeGT = int(self.methodsdict[m]['winSizeGT']) |
|
bpmGT, timesGT = sigGT.getBPM(winSizeGT) |
|
|
|
res.addData('sigFilename', fname) |
|
res.addData('bpmGT', sigGT.bpm) |
|
res.addData('timeGT', sigGT.times) |
|
|
|
|
|
|
|
self.methodsdict[m]['video'] = video |
|
self.methodsdict[m]['verb'] = verb |
|
|
|
self.__merge(self.methodsdict[m], self.videodict) |
|
method = methodFactory(m, **self.methodsdict[m]) |
|
bpmES, timesES = method.runOffline(**self.methodsdict[m]) |
|
|
|
res.addData('bpmES', bpmES) |
|
res.addData('timeES', timesES) |
|
|
|
|
|
RMSE, MAE, MAX, PCC = getErrors(bpmES, bpmGT, timesES, timesGT) |
|
|
|
res.addData('RMSE', RMSE) |
|
res.addData('MAE', MAE) |
|
res.addData('PCC', PCC) |
|
res.addData('MAX', MAX) |
|
res.addDataSerie() |
|
|
|
if '1' in str(verb): |
|
printErrors(RMSE, MAE, MAX, PCC) |
|
if '4' in str(verb): |
|
displayErrors(bpmES, bpmGT, timesES, timesGT) |
|
|
|
|
|
if saveResults: |
|
res.saveResults() |
|
|
|
return res |
|
|
|
|
|
def parse_cfg(self, configFilename): |
|
""" parses the given config file for experiments. """ |
|
|
|
self.parser = configparser.ConfigParser(inline_comment_prefixes=('#', ';')) |
|
self.parser.optionxform = str |
|
if not self.parser.read(configFilename): |
|
raise FileNotFoundError(configFilename) |
|
|
|
|
|
assert not self.parser.has_section('DEFAULT'),"ERROR... DEFAULT section is mandatory!" |
|
|
|
|
|
self.defaultdict = dict(self.parser['DEFAULT'].items()) |
|
|
|
|
|
self.videodict = dict(self.parser['VIDEO'].items()) |
|
|
|
|
|
if self.videodict['videoIdx'] == 'all': |
|
self.videoIdx = 'all' |
|
else: |
|
svid = ast.literal_eval(self.videodict['videoIdx']) |
|
self.videoIdx = [int(v) for v in svid] |
|
|
|
|
|
self.methodsdict = {} |
|
self.methods = ast.literal_eval(self.defaultdict['methods']) |
|
for x in self.methods: |
|
self.methodsdict[x] = dict(self.parser[x].items()) |
|
|
|
def __merge(self, dict1, dict2): |
|
for key in dict2: |
|
if key not in dict1: |
|
dict1[key]= dict2[key] |
|
|
|
def __verbose(self, verb): |
|
if verb == 'a': |
|
print("** Run the test with the following config:") |
|
print(" dataset: " + self.videodict['dataset'].upper()) |
|
print(" methods: " + str(self.methods)) |
|
|
|
|
|
class TestResult(): |
|
""" Manage the results of a test for a given video dataset and multiple VHR methods""" |
|
|
|
def __init__(self, filename=None): |
|
|
|
if filename == None: |
|
self.dataFrame = pd.DataFrame() |
|
else: |
|
self.dataFrame = pd.read_hdf(filename) |
|
self.dict = None |
|
|
|
def addDataSerie(self): |
|
|
|
if self.dict != None: |
|
self.dataFrame = self.dataFrame.append(self.dict, ignore_index=True) |
|
|
|
def newDataSerie(self): |
|
|
|
D = {} |
|
D['method'] = '' |
|
D['dataset'] = '' |
|
D['videoIdx'] = '' |
|
D['sigFilename'] = '' |
|
D['videoFilename'] = '' |
|
D['EVM'] = False |
|
D['mask'] = '' |
|
D['RMSE'] = '' |
|
D['MAE'] = '' |
|
D['PCC'] = '' |
|
D['MAX'] = '' |
|
D['telapse'] = '' |
|
D['bpmGT'] = '' |
|
D['bpmES'] = '' |
|
D['timeGT'] = '' |
|
D['timeES'] = '' |
|
self.dict = D |
|
|
|
def addData(self, key, value): |
|
self.dict[key] = value |
|
|
|
def saveResults(self, outFilename=None): |
|
if outFilename == None: |
|
outFilename = "testResults.h5" |
|
else: |
|
self.outFilename = outFilename |
|
|
|
|
|
self.dataFrame.to_hdf(outFilename, key='df', mode='w') |
|
|