-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpos_test.py
More file actions
198 lines (146 loc) · 5.25 KB
/
Copy pathpos_test.py
File metadata and controls
198 lines (146 loc) · 5.25 KB
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
import matplotlib
#matplotlib.use("TkAgg")
# matplotlib.use("MacOSX")
from matplotlib import pyplot as plt
import os
import sys
sys.path.insert(0, './unused/SkinDetector')
import pkg_resources
import numpy as np
import cv2
import dlib
from imutils.video import VideoStream
from imutils import face_utils
import imutils
import argparse
import unused.SkinDetector.skin_detector as skin_detector
video_file_path = './output.avi'
left_increase_ratio = 0.05
top_increase_ratio = 0.25
camera = cv2.VideoCapture(video_file_path)
start_idx = 0
end_idx = 200
framerate = 30
if end_idx > 0:
nb_frames = end_idx - start_idx
frame_counter = 0
i = start_idx
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('./unused/SkinDetector/shape_predictor_68_face_landmarks.dat')
while (i >= start_idx and i < end_idx):
(grabbed, frame) = camera.read()
#image vertical flip using cv2
new_frame = cv2.flip(frame, 1)
frame[:,0:256, :] = new_frame[:, 0:256, :]
if not grabbed:
continue
# print('Processing frame: {}'.format(i))
h, w, c = frame.shape
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
rects = detector(gray, 0)
if len(rects) == 0:
print('No face detected')
continue
if (len(rects) > 0):
rect = rects[0]
'''
shape = predictor(gray, rect)
shape = face_utils.shape_to_np(shape)
for counter,(x, y) in enumerate(shape):
cv2.circle(show_frame, (x, y), 4, (0, 0, 255), -1)
cv2.putText(show_frame,str(counter),(x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.4,(255,255,255),1)
'''
left, right, top, bottom = rect.left(), rect.right(), rect.top(), rect.bottom()
width = abs(right - left)
height = abs(bottom - top)
# print("Left, right, top, bottom: ", left, right, top, bottom)
# print("Width and Height of bounding box : ",width,height)
face_left = int(left - (left_increase_ratio / 2) * width)
face_top = int(top - (top_increase_ratio) * height)
# face_right = int(right + (area_increase_ratio/2)*width)
# face_bottom = int(bottom + (area_increase_ratio/2)*height)
face_right = right
face_bottom = bottom
# print("Increased coordinates: ", face_left, face_right, face_top, face_bottom)
if face_left < 0:
face_left = 0
if face_top < 0:
face_top = 0
if face_right > w:
face_right = w
if face_bottom > h:
face_bottom = h
face = frame[face_top:face_bottom, face_left:face_right]
if (face.size == 0):
continue
# continue
# Extract face skin pixels
mask = skin_detector.process(face)
# print("Mask shape: ",mask.shape)
masked_face = cv2.bitwise_and(face, face, mask=mask)
number_of_skin_pixels = np.sum(mask > 0)
# compute mean
r = np.sum(masked_face[:, :, 2]) / number_of_skin_pixels
g = np.sum(masked_face[:, :, 1]) / number_of_skin_pixels
b = np.sum(masked_face[:, :, 0]) / number_of_skin_pixels
if frame_counter == 0:
mean_rgb = np.array([r, g, b])
else:
mean_rgb = np.vstack((mean_rgb, np.array([r, g, b])))
# print("Mean RGB -> R = {0}, G = {1}, B = {2} ".format(r, g, b))
frame_counter += 1
i += 1
l = int(framerate * 1.6)
H = np.zeros(mean_rgb.shape[0])
for t in range(0, (mean_rgb.shape[0] - l)):
# t = 0
# Step 1: Spatial averaging
C = mean_rgb[t:t + l - 1, :].T
# C = mean_rgb.T
# print("C shape", C.shape)
# print("t={0},t+l={1}".format(t, t + l))
# Step 2 : Temporal normalization
mean_color = np.mean(C, axis=1)
# print("Mean color", mean_color)
diag_mean_color = np.diag(mean_color)
# print("Diagonal",diag_mean_color)
diag_mean_color_inv = np.linalg.inv(diag_mean_color)
# print("Inverse",diag_mean_color_inv)
Cn = np.matmul(diag_mean_color_inv, C)
# Cn = diag_mean_color_inv@C
# print("Temporal normalization", Cn)
# print("Cn shape", Cn.shape)
projection_matrix = np.array([[0, 1, -1], [-2, 1, 1]])
S = np.matmul(projection_matrix, Cn)
# S = projection_matrix@Cn
# print("S matrix", S)
# print("S shape", S.shape)
if False:
f = np.arange(0, S.shape[1])
# plt.ylim(0,100000)
plt.plot(f, S[0, :], 'c', f, S[1, :], 'm')
plt.title("Projection matrix")
plt.show()
# Step 4:
# 2D signal to 1D signal
std = np.array([1, np.std(S[0, :]) / np.std(S[1, :])])
# print("std", std)
P = np.matmul(std, S)
# P = std@S
# print("P", P)
if False:
f = np.arange(0, len(P))
plt.plot(f, P, 'k')
plt.title("Alpha tuning")
plt.show()
# Step 5: Overlap-Adding
H[t:t + l - 1] = H[t:t + l - 1] + (P - np.mean(P)) / np.std(P)
# print("Pulse", H)
signal = H
if i == 200:
f = np.arange(0,200)
plt.plot(f,H)
plt.show()
break
# print("Pulse shape", H.shape)
segment_length = (2 * signal.shape[0]) // (12 + 1)