File size: 12,925 Bytes
39b4c8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import numpy as np
import ast
import plotly.graph_objects as go
from scipy.signal import medfilt, detrend
from abc import ABCMeta, abstractmethod
from importlib import import_module
from ..signals.bvp import BVPsignal
from ..utils import filters, printutils
from ..utils import detrending

def methodFactory(methodName, *args, **kwargs):
    try:
        moduleName = methodName.lower()
        className = methodName.upper()
        methodModule = import_module('.methods.' + moduleName, package='pyVHR')
        classOBJ = getattr(methodModule, className)
        obj = classOBJ(**kwargs)

    except (AttributeError, ModuleNotFoundError):
        raise ImportError('{} is not part of pyVHR method collection!'.format(methodName))

    return obj
    
class VHRMethod(metaclass=ABCMeta):
    """
    Manage VHR approaches (parent class for new approach)
    """
    
    def __init__(self, **kwargs):
        self.video = kwargs['video']
        self.verb = kwargs['verb']
        
    @abstractmethod
    def apply(self, X):
        pass

    def runOffline(self, **kwargs):
        
        # -- parse params
        startTime, endTime, winSize, timeStep, zeroMeanSTDnorm, BPfilter, minHz, maxHz, detrFilter, \
        detrMethod, detrLambda = self.__readparams(**kwargs)
        
        fs = self.video.frameRate
        
        # -- check times
        if endTime > self.video.duration:
            endTime = self.video.duration
        assert startTime <= endTime, "Time interval error!"
        assert timeStep > 0, "Time step must be positive!"
        assert winSize < (endTime-startTime),"Winsize too big!"

        # -- verbose prints
        if '1' in str(self.verb):
            self.__verbose(startTime, endTime, winSize)
        
        if self.video.doEVM is True:
            self.video.applyEVM()
        else:
            self.video.processedFaces = self.video.faces

        timeSteps = np.arange(startTime,endTime,timeStep)
        T = startTime    # times where bpm are estimated
        RADIUS = winSize/2

        bpmES = []     # bmp estimtes
        timesES = []   # times of bmp estimtes

        # -- loop on video signal chunks
        startFrame = int(T*self.video.frameRate)
        count = 0
        while T <= endTime:
            endFrame = np.min([self.video.numFrames, int((T+RADIUS)*self.video.frameRate)])

            # -- extract ROIs on the frame range
            self.frameSubset = np.arange(startFrame, endFrame)

            self.ROImask = kwargs['ROImask']

            # -- type of signal extractor
            if self.ROImask == 'rect':
                rects = ast.literal_eval(kwargs['rectCoords'])
                self.rectCoords = []
                for x in rects:
                    rect = []
                    for y in x:
                        rect.append(int(y))
                    self.rectCoords.append(rect)
                self.video.setMask(self.ROImask, rectCoords=self.rectCoords)
            elif self.ROImask == 'skin_adapt':
                self.video.setMask(self.ROImask, skinThresh_adapt=float(kwargs['skinAdapt']))
            elif self.ROImask == 'skin_fix':
                threshs = ast.literal_eval(kwargs['skinFix'])
                self.threshSkinFix = [int(x) for x in threshs]
                self.video.setMask(self.ROImask, skinThresh_fix=self.threshSkinFix)
            else:
                raise ValueError(self.ROImask + " : Unimplemented Signal Extractor!")
                
            self.video.extractSignal(self.frameSubset, count)

            # -- RGB computation  
            RGBsig = self.video.getMeanRGB()
            
            # -- print RGB raw data
            if '2' in str(self.verb):
                printutils.multiplot(y=RGBsig, name=['ch B', 'ch R','ch G'], title='RGB raw data')
                        
            # -- RGBsig preprocessing
            if zeroMeanSTDnorm:
                RGBsig = filters.zeroMeanSTDnorm(RGBsig)      
            if detrFilter:
                if detrMethod == 'tarvainen':
                    #TODO controllare il detrending di tarvainen
                    RGBsig[0] = detrending.detrend(RGBsig[0], detrLambda)
                    RGBsig[1] = detrending.detrend(RGBsig[1], detrLambda)
                    RGBsig[2] = detrending.detrend(RGBsig[2], detrLambda)
                else:
                    RGBsig = detrend(RGBsig)
            if BPfilter:
                RGBsig = filters.BPfilter(RGBsig, minHz, maxHz, fs)
            
            # -- print postproce
            if '2' in str(self.verb):
                printutils.multiplot(y=RGBsig, name=['ch B', 'ch R','ch G'], title='RGB postprocessing')
            
            # -- apply the selected method to estimate BVP
            rPPG = self.apply(RGBsig)
                        
            # BVP postprocessing 
            startTime = np.max([0, T-winSize/self.video.frameRate])
            bvpChunk = BVPsignal(rPPG, self.video.frameRate, startTime, minHz, maxHz, self.verb)
            
            # -- post processing: filtering 
            
            # TODO: valutare se mantenere!!
            #bvpChunk.data = filters.BPfilter(bvpChunk.data, bvpChunk.minHz, bvpChunk.maxHz, bvpChunk.fs)

            if '2' in str(self.verb):
                bvpChunk.plot(title='BVP estimate by ' + self.methodName)
          
            # -- estimate BPM by PSD
            bvpChunk.PSD2BPM(chooseBest=True)

            # -- save the estimate
            bpmES.append(bvpChunk.bpm)
            timesES.append(T)

            # -- define the frame range for each time step            
            T += timeStep
            startFrame = np.max([0, int((T-RADIUS)*self.video.frameRate)])

            count += 1

        # set final values
        self.bpm = np.array(bpmES).T
        
        # TODO controllare se mettere o no il filtro seguente
        #self.bpm = self.bpm_time_filter(self.bpm, 3)
        self.times = np.array(timesES)
        
        return self.bpm, self.times

    @staticmethod    
    def makeMethodObject(video, methodName='ICA'):
        if methodName == 'CHROM':
            m = methods.CHROM(video)
        elif methodName == 'LGI':
            m = methods.LGI(video)
        elif methodName == 'SSR':
            m = methods.SSR(video)
        elif methodName == 'PBV':
            m = methods.PBV(video)
        elif methodName == 'POS':
            m = methods.POS(video)
        elif methodName == 'Green':
            m = methods.Green(video)
        elif methodName == 'PCA':
            m = methods.PCA(video)
        elif methodName == 'ICA':
            m = methods.ICA(video)
        else:
            raise ValueError("Unknown method!")
        return m

    def __readparams(self, **kwargs):
        
        # get params from kwargs or set default
        if 'startTime' in kwargs:
            startTime = float(kwargs['startTime'])
        else:
            startTime = 0
        if 'endTime' in kwargs:
            if kwargs['endTime']=='INF':
                endTime = np.Inf
            else:
                endTime = float(kwargs['endTime'])
        else:
            endTime=np.Inf
        if 'winSize' in kwargs:
            winSize = int(kwargs['winSize'])
        else:
            winSize = 5
        if 'timeStep' in kwargs:
            timeStep = float(kwargs['timeStep'])
        else:
            timeStep = 1
        if 'zeroMeanSTDnorm' in kwargs:
            zeroMeanSTDnorm = int(kwargs['zeroMeanSTDnorm'])
        else:
            zeroMeanSTDnorm = 0
        if 'BPfilter' in kwargs:
            BPfilter = int(kwargs['BPfilter'])
        else:
            BPfilter = 1
        if 'minHz' in kwargs:
            minHz = float(kwargs['minHz'])
        else:
            minHz = .75
        if 'maxHz' in kwargs:
            maxHz = float(kwargs['maxHz'])
        else:
            maxHz = 4.
        if 'detrending' in kwargs:
            detrending = int(kwargs['detrending'])
        else:
            detrending = 0
        if detrending:
            if 'detrLambda' in kwargs:
                detrLambda = kwargs['detrLambda']
            else:
                detrLambda = 10
        else:
            detrLambda = 10
        if 'detrMethod' in kwargs:
            detrMethod = kwargs['detrMethod']
        else:
            detrMethod = 'tarvainen'
            
        return startTime, endTime, winSize, timeStep, zeroMeanSTDnorm, BPfilter, minHz, maxHz,\
                detrending, detrMethod, detrLambda
    
    def RMSEerror(self, bvpGT):
        """ RMSE: """

        diff = self.__diff(bvpGT)
        n,m = diff.shape  # n = num channels, m = bpm length
        df = np.zeros(n)
        for j in range(m):
            for c in range(n):
                df[c] += np.power(diff[c,j],2)

        # -- final RMSE
        RMSE = np.sqrt(df/m)
        return RMSE

    def MAEerror(self, bvpGT):
        """ MAE: """

        diff = self.__diff(bvpGT)
        n,m = diff.shape  # n = num channels, m = bpm length
        df = np.sum(np.abs(diff),axis=1)

        # -- final MAE
        MAE = df/m
        return MAE

    def MAXError(self, bvpGT):
        """ MAE: """

        diff = self.__diff(bvpGT)
        n,m = diff.shape  # n = num channels, m = bpm length
        df = np.max(np.abs(diff),axis=1)

        # -- final MAE
        MAX = df
        return MAX

    def PearsonCorr(self, bvpGT):
        from scipy import stats

        diff = self.__diff(bvpGT)
        bpmES = self.bpm
        n,m = diff.shape  # n = num channels, m = bpm length
        CC = np.zeros(n)
        for c in range(n):
            # -- corr
            r,p = stats.pearsonr(diff[c,:]+bpmES[c,:],bpmES[c,:])
            CC[c] = r
        return CC

    def printErrors(self, bvpGT):
        RMSE = self.RMSEerror(bvpGT)
        MAE = self.MAEerror(bvpGT)
        CC = self.PearsonCorr(bvpGT)
        print('\nErrors:')
        print('        RMSE: ' + str(RMSE))
        print('        MAE : ' + str(MAE))
        print('        CC  : ' + str(CC))

    def displayError(self, bvpGT):
        bpmGT = bvpGT.bpm
        timesGT = bvpGT.times
        bpmES = self.bpm
        timesES = self.times
        diff = self.__diff(bvpGT)
        n,m = diff.shape  # n = num channels, m = bpm length
        df = np.abs(diff)
        dfMean = np.around(np.mean(df,axis=1),1)

        # -- plot errors
        fig = go.Figure()
        name = 'Ch 1 (µ = ' + str(dfMean[0])+ ' )'
        fig.add_trace(go.Scatter(x=timesES, y=df[0,:], name=name, mode='lines+markers'))
        if n > 1:
            name = 'Ch 2 (µ = ' + str(dfMean[1])+ ' )'
            fig.add_trace(go.Scatter(x=timesES, y=df[1,:], name=name, mode='lines+markers'))
            name = 'Ch 3 (µ = ' + str(dfMean[2])+ ' )'
            fig.add_trace(go.Scatter(x=timesES, y=df[2,:], name=name, mode='lines+markers'))
        fig.update_layout(xaxis_title='Times (sec)', yaxis_title='MAE', showlegend=True)
        fig.show()
        
        # -- plot bpm Gt and ES 
        fig = go.Figure()
        GTmean = np.around(np.mean(bpmGT),1)
        name = 'GT (µ = ' + str(GTmean)+ ' )'
        fig.add_trace(go.Scatter(x=timesGT, y=bpmGT, name=name, mode='lines+markers'))
        ESmean = np.around(np.mean(bpmES[0,:]),1)
        name = 'ES1 (µ = ' + str(ESmean)+ ' )'
        fig.add_trace(go.Scatter(x=timesES, y=bpmES[0,:], name=name, mode='lines+markers'))
        if n > 1:
            ESmean = np.around(np.mean(bpmES[1,:]),1)
            name = 'ES2 (µ = ' + str(ESmean)+ ' )'
            fig.add_trace(go.Scatter(x=timesES, y=bpmES[1,:], name=name, mode='lines+markers'))
            ESmean = np.around(np.mean(bpmES[2,:]),1)
            name = 'E3 (µ = ' + str(ESmean)+ ' )'
            fig.add_trace(go.Scatter(x=timesES, y=bpmES[2,:], name=name, mode='lines+markers'))
        
        
        
        fig.update_layout(xaxis_title='Times (sec)', yaxis_title='BPM', showlegend=True)
        fig.show()



    def __diff(self, bvpGT):
        bpmGT = bvpGT.bpm
        timesGT = bvpGT.times
        bpmES = self.bpm
        timesES = self.times
        n,m = bpmES.shape  # n = num channels, m = bpm length

        diff = np.zeros((n,m))
        for j in range(m):
            t = timesES[j]
            i = np.argmin(np.abs(t-timesGT))
            for c in range(n):
                diff[c,j] = bpmGT[i]-bpmES[c,j]
        return diff

    def bpm_time_filter(self, bpm, w_len):

        n_sig = bpm.shape[0]
        filtered_bpm = []
    
        for s in range(n_sig):
            x = bpm[s,:]
            x = medfilt(x, w_len)
            filtered_bpm.append(x)

        filtered_bpm = np.vstack(filtered_bpm)

        return filtered_bpm
    
        
    def __verbose(self, startTime, endTime, winSize):
        print("\n    * %s params: start time = %.1f, end time = %.1f, winsize = %.1f (sec)" 
              %(self.methodName, startTime, endTime, winSize))