مرحبا هبر.
بمجرد أن صادفت وصفًا لتطبيق Android يحدد معدل ضربات القلب بواسطة كاميرا الهاتف ، فقط من خلال الصورة العامة. لم يتم تطبيق الكاميرا على الإصبع ، ولم تكن مضاءة بواسطة مؤشر LED. كانت النقطة المثيرة للاهتمام هي أن المراجعين لم يؤمنوا بإمكانية تحديد النبض ، وتم رفض الطلب. لا أعرف كيف انتهى الأمر بمؤلف البرنامج ، ولكن أصبح من المثير للاهتمام التحقق مما إذا كان هذا ممكنًا.
لمن يهتم بما حدث استمراره تحت الخفض.
بالطبع ، لن أقوم بإنشاء تطبيق لنظام Android ، فمن الأسهل بكثير اختبار الفكرة في Python.
نتلقى البيانات من الكاميرا
أولاً ، نحتاج إلى الحصول على البث من كاميرا الويب ، والتي سنستخدم فيها OpenCV. الشفرة مشتركة بين الأنظمة الأساسية ويمكن تشغيلها على كل من Windows و Linux / OSX.
import cv2
import io
import time
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)
while(True):
ret, frame = cap.read()
# Our operations on the frame come here
img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the frame
cv2.imshow('Crop', crop_img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()فكرة تحديد النبض هي أن لون الجلد يتغير قليلاً بسبب تدفق الدم في الأوعية ، لذلك نحتاج إلى محصول من الصورة ، والذي سيحتوي فقط على جزء من الجلد.
x, y, w, h = 800, 500, 100, 100
crop_img = img[y:y + h, x:x + w]
cv2.imshow('Crop', crop_img)إذا تم كل شيء بشكل صحيح ، عند بدء البرنامج ، يجب أن نحصل على شيء كهذا من الكاميرا (غير واضح لأسباب الخصوصية) ونقص:

علاج او معاملة
, , . .
heartbeat_count = 128
heartbeat_values = [0]*heartbeat_count
heartbeat_times = [time.time()]*heartbeat_count
while True:
...
# Update the list
heartbeat_values = heartbeat_values[1:] + [np.average(crop_img)]
heartbeat_times = heartbeat_times[1:] + [time.time()]
numpy.average , , .
:
fig = plt.figure()
ax = fig.add_subplot(111)
while(True):
...
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()
cv2.imshow('Graph', plot_img_np): OpenCV numpy, matplotlib , numpy.fromstring.
.
, , , " ", - . - !

, , , . , ! , 0.5% , " ", . , , 75bpm. , :

, .. , , .
, . , . , , OpenCV . , .
, - , ? , . cap = cv2.VideoCapture(0) cap = cv2.VideoCapture("video.mp4"), .
, .
Spoiler
import numpy as np
from matplotlib import pyplot as plt
import cv2
import io
import time
# Camera stream
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1280)
cap.set(cv2.CAP_PROP_FPS, 30)
# Video stream (optional)
# cap = cv2.VideoCapture("videoplayback.mp4")
# Image crop
x, y, w, h = 800, 500, 100, 100
heartbeat_count = 128
heartbeat_values = [0]*heartbeat_count
heartbeat_times = [time.time()]*heartbeat_count
# Matplotlib graph surface
fig = plt.figure()
ax = fig.add_subplot(111)
while(True):
# Capture frame-by-frame
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]
# Update the data
heartbeat_values = heartbeat_values[1:] + [np.average(crop_img)]
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 = 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', crop_img)
cv2.imshow('Graph', plot_img_np)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()وكالعادة كل التجارب الناجحة