import cv2 from BPApi import BPModel def main(): cap = cv2.VideoCapture(0) # 使用摄像头 #设置视频宽高 cap.set(3, 1920) cap.set(4, 1080) video_fs = cap.get(5) # print(video_fs) # 加载模型 model = BPModel(model_path=r'final/best.pth', fps=video_fs) frames = [] text = ["calculating..."] font = cv2.FONT_HERSHEY_SIMPLEX face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml') while True: ret, frame = cap.read() # 检测人脸 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, 1.3, 5) if faces is not None and len(faces) > 0: # 将第一个人脸区域的图像截取 x, y, w, h = faces[0] face = frame[y:y + h, x:x + w] frames.append(face) cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 255, 0), 2) print(len(frames)) if len(frames) == 250: sbp_outputs, dbp_outputs = model.predict(frames) print(sbp_outputs, dbp_outputs) text.clear() text.append('SBP: {:.2f} mmHg'.format(sbp_outputs)) text.append('DBP: {:.2f} mmHg'.format(dbp_outputs)) frames = [] # 去除列表最前面的100个元素 # frames=frames[50:] for i, t in enumerate(text): cv2.putText(frame, t, (10, 60 + i * 20), font, 0.6, (0, 255, 0), 2) cv2.imshow('Blood Pressure Detection', frame) key = cv2.waitKey(1) & 0xFF if key == ord('q'): break cap.release() cv2.destroyAllWindows() if __name__ == '__main__': main()