Skip to content

Commit 285cd41

Browse files
Merge pull request #1587 from raj3000k/master
Added Heart Rate Detection using OpenCV(webcam).
2 parents e01b704 + 7a7c1ca commit 285cd41

File tree

5 files changed

+71
-0
lines changed

5 files changed

+71
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import numpy as np
2+
from matplotlib import pyplot as plt
3+
import cv2
4+
import io
5+
import time
6+
# Camera stream
7+
cap = cv2.VideoCapture(0)
8+
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
9+
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1280)
10+
cap.set(cv2.CAP_PROP_FPS, 30)
11+
# Video stream (optional, not tested)
12+
# cap = cv2.VideoCapture("video.mp4")
13+
# Image crop
14+
x, y, w, h = 800, 500, 100, 100
15+
x, y, w, h = 950, 300, 100, 100
16+
heartbeat_count = 128
17+
heartbeat_values = [0]*heartbeat_count
18+
heartbeat_times = [time.time()]*heartbeat_count
19+
# Matplotlib graph surface
20+
fig = plt.figure()
21+
ax = fig.add_subplot(111)
22+
while True:
23+
# Capture frame-by-frame
24+
ret, frame = cap.read()
25+
img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
26+
crop_img = img[y:y + h, x:x + w]
27+
# Update the data
28+
heartbeat_values = heartbeat_values[1:] + [np.average(crop_img)]
29+
heartbeat_times = heartbeat_times[1:] + [time.time()]
30+
# Draw matplotlib graph to numpy array
31+
ax.plot(heartbeat_times, heartbeat_values)
32+
fig.canvas.draw()
33+
plot_img_np = np.fromstring(fig.canvas.tostring_rgb(),
34+
dtype=np.uint8, sep='')
35+
plot_img_np = plot_img_np.reshape(fig.canvas.get_width_height()[::-1] + (3,))
36+
plt.cla()
37+
# Display the frames
38+
cv2.imshow('Crop', crop_img)
39+
cv2.imshow('Graph', plot_img_np)
40+
if cv2.waitKey(1) & 0xFF == ord('q'):
41+
break
42+
cap.release()
43+
cv2.destroyAllWindows()
314 KB
Loading
Binary file not shown.
18.1 KB
Loading
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# **Heart Rate Detection**
2+
3+
- Heart Rate detection using the eye's movement, OpenCV and various algorithms.
4+
5+
# Requirements
6+
7+
```
8+
- pip install -U pip
9+
- pip install -U matplotlib
10+
- pip install opencv-python
11+
- pip install numpy
12+
```
13+
14+
# Sample Image-
15+
16+
![Alt text](./SS-heratGraph.png)
17+
18+
# Video-
19+
20+
[![Watch the video](./click%20Here.jpeg)](./SampleVideo-HeartRate-Detection-Raj%20Motwani.mp4)
21+
22+
# Author -
23+
24+
- **_Raj Motwani_**
25+
26+
- <a href="https://www.linkedin.com/in/raj-motwani-978143204/" target="_blank">LinkedIn</a>
27+
28+
- <a href="https://github.com/raj3000k" target="_blank">GitHub</a>

0 commit comments

Comments
 (0)