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

Windows10下face_recognition与DeepFace共存致编码生成崩溃求助

解决face_recognition与DeepFace在Windows10环境下的依赖冲突问题

问题背景

开发人脸识别桌面应用,使用face_recognition和DeepFace库,基于venv虚拟环境。代码在Colab上运行正常,但Windows10本地环境下:

  • 出现TensorFlow警告(已尝试通过环境变量TF_ENABLE_ONEDNN_OPTS=0关闭无效)
  • 触发face_recognition生成人脸编码时应用直接崩溃
  • 卸载Keras后,face_recognition编码功能恢复正常,但DeepFace依赖Keras无法使用

环境版本

  • Keras 2.15.0
  • TensorFlow 2.15.0

警告信息

2024-02-04 12:16:00.858406: I tensorflow/core/util/port.cc:113] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable TF_ENABLE_ONEDNN_OPTS=0.
WARNING:tensorflow:From C:\Users\xxx\Desktop\xxxx\xxxxx\xxxxxx\venv\lib\site-packages\keras\src\losses.py:2976: The name tf.losses.sparse_softmax_cross_entropy is deprecated. Please use tf.compat.v1.losses.sparse_softmax_cross_entropy instead.

核心导入代码

import tkinter as tk
from tkinter import filedialog
from PIL import ImageTk, Image
import json
from imutils import paths
import argparse
import pickle
import cv2
import os
import face_recognition
import time
from deepface import DeepFace
from retinaface import RetinaFace
from imutils.video import VideoStream
import imutils

问题根源

face_recognition依赖的dlib库(C++实现)与TensorFlow/Keras在Windows环境下存在底层运行时冲突:

  1. dlib的C++运行时和TensorFlow的oneDNN优化模块存在兼容性问题
  2. Keras 2.15.0调用TensorFlow旧API的行为,在Windows环境下触发未被Linux(Colab)暴露的隐性冲突

解决方案

方案1:子进程隔离运行环境

将face_recognition的编码逻辑与DeepFace的逻辑完全隔离,通过子进程调用避免环境污染:

  1. 创建独立编码脚本face_encoding_service.py:
import face_recognition
import sys
import json

if __name__ == "__main__":
    image_path = sys.argv[1]
    image = face_recognition.load_image_file(image_path)
    encodings = face_recognition.face_encodings(image)
    # 将编码转为可序列化的列表格式输出
    print(json.dumps([list(enc) for enc in encodings]))
  1. 主应用中通过子进程调用该脚本:
import subprocess
import json
import os

def get_face_encodings(image_path):
    result = subprocess.run(
        ["python", "face_encoding_service.py", image_path],
        capture_output=True,
        text=True,
        cwd=os.path.dirname(os.path.abspath(__file__))
    )
    if result.returncode == 0:
        return json.loads(result.stdout)
    else:
        raise RuntimeError(f"编码生成失败: {result.stderr}")

此方案彻底隔离两个库的运行环境,从根源避免冲突。

方案2:调整依赖版本组合

降级TensorFlow和Keras到兼容性更稳定的版本:

# 卸载现有版本
pip uninstall -y tensorflow keras
# 安装指定版本
pip install tensorflow==2.14.0 keras==2.14.0 face-recognition deepface

TensorFlow 2.14.0与Keras 2.14.0的组合在Windows环境下经过更充分的兼容性测试,可降低冲突概率。

方案3:强制TensorFlow使用旧版API

在导入DeepFace前覆盖TensorFlow的API调用,避免Keras触发的警告及潜在冲突:

import tensorflow as tf
# 禁用即时执行模式,兼容旧版API
tf.compat.v1.disable_eager_execution()
# 替换Keras调用的旧API
tf.losses.sparse_softmax_cross_entropy = tf.compat.v1.losses.sparse_softmax_cross_entropy

# 之后再导入其他库
import face_recognition
from deepface import DeepFace
# ... 其余导入代码

验证步骤

  1. 单独测试face_recognition的编码生成功能,确认无崩溃
  2. 测试DeepFace的人脸识别/分析功能,确认正常运行
  3. 整合两个功能进行完整流程测试

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 14:58:25