文章目录
前言
本文简单介绍了 ESP32-S3-CAM 的人脸姿态检测系统。
一、实例代码
import cv2
import numpy as np
import requests
from io import BytesIO
import threading
import time
from collections import deque
import pygame # 用于播放警告声音
class FaceDetectionStream:
def __init__(self, base_url):
self.base_url = base_url
self.stopped = False
self.frames = deque(maxlen=1)
self.face_data = deque(maxlen=1)
self.thread = threading.Thread(target=self.update, args=())
self.thread.daemon = True
def start(self):
self.thread.start()
return self
def update(self):
while not self.stopped:
try:
# 并行获取图像和人脸数据
img_thread = threading.Thread(target=self._update_image)
data_thread = threading.Thread(target=self._update_face_data)
img_thread.start()
data_thread.start()
img_thread.join()
data_thread.join()
time.sleep(0.05) # 控制请求频率
except Exception as e:
print(f"更新流时出错: {
e}")
time.sleep(1) # 出错时等待更长时间
def _update_image(self):
try:
response = requests.get(f"{
self.base_url}/capture", timeout=3)
if response.status_code == 200:
img_array = np.asarray(bytearray(response.content), dtype=np.uint8)
frame = cv2.imdecode(img_array, cv2.IMREAD_COLOR)
self.frames.append(frame)
except Exception as e:
print(f"获取图像时出错: {
e}")
def _update_face_data(self):
try:
response = requests.get(f"{
self.base_url}/faces", timeout=3)
if response.status_code == 200:
self.face_data.append(response.json())
except Exception as e:
print(f"获取人脸数据时出错: {
e}")
def read(self):
return {
'frame': self.frames[-1] if self.frames else None,
'face_data': self.face_data[-1] if self.face_data else None
}
def stop(self):
self.stopped = True
self.thread.join()
class PoseEstimator:
def __init__(self):
# 3D人脸模型关键点(用于姿态估计)
self.model_points = np.array([
(0.0, 0.0, 0.0), # 鼻尖
(0.0, -330.0, -65.0), # 下巴
(-225.0, 170.0