You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

运行MediaPipe人脸检测函数时遇AttributeError如何解决?

问题分析与解决

核心原因

你遇到的AttributeError是因为未正确获取MediaPipe人脸检测的结果实例,直接访问了SolutionOutputs类本身的属性,而非调用process()方法返回的有效结果对象。此外代码中还有一处拼写错误。

修复步骤

1. 正确调用process()方法获取检测结果

你已经初始化了人脸检测对象,但必须传入图像调用process()才能生成包含检测数据的结果对象。示例代码如下:

import cv2
import mediapipe as mp

mpFD = mp.solutions.face_detection
fD = mpFD.FaceDetection()

def faceDetection(image):
    # MediaPipe要求输入为RGB格式,OpenCV默认BGR需转换
    rgb_img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    # 调用process得到检测结果
    results = fD.process(rgb_img)
    
    if results.detections:
        # 修正拼写错误:detection → detections
        for detection in results.detections:
            print(detection)

# 示例:读取图片执行检测
test_img = cv2.imread("your_image.jpg")
faceDetection(test_img)

2. 修正循环中的拼写错误

原代码中for detection in results.detection: 应为results.detections(复数形式),否则即使解决了核心问题,仍会触发新的属性错误。

关于教程的说明

教程中的代码必然包含了process()调用步骤,只是你可能遗漏了这部分内容。MediaPipe所有视觉解决方案都需要通过process()传入图像数据,才能生成带有检测/识别结果的输出对象。

内容的提问来源于stack exchange,提问作者The Crafters

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 18:55:35