Skip to content

Repository files navigation

VitalSignsMonitor

English | 中文


English

A real-time vital signs monitoring system based on computer vision that enables contactless heart rate, SpO2, and respiration rate detection through webcam and Web API.

✨ Features

  • 🎥 Real-time Video Processing - Real-time video frame capture and processing through webcam
  • ❤️ Heart Rate Detection - Extract heart rate from facial video using POS (Plane-Orthogonal-to-Skin) algorithm
  • 🩺 SpO2 Detection - Blood oxygen saturation detection based on red and green light signal ratios
  • 🌬️ Respiration Rate Monitoring - Detect breathing frequency by analyzing facial region signal changes
  • 👤 Face Detection & Tracking - Automatically detect and track faces, extract forehead and cheek regions
  • 🌐 Web Interface - Intuitive real-time monitoring interface
  • 🔒 CORS Support - Cross-origin access support

🛠️ Tech Stack

  • .NET 10.0 - Backend framework
  • OpenCvSharp 4.11.0 - Computer vision processing
  • MathNet.Numerics 5.0.0 - Mathematical computation and signal processing (FFT, filtering, etc.)
  • ASP.NET Core Web API - RESTful API service
  • HTML5 + JavaScript - Frontend interface

📋 Requirements

  • .NET 10.0 SDK or higher
  • Windows OS (OpenCvSharp Windows runtime included)
  • Webcam device
  • Modern browser (WebRTC support)

🚀 Quick Start

1. Clone Repository

git clone https://github.com/zhouda1fu/VitalSignsMonitor.git
cd VitalSignsMonitor

2. Restore Dependencies

dotnet restore

3. Run Project

dotnet run

4. Access Application

Open browser and visit: https://localhost:7104 or http://localhost:5180

📖 Usage

  1. Start Application - Run dotnet run to start the Web API service
  2. Open Browser - Visit the application homepage
  3. Start Camera - Click "Start Camera" button and allow browser camera access
  4. Position Face - Ensure your face is clearly visible, the system will automatically detect it
  5. Wait for Initialization - The system needs about 1-2 seconds to collect data
  6. View Results - Real-time display of heart rate, SpO2, and respiration rate

🔬 How It Works

Heart Rate Detection (POS Algorithm)

  1. Face Detection - Use Haar cascade classifier to detect faces
  2. ROI Extraction - Extract forehead and cheek regions as regions of interest
  3. Skin Detection - Use YCrCb color space for skin region segmentation
  4. Signal Extraction - Calculate average RGB values of ROI regions
  5. POS Projection - Project RGB signals to a direction orthogonal to the skin plane
  6. Frequency Domain Analysis - Use FFT to analyze signals, find peak frequency in 0.75-3.0 Hz (45-180 BPM) range

SpO2 Detection

  1. Signal Filtering - Apply bandpass filter (0.8-3.0 Hz) to red and green light signals
  2. AC/DC Calculation - Calculate AC and DC components
  3. Ratio Calculation - Calculate R value = (R_AC/R_DC) / (G_AC/G_DC)
  4. SpO2 Estimation - Use empirical formula: SpO2 = 105 - 20 × R
  5. Smoothing - Apply median filtering to smooth results

Respiration Rate Detection

  1. Long Time Window - Use 30-second data window
  2. Resampling - Resample signal to 10 Hz
  3. Detrending - Remove trend using moving average window
  4. Frequency Domain Analysis - Find peak frequency in 0.15-0.5 Hz (9-30 breaths/min) range

📡 API Endpoints

POST /Pulse/process-frame

Process video frame and return vital signs data.

Request Format:

  • Content-Type: multipart/form-data
  • Parameter: frame (image file)

Response Format:

{
  "heartRateBpm": 72.5,
  "spO2Percent": 98.2,
  "respirationRate": 16.3,
  "status": "Measuring",
  "hasFace": true
}

Status Descriptions:

  • Initializing - System initializing
  • Gathering Data... - Collecting data (requires at least 1 second of data)
  • No Face Detected - No face detected
  • No Skin Detected - No skin region detected
  • Measuring - Currently measuring

⚠️ Important Notes

  1. Accuracy Limitations - This system is for research/demonstration purposes only and cannot replace professional medical equipment
  2. Environment Requirements - Requires good lighting conditions, avoid strong light and shadows
  3. Stability - Need to remain relatively still, minimize head movement
  4. Privacy - All video processing is done locally and not uploaded to server
  5. Performance - Modern CPU recommended, processing speed depends on hardware performance

🤝 Contributing

Issues and Pull Requests are welcome!

📄 License

This project is licensed under the MIT License. See LICENSE file for details.

🙏 Acknowledgments

  • OpenCV - Computer vision library
  • Math.NET Numerics - Mathematical computation library
  • POS algorithm references related academic research

📧 Contact

For questions or suggestions, please contact via GitHub Issues.


Disclaimer: This software is for research and educational purposes only and should not be used for medical diagnosis. Do not use in medical environments. Consult healthcare professionals for health concerns.


中文

一个基于计算机视觉的实时生命体征监测系统,通过摄像头和 Web API 实现无接触式心率、血氧饱和度和呼吸率检测。

✨ 功能特性

  • 🎥 实时视频处理 - 通过摄像头实时捕获和处理视频帧
  • ❤️ 心率检测 - 使用 POS (Plane-Orthogonal-to-Skin) 算法从面部视频中提取心率
  • 🩺 血氧饱和度 (SpO2) - 基于红光和绿光信号比率的血氧检测
  • 🌬️ 呼吸率监测 - 通过分析面部区域的信号变化检测呼吸频率
  • 👤 人脸检测与跟踪 - 自动检测和跟踪人脸,提取前额和脸颊区域
  • 🌐 Web 界面 - 提供直观的实时监测界面
  • 🔒 CORS 支持 - 支持跨域访问

🛠️ 技术栈

  • .NET 10.0 - 后端框架
  • OpenCvSharp 4.11.0 - 计算机视觉处理
  • MathNet.Numerics 5.0.0 - 数学计算和信号处理(FFT、滤波等)
  • ASP.NET Core Web API - RESTful API 服务
  • HTML5 + JavaScript - 前端界面

📋 系统要求

  • .NET 10.0 SDK 或更高版本
  • Windows 操作系统(已包含 OpenCvSharp Windows 运行时)
  • 摄像头设备
  • 现代浏览器(支持 WebRTC)

🚀 快速开始

1. 克隆仓库

git clone https://github.com/zhouda1fu/VitalSignsMonitor.git
cd VitalSignsMonitor

2. 还原依赖

dotnet restore

3. 运行项目

dotnet run

4. 访问应用

打开浏览器访问:https://localhost:7104http://localhost:5180

📖 使用方法

  1. 启动应用 - 运行 dotnet run 启动 Web API 服务
  2. 打开浏览器 - 访问应用首页
  3. 启动摄像头 - 点击 "Start Camera" 按钮,允许浏览器访问摄像头
  4. 定位人脸 - 确保面部清晰可见,系统会自动检测人脸
  5. 等待初始化 - 系统需要约 1-2 秒收集数据
  6. 查看结果 - 实时显示心率、血氧饱和度和呼吸率

🔬 工作原理

心率检测 (POS 算法)

  1. 人脸检测 - 使用 Haar 级联分类器检测人脸
  2. ROI 提取 - 提取前额和脸颊区域作为感兴趣区域
  3. 皮肤检测 - 使用 YCrCb 颜色空间进行皮肤区域分割
  4. 信号提取 - 计算 ROI 区域的平均 RGB 值
  5. POS 投影 - 将 RGB 信号投影到与皮肤平面正交的方向
  6. 频域分析 - 使用 FFT 分析信号,在 0.75-3.0 Hz (45-180 BPM) 范围内查找峰值频率

血氧饱和度检测

  1. 信号滤波 - 对红光和绿光信号进行带通滤波 (0.8-3.0 Hz)
  2. AC/DC 计算 - 计算交流分量和直流分量
  3. 比率计算 - 计算 R 值 = (R_AC/R_DC) / (G_AC/G_DC)
  4. SpO2 估算 - 使用经验公式:SpO2 = 105 - 20 × R
  5. 平滑处理 - 使用中位数滤波平滑结果

呼吸率检测

  1. 长时间窗口 - 使用 30 秒的数据窗口
  2. 重采样 - 将信号重采样到 10 Hz
  3. 去趋势 - 使用移动平均窗口去除趋势
  4. 频域分析 - 在 0.15-0.5 Hz (9-30 次/分钟) 范围内查找峰值频率

📡 API 端点

POST /Pulse/process-frame

处理视频帧并返回生命体征数据。

请求格式:

  • Content-Type: multipart/form-data
  • 参数:frame (图像文件)

响应格式:

{
  "heartRateBpm": 72.5,
  "spO2Percent": 98.2,
  "respirationRate": 16.3,
  "status": "Measuring",
  "hasFace": true
}

状态说明:

  • Initializing - 系统初始化中
  • Gathering Data... - 正在收集数据(需要至少 1 秒数据)
  • No Face Detected - 未检测到人脸
  • No Skin Detected - 未检测到皮肤区域
  • Measuring - 正在测量

📁 项目结构

VitalSignsMonitor/
├── Controllers/
│   └── PulseController.cs          # API 控制器
├── Services/
│   └── VitalSignsService.cs        # 核心生命体征处理服务
├── wwwroot/
│   └── index.html                   # Web 前端界面
├── haarcascade_frontalface_alt.xml  # 人脸检测分类器
├── Program.cs                        # 应用程序入口
└── VitalSignsMonitor.csproj          # 项目配置文件

⚙️ 配置说明

CORS 配置

默认配置允许所有来源访问。在生产环境中,建议修改 Program.cs 中的 CORS 策略:

builder.AllowSpecificOrigins("https://yourdomain.com");

服务生命周期

当前 VitalSignsService 注册为 Singleton,适用于单用户场景。对于多用户生产环境,建议:

  • 使用 Scoped 生命周期
  • 为每个会话创建独立的服务实例
  • 使用字典管理多个会话:Dictionary<string, VitalSignsService>

🔍 算法参数

缓冲区大小

  • 心率缓冲区: 300 帧 (~10 秒 @ 30 FPS)
  • 呼吸缓冲区: 900 帧 (~30 秒 @ 30 FPS)

频率范围

  • 心率: 0.75 - 3.0 Hz (45 - 180 BPM)
  • 呼吸率: 0.15 - 0.5 Hz (9 - 30 次/分钟)

检测参数

  • 人脸检测间隔: 每 10 帧检测一次(已有人脸时)
  • 人脸丢失阈值: 连续 30 帧未检测到人脸则重置

⚠️ 注意事项

  1. 精度限制 - 此系统为研究/演示用途,不能替代专业医疗设备
  2. 环境要求 - 需要良好的光照条件,避免强光和阴影
  3. 稳定性 - 需要保持相对静止,减少头部移动
  4. 隐私 - 所有视频处理在本地完成,不会上传到服务器
  5. 性能 - 建议使用现代 CPU,处理速度取决于硬件性能

🤝 贡献

欢迎提交 Issue 和 Pull Request!

📄 许可证

本项目采用 MIT 许可证。详见 LICENSE 文件。

🙏 致谢

📧 联系方式

如有问题或建议,请通过 GitHub Issues 联系。


免责声明: 本软件仅供研究和教育用途,不能用于医疗诊断。请勿在医疗环境中使用,如有健康问题请咨询专业医疗人员。

About

基于摄像头的生理指标识别

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages