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

开发人脸识别应用时遇TypeError参数不兼容问题求助

TypeError: call(): incompatible function arguments 问题解决

错误信息

TypeError: __call__(): incompatible function arguments. The following argument types are supported:
    1. (self: _dlib_pybind11.fhog_object_detector, image: array, upsample_num_times: int=0) -> _dlib_pybind11.rectangles

Invoked with: <_dlib_pybind11.fhog_object_detector object at 0x111932430>, None, 1

问题根源

报错核心是dlib人脸检测器被传入了None而非有效的图像数组,触发场景:

  1. 参考图片加载失败:cv2.imread(file_path)在遇到路径错误、文件损坏、非图片格式时返回None,该值直接传入face_recognition.face_encodings后触发内部检测器报错
  2. compute_face_encoding函数未做入参合法性校验,导致无效的None值进入后续调用链

解决方案

1. 新增参考图片加载有效性检查

遍历参考图片目录时,跳过无法读取的文件及子目录

2. 增强compute_face_encoding参数校验

先判断传入图像是否有效,避免无效参数进入face_recognition调用

修改后的完整代码

import cv2
import os
import dlib
import numpy as np
import face_recognition

detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")

def compute_face_encoding(image):
    # 新增:先校验图像是否有效
    if image is None:
        return None
    face_encoding = face_recognition.face_encodings(image)
    if len(face_encoding) > 0:
        return face_encoding[0]
    else:
        return None

def compare_face_encodings(face_encoding1, face_encoding2):
    distance = np.linalg.norm(face_encoding1 - face_encoding2)
    threshold = 0.6
    return distance < threshold

def main():
    reference_images_folder = "face_images"
    reference_encodings = []
    reference_image_names = []

    for filename in os.listdir(reference_images_folder):
        file_path = os.path.join(reference_images_folder, filename)
        # 新增:跳过子目录,只处理文件
        if not os.path.isfile(file_path):
            continue
        reference_image = cv2.imread(file_path)
        # 新增:检查图片是否加载成功
        if reference_image is None:
            print(f"无法加载图片:{file_path}")
            continue
        reference_encoding = compute_face_encoding(reference_image)
        if reference_encoding is not None:
            reference_encodings.append(reference_encoding)
            reference_image_names.append(filename)

    cap = cv2.VideoCapture(0)

    while True:
        ret, frame = cap.read()

        # 新增:同时校验ret和frame有效性
        if not ret or frame is None:
            break

        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        faces = detector(gray)

        for face in faces:
            landmarks = predictor(gray, face)
            face_encoding = compute_face_encoding(frame)
            if face_encoding is None:
                continue

            for i in range(len(reference_encodings)):
                reference_encoding = reference_encodings[i]
                reference_image_name = reference_image_names[i]
                is_match = compare_face_encodings(reference_encoding, face_encoding)

                x1, y1 = face.left(), face.top()
                x2, y2 = face.right(), face.bottom()
                color = (0, 255, 0) if is_match else (0, 0, 255)
                cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2)
                text = reference_image_name if is_match else "Unknown"
                cv2.putText(frame, text, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, color, 2)

                if is_match:
                    print(f"Match found: Reference Image = {reference_image_name}")

        cv2.imshow('Real-Time Face Recognition', frame)
        if cv2.waitKey(1) == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()

if __name__ == "__main__":
    main()

额外建议

  • 确保shape_predictor_68_face_landmarks.dat文件存在于当前运行目录,或指定完整路径
  • face_images目录下只存放格式正确的图片文件,避免混入其他类型文件或子目录

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 12:37:09