|
import numpy as np |
|
from .utils.SkinColorFilter import SkinColorFilter |
|
from .base import VHRMethod |
|
|
|
class SSR(VHRMethod): |
|
methodName = 'SSR' |
|
|
|
def __init__(self, **kwargs): |
|
super(SSR, self).__init__(**kwargs) |
|
|
|
def apply(self, X): |
|
|
|
K = len(self.video.faceSignal) |
|
l = self.video.frameRate |
|
|
|
P = np.zeros(K) |
|
|
|
L = np.zeros((3, K), dtype='float64') |
|
U = np.zeros((3, 3, K), dtype='float64') |
|
|
|
for k in range(K): |
|
n_roi = len(self.video.faceSignal[k]) |
|
VV = [] |
|
|
|
for r in range(n_roi): |
|
V = self.video.faceSignal[k][r].astype(np.float64) |
|
idx = V!=0 |
|
idx2 = np.logical_and(np.logical_and(idx[:,:,0], idx[:,:,1]), idx[:,:,2]) |
|
V_skin_only = V[idx2] |
|
VV.append(V_skin_only) |
|
|
|
VV = np.vstack(VV) |
|
|
|
C = self.__build_correlation_matrix(VV) |
|
|
|
|
|
L[:,k], U[:,:,k] = self.__eigs(C) |
|
|
|
|
|
if k >= l: |
|
tau = k - l |
|
p = self.__build_p(tau, k, l, U, L) |
|
P[tau:k] += p |
|
|
|
if np.isnan(np.sum(P)): |
|
print('NAN') |
|
print(self.video.faceSignal[k]) |
|
|
|
bvp = P |
|
|
|
return bvp |
|
|
|
def __build_p(self, τ, k, l, U, Λ): |
|
""" |
|
builds P |
|
Parameters |
|
---------- |
|
k: int |
|
The frame index |
|
l: int |
|
The temporal stride to use |
|
U: numpy.ndarray |
|
The eigenvectors of the c matrix (for all frames up to counter). |
|
Λ: numpy.ndarray |
|
The eigenvalues of the c matrix (for all frames up to counter). |
|
Returns |
|
------- |
|
p: numpy.ndarray |
|
The p signal to add to the pulse. |
|
""" |
|
|
|
SR = np.zeros((3, l), 'float64') |
|
z = 0 |
|
|
|
for t in range(τ, k, 1): |
|
a = Λ[0, t] |
|
b = Λ[1, τ] |
|
c = Λ[2, τ] |
|
d = U[:, 0, t].T |
|
e = U[:, 1, τ] |
|
f = U[:, 2, τ] |
|
g = U[:, 1, τ].T |
|
h = U[:, 2, τ].T |
|
x1 = a / b |
|
x2 = a / c |
|
x3 = np.outer(e, g) |
|
x4 = np.dot(d, x3) |
|
x5 = np.outer(f, h) |
|
x6 = np.dot(d, x5) |
|
x7 = np.sqrt(x1) |
|
x8 = np.sqrt(x2) |
|
x9 = x7 * x4 |
|
x10 = x8 * x6 |
|
x11 = x9 + x10 |
|
SR[:, z] = x11 |
|
z += 1 |
|
|
|
|
|
s0 = SR[0, :] |
|
s1 = SR[1, :] |
|
p = s0 - ((np.std(s0) / np.std(s1)) * s1) |
|
p = p - np.mean(p) |
|
return p |
|
|
|
def __get_skin_pixels(self, skin_filter, face, do_skininit): |
|
""" |
|
get eigenvalues and eigenvectors, sort them. |
|
Parameters |
|
---------- |
|
C: numpy.ndarray |
|
The RGB values of skin-colored pixels. |
|
Returns |
|
------- |
|
Λ: numpy.ndarray |
|
The eigenvalues of the correlation matrix |
|
U: numpy.ndarray |
|
The (sorted) eigenvectors of the correlation matrix |
|
""" |
|
ROI = face |
|
|
|
if do_skininit: |
|
skin_filter.estimate_gaussian_parameters(ROI) |
|
|
|
skin_mask = skin_filter.get_skin_mask(ROI) |
|
|
|
V = ROI[skin_mask] |
|
V = V.astype('float64') / 255.0 |
|
|
|
return V |
|
|
|
|
|
def __build_correlation_matrix(self, V): |
|
|
|
|
|
V_T = V.T |
|
N = V.shape[0] |
|
|
|
C = np.dot(V_T, V) |
|
C = C / N |
|
|
|
return C |
|
|
|
def __eigs(self, C): |
|
""" |
|
get eigenvalues and eigenvectors, sort them. |
|
Parameters |
|
---------- |
|
C: numpy.ndarray |
|
The RGB values of skin-colored pixels. |
|
Returns |
|
------- |
|
Λ: numpy.ndarray |
|
The eigenvalues of the correlation matrix |
|
U: numpy.ndarray |
|
The (sorted) eigenvectors of the correlation matrix |
|
""" |
|
|
|
L, U = np.linalg.eig(C) |
|
idx = L.argsort() |
|
idx = idx[::-1] |
|
L_ = L[idx] |
|
U_ = U[:, idx] |
|
|
|
return L_, U_ |
|
|