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

使用face_recognition处理视频出现TypeError错误求助

问题

基于face_recognition官方仓库示例facerec_from_video_file.py修改的视频人脸识别代码,运行时程序开始写入视频帧,但写入第4帧后抛出TypeError错误,提示compute_face_descriptor()函数参数不兼容,请求排查问题。

错误信息

Writing frame 1 / 6218
Writing frame 2 / 6218
Writing frame 3 / 6218
Writing frame 4 / 6218
Traceback (most recent call last):
  File "/Users/main/Desktop/pypred/src/main.py", line 55, in <module>
    face_encodings = fr.face_encodings(rgb_frame, face_locations)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/face_recognition/api.py", line 214, in face_encodings
    return [np.array(face_encoder.compute_face_descriptor(face_image, raw_landmark_set, num_jitters)) for raw_landmark_set in raw_landmarks]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/face_recognition/api.py", line 214, in <listcomp>
    return [np.array(face_encoder.compute_face_descriptor(face_image, raw_landmark_set, num_jitters)) for raw_landmark_set in raw_landmarks]
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: compute_face_descriptor(): incompatible function arguments. The following argument types are supported:
    1. (self: _dlib_pybind11.face_recognition_model_v1, img: numpy.ndarray[(rows,cols,3),numpy.uint8], face: _dlib_pybind11.full_object_detection, num_jitters: int = 0, padding: float = 0.25) -> _dlib_pybind11.vector
    2. (self: _dlib_pybind11.face_recognition_model_v1, img: numpy.ndarray[(rows,cols,3),numpy.uint8], num_jitters: int = 0) -> _dlib_pybind11.vector
    3. (self: _dlib_pybind11.face_recognition_model_v1, img: numpy.ndarray[(rows,cols,3),numpy.uint8], faces: _dlib_pybind11.full_object_detections, num_jitters: int = 0, padding: float = 0.25) -> _dlib_pybind11.vectors
    4. (self: _dlib_pybind11.face_recognition_model_v1, batch_img: List[numpy.ndarray[(rows,cols,3),numpy.uint8]], batch_faces: List[_dlib_pybind11.full_object_detections], num_jitters: int = 0, padding: float = 0.25) -> _dlib_pybind11.vectorss
    5. (self: _dlib_pybind11.face_recognition_model_v1, batch_img: List[numpy.ndarray[(rows,cols,3),numpy.uint8]], num_jitters: int = 0) -> _dlib_pybind11.vectors

Invoked with: <_dlib_pybind11.face_recognition_model_v1 object at 0x1046a0270>, array([[[40, 32, 31],
        [40, 32, 31],
        [40, 32, 31],
        ...,
        [61, 91, 49],
        [61, 91, 49],
        [61, 91, 49]],

       [[40, 32, 31],
        [40, 32, 31],
        [40, 32, 31],
        ...,
        [61, 91, 49],
        [61, 91, 49],
        [61, 91, 49]],

       [[40, 32, 31],
        [40, 32, 31],
        [40, 32, 31],
        ...,
        [61, 91, 49],
        [61, 91, 49],
        [61, 91, 49]],

       ...,

       [[17, 30, 16],
        [13, 26, 12],
        [11, 24, 10],
        ...,
        [15, 16,  9],
        [15, 16,  9],
        [15, 16,  9]],

       [[17, 30, 16],
        [13, 26, 12],
        [11, 24, 10],
        ...,
        [15, 16,  9],
        [15, 16,  9],
        [15, 16,  9]],

       [[17, 30, 16],
        [13, 26, 12],
        [11, 24, 10],
        ...,
        [15, 16,  9],
        [15, 16,  9],
        [16, 17, 10]]], dtype=uint8), <_dlib_pybind11.full_object_detection object at 0x10772cf30>, 1

相关代码

input_movie = cv2.VideoCapture("moneky.mp4")
length = int(input_movie.get(cv2.CAP_PROP_FRAME_COUNT))
fourcc = cv2.VideoWriter_fourcc(*'XVID')
output_movie = cv2.VideoWriter('output.avi', fourcc, 29.97, (1280, 720))

joe_image = fr.load_image_file("joe.png")
lmm_face_encoding = fr.face_encodings(joe_image)[0]

known_faces = [
    lmm_face_encoding
]

face_locations = []
face_encodings = []
face_names = []
frame_number = 0

while True:
    # Grab a single frame of video
    ret, frame = input_movie.read()
    frame_number += 1

    # Quit when the input video file ends
    if not ret:
        break

    # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
    rgb_frame = frame[:, :, ::-1]

    # Find all the faces and face encodings in the current frame of video
    face_locations = fr.face_locations(rgb_frame)
    face_encodings = fr.face_encodings(rgb_frame, face_locations)

    face_names = []
    for face_encoding in face_encodings:
        # See if the face is a match for the known face(s)
        match = fr.compare_faces(known_faces, face_encoding, tolerance=0.50)

        # If you had more than 2 faces, you could make this logic a lot prettier
        # but I kept it simple for the demo
        name = None
        if match[0]:
            name = "joe rogan"

        face_names.append(name)

    # Label the results
    for (top, right, bottom, left), name in zip(face_locations, face_names):
        if not name:
            continue

        # Draw a box around the face
        cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)

        # Draw a label with a name below the face
        cv2.rectangle(frame, (left, bottom - 25), (right, bottom), (0, 0, 255), cv2.FILLED)
        font = cv2.FONT_HERSHEY_DUPLEX
        cv2.putText(frame, name, (left + 6, bottom - 6), font, 0.5, (255, 255, 255), 1)

    # Write the resulting image to the output video file
    print("Writing frame {} / {}".format(frame_number, length))
    output_movie.write(frame)

# All done!
input_movie.release()
cv2.destroyAllWindows()
解决方案

这个错误的核心原因是face_recognition库与dlib库版本不兼容:当前使用的face_recognition版本在调用compute_face_descriptor时传入的参数格式,和dlib实际支持的参数签名不匹配。

解决步骤:

  1. 卸载现有冲突版本
    pip uninstall -y face_recognition dlib
    
  2. 安装经过验证的稳定兼容版本组合
    推荐安装face_recognition 1.3.0 + dlib 19.22.0:
    pip install dlib==19.22.0 face_recognition==1.3.0
    
  3. 若dlib编译失败(如ARM架构Mac或Linux),改用预编译包:
    pip install dlib-bin==19.22.0
    

替换版本后重新运行代码即可解决参数不兼容的问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 14:54:55