Created
September 4, 2020 12:58
-
-
Save anton-petrov/2b86d719a81604003c5e58fad71c75fc to your computer and use it in GitHub Desktop.
Пульс по вебкамере
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # https://habr.com/ru/post/517424/ | |
| import cv2 | |
| import io | |
| import time | |
| import numpy as np | |
| from matplotlib import pyplot as plt | |
| cap = cv2.VideoCapture(0) | |
| cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920) | |
| cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080) | |
| cap.set(cv2.CAP_PROP_FPS, 30) | |
| x, y, w, h = 800, 500, 100, 100 | |
| heartbeat_count = 128 | |
| heartbeat_values = [0] * heartbeat_count | |
| heartbeat_times = [time.time()] * heartbeat_count | |
| fig = plt.figure() | |
| ax = fig.add_subplot() | |
| while(True): | |
| ret, frame = cap.read() | |
| # Our operations on the frame come here | |
| img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | |
| crop_img = img[y:y + h, x:x + w] | |
| window_name = 'image' | |
| heartbeat_values = heartbeat_values[1:] + [np.average(crop_img)] | |
| heartbeat_times = heartbeat_times[1:] + [time.time()] | |
| ax.plot(heartbeat_times, heartbeat_values) | |
| fig.canvas.draw() | |
| plot_img_np = 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 frame | |
| cv2.imshow('Crop', crop_img) | |
| cv2.imshow('Graph', plot_img_np) | |
| if cv2.waitKey(1) & 0xFF == ord('q'): | |
| break | |
| cap.release() | |
| cv2.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment