-
Notifications
You must be signed in to change notification settings - Fork 0
/
Offline_Analysis.py
102 lines (87 loc) · 3.05 KB
/
Offline_Analysis.py
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
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 9 15:40:03 2019
@author: Omar Al Jaroudi
"""
#!/usr/bin/python
# -*- coding: utf-8 -*-
import cv2
import numpy as np
import time
import warnings
warnings.filterwarnings("ignore")
from matplotlib import pyplot as plt
from signal_processing import Signal_processing
from image_processing import Image_processing
sp = Signal_processing()
ip = Image_processing()
path = "133"
cap = cv2.VideoCapture('./Offline Videos/Omar_'+path+'.mp4')
count = 0
frame_count=0
Bsig =np.array([]); Gsig =np.array([]); Rsig =np.array([]);
t0 = time.time()
times = []
bpm_old = 0
p1_old = 0;p2_old = 0;p3_old = 0;p4_old = 0
heartRate = []
fps = cap.get(cv2.CAP_PROP_FPS)
while cap.isOpened():
success,image = cap.read()
if(cv2.waitKey(1)==13 or success==False):
break
height, width = image.shape[:2];
start_row, start_col = int(height * .1), int(width * .2)
end_row, end_col = int(height * .9), int(width * .8)
image = image[start_row:end_row , start_col:end_col]
image_landmarks,landmarks = ip.landmark_image(image)
imageOrg = image.copy()
warp = image.copy();
if landmarks != "error" and landmarks!=():
p1,p2,p3,p4,mean_eye = ip.forehead(landmarks)
points = [p1,p2,p3,p4,p1_old,p2_old,p3_old,p4_old]
points = ip.Stabilize(points)
p1 = points[0]
p2 = points[1]
p3 = points[2]
p4 = points[3]
p1_old = p1
p2_old = p2
p3_old = p3
p4_old = p4
pts = np.array( [p1, p2, p3, p4], np.int32)
pts = pts.reshape((-1,1,2))
cv2.polylines(image, [pts], True, (0,0,255), 3)
cv2.circle(image, tuple(p1.astype(int)), 3, color=(0, 255, 255))
cv2.circle(image, tuple(mean_eye.astype(int)), 3, color=(0, 255, 255))
mask = np.zeros(imageOrg.shape, dtype=np.uint8)
roi_corners = np.array([[p1, p2, p3,p4]], dtype=np.int32)
channel_count = imageOrg.shape[2] # i.e. 3 or 4 depending on your image
ignore_mask_color = (255,)*channel_count
cv2.fillPoly(mask, roi_corners, ignore_mask_color)
# apply the mask
roi = cv2.bitwise_and(imageOrg, mask)
cv2.imshow("ROI",roi)
B, G, R = cv2.split(roi)
if(count==50):
print(1)
t0 = time.time()
if(count>=50):
times.append(time.time() - t0)
Bsig = np.append(Bsig,np.mean(B[np.nonzero(B)]))
Gsig = np.append(Gsig,np.mean(G[np.nonzero(G)]))
Rsig = np.append(Rsig,np.mean(R[np.nonzero(R)]))
frame_count+=1
cv2.imshow('Image', image)
if (frame_count==300):
# bpm = sp.Analyze(Rsig,Gsig,Bsig,fps)
bpm = sp.Analyze(Rsig,Gsig,Bsig,fps)
heartRate.append(bpm)
times = []
Bsig =Bsig[90:]; Gsig = Gsig[90:]; Rsig = Rsig[90:];
frame_count = 210
count+=1
plt.plot(heartRate)
plt.show()
cap.release()
cv2.destroyAllWindows()