用户在使用OpenMV4 H7 Plus运行HaarCascade人脸检测代码时遇到错误。
具体的错误信息:
Traceback (most recent call last): File "", line 12, in OSError: Could not find the file OpenMV v4.5.9; MicroPython v1.23.0-r19; OPENMV4P-STM32H743 Type "help()" for more information.故障代码:
import sensor, time, image, gc
# ========== 初始化设置 ==========
sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE) # Haar Cascade需要灰度图
sensor.set_framesize(sensor.HQVGA) # 320x240分辨率,平衡速度与精度
sensor.skip_frames(time=2000) # 等待摄像头稳定
# ========== 加载检测模型 ==========
# 使用OpenMV内置模型,不是外部文件
face_cascade = image.HaarCascade("frontalface", stages=25)
eye_cascade = image.HaarCascade("eye", stages=24)
print("INFO: 模型加载完成,可用内存: {}KB".format(gc.mem_free() // 1024))
# ========== 状态跟踪变量 ==========
eye_closed_counter = 0 # 连续闭眼帧数计数
EYE_CLOSED_THRESHOLD = 10 # 连续多少帧检测不到眼睛算闭眼
frame_count = 0 # 总帧数计数
eye_detect_history = [] # 眼睛检测历史(用于状态平滑)
HISTORY_SIZE = 5 # 历史记录大小
# ========== 颜色定义 ==========
COLOR_FACE = (255, 0, 0) # 蓝色 - 人脸框
COLOR_EYE_OPEN = (0, 255, 0) # 绿色 - 睁眼框
COLOR_EYE_CLOSED = (0, 0, 255) # 红色 - 闭眼或无眼框
COLOR_TEXT = (255, 255, 255) # 白色 - 文字
clock = time.clock()
# ========== 辅助函数 ==========
def update_eye_history(detected):
"""更新眼睛检测历史记录"""
eye_detect_history.append(1 if detected else 0)
if len(eye_detect_history) > HISTORY_SIZE:
eye_detect_history.pop(0)
# 返回最近HISTORY_SIZE帧中检测到眼睛的比例
if len(eye_detect_history) == 0:
return 0
return sum(eye_detect_history) / len(eye_detect_history)
def get_eye_status(face_rect, img):
"""检测并返回眼睛状态"""
(x, y, w, h) = face_rect
# 眼睛通常位于人脸的上半部分
# 调整这些参数以适应不同人脸
eye_region_y = y + int(h * 0.2) # 从人脸顶部20%开始
eye_region_h = int(h * 0.4) # 高度为人脸的40%
eye_roi = (x, eye_region_y, w, eye_region_h)
# 在眼睛区域内检测眼睛
# 调整threshold和scale_factor以适应你的环境
eyes = img.find_features(eye_cascade,
threshold=0.2,
scale_factor=1.1,
roi=eye_roi)
return eyes, eye_roi
# ========== 主循环 ==========
print("=== 人脸与眼睛检测系统启动 ===")
print("正在初始化...")
try:
while True:
clock.tick()
frame_count += 1
img = sensor.snapshot()
# 1. 人脸检测
faces = img.find_features(face_cascade,
threshold=0.75,
scale_factor=1.35)
if faces:
for i, face_rect in enumerate(faces):
(fx, fy, fw, fh) = face_rect
# 绘制人脸框
img.draw_rectangle(face_rect, color=COLOR_FACE, thickness=2)
# 2. 眼睛检测
eyes, eye_roi = get_eye_status(face_rect, img)
# 绘制眼睛检测区域(调试用,可注释掉)
# img.draw_rectangle(eye_roi, color=(100, 100, 100), thickness=1)
# 3. 处理检测到的眼睛
detected_eyes = 0
eye_boxes = []
for eye in eyes:
(ex, ey, ew, eh) = eye
# 计算眼睛宽高比(简化版)
eye_ratio = eh / ew if ew > 0 else 0
# 过滤掉不符合眼睛特征的区域
if 0.2 < eye_ratio < 1.0 and ew > 5 and eh > 5:
detected_eyes += 1
eye_boxes.append(eye)
# 绘制眼睛框 - 绿色表示睁开的眼睛
img.draw_rectangle(eye, color=COLOR_EYE_OPEN, thickness=1)
# 在眼睛中心画点
img.draw_circle(ex + ew//2, ey + eh//2, 2, color=COLOR_EYE_OPEN)
# 4. 判断眼睛状态
eye_detected_ratio = update_eye_history(detected_eyes >= 1) # 至少检测到一只眼睛
if detected_eyes >= 1: # 检测到眼睛
eye_closed_counter = max(0, eye_closed_counter - 2) # 快速恢复
status_text = "睁眼 ({}只)".format(detected_eyes)
status_color = COLOR_EYE_OPEN
else: # 未检测到眼睛
eye_closed_counter += 1
if eye_closed_counter >= EYE_CLOSED_THRESHOLD:
status_text = "闭眼警告!"
status_color = COLOR_EYE_CLOSED
# 在脸上画一个"X"表示闭眼
img.draw_cross(fx + fw//2, fy + fh//2,
color=COLOR_EYE_CLOSED, size=20, thickness=2)
else:
status_text = "眨眼/未检测"
status_color = (255, 255, 0) # 黄色
# 5. 显示状态信息
# 人脸标签
face_label = "人脸 {}".format(i+1)
img.draw_string(fx, fy-15, face_label,
color=COLOR_TEXT, scale=1.2, mono_space=False)
# 眼睛状态
img.draw_string(fx, fy + fh + 5, status_text,
color=status_color, scale=1.2, mono_space=False)
# 详细数据
detail_text = "帧:{} 计数:{} 历史:{:.1f}".format(
frame_count, eye_closed_counter, eye_detected_ratio)
img.draw_string(fx, fy + fh + 20, detail_text,
color=(200, 200, 200), scale=0.8, mono_space=False)
# 打印调试信息
if frame_count % 20 == 0: # 每20帧打印一次
print("人脸{}: {} | 检测到{}只眼睛 | 闭眼计数: {}".format(
i+1, status_text, detected_eyes, eye_closed_counter))
else:
# 没有人脸时重置状态
eye_closed_counter = max(0, eye_closed_counter - 1)
if frame_count % 30 == 0:
print("未检测到人脸")
# 6. 显示帧率
fps = clock.fps()
fps_text = "FPS: {:.1f}".format(fps)
img.draw_string(5, 5, fps_text, color=COLOR_TEXT, scale=1.2)
# 7. 内存监控(可选)
if frame_count % 50 == 0:
mem_free = gc.mem_free() // 1024
mem_text = "内存: {}KB".format(mem_free)
img.draw_string(img.width() - 80, 5, mem_text,
color=COLOR_TEXT, scale=0.8)
gc.collect() # 垃圾回收
except KeyboardInterrupt:
print("\n=== 程序被用户中断 ===")
print("总计处理帧数: {}".format(frame_count))
print("最终可用内存: {}KB".format(gc.mem_free() // 1024))问题描述:代码在加载HaarCascade模型时出现"Could not find the file"错误,尝试了不同固件版本(4.7.0和4.5.9)都无法解决。
推荐更改使用目标检测方式识别人脸: