依赖于 dlib 的 python 可执行文件不起作用

Posted

技术标签:

【中文标题】依赖于 dlib 的 python 可执行文件不起作用【英文标题】:python executable that has dependency on dlib not working 【发布时间】:2020-03-18 15:04:10 【问题描述】:

大家好,我有一个依赖于 dlib 的 python 脚本,例如 import dlib 现在我已经创建了一个可执行文件(使用 pyinstaller),它在我的机器上运行良好,但出现 ImportError: DLL load failed: A dynamic link库 (DLL) 初始化例程在另一台机器上失败。在挖掘出发生这种情况的行之后,基本上是导入 dlib,这让我认为 dlib 没有正确包含在我的可执行文件中。我的 dlib 版本 19.18.0 和我试图在其上运行 exe 的另一台机器没有安装 python。需要帮助另一台机器上的错误

F:\FaceRecogDemo\FaceRecogDemo\dist>recognizefaces.exe --debug --encodings ../encodings.pickle --image ../example1.jpg
Traceback (most recent call last):
  File "D:\FaceRecogDemo\recognizefaces.py", line 2, in <module>
  File "c:\programdata\anaconda3\envs\mywindowscv\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module
  File "D:\FaceRecogDemo\face_recognition\__init__.py", line 7, in <module>
  File "c:\programdata\anaconda3\envs\mywindowscv\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module
  File "D:\FaceRecogDemo\face_recognition\api.py", line 4, in <module>
ImportError: DLL load failed: A dynamic link library (DLL) initialization routine failed.
[14720] Failed to execute script recognizefaces

我的识别脸.py 脚本

import face_recognition
import argparse
import pickle
import cv2

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-e", "--encodings", required=True,
    help="path to serialized db of facial encodings")
ap.add_argument("-i", "--image", required=True,
    help="path to input image")
ap.add_argument("-d", "--detection-method", type=str, default="cnn",
    help="face detection model to use: either `hog` or `cnn`")
args = vars(ap.parse_args())
# load the known faces and embeddings
print("[INFO] loading encodings...")
data = pickle.loads(open(args["encodings"], "rb").read())

# load the input image and convert it from BGR to RGB
image = cv2.imread(args["image"])
rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# detect the (x, y)-coordinates of the bounding boxes corresponding
# to each face in the input image, then compute the facial embeddings
# for each face
print("[INFO] recognizing faces...")
boxes = face_recognition.face_locations(rgb,
    model=args["detection_method"])
encodings = face_recognition.face_encodings(rgb, boxes)

# initialize the list of names for each face detected
names = []
# loop over the facial embeddings
for encoding in encodings:
    # attempt to match each face in the input image to our known
    # encodings
    matches = face_recognition.compare_faces(data["encodings"],
        encoding)
    name = "Unknown"
    # check to see if we have found a match
    if True in matches:
        # find the indexes of all matched faces then initialize a
        # dictionary to count the total number of times each face
        # was matched
        matchedIdxs = [i for (i, b) in enumerate(matches) if b]
        counts = 

        # loop over the matched indexes and maintain a count for
        # each recognized face face
        for i in matchedIdxs:
            name = data["names"][i]
            counts[name] = counts.get(name, 0) + 1

        # determine the recognized face with the largest number of
        # votes (note: in the event of an unlikely tie Python will
        # select first entry in the dictionary)
        name = max(counts, key=counts.get)

    # update the list of names
    names.append(name)
print(names)

我的两台机器都有 Windows 10 操作系统

【问题讨论】:

@JayD dlib 依赖于机器。您需要确保正在运行的机器上有可用的二进制文件。 @AdnanUmer 好的,你能告诉我如何准确地找出答案 您编译该二进制文件的操作系统是什么,目标机器上运行的操作系统是什么?从 Windows 的堆栈跟踪中可以明显看出。但是哪个版本的Windows? 32 位还是 64 位?您可以在系统属性中找到该信息 @AdnanUmer windows10 EducationN 是 exe 工作正常,而 windows10 Pro N 是 64 位 exe 都抛出错误 尝试为 windows 10 Pro N 编译它。你可能必须在目标机器上这样做。 【参考方案1】:

我已经解决了这个问题,在我的情况下是由于直接编译 dlib(我不知道为什么)。

以下是步骤:

创建新环境 安装项目的其他要求 确保在您的开发 PC 上安装了 Visual Studio pip install cmake pip install face_recognition 现在您可以使用 dlib,因为 face_recognition 也会安装 dlib。 现在您可以创建 exe 文件,如果仍然存在问题,请将 dlib 文件夹从 site_packages 文件夹复制到放置 exe 文件的文件夹中。

在我的例子中,它可以在其他两个版本的 windows 上运行,以前它不能运行

【讨论】:

【参考方案2】:

    问题也可能出在环境变量上,你能检查一下你执行文件的机器上的%PATH%吗?多个 python 版本及其在 PATH 中的正确配置。

    问题也可能是由 Visual C++ 发行版引起的,一旦两台机器上的发行版相同,请检查。

    尝试在路径变量中添加opencv DLL并检查。问题是anaconda发行版中缺少python3.dll。您可以下载 python 二进制文件here 并从 zip 存档中提取 dll。将它放在 PATH 中的文件夹中(例如 C:\Users\MyName\Anaconda3),导入应该可以工作。

Can't import cv2; "DLL load failed"

【讨论】:

【参考方案3】:

由于某种原因,Pyinstaller 似乎没有选择 dlib。在命令行上构建二进制文件时,请尝试使用以下标志 pyinstaller --hidden-import dlib 将 dlib 显式添加到包中。

https://pyinstaller.readthedocs.io/en/stable/usage.html#what-to-bundle-where-to-search

【讨论】:

以上是关于依赖于 dlib 的 python 可执行文件不起作用的主要内容,如果未能解决你的问题,请参考以下文章

使用 Cmake 创建一个依赖于 dlib 的共享库

linux下安装python dlib依赖

安装 dlib [重复]

Python 冻结的可执行文件在某些​​计算机上不起作用

CMake构建静态可执行文件

从具有依赖项的python程序创建可执行文件