-
Notifications
You must be signed in to change notification settings - Fork 0
/
bpm.py
69 lines (48 loc) · 1.69 KB
/
bpm.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
import numpy as np
import matplotlib; matplotlib.use('agg')
from matplotlib import pyplot as plt
import cv2
import io
import time
from numpy import ndarray
webCam = cv2.VideoCapture(0)
heartbeat_count = 128
heartbeat_values = [0] * heartbeat_count
heartbeat_times = [time.time()] * heartbeat_count
# Matplotlib graph surface
fig = plt.figure()
assert isinstance(fig.add_subplot, object)
ax = fig.add_subplot(111)
while True:
# FPS
ret, frame = webCam.read()
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# img = cv2.imread(frame)
gray_filter = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Detect faces
faces = face_cascade.detectMultiScale(gray_filter, 1.1, 4)
# Draw rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
faces = frame[y:y + h, x:x + w]
# crop_img = img[y:y + h, x:x + w]
# Read the input image
# img = cv2.imread(img)
cv2.imshow('img', gray_filter)
# Update the data
heartbeat_values = heartbeat_values[1:] + [np.average(faces)]
heartbeat_times = heartbeat_times[1:] + [time.time()]
# Draw matplotlib graph to numpy array
ax.plot(heartbeat_times, heartbeat_values)
fig.canvas.draw()
plot_img_np: ndarray = np.fromstring(fig.canvas.tostring_rgb(),
dtype=np.uint8, sep='')
plot_img_np = plot_img_np.reshape(fig.canvas.get_width_height()[::-1] + (3,))
plt.cla()
# Display the frames
cv2.imshow('Crop', gray_filter)
cv2.imshow('Graph', plot_img_np)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
webCam.release()
cv2.destroyAllWindows()