眨眼频率、眼动分析、心率、视频录制

This commit is contained in:
邓智航
2026-01-25 15:19:01 +08:00
parent 6e882d2aa4
commit b3997c2646
7 changed files with 977 additions and 188 deletions

97
reproject/test.py Normal file
View File

@@ -0,0 +1,97 @@
import cv2
import threading
import time
import queue
import socket
import json
import urllib.request
import struct
import mediapipe as mp
import numpy as np
from analyzer import MonitorSystem
mp_face_mesh = mp.solutions.face_mesh
face_mesh = mp_face_mesh.FaceMesh(
max_num_faces=1,
refine_landmarks=True,
min_detection_confidence=0.5,
min_tracking_confidence=0.5
)
def apply_soft_roi(frame):
h, w = frame.shape[:2]
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
black_bg = np.zeros((h, w, 3), dtype=np.uint8)
results = face_mesh.process(rgb_frame)
if not results.multi_face_landmarks:
return frame
landmarks = results.multi_face_landmarks[0].landmark
xs = [l.x for l in landmarks]
ys = [l.y for l in landmarks]
# 计算人脸框
face_loc = (
int(min(ys) * h - 0.1 * h), int(max(xs) * w + 0.1 * w),
int(max(ys) * h + 0.1 * h), int(min(xs) * w - 0.1 * w)
)
pad = 30
face_loc = (max(0, face_loc[0]-pad), min(w, face_loc[1]+pad),
min(h, face_loc[2]+pad), max(0, face_loc[3]-pad))
top = face_loc[0]
right = face_loc[1]
bottom = face_loc[2]
left = face_loc[3]
scale_factor = 10
small_bg = cv2.resize(frame, (w // scale_factor, h // scale_factor), interpolation=cv2.INTER_LINEAR)
# 使用 INTER_NEAREST 马赛克效果
# 使用 INTER_LINEAR 毛玻璃模糊效果
blurred_frame = cv2.resize(small_bg, (w, h), interpolation=cv2.INTER_LINEAR)
face_roi = frame[top:bottom, left:right]
blurred_frame[top:bottom, left:right] = face_roi
black_bg[top:bottom, left:right] = face_roi
return blurred_frame
def test_compression_efficiency():
# 1. 读取一张测试图 (或者用摄像头抓一帧)
cap = cv2.VideoCapture(0)
time.sleep(5) # 等待摄像头稳定
while True:
ret, frame = cap.read()
cap.release()
if not ret:
print("无法读取摄像头")
return
# 2. 生成模糊处理后的图
processed_frame = apply_soft_roi(frame) # 使用你的函数
# 3. 【关键】模拟网络传输/存储:进行 JPG 编码
# 这里的 80 代表 JPEG 质量,模拟视频编码过程
encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 80]
# 编码原图
_, encoded_original = cv2.imencode('.jpg', frame, encode_param)
original_size = len(encoded_original)
# 编码处理后的图
_, encoded_processed = cv2.imencode('.jpg', processed_frame, encode_param)
processed_size = len(encoded_processed)
# 4. 对比结果
print(f"原始画面编码后大小: {original_size / 1024:.2f} KB")
print(f"ROI处理编码后大小: {processed_size / 1024:.2f} KB")
savings = (original_size - processed_size) / original_size * 100
print(f"📉 带宽/存储节省了: {savings:.2f}%")
# 可视化对比
cv2.imshow("Original", frame)
cv2.imshow("Processed (Bandwidth Saver)", processed_frame)
# 运行测试
# 注意:你需要先定义 face_mesh 和 apply_soft_roi 才能运行
test_compression_efficiency()