AttributeError: 模块 'face_recognition' 没有属性 'face_encodings',尝试作为 REST API 使用时出现

0 投票
1 回答
28 浏览
提问于 2025-04-13 18:54

我实现了一个简单的人脸识别方案。

输入内容

  • 参考图像
  • 需要匹配的图像
    这两个都是用base64编码的

在我的识别函数中,我将参考图像和需要匹配的图像解码,代码如下

def decode_base64_image(base64_string):
    image_data = base64.b64decode(base64_string)
    image = Image.open(io.BytesIO(image_data))
    image_array = np.array(image)
    return image_array

def recognize_face(reference_image_base64, frame_to_match_base64):
    # Decode the reference image from base64
    reference_image = decode_base64_image(reference_image_base64)

    # Decode the frame to match from base64
    frame_to_match = decode_base64_image(frame_to_match_base64)

    known_face_encodings = face_recognition.face_encodings(reference_image)
    ...........

执行得很顺利,没有任何问题。

我尝试将代码扩展为一个API,并像下面这样调用这个函数

# Perform face recognition on the frame
    face_data = recognize_face(reference_image_base64, frame_to_match_base64)

但是当我用flask执行这个API,并像下面这样调用我的测试脚本时

# Load the reference image and encode it to base64
reference_image_path = 'FileName.jpg'
with open(reference_image_path, 'rb') as f:
    reference_image_bytes = f.read()
reference_image_base64 = base64.b64encode(reference_image_bytes).decode('utf-8')
..............

# Encode the frame to base64
    _, frame_to_match_bytes = cv2.imencode('.jpg', frame)
    frame_to_match_base64 = base64.b64encode(frame_to_match_bytes).decode('utf-8')

    # Prepare the request data
    data = {
        'reference_image': reference_image_base64,
        'frame_to_match': frame_to_match_base64
    }

    # Send the POST request to the API
    response = requests.post('http://localhost:5000/match_face', json=data)
    result = response.json().get('result')

在API的flask控制台上出现了错误

known_face_encodings = face_recognition.face_encodings(reference_image)
AttributeError: module 'face_recognition' has no attribute 'face_encodings'

通过在正常函数和API的两种情况下添加下面这行代码,确认两者的输出都是1076

print(f"size of ref image is : {len(reference_image)}")

我在执行步骤或调用顺序上是不是漏掉了什么? 有没有什么建议可以尝试一下?

开发环境 -Python 3.10.13 -cv2 4.9.0 -face_recognition 1.3.0

尝试在python中将face_recognition库作为函数使用 - 成功, 尝试将相同的代码作为API并做了一些修改 - 失败

1 个回答

暂无回答

撰写回答