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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427 | def nervotec_compute(attempt_id, source_path, save_results, record_videos, scan_duration=60,
motion_tracking = True, skin_segmentor='HSVandYCrCbSkinSegmentation', collect_truth_values=True,
extreme_pixel_removal=False, gamma_value=1.0, window_length=256,
hop_length=256,rgb_averaging=False,ground_truth_file=None):
"""
nervotec_compute() is a program for physiological signal extraction from a
given source (video or camera). The program uses a face detector to perform
the extraction of physiological signals.
Parameters
----------
source_path (str): The path of the video or camera to use as the source. The format should be either "cam" or the path to the video file.
save_results (str): The string identifier to use in the filename of the output data.
record_videos (bool): Whether or not to record the video stream to disk.
scan_duration (int, optional): The duration of the scan in seconds. Defaults to 60.
face_detector (str, optional): The type of face detector to use. Defaults to 'BlazefaceFacemesh'.
collect_truth_values (bool, optional): Whether or not to collect ground truth values for the physiological signals. Defaults to True.
extreme_pixel_removal (bool, optional): Whether or not to remove pixels using pre-defined thresholds
gamma_value (float, optional): Gamma value for the gamma-correction
eularian_color_magnification(bool, optional): Whether or not to do E ularian Color Magnification
Methodology
-----------
1. Collects configuration data and creates a dictionary cfg to summarize the results.
2. Sets up necessary directories and names to store results.
3. Initializes necessary variables and objects, such as the frame provider, face detector, and landmark selectors.
4. Extracts frames from the frame provider and performs the signal extraction.
5. After the scan duration is reached, the instantaneous results are saved to disk.
6. The mean of the results across all windows is calculated and saved to disk.
7. The function returns the data frame of the mean results.
"""
attempt_id = save_id(attempt_id)
logger.set_id(attempt_id)
try:
logger.info(f"New Attempt id: {attempt_id}")
######################## Initialization ########################
logger.info("Initializing...")
store_timestamp = time.strftime("%Y%m%d-%H%M%S")
input_name = Path(source_path.split(':')[0]).stem.split('.')[0] # extract input name from the source path
# setup necessary directories and names
os.makedirs('results', exist_ok=True)
results_folder = os.path.join('results', input_name + '_' + save_results + '_' + store_timestamp)
videos_folder = os.path.join(results_folder, 'videos')
data_folder = os.path.join(results_folder, 'time_series')
os.makedirs(results_folder, exist_ok=True)
os.makedirs(data_folder, exist_ok=True)
frame_count = 0
window_count = 0
input_frames = 0
plot_window_bvp = False
if plot_window_bvp:
fig, plot = plot_bvp(fig=None,window_size=window_length,bvp=None,hr=None,peaks=None,bvp_method=None,lm=None)
cfg = {}
######################## Setup Frame Provider ########################
if save_results == 'scan':
logger.info("Opening camera.. ")
if len(source_path.split(':')) == 3:
camera_id = int(source_path.split(':')[1])
hd_select = int(source_path.split(':')[2])
source_path = source_path.split(':')[0]
frame_provider = CameraFrameProvider(camera_id, record_videos, videos_folder, 'FFV1',hd_select)
elif save_results[:3] == 'pgm':
logger.info("Opening PGMs.. ")
# print('Source Path:', source_path)
frame_provider = PGMFrameProvider(source_path)
elif save_results == 'stream':
logger.info('opening Stream..!')
measure_time.checkpoint("until stream frame provider starts")
frame_provider = StreamFrameProvider(attempt_id)
else:
logger.info("Opening video.. ")
# print('Source Path:', source_path)
frame_provider = VideoFrameProvider(source_path)
FPS = frame_provider.get_fps() # get FPS from the frame provider
######################## Setting BVP Methods ########################
bvp_methods = ['CHROM']
bvp_multimethod = BVP(bvp_methods)
if hop_length == 0:
hop_length = window_length
#####################################################################
cfg['attempt_id'] = str(attempt_id)
cfg['source'] = source_path
cfg['scan duration'] = scan_duration
cfg['FPS'] = FPS
cfg['skin segmentation method'] = skin_segmentor
cfg['pixel outliers removal'] = extreme_pixel_removal
cfg['window_length'] = window_length
cfg['hop_length'] = hop_length
cfg['rgb_averaging'] = rgb_averaging
cfg['bvp_methods'] = bvp_methods
cfg['motion_tracking'] = motion_tracking
cfg['gamma_value'] = gamma_value
######################## Face detector and landmarks ################
face_detector = BlazefaceFacemesh(tracking=motion_tracking)
# # setup landmark selectors
# lm_list = [face_detector.lm_big_forehead,
# face_detector.lm_small_forehead,
# face_detector.lm_cheeks_and_nose,
# face_detector.lm_left_cheek,
# face_detector.lm_right_cheek,
# face_detector.lm_crop_face
# ]
lm_list = [
face_detector.lm_small_forehead
]
lm_name_dict = {0: 'Small forehead'}
if skin_segmentor is not None:
skin_segmentor = get_class(skin_segmentor)()
# define placeholders
rgb_list = np.zeros((len(lm_list),3,window_length))
estimates = []
transform_times = []
######################## Starting the Scan ########################
# Check the source validity
if not frame_provider.isOpened():
logger.error("Could not open frame provider!")
sys.exit() #TODO replace exits with proper exceptions
success, image, time_start = frame_provider.get_next()
if not success:
for i in range(15):
success, image, time_start = frame_provider.get_next()
print(success)
if success:
break
else:
continue
if success:
pass
else:
logger.error("Cannot read video file!")
sys.exit() #TODO replace exits with proper exceptions
measure_time.checkpoint("until sfp reads the first frame")
start_time = tt.time()
while frame_provider.isOpened():
# extract frames from the frame provider
success, image, time_now = frame_provider.get_next()
# ignore unsuccessful frame loops
if source_path == 'cam' and not success:
continue
# if (source_path == 'cam' and (time_elapsed) > scan_duration) or (source_path != 'cam' and not success):
if (source_path == 'cam' and (input_frames+1 == int(scan_duration*FPS))) or (source_path != 'cam' and not success):
logger.info('End of the scan')
break
input_frames += 1
# To improve performance, optionally mark the image as not writeable to
# pass by reference.
image.flags.writeable = False
# Detecting faces
results = face_detector.detect_faces(image)
if results:
b_t = tt.time()
if gamma_value != 1.0:
image = adjust_gamma(image,gamma_value)
if skin_segmentor:
skin_mask = skin_segmentor.extraction_mask(image,results)
else:
skin_mask = None
if extreme_pixel_removal:
standard_mask = threshold_mask(image, RGB_LOW_TH, RGB_HIGH_TH)
else:
standard_mask = None
for num, lm in enumerate(lm_list):
# get face landmarks from the face detector
coords = face_detector.coords_for_landmark(results, lm, image)
# get mean values for RGB layers in ROIs
rgb_means = face_detector.rgb_means(coords, image, [skin_mask, standard_mask])
# update RGB means to rgb_list
rgb_list[num,:,frame_count] = rgb_means # append RGB means
# append time taken for get landmarks and rgb means for all ROIs
transform_times.append(tt.time() - b_t)
frame_count = frame_count + 1
# separate windows per window_length frames
if frame_count == window_length:
if window_count == 0:
measure_time.checkpoint('until starts the first window')
frame_count = window_length - hop_length
window_count = window_count + 1
if rgb_averaging:
rgb_signal = moving_average(rgb_list,5,True)
else:
rgb_signal = rgb_list
spo2_raw = get_SpO2(rgb_signal)
############################################################
### Calculating Vitals ###
############################################################
bvp_B_wod = BVP_B_wod_from_RGB(rgb_signal, bvp_multimethod, FPS)
hr_B_wod_ro, hrv_dict_B_wod_ro, bvp_B_wod_ro_peaks = calculate_vitals(bvp_B_wod,FPS,trim=False,outliers_removal=True,replace_median=False)
########## RR calculation ##############
rr_dict_fft = RR_from_RGB(rgb_signal, bvp_multimethod, FPS, minHz=0.15, maxHz=0.4, calc_method='fft')
########## Create the BVP plot ##############
if plot_window_bvp:
fig, plot = plot_bvp(fig=fig,window_size=window_length,bvp={'BVP_B_wod':bvp_B_wod},hr={'BVP_B_wod_ro':hr_B_wod_ro},
peaks={'BVP_B_wod_ro':bvp_B_wod_ro_peaks},bvp_method='CHROM',lm=0)
################################################
logger.info(f'Time : {tt.time()-start_time}\tWindow : {window_count}')
logger.info(f'SpO2: {lm_name_dict[0]} : {round(float(spo2_raw[0]*100),2)}')
# print('\nUsing B BVPs')
print_window_results(hr_B_wod_ro,rr_dict_fft,hrv_dict_B_wod_ro,bvp_methods,lm_name_dict,window_count,selected_lm=0)
hr = hr_B_wod_ro['CHROM']
rr = rr_dict_fft['CHROM'][0]
RMSSD = hrv_dict_B_wod_ro['CHROM'][0][0]
SDNN = hrv_dict_B_wod_ro['CHROM'][0][1]
PNN50 = hrv_dict_B_wod_ro['CHROM'][0][2]
estimates_dict = dict(window_count=float(window_count),
SPO2 = "{:.2f}".format((spo2_raw*100.0).item()),
HR="{:.2f}".format(hr.item()),
RR="{:.2f}".format(rr),
RMSSD="{:.2f}".format(RMSSD),
SDNN="{:.2f}".format(SDNN),
PNN50="{:.2f}".format(PNN50))
estimates_dict['job_id'] = attempt_id
if window_count == 1:
measure_time.checkpoint("until completes the first window calculation")
global_queue.put_to_queue(estimates_dict)
estimates_tuple = (window_count, spo2_raw*100.0, [hr_B_wod_ro],
[rr_dict_fft],
[hrv_dict_B_wod_ro],None)
estimates.append(estimates_tuple)
with open('outputs.sav','wb') as f:
joblib.dump(estimates_dict,f)
if save_results is not None:
save_intermediate_data(data_folder,window_count,rgb_signal,{'BVP_B_wod_ro':bvp_B_wod})
rgb_list[:,:,:(window_length-hop_length)] = rgb_list[:,:,hop_length:]
rgb_list[:,:,window_length-hop_length:] = np.zeros((len(lm_list),3, hop_length))
if results:
image = face_detector.draw_landmarks(results, lm_list, image,[skin_mask, standard_mask])
image = cv2.flip(image, 1)
else:
logger.error('No faces identified')
if plot_window_bvp:
plot = cv2.resize(plot, (image.shape[1],image.shape[0]))
image = np.hstack((plot, image))
# cv2.imshow(f'{source_path} input', image)
# if cv2.waitKey(1) & 0xFF == 27:
# break
if save_results:
create_and_save_dataframes(estimates,['BVP_B_wod_ro'],bvp_methods,lm_name_dict,results_folder,print_mean = True)
total_exec_time = round(tt.time()-start_time,3)
total_no_frames = input_frames + 1 if source_path == 'cam' else input_frames
avg_detection_time = round(sum(face_detector.det_times) / len(face_detector.det_times),2)
cfg['total execution time'] = total_exec_time
cfg['total no of frames'] = total_no_frames
cfg['average detection time'] = avg_detection_time
logger.info(f"\nFPS setting: {round(FPS,2)}")
logger.info(f"Total execution time: {total_exec_time}")
logger.info(f"Total number of frames: {total_no_frames}")
logger.info("")
logger.info(f'Detection avg time: {avg_detection_time}')
if len(face_detector.track_times) > 0:
avg_tracking_time = round(1000*sum(face_detector.track_times) / len(face_detector.track_times),2)
cfg['average tracking time'] = avg_tracking_time
logger.info(f'Tracking avg time: {avg_tracking_time}ms')
if len(transform_times) > 0:
avg_transform_time = round(1000*sum(transform_times) / len(transform_times),2)
cfg['average transform time'] = avg_transform_time
logger.info(f'Transform avg time: {avg_transform_time}ms')
logger.info("")
if save_results is not None:
with open(os.path.join(results_folder, 'config.json'),'w') as f:
json.dump(cfg,f)
frame_provider.release()
cv2.destroyAllWindows()
if collect_truth_values:
collect_ground_truth(results_folder,ground_truth_file)
logger.info('---End attempt---')
except Exception as e:
logger.error(f"Exception in id : {str(attempt_id)}")
logger.error(f"Exception caught: {e}")
traceback.print_exc()
|